author | ymh <ymh.work@gmail.com> |
Mon, 20 Oct 2014 13:43:04 +0200 | |
changeset 45 | 8d2fb4ec85d7 |
parent 43 | e27c3c1c57f1 |
child 51 | 7792f095d94a |
permissions | -rw-r--r-- |
43
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
# |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
# See LICENCE for detail |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright (c) 2014 IRI |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
# |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
import json |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
7 |
import urllib |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
8 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
from flask.ext.admin import Admin |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
from flask.ext.admin.contrib.sqla import ModelView |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
from flask.ext.admin.contrib.sqla.form import AdminModelConverter |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
12 |
from flask.ext.admin.contrib.sqla.filters import BooleanEqualFilter |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
from flask.ext.admin.model.form import InlineFormAdmin, converts |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
from jinja2 import Markup |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
16 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
from wtforms import fields |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
18 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
import database |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
import models |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
import config |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
from webapp import app |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
admin = Admin(app) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
26 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
def build_ldt_url(view, context, model, name): |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
# `view` is current administrative view |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
# `context` is instance of jinja2.runtime.Context |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
# `model` is model instance |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
# `name` is property name |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
32 |
if not model.project_id \ |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
or not model.categories_json \ |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
34 |
or not isinstance(model.categories_json, dict) \ |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
or not getattr(config,'LDT_PLATFORM_BASE_URL',None): |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
return '' |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
output = [] |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
39 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
40 |
for c in model.categories_json.get("categories",[]): |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
41 |
if "subcategories" in c and len(c["subcategories"])>0: |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
42 |
for sc in c["subcategories"]: |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
43 |
output.append({"name": sc["code"],"keywords": [ sc["label"] ], "color" : sc["color"] }) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
44 |
else: |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
45 |
output.append({"name": c["code"],"keywords": [ c["label"] ], "color" : c["color"] }) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
46 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
47 |
base_url = getattr(config,'LDT_PLATFORM_BASE_URL','') + "/ldt/embediframe/?" |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
48 |
url_query_params = {'project_id': model.project_id, 'polemic': 'all', 'polemics_list': json.dumps(output, separators=(',',':'))} |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
49 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
50 |
url = base_url + urllib.urlencode(url_query_params) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
51 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
52 |
return Markup("<a href=\"%s\" target=\"_blank\">%s</a>" % (url,url[:80]+"...")) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
53 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
54 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
class EventView(ModelView): |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
column_searchable_list = ('code', 'label') |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
column_list = ('code', 'label', 'start_date', 'active') |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
58 |
column_filters = ('code', 'label', 'start_date', 'active') |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
59 |
form_create_rules = ('code', 'label', 'description', 'start_date') |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
60 |
inline_models = (models.EventSession,) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
61 |
#form_edit_rules = ('label', 'description', 'start_date', 'active', 'code') |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
62 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
def __init__(self, session, **kwargs): |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
64 |
super(EventView, self).__init__(models.Event, session, **kwargs) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
65 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
66 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
67 |
class DictToJSONField(fields.TextAreaField): |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
68 |
def process_data(self, value): |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
69 |
if value is None: |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
70 |
value = {} |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
71 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
72 |
self.data = json.dumps(value, indent=2) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
73 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
74 |
def process_formdata(self, valuelist): |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
75 |
if valuelist: |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
76 |
self.data = json.loads(valuelist[0]) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
77 |
else: |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
78 |
self.data = '{}' |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
79 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
80 |
class JsonAdminConverter(AdminModelConverter): |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
81 |
@converts('JSON') |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
82 |
def conv_JSON(self, field_args, **extra): |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
83 |
return DictToJSONField(**field_args) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
84 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
85 |
class EventSessionView(ModelView): |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
86 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
column_default_sort = ( models.Event.code, True) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
88 |
column_sortable_list = (('event', models.Event.code),) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
89 |
column_auto_select_related = True |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
90 |
column_list = ('event', 'project_id', 'order', 'categories_json', 'url') |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
91 |
column_filters = ('event',) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
92 |
column_formatters = {'url': build_ldt_url, 'categories_json': lambda v, c, m, p: json.dumps(m.categories_json)[:80] if m.categories_json else ''} |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
93 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
94 |
model_form_converter=JsonAdminConverter |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
95 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
96 |
def __init__(self, session, **kwargs): |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
97 |
super(EventSessionView, self).__init__(models.EventSession, session, **kwargs) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
98 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
99 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
100 |
|
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
101 |
admin.add_view(EventView(database.db_session)) |
e27c3c1c57f1
end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
102 |
admin.add_view(EventSessionView(database.db_session)) |