420dff825ecd6219d25940777068d9cc0a5a851a
2 # floof/floof/model/art.py
4 # Copyright (c) 2009 Scribblr
7 # from elixir import Entity, Field, Integer, Unicode
10 from pylons
import config
12 from floof
.lib
.file_storage
import get_path
, save_file
13 from floof
.lib
.dbhelpers
import find_or_create
17 title
= Field(Unicode(120))
18 original_filename
= Field(Unicode(120))
21 uploaded_by
= 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 tag
in tags
.split():
47 raise "Long Tag!" # can we handle this more gracefully?
48 # sqlite seems happy to store strings much longer than the supplied limit...
50 # elixir should really have its own find_or_create.
51 tagtext
= find_or_create(TagText
, text
=tag
)
52 tag
= find_or_create(Tag
, art
=self
, tagger
=user
, tagtext
=tagtext
)
55 def __unicode__(self
):
56 return self
.get_path()
60 # look into how ondelete works. This just sets a database property.
61 art
= ManyToOne('Art', ondelete
='cascade')
62 tagger
= ManyToOne('User')
63 tagtext
= ManyToOne('TagText')
65 # this text setter is no longer useful since I changed the way Art#add_tags works
66 # but I'll leave it in here just for several minutes nostalgia.
67 # def set_text(self, text):
68 # self.tagtext = TagText.get_by(text=text)
69 # if not self.tagtext:
70 # self.tagtext = TagText(text=text)
72 # text = property(lambda self: self.tagtext.text, set_text)
74 def __unicode__(self
):
77 return unicode(self
.tagtext
)
80 class TagText(Entity
):
81 text
= Field(Unicode(50)) # gotta enforce this somehow
82 tags
= OneToMany('Tag')
84 def __unicode__(self
):