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 from floof.lib.dbhelpers import find_or_create
15
16
17 class Art(Entity):
18 title = Field(Unicode(120))
19 original_filename = Field(Unicode(120))
20 hash = Field(String)
21
22 uploaded_by = ManyToOne('User')
23 tags = OneToMany('Tag')
24
25 # def __init__(self, **kwargs):
26 # # I wanted to check for the existence of the file, but...
27 # # for some reason this FieldStorage object always conditions as falsey.
28 # # self.hash = save_file("art", kwargs.pop('file'))
29 # super(Art, self).__init__(**kwargs)
30 # # this is what super is doing, pretty much.
31 # # for key, value in kwargs.items():
32 # # setattr(self, key, value)
33 # left for posterity.
34
35 def set_file(self, file):
36 self.hash = save_file("art", file)
37
38 file = property(get_path, set_file)
39
40 def get_path(self):
41 if self.hash:
42 return get_path("art", self.hash)
43
44
45 def add_tags(self, tags, user):
46 for text in tags.split():
47 if text[0] == '-':
48 # Nega-tags
49 tagtext = TagText.get_by(text=text[1:])
50 if tagtext:
51 tag = Tag.get_by(art=self, tagger=user, tagtext=tagtext)
52 if tag:
53 elixir.session.delete(tag)
54
55 else:
56 if len(text) > 50:
57 raise "Long Tag!" # can we handle this more gracefully?
58 # sqlite seems happy to store strings much longer than the supplied limit...
59
60
61
62 # elixir should really have its own find_or_create.
63 tagtext = find_or_create(TagText, text=text)
64 tag = find_or_create(Tag, art=self, tagger=user, tagtext=tagtext)
65
66
67 def __unicode__(self):
68 return self.get_path()
69
70
71 class Tag(Entity):
72 # look into how ondelete works. This just sets a database property.
73 art = ManyToOne('Art', ondelete='cascade')
74 tagger = ManyToOne('User')
75 tagtext = ManyToOne('TagText')
76
77 # this text setter is no longer useful since I changed the way Art#add_tags works
78 # but I'll leave it in here just for several minutes nostalgia.
79 # def set_text(self, text):
80 # self.tagtext = TagText.get_by(text=text)
81 # if not self.tagtext:
82 # self.tagtext = TagText(text=text)
83 #
84 # text = property(lambda self: self.tagtext.text, set_text)
85
86 def __unicode__(self):
87 if not self.tagtext:
88 return "(broken)"
89 return unicode(self.tagtext)
90
91
92 class TagText(Entity):
93 text = Field(Unicode(50)) # gotta enforce this somehow
94 tags = OneToMany('Tag')
95
96 def __unicode__(self):
97 return self.text