author | ymh <ymh.work@gmail.com> |
Sat, 25 Oct 2014 00:42:30 +0200 | |
changeset 64 | 2f5d0e9fe7b5 |
parent 63 | 289049253263 |
child 66 | 658561ea9e65 |
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 |
|
52
86d805a56843
add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
9 |
from dateutil.tz import tzutc |
86d805a56843
add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
10 |
|
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
|
11 |
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
|
12 |
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
|
13 |
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
|
14 |
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
|
15 |
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
|
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 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
|
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 |
from wtforms import fields |
51 | 20 |
from wtforms.ext.dateutil import fields as dateutil_fields |
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
|
21 |
|
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 |
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
|
23 |
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
|
24 |
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
|
25 |
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
|
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 |
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
|
28 |
|
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 |
|
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 |
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
|
31 |
# `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
|
32 |
# `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
|
33 |
# `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
|
34 |
# `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
|
35 |
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
|
36 |
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
|
37 |
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
|
38 |
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
|
39 |
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
|
40 |
|
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 |
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
|
42 |
|
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 |
for c in model.categories_json.get("categories",[]): |
61 | 44 |
#if "subcategories" in c and len(c["subcategories"])>0: |
45 |
# for sc in c["subcategories"]: |
|
46 |
# output.append({"name": sc["code"],"keywords": [ sc["label"] ], "color" : sc["color"] }) |
|
47 |
#else: |
|
48 |
output.append({"name": c["code"] if "code" in c else "","keywords": [ c["label"] ], "color" : c["color"] }) |
|
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
|
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 |
base_url = getattr(config,'LDT_PLATFORM_BASE_URL','') + "/ldt/embediframe/?" |
64 | 51 |
url_query_params = {'project_id': model.project_id, |
52 |
'polemic': 'all', |
|
53 |
'polemic__max_elements': 200, |
|
61 | 54 |
'createannotation': True, |
55 |
'createannotation_polemics': False, |
|
62 | 56 |
'tag_titles': "", |
61 | 57 |
'polemics_list': json.dumps(output, separators=(',',':'))} |
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
|
58 |
|
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 |
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
|
60 |
|
61 | 61 |
embed = "<iframe src="%s" width="570" height="711" seamless="seamless"></iframe>" % url |
62 |
||
63 |
return Markup("<a href=\"%s\" target=\"_blank\">Lien</a> :<br/><input value=\"%s\"/><br/>Code embed :<input value=\"%s\"/>" % (url, url, embed)) |
|
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
|
64 |
|
63
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
65 |
class StartTSField(dateutil_fields.DateTimeField): |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
66 |
def __init__(self, *args, **kwargs): |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
67 |
if 'display_format' not in kwargs: |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
68 |
kwargs['display_format']='iso' |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
69 |
super(StartTSField, self).__init__(*args, **kwargs) |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
70 |
|
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
71 |
def _value(self): |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
72 |
if self.raw_data: |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
73 |
return ' '.join(self.raw_data) |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
74 |
elif self.display_format == 'iso': |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
75 |
return self.data and self.data.isoformat() or '' |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
76 |
else: |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
77 |
return self.data and self.data.strftime(self.display_format) or '' |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
78 |
|
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
79 |
def process_formdata(self, valuelist): |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
80 |
super(StartTSField, self).process_formdata(valuelist) |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
81 |
if self.data and self.data.tzinfo is None: |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
82 |
self.data = self.data.replace(tzinfo=tzutc()) |
289049253263
reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents:
62
diff
changeset
|
83 |
|
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
|
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 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
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
form_create_rules = ('code', 'label', 'description', 'start_date') |
64 | 90 |
inline_models = [(models.EventSession, {'form_overrides': { "start_ts": StartTSField, },})] |
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
|
91 |
|
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 |
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
|
93 |
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
|
94 |
|
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 |
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
|
97 |
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
|
98 |
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
|
99 |
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
|
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 |
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
|
102 |
|
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
|
103 |
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
|
104 |
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
|
105 |
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
|
106 |
else: |
52
86d805a56843
add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
107 |
self.data = None |
86d805a56843
add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
108 |
|
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
|
109 |
|
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
|
110 |
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
|
111 |
@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
|
112 |
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
|
113 |
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
|
114 |
|
52
86d805a56843
add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
115 |
|
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
|
116 |
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
|
117 |
|
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
|
118 |
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
|
119 |
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
|
120 |
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
|
121 |
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
|
122 |
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
|
123 |
column_formatters = {'url': build_ldt_url, 'categories_json': lambda v, c, m, p: json.dumps(m.categories_json)[:80] if m.categories_json else ''} |
52
86d805a56843
add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents:
51
diff
changeset
|
124 |
form_overrides = { "start_ts": StartTSField, } |
51 | 125 |
form_widget_args = { |
126 |
'categories_json': { |
|
127 |
'rows': 10, |
|
128 |
'class': 'input-xlarge' |
|
129 |
}, |
|
130 |
'start_ts': { |
|
131 |
'class': 'input-xlarge' |
|
132 |
}, |
|
133 |
'project_id': { |
|
134 |
'class': 'input-xlarge' |
|
135 |
} |
|
136 |
} |
|
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
|
137 |
|
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
|
138 |
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
|
139 |
|
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
|
140 |
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
|
141 |
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
|
142 |
|
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
|
143 |
|
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
|
144 |
|
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
|
145 |
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
|
146 |
admin.add_view(EventSessionView(database.db_session)) |