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
18 title
= Field(Unicode(120))
19 original_filename
= Field(Unicode(120))
22 uploaded_by
= ManyToOne('User')
23 tags
= OneToMany('Tag')
25 # def __init__(self, **kwargs):
26 # # I wanted to check for the existence of the file, but...
27 # # for some reason this FieldStorage object always conditions as falsey.
28 # # self.hash = save_file("art", kwargs.pop('file'))
29 # super(Art, self).__init__(**kwargs)
30 # # this is what super is doing, pretty much.
31 # # for key, value in kwargs.items():
32 # # setattr(self, key, value)
35 def set_file(self
, file):
36 self
.hash = save_file("art", file)
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...
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
)
67 def __unicode__(self
):
68 return self
.get_path()
72 # look into how ondelete works. This just sets a database property.
73 art
= ManyToOne('Art', ondelete
='cascade')
74 tagger
= ManyToOne('User')
75 tagtext
= ManyToOne('TagText')
77 # this text setter is no longer useful since I changed the way Art#add_tags works
78 # but I'll leave it in here just for several minutes nostalgia.
79 # def set_text(self, text):
80 # self.tagtext = TagText.get_by(text=text)
81 # if not self.tagtext:
82 # self.tagtext = TagText(text=text)
84 # text = property(lambda self: self.tagtext.text, set_text)
86 def __unicode__(self
):
89 return unicode(self
.tagtext
)
92 class TagText(Entity
):
93 text
= Field(Unicode(50)) # gotta enforce this somehow
94 tags
= OneToMany('Tag')
96 def __unicode__(self
):