delete tags by clicking the x, or typing in -tagtext
[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 import elixir
10
11 from pylons import config
12
13 from floof.lib.file_storage import get_path, save_file
14
15 from floof.lib.dbhelpers import find_or_create
16
17
18
19 class Art(Entity):
20 title = Field(Unicode(120))
21 original_filename = Field(Unicode(120))
22 hash = Field(String)
23
24 uploaded_by = ManyToOne('User')
25 tags = OneToMany('Tag')
26
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)
35 # left for posterity.
36
37 def set_file(self, file):
38 self.hash = save_file("art", file)
39
40 file = property(get_path, set_file)
41
42 def get_path(self):
43 if self.hash:
44 return get_path("art", self.hash)
45
46
47 def add_tags(self, tags, user):
48 for text in tags.split():
49 if text[0] == '-':
50 # Nega-tags
51 tagtext = TagText.get_by(text=text[1:])
52 if tagtext:
53 tag = Tag.get_by(art=self, tagger=user, tagtext=tagtext)
54 if tag:
55 elixir.session.delete(tag)
56
57 else:
58 if len(text) > 50:
59 raise "Long Tag!" # can we handle this more gracefully?
60 # sqlite seems happy to store strings much longer than the supplied limit...
61
62
63
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)
67
68
69 def __unicode__(self):
70 return self.get_path()
71
72
73 class Tag(Entity):
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')
78
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)
85 #
86 # text = property(lambda self: self.tagtext.text, set_text)
87
88 def __unicode__(self):
89 if not self.tagtext:
90 return "(broken)"
91 return unicode(self.tagtext)
92
93
94 class TagText(Entity):
95 text = Field(Unicode(50)) # gotta enforce this somehow
96 tags = OneToMany('Tag')
97
98 def __unicode__(self):
99 return self.text