c3fba3afada439c2b2fc376634d631c0499e76ec
[zzz-floof.git] / floof / model / art.py
1 #
2 # floof/floof/model/art.py
3 #
4 # Copyright (c) 2009 Scribblr
5 #
6
7 # from elixir import Entity, Field, Integer, Unicode
8 from elixir import *
9 import elixir
10
11 from pylons import config
12
13 from floof.lib.file_storage import get_path, save_file
14 from floof.lib.dbhelpers import find_or_create, update_or_create
15
16 class Art(Entity):
17 title = Field(Unicode(120))
18 original_filename = Field(Unicode(120))
19 hash = Field(String)
20
21 uploader = ManyToOne('User')
22 tags = OneToMany('Tag')
23
24 # def __init__(self, **kwargs):
25 # # I wanted to check for the existence of the file, but...
26 # # for some reason this FieldStorage object always conditions as falsey.
27 # # self.hash = save_file("art", kwargs.pop('file'))
28 # super(Art, self).__init__(**kwargs)
29 # # this is what super is doing, pretty much.
30 # # for key, value in kwargs.items():
31 # # setattr(self, key, value)
32 # left for posterity.
33
34 def set_file(self, file):
35 self.hash = save_file("art", file)
36
37 file = property(get_path, set_file)
38
39 def get_path(self):
40 if self.hash:
41 return get_path("art", self.hash)
42
43
44 def add_tags(self, tags, user):
45 for text in tags.split():
46 if text[0] == '-':
47 # Nega-tags
48 tagtext = TagText.get_by(text=text[1:])
49 if tagtext:
50 tag = Tag.get_by(art=self, tagger=user, tagtext=tagtext)
51 if tag:
52 elixir.session.delete(tag)
53
54 else:
55 if len(text) > 50:
56 raise "Long Tag!" # can we handle this more gracefully?
57 # sqlite seems happy to store strings much longer than the supplied limit...
58
59 # elixir should really have its own find_or_create.
60 tagtext = find_or_create(TagText, text=text)
61 tag = find_or_create(Tag, art=self, tagger=user, tagtext=tagtext)
62
63
64
65
66 def rate(self, score, user):
67 return update_or_create(Rating, {"rater":user, "art":self}, {"score":score})
68
69 def user_score(self, user):
70 rating = Rating.get_by(rater=user, art=self)
71 if rating:
72 return rating.score
73 return Rating.default
74
75 def __unicode__(self):
76 return self.get_path()
77
78
79 class Tag(Entity):
80 # look into how ondelete works. This just sets a database property.
81 art = ManyToOne('Art', ondelete='cascade')
82 tagger = ManyToOne('User', ondelete='cascade')
83 tagtext = ManyToOne('TagText')
84
85 # this text setter is no longer useful since I changed the way Art#add_tags works
86 # but I'll leave it in here just for several minutes nostalgia.
87 # def set_text(self, text):
88 # self.tagtext = TagText.get_by(text=text)
89 # if not self.tagtext:
90 # self.tagtext = TagText(text=text)
91 #
92 # text = property(lambda self: self.tagtext.text, set_text)
93
94 def __unicode__(self):
95 if not self.tagtext:
96 return "(broken)"
97 return unicode(self.tagtext)
98
99
100 class TagText(Entity):
101 text = Field(Unicode(50)) # gotta enforce this somehow
102 tags = OneToMany('Tag')
103
104 def __unicode__(self):
105 return self.text
106
107
108 class Rating(Entity):
109 art = ManyToOne('Art', ondelete='cascade')
110 rater = ManyToOne('User', ondelete='cascade')
111 score = Field(Integer)
112
113 options = {-1:"sucks", 0:"undecided", 1:"good", 2:"great"}
114 default = 0
115 # options = ["sucks","neutral","good","great"]
116
117
118 Rating.reverse_options = dict (zip(Rating.options.values(), Rating.options.keys()))