730bf72f39e59ea78dfdf31996a38d72ffe8da00
4 from pylons
import request
, response
, session
, tmpl_context
as c
, url
5 from pylons
.controllers
.util
import abort
, redirect
8 from floof
.model
import Art
, ArtUser
, ArtUserType
, Tag
, TagText
, User
9 from floof
.lib
.base
import BaseController
, render
10 from floof
.lib
.dbhelpers
import find_or_create
11 from floof
.lib
import helpers
as h
13 log
= logging
.getLogger(__name__
)
15 class TagController(BaseController
):
17 # TODO: login required
18 def delete(self
, art_id
, id):
19 tag
= h
.get_object_or_404(Tag
, id=id)
20 elixir
.session
.delete(tag
)
21 elixir
.session
.commit()
22 redirect(url('show_art', id=art_id
))
24 # TODO: login required
25 def create(self
, art_id
):
26 c
.art
= h
.get_object_or_404(Art
, id=art_id
)
28 tag_string
= request
.params
.get('tags', '')
32 for tag_text
in tag_string
.split():
33 original_tag_text
= tag_text
34 tag_text
= tag_text
.lower()
36 # Adding or removing a tag?
37 if tag_text
[0] == '-':
39 tag_text
= tag_text
[1:]
41 # Allow "+foo" to mean "add foo"
42 if tag_text
[0] == '+':
43 tag_text
= tag_text
[1:]
46 # Check for special namespaces
49 prefix
, tag_text
= tag_text
.split(':', 1)
50 if prefix
not in ['by', 'for', 'of']:
51 # This is bogus. Skip it.
52 bad_tags
.append(original_tag_text
)
56 # XXX this needs supporting. silently ignore for now
59 # Must be 3-50 alphanumeric characters
60 if not re
.match('^[a-z0-9]{3,50}$', tag_text
):
61 bad_tags
.append(original_tag_text
)
66 target_user
= User
.get_by(name
=tag_text
)
68 # Special tag; at the moment, just a relationship
76 user_assoc_data
= dict(art
=c
.art
, user
=target_user
, type=rel
)
78 find_or_create(ArtUser
, **user_assoc_data
)
81 # XXX this will die for nonassociations
82 user_assoc
= ArtUser
.get_by(art
=c
.art
, **user_assoc_data
)
88 tag
= find_or_create(TagText
, text
=tag_text
)
89 find_or_create(Tag
, art
=c
.art
, tagger
=c
.user
, tagtext
=tag
)
92 tag
= TagText
.get_by(text
=tag_text
)
95 tag_assoc
= Tag
.get_by(art
=c
.art
, tagger
=c
.user
, tagtext
=tag
)
98 elixir
.session
.commit()
99 redirect(url('show_art', id=c
.art
.id))