annot-server/webapp/admin.py
author ymh <ymh.work@gmail.com>
Wed, 22 Oct 2014 08:53:33 +0200
changeset 52 86d805a56843
parent 51 7792f095d94a
child 53 7e3156578f28
permissions -rw-r--r--
add utc tzinfo for start_ts when not provided
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
    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",[]):
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
        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
    45
            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
    46
                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
    47
        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
    48
            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
    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/?"
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
    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
    52
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
    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
    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
    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
    56
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
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
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
    59
    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
    60
    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
    61
    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
    62
    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
    63
    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
    64
    #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
    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
    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
    67
        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
    68
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
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
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
    71
    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
    72
        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
    73
            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
    74
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
        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
    76
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
    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
    78
        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
    79
            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
    80
        else:
52
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    81
            self.data = None
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    82
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
    83
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
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
    85
    @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
    86
    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
    87
        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
    88
52
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    89
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    90
class StartTSField(dateutil_fields.DateTimeField):
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    91
    def __init__(self, *args, **kwargs):
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    92
        if 'display_format' not in kwargs:
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    93
            kwargs['display_format']='%Y-%m-%dT%H:%M:%S.%f%z'
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    94
        super(StartTSField, self).__init__(*args, **kwargs)
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    95
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    96
    def process_formdata(self, valuelist):
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    97
        super(StartTSField, self).process_formdata(valuelist)
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    98
        if self.data and self.data.tzinfo is None:
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    99
            self.data = self.data.replace(tzinfo=tzutc())
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
   100
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
   101
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
   102
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
   103
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
    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
   105
    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
   106
    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
   107
    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
   108
    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
   109
    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
   110
    form_overrides = { "start_ts": StartTSField, }
51
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   111
    form_widget_args = {
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   112
        'categories_json': {
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   113
            'rows': 10,
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   114
            'class': 'input-xlarge'
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   115
        },
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   116
        'start_ts': {
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   117
            'class': 'input-xlarge'
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   118
        },
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   119
        'project_id': {
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   120
            'class': 'input-xlarge'
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   121
        }
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   122
    }
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
   123
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
   124
    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
   125
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
   126
    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
   127
        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
   128
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
   129
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
   130
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
   131
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
   132
admin.add_view(EventSessionView(database.db_session))