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
17 title
= Field(Unicode(120))
18 original_filename
= Field(Unicode(120))
21 uploader
= ManyToOne('User')
22 tags
= OneToMany('Tag')
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)
34 def set_file(self
, file):
35 self
.hash = save_file("art", file)
37 file = property(get_path
, set_file
)
41 return get_path("art", self
.hash)
44 def add_tags(self
, tags
, user
):
45 for text
in tags
.split():
48 tagtext
= TagText
.get_by(text
=text
[1:])
50 tag
= Tag
.get_by(art
=self
, tagger
=user
, tagtext
=tagtext
)
52 elixir
.session
.delete(tag
)
56 raise "Long Tag!" # can we handle this more gracefully?
57 # sqlite seems happy to store strings much longer than the supplied limit...
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
)
66 def rate(self
, score
, user
):
67 return update_or_create(Rating
, {"rater":user
, "art":self
}, {"score":score
})
69 def user_score(self
, user
):
70 rating
= Rating
.get_by(rater
=user
, art
=self
)
75 def __unicode__(self
):
76 return self
.get_path()
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')
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)
92 # text = property(lambda self: self.tagtext.text, set_text)
94 def __unicode__(self
):
97 return unicode(self
.tagtext
)
100 class TagText(Entity
):
101 text
= Field(Unicode(50)) # gotta enforce this somehow
102 tags
= OneToMany('Tag')
104 def __unicode__(self
):
108 class Rating(Entity
):
109 art
= ManyToOne('Art', ondelete
='cascade')
110 rater
= ManyToOne('User', ondelete
='cascade')
111 score
= Field(Integer
)
114 # def score(self, value):
116 options
= {-1:"sucks", 0:"undecided", 1:"good", 2:"great"}
118 # options = ["sucks","neutral","good","great"]
121 Rating
.reverse_options
= dict (zip(Rating
.options
.values(), Rating
.options
.keys()))