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 set_file(self
, file):
27 self
.hash = save_file("art", file)
28 self
.original_filename
= file.filename
30 file = property(get_path
, set_file
)
34 return get_path("art", self
.hash)
37 def add_tags(self
, tags
, user
):
38 for text
in tags
.split():
41 tagtext
= TagText
.get_by(text
=text
[1:])
43 tag
= Tag
.get_by(art
=self
, tagger
=user
, tagtext
=tagtext
)
45 elixir
.session
.delete(tag
)
49 raise "Long Tag!" # can we handle this more gracefully?
50 # sqlite seems happy to store strings much longer than the supplied limit...
52 # elixir should really have its own find_or_create.
53 tagtext
= find_or_create(TagText
, text
=text
)
54 tag
= find_or_create(Tag
, art
=self
, tagger
=user
, tagtext
=tagtext
)
59 def rate(self
, score
, user
):
60 return update_or_create(Rating
, {"rater":user
, "art":self
}, {"score":score
})
62 def user_score(self
, user
):
63 rating
= Rating
.get_by(rater
=user
, art
=self
)
68 def __unicode__(self
):
69 return self
.get_path()
73 # look into how ondelete works. This just sets a database property.
74 art
= ManyToOne('Art', ondelete
='cascade')
75 tagger
= ManyToOne('User', ondelete
='cascade')
76 tagtext
= ManyToOne('TagText')
78 def __unicode__(self
):
81 return unicode(self
.tagtext
)
84 class TagText(Entity
):
85 text
= Field(Unicode(50)) # gotta enforce this somehow
86 tags
= OneToMany('Tag')
88 def __unicode__(self
):
93 art
= ManyToOne('Art', ondelete
='cascade')
94 rater
= ManyToOne('User', ondelete
='cascade')
95 score
= Field(Integer
)
97 options
= {-1:"sucks", 0:"undecided", 1:"good", 2:"great"}
101 Rating
.reverse_options
= dict (zip(Rating
.options
.values(), Rating
.options
.keys()))