b709610219b76ccf260d287b239c90fc5a6ac7d1
[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, unique=True, required=True)
20
21 uploader = ManyToOne('User', required=True)
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 self.original_filename = file.filename
37
38 file = property(get_path, set_file)
39
40 def get_path(self):
41 if self.hash:
42 return get_path("art", self.hash)
43
44
45 def add_tags(self, tags, user):
46 for text in tags.split():
47 if text[0] == '-':
48 # Nega-tags
49 tagtext = TagText.get_by(text=text[1:])
50 if tagtext:
51 tag = Tag.get_by(art=self, tagger=user, tagtext=tagtext)
52 if tag:
53 elixir.session.delete(tag)
54
55 else:
56 if len(text) > 50:
57 raise "Long Tag!" # can we handle this more gracefully?
58 # sqlite seems happy to store strings much longer than the supplied limit...
59
60 # elixir should really have its own find_or_create.
61 tagtext = find_or_create(TagText, text=text)
62 tag = find_or_create(Tag, art=self, tagger=user, tagtext=tagtext)
63
64
65
66
67 def rate(self, score, user):
68 return update_or_create(Rating, {"rater":user, "art":self}, {"score":score})
69
70 def user_score(self, user):
71 rating = Rating.get_by(rater=user, art=self)
72 if rating:
73 return rating.score
74 return Rating.default
75
76 def __unicode__(self):
77 return self.get_path()
78
79
80 class Tag(Entity):
81 # look into how ondelete works. This just sets a database property.
82 art = ManyToOne('Art', ondelete='cascade')
83 tagger = ManyToOne('User', ondelete='cascade')
84 tagtext = ManyToOne('TagText')
85
86 # this text setter is no longer useful since I changed the way Art#add_tags works
87 # but I'll leave it in here just for several minutes nostalgia.
88 # def set_text(self, text):
89 # self.tagtext = TagText.get_by(text=text)
90 # if not self.tagtext:
91 # self.tagtext = TagText(text=text)
92 #
93 # text = property(lambda self: self.tagtext.text, set_text)
94
95 def __unicode__(self):
96 if not self.tagtext:
97 return "(broken)"
98 return unicode(self.tagtext)
99
100
101 class TagText(Entity):
102 text = Field(Unicode(50)) # gotta enforce this somehow
103 tags = OneToMany('Tag')
104
105 def __unicode__(self):
106 return self.text
107
108
109 class Rating(Entity):
110 art = ManyToOne('Art', ondelete='cascade')
111 rater = ManyToOne('User', ondelete='cascade')
112 score = Field(Integer)
113
114 # @score.setter
115 # def score(self, value):
116
117 options = {-1:"sucks", 0:"undecided", 1:"good", 2:"great"}
118 default = 0
119 # options = ["sucks","neutral","good","great"]
120
121
122 Rating.reverse_options = dict (zip(Rating.options.values(), Rating.options.keys()))