66cbe0bb4b3574cacfaeadebcf2a3b122286044e
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
14 from floof
.lib
.dbhelpers
import find_or_create
19 title
= Field(Unicode(120))
20 original_filename
= Field(Unicode(120))
23 uploaded_by
= ManyToOne('User')
24 tags
= OneToMany('Tag')
26 # def __init__(self, **kwargs):
27 # # I wanted to check for the existence of the file, but...
28 # # for some reason this FieldStorage object always conditions as falsey.
29 # # self.hash = save_file("art", kwargs.pop('file'))
30 # super(Art, self).__init__(**kwargs)
31 # # this is what super is doing, pretty much.
32 # # for key, value in kwargs.items():
33 # # setattr(self, key, value)
36 def set_file(self
, file):
37 self
.hash = save_file("art", file)
39 file = property(get_path
, set_file
)
43 return get_path("art", self
.hash)
46 def add_tags(self
, tags
, user
):
47 for tag
in tags
.split():
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
=tag
)
54 tag
= find_or_create(Tag
, art
=self
, tagger
=user
, tagtext
=tagtext
)
57 def __unicode__(self
):
58 return self
.get_path()
62 # look into how ondelete works. It just sets a database property.
63 art
= ManyToOne('Art', ondelete
='cascade')
64 tagger
= ManyToOne('User')
65 tagtext
= ManyToOne('TagText')
67 # this text setter is no longer useful since I changed the way Art#add_tags works
68 # but I'll leave it in here just for several minutes nostalgia.
69 # def set_text(self, text):
70 # self.tagtext = TagText.get_by(text=text)
71 # if not self.tagtext:
72 # self.tagtext = TagText(text=text)
74 # text = property(lambda self: self.tagtext.text, set_text)
76 def __unicode__(self
):
79 return unicode(self
.tagtext
)
82 class TagText(Entity
):
83 text
= Field(Unicode(50)) # gotta enforce this somehow
84 tags
= OneToMany('Tag')
86 def __unicode__(self
):