-
Notifications
You must be signed in to change notification settings - Fork 1
/
ajax_tag.py
51 lines (48 loc) · 1.14 KB
/
ajax_tag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
from common import tag_clean, tag_prefix, tags_as_html, tag_post, tagtypes
from bottle import post, request
@post("/ajax-tag")
def ajax_tag(client):
forms = request.forms.decode('utf-8')
tags = forms.tags
name = forms.name
if name:
try:
type = forms.type
client.add_tag(tag_clean(name), type)
tags = name
except Exception:
msg = 'Failed to create'
m = forms.m.split()
full = set()
weak = set()
remove = set()
failed = []
for t in tags.split():
tag = client.find_tag(tag_clean(t))
if tag:
p = tag_prefix(t)
if p == "~":
weak.add(tag)
elif p == "-":
remove.add(tag)
else:
full.add(tag)
else:
failed.append(t)
res = {}
msg = ''
if full or weak or remove:
with client.begin_transaction():
for p in map(client.get_post, m):
if not p:
msg = 'Posts missing?'
elif tag_post(p, full, weak, remove):
p = client.get_post(p.md5)
res[p.md5] = tags_as_html(p)
if not res and not msg and not failed and not (name and name[0] == '-'):
msg = 'Nothing to do?'
res = dict(failed=' '.join(failed), m=res, msg=msg)
if failed:
res["types"] = tagtypes()
return res