some nice mixins for tags, ratings, relations
[zzz-floof.git] / floof / model / tags.py
1 from elixir import *
2 from art import Art
3 from floof.lib.dbhelpers import find_or_create, update_or_create
4
5
6 class Tag(Entity):
7 # look into how ondelete works. This just sets a database property.
8 art = ManyToOne('Art', ondelete='cascade')
9 tagger = ManyToOne('User', ondelete='cascade')
10 tagtext = ManyToOne('TagText')
11
12 def __unicode__(self):
13 if not self.tagtext:
14 return "(broken)"
15 return unicode(self.tagtext)
16
17
18 class TagText(Entity):
19 text = Field(Unicode(50)) # gotta enforce this somehow
20 tags = OneToMany('Tag')
21
22 def __unicode__(self):
23 return self.text
24
25
26 class TagMixin(object):
27 def add_tags(self, tags, user):
28 for text in tags.split():
29 if text[0] == '-':
30 # Nega-tags
31 tagtext = TagText.get_by(text=text[1:])
32 if tagtext:
33 tag = Tag.get_by(art=self, tagger=user, tagtext=tagtext)
34 if tag:
35 elixir.session.delete(tag)
36
37 else:
38 if len(text) > 50:
39 raise "Long Tag!" # can we handle this more gracefully?
40 # sqlite seems happy to store strings much longer than the supplied limit...
41
42 # elixir should really have its own find_or_create.
43 tagtext = find_or_create(TagText, text=text)
44 tag = find_or_create(Tag, art=self, tagger=user, tagtext=tagtext)
45
46 Art.__bases__ += (TagMixin, )