2 # floof/floof/model/art.py
4 # Copyright (c) 2009 Scribblr
7 # from elixir import Entity, Field, Integer, Unicode
11 from pylons
import config
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
18 title
= Field(Unicode(120))
19 original_filename
= Field(Unicode(120))
20 hash = Field(String
, unique
=True, required
=True)
22 uploader
= ManyToOne('User', required
=True)
23 tags
= OneToMany('Tag')
24 discussion
= ManyToOne('Discussion')
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)
36 def set_file(self
, file):
37 self
.hash = save_file("art", file)
38 self
.original_filename
= file.filename
40 file = property(get_path
, set_file
)
44 return get_path("art", self
.hash)
47 def add_tags(self
, tags
, user
):
48 for text
in tags
.split():
51 tagtext
= TagText
.get_by(text
=text
[1:])
53 tag
= Tag
.get_by(art
=self
, tagger
=user
, tagtext
=tagtext
)
55 elixir
.session
.delete(tag
)
59 raise "Long Tag!" # can we handle this more gracefully?
60 # sqlite seems happy to store strings much longer than the supplied limit...
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
)
69 def rate(self
, score
, user
):
70 return update_or_create(Rating
, {"rater":user
, "art":self
}, {"score":score
})
72 def user_score(self
, user
):
73 rating
= Rating
.get_by(rater
=user
, art
=self
)
78 def __unicode__(self
):
79 return self
.get_path()
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')
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)
95 # text = property(lambda self: self.tagtext.text, set_text)
97 def __unicode__(self
):
100 return unicode(self
.tagtext
)
103 class TagText(Entity
):
104 text
= Field(Unicode(50)) # gotta enforce this somehow
105 tags
= OneToMany('Tag')
107 def __unicode__(self
):
111 class Rating(Entity
):
112 art
= ManyToOne('Art', ondelete
='cascade')
113 rater
= ManyToOne('User', ondelete
='cascade')
114 score
= Field(Integer
)
117 # def score(self, value):
119 options
= {-1:"sucks", 0:"undecided", 1:"good", 2:"great"}
121 # options = ["sucks","neutral","good","great"]
124 Rating
.reverse_options
= dict (zip(Rating
.options
.values(), Rating
.options
.keys()))