420dff825ecd6219d25940777068d9cc0a5a851a
[zzz-floof.git] / floof / model / art.py
1 #
2 # floof/floof/model/art.py
3 #
4 # Copyright (c) 2009 Scribblr
5 #
6
7 # from elixir import Entity, Field, Integer, Unicode
8 from elixir import *
9
10 from pylons import config
11
12 from floof.lib.file_storage import get_path, save_file
13 from floof.lib.dbhelpers import find_or_create
14
15
16 class Art(Entity):
17 title = Field(Unicode(120))
18 original_filename = Field(Unicode(120))
19 hash = Field(String)
20
21 uploaded_by = ManyToOne('User')
22 tags = OneToMany('Tag')
23
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)
32 # left for posterity.
33
34 def set_file(self, file):
35 self.hash = save_file("art", file)
36
37 file = property(get_path, set_file)
38
39 def get_path(self):
40 if self.hash:
41 return get_path("art", self.hash)
42
43
44 def add_tags(self, tags, user):
45 for tag in tags.split():
46 if len(tag) > 50:
47 raise "Long Tag!" # can we handle this more gracefully?
48 # sqlite seems happy to store strings much longer than the supplied limit...
49
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)
53
54
55 def __unicode__(self):
56 return self.get_path()
57
58
59 class Tag(Entity):
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')
64
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)
71 #
72 # text = property(lambda self: self.tagtext.text, set_text)
73
74 def __unicode__(self):
75 if not self.tagtext:
76 return "(broken)"
77 return unicode(self.tagtext)
78
79
80 class TagText(Entity):
81 text = Field(Unicode(50)) # gotta enforce this somehow
82 tags = OneToMany('Tag')
83
84 def __unicode__(self):
85 return self.text