b709610219b76ccf260d287b239c90fc5a6ac7d1
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))
19 hash = Field(String
, unique
=True, required
=True)
21 uploader
= ManyToOne('User', required
=True)
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)
36 self
.original_filename
= file.filename
38 file = property(get_path
, set_file
)
42 return get_path("art", self
.hash)
45 def add_tags(self
, tags
, user
):
46 for text
in tags
.split():
49 tagtext
= TagText
.get_by(text
=text
[1:])
51 tag
= Tag
.get_by(art
=self
, tagger
=user
, tagtext
=tagtext
)
53 elixir
.session
.delete(tag
)
57 raise "Long Tag!" # can we handle this more gracefully?
58 # sqlite seems happy to store strings much longer than the supplied limit...
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
)
67 def rate(self
, score
, user
):
68 return update_or_create(Rating
, {"rater":user
, "art":self
}, {"score":score
})
70 def user_score(self
, user
):
71 rating
= Rating
.get_by(rater
=user
, art
=self
)
76 def __unicode__(self
):
77 return self
.get_path()
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')
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)
93 # text = property(lambda self: self.tagtext.text, set_text)
95 def __unicode__(self
):
98 return unicode(self
.tagtext
)
101 class TagText(Entity
):
102 text
= Field(Unicode(50)) # gotta enforce this somehow
103 tags
= OneToMany('Tag')
105 def __unicode__(self
):
109 class Rating(Entity
):
110 art
= ManyToOne('Art', ondelete
='cascade')
111 rater
= ManyToOne('User', ondelete
='cascade')
112 score
= Field(Integer
)
115 # def score(self, value):
117 options
= {-1:"sucks", 0:"undecided", 1:"good", 2:"great"}
119 # options = ["sucks","neutral","good","great"]
122 Rating
.reverse_options
= dict (zip(Rating
.options
.values(), Rating
.options
.keys()))