e57d0c548ba22a01c9e2698f00a5cb7d07108e3e
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
12 from sqlalchemy
import func
14 log
= logging
.getLogger(__name__
)
16 class TagController(BaseController
):
18 # TODO: login required
19 def delete(self
, art_id
, id):
20 tag
= h
.get_object_or_404(Tag
, id=id)
21 elixir
.session
.delete(tag
)
22 elixir
.session
.commit()
23 redirect(url('show_art', id=art_id
))
25 # TODO: login required
26 def create(self
, art_id
):
27 c
.art
= h
.get_object_or_404(Art
, id=art_id
)
29 tag_string
= request
.params
.get('tags', '')
33 for tag_text
in tag_string
.split():
34 original_tag_text
= tag_text
35 tag_text
= tag_text
.lower()
37 # Adding or removing a tag?
38 if tag_text
[0] == '-':
40 tag_text
= tag_text
[1:]
42 # Allow "+foo" to mean "add foo"
43 if tag_text
[0] == '+':
44 tag_text
= tag_text
[1:]
47 # Check for special namespaces
50 prefix
, tag_text
= tag_text
.split(':', 1)
51 if prefix
not in ['by', 'for', 'of']:
52 # This is bogus. Skip it.
53 bad_tags
.append(original_tag_text
)
57 # XXX this needs supporting. silently ignore for now
60 # Must be 3-50 alphanumeric characters
61 if not re
.match('^[a-z0-9]{3,50}$', tag_text
):
62 bad_tags
.append(original_tag_text
)
67 target_user
= User
.query
.filter(func
.lower(User
.name
) == tag_text
) \
70 # Special tag; at the moment, just a relationship
78 user_assoc_data
= dict(art
=c
.art
, user
=target_user
, type=rel
)
80 find_or_create(ArtUser
, **user_assoc_data
)
83 # XXX this will die for nonassociations
84 user_assoc
= ArtUser
.get_by(art
=c
.art
, **user_assoc_data
)
90 tag
= find_or_create(TagText
, text
=tag_text
)
91 find_or_create(Tag
, art
=c
.art
, tagger
=c
.user
, tagtext
=tag
)
94 tag
= TagText
.get_by(text
=tag_text
)
97 tag_assoc
= Tag
.get_by(art
=c
.art
, tagger
=c
.user
, tagtext
=tag
)
100 elixir
.session
.commit()
101 redirect(url('show_art', id=c
.art
.id))