66cbe0bb4b3574cacfaeadebcf2a3b122286044e
[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
14 from floof.lib.dbhelpers import find_or_create
15
16
17
18 class Art(Entity):
19 title = Field(Unicode(120))
20 original_filename = Field(Unicode(120))
21 hash = Field(String)
22
23 uploaded_by = ManyToOne('User')
24 tags = OneToMany('Tag')
25
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)
34 # left for posterity.
35
36 def set_file(self, file):
37 self.hash = save_file("art", file)
38
39 file = property(get_path, set_file)
40
41 def get_path(self):
42 if self.hash:
43 return get_path("art", self.hash)
44
45
46 def add_tags(self, tags, user):
47 for tag in tags.split():
48 if len(tag) > 50:
49 raise "Long Tag!" # can we handle this more gracefully?
50 # sqlite seems happy to store strings much longer than the supplied limit...
51
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)
55
56
57 def __unicode__(self):
58 return self.get_path()
59
60
61 class Tag(Entity):
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')
66
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)
73 #
74 # text = property(lambda self: self.tagtext.text, set_text)
75
76 def __unicode__(self):
77 if not self.tagtext:
78 return "(broken)"
79 return unicode(self.tagtext)
80
81
82 class TagText(Entity):
83 text = Field(Unicode(50)) # gotta enforce this somehow
84 tags = OneToMany('Tag')
85
86 def __unicode__(self):
87 return self.text