annot-server/webapp/admin.py
author ymh <ymh.work@gmail.com>
Wed, 22 Oct 2014 01:52:52 +0200
changeset 51 7792f095d94a
parent 43 e27c3c1c57f1
child 52 86d805a56843
permissions -rw-r--r--
correct admin for start_ts
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
e27c3c1c57f1 end 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
51
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
    18
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
    19
e27c3c1c57f1 end 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 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
    21
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
    22
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
    23
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
    24
e27c3c1c57f1 end 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
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
    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
e27c3c1c57f1 end 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
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
    29
    # `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
    30
    # `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
    31
    # `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
    32
    # `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
    33
    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
    34
        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
    35
        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
    36
        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
    37
        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
    38
e27c3c1c57f1 end 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
    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
    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
    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
    42
        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
    43
            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
    44
                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
    45
        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
    46
            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
    47
e27c3c1c57f1 end 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
    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
    49
    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
    50
e27c3c1c57f1 end 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 = 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
    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
    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
    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
e27c3c1c57f1 end 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
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
    57
    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
    58
    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
    59
    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
    60
    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
    61
    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
    62
    #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
    63
e27c3c1c57f1 end 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
    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
    65
        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
    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
e27c3c1c57f1 end 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
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
    69
    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
    70
        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
    71
            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
e27c3c1c57f1 end 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
        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
    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
    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
    76
        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
    77
            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
    78
        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
    79
            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
    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
    81
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
    82
    @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
    83
    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
    84
        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
    85
e27c3c1c57f1 end 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
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
    87
e27c3c1c57f1 end 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_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
    89
    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
    90
    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
    91
    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
    92
    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
    93
    column_formatters = {'url': build_ldt_url, 'categories_json': lambda v, c, m, p: json.dumps(m.categories_json)[:80] if m.categories_json else ''}
51
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
    94
    form_args = {
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
    95
        "start_ts":{ "display_format": '%Y-%m-%dT%H:%M:%S.%f%z'},
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
    96
    }
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
    97
    form_overrides = { "start_ts": dateutil_fields.DateTimeField, }
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
    98
    form_widget_args = {
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
    99
        'categories_json': {
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   100
            'rows': 10,
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   101
            'class': 'input-xlarge'
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   102
        },
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   103
        'start_ts': {
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   104
            'class': 'input-xlarge'
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   105
        },
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   106
        'project_id': {
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   107
            'class': 'input-xlarge'
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   108
        }
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   109
    }
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
   110
e27c3c1c57f1 end 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
    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
   112
e27c3c1c57f1 end 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
    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
   114
        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
   115
e27c3c1c57f1 end 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
e27c3c1c57f1 end 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
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
   119
admin.add_view(EventSessionView(database.db_session))