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