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
15 from floof
.lib
.dbhelpers
import find_or_create
20 title
= Field(Unicode(120))
21 original_filename
= Field(Unicode(120))
24 uploaded_by
= ManyToOne('User')
25 tags
= OneToMany('Tag')
27 # def __init__(self, **kwargs):
28 # # I wanted to check for the existence of the file, but...
29 # # for some reason this FieldStorage object always conditions as falsey.
30 # # self.hash = save_file("art", kwargs.pop('file'))
31 # super(Art, self).__init__(**kwargs)
32 # # this is what super is doing, pretty much.
33 # # for key, value in kwargs.items():
34 # # setattr(self, key, value)
37 def set_file(self
, file):
38 self
.hash = save_file("art", file)
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...
64 # elixir should really have its own find_or_create.
65 tagtext
= find_or_create(TagText
, text
=text
)
66 tag
= find_or_create(Tag
, art
=self
, tagger
=user
, tagtext
=tagtext
)
69 def __unicode__(self
):
70 return self
.get_path()
74 # look into how ondelete works. It just sets a database property.
75 art
= ManyToOne('Art', ondelete
='cascade')
76 tagger
= ManyToOne('User')
77 tagtext
= ManyToOne('TagText')
79 # this text setter is no longer useful since I changed the way Art#add_tags works
80 # but I'll leave it in here just for several minutes nostalgia.
81 # def set_text(self, text):
82 # self.tagtext = TagText.get_by(text=text)
83 # if not self.tagtext:
84 # self.tagtext = TagText(text=text)
86 # text = property(lambda self: self.tagtext.text, set_text)
88 def __unicode__(self
):
91 return unicode(self
.tagtext
)
94 class TagText(Entity
):
95 text
= Field(Unicode(50)) # gotta enforce this somehow
96 tags
= OneToMany('Tag')
98 def __unicode__(self
):