annot-server/webapp/admin.py
author rougeronj
Thu, 22 Jan 2015 09:26:43 +0100
changeset 111 a7b72620d227
parent 71 fb4ad86bc7f2
permissions -rw-r--r--
Add variable "wait". When this variable set, the annotsroll wait ignore some annotations, and wait before printing an otherone so there is no superposition. Can be passed as an options
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
66
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
     7
import logging
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
     8
from StringIO import StringIO
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
     9
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
    10
52
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    11
from dateutil.tz import tzutc
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    12
66
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    13
from flask import url_for, render_template, request
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    14
from flask.ext.admin import Admin, BaseView, expose
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
    15
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
    16
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
    17
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
    18
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
    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
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
    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
from wtforms import fields
51
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
    23
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
    24
66
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    25
import config
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
    26
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
    27
import models
66
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    28
import utils
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
    29
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
    30
e27c3c1c57f1 end 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
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
    32
e27c3c1c57f1 end 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
e27c3c1c57f1 end 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
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
    35
    # `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
    36
    # `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
    37
    # `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
    38
    # `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
    39
    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
    40
        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
    41
        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
    42
        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
    43
        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
    44
e27c3c1c57f1 end 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 = []
e27c3c1c57f1 end 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
    for c in model.categories_json.get("categories",[]):
61
511bff609c3d add iframe and correct url in eventsession admi
cavaliet
parents: 60
diff changeset
    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
2f5d0e9fe7b5 correct syntax error
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    51
    url_query_params = {'project_id': model.project_id,
2f5d0e9fe7b5 correct syntax error
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    52
                        'polemic': 'all',
2f5d0e9fe7b5 correct syntax error
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    53
                        'polemic__max_elements': 200,
61
511bff609c3d add iframe and correct url in eventsession admi
cavaliet
parents: 60
diff changeset
    54
                        'createannotation': True,
511bff609c3d add iframe and correct url in eventsession admi
cavaliet
parents: 60
diff changeset
    55
                        'createannotation_polemics': False,
62
cavaliet
parents: 61
diff changeset
    56
                        'tag_titles': "",
61
511bff609c3d add iframe and correct url in eventsession admi
cavaliet
parents: 60
diff changeset
    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
71
fb4ad86bc7f2 embed code
cavaliet
parents: 67
diff changeset
    61
    embed = "<iframe src=&quot;%s&quot; width=&quot;570&quot; height=&quot;711&quot; seamless=&quot;seamless&quot; frameborder=&quot;0&quot;></iframe>" % url
61
511bff609c3d add iframe and correct url in eventsession admi
cavaliet
parents: 60
diff changeset
    62
511bff609c3d add iframe and correct url in eventsession admi
cavaliet
parents: 60
diff changeset
    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
66
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    65
def build_sync_form(view, context, model, name):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    66
    # `view` is current administrative view
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    67
    # `context` is instance of jinja2.runtime.Context
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    68
    # `model` is model instance
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    69
    # `name` is property name
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    70
    if not model.project_id \
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    71
        or not model.start_ts \
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    72
        or not getattr(config,'LDT_PLATFORM_BASE_URL',None):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    73
        return ''
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    74
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    75
    url = url_for('eventsessionview.sync')
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    76
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    77
    return Markup(render_template('admin/sync_form.html', model=model, url=url))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    78
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
    79
63
289049253263 reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    80
class StartTSField(dateutil_fields.DateTimeField):
289049253263 reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    81
    def __init__(self, *args, **kwargs):
289049253263 reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    82
        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
    83
            kwargs['display_format']='iso'
289049253263 reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    84
        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
    85
289049253263 reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    86
    def _value(self):
289049253263 reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    87
        if self.raw_data:
289049253263 reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    88
            return ' '.join(self.raw_data)
289049253263 reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    89
        elif self.display_format == 'iso':
289049253263 reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    90
            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
    91
        else:
289049253263 reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    92
            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
    93
289049253263 reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    94
    def process_formdata(self, valuelist):
289049253263 reindroduce the start_ts in the eventsession iline view
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    95
        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
    96
        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
    97
            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
    98
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
    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
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
   101
    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
   102
    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
   103
    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
   104
    form_create_rules = ('code', 'label', 'description', 'start_date')
64
2f5d0e9fe7b5 correct syntax error
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
   105
    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
   106
e27c3c1c57f1 end 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
    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
   108
        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
   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
e27c3c1c57f1 end 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
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
   112
    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
   113
        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
   114
            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
   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
        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
   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
    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
   119
        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
   120
            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
   121
        else:
52
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
   122
            self.data = None
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
   123
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
   124
e27c3c1c57f1 end 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
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
   126
    @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
   127
    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
   128
        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
   129
52
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
   130
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
   131
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
   132
e27c3c1c57f1 end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
    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
   134
    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
   135
    column_auto_select_related = True
66
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   136
    column_list = ('event', 'project_id', 'order', 'categories_json', 'url', 'sync')
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
    column_filters = ('event',)
66
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   138
    column_formatters = {
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   139
        'sync': build_sync_form,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   140
        'url': build_ldt_url,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   141
        'categories_json': lambda v, c, m, p: json.dumps(m.categories_json)[:80] if m.categories_json else ''
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   142
    }
52
86d805a56843 add utc tzinfo for start_ts when not provided
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
   143
    form_overrides = { "start_ts": StartTSField, }
51
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   144
    form_widget_args = {
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   145
        'categories_json': {
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   146
            'rows': 10,
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   147
            'class': 'input-xlarge'
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   148
        },
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   149
        'start_ts': {
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   150
            'class': 'input-xlarge'
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   151
        },
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   152
        'project_id': {
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   153
            'class': 'input-xlarge'
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   154
        }
7792f095d94a correct admin for start_ts
ymh <ymh.work@gmail.com>
parents: 43
diff changeset
   155
    }
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
   156
e27c3c1c57f1 end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
    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
   158
e27c3c1c57f1 end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
    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
   160
        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
   161
66
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   162
    @expose("/sync/",methods=('POST', ))
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   163
    def sync(self):
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   164
        event_session = database.db_session.query(models.EventSession).filter_by(id=int(request.form['event_session_id'])).first()
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   165
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   166
        if not event_session:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   167
            abort(404)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   168
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   169
        stream = StringIO()
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   170
        handler = logging.StreamHandler(stream)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   171
        logger = logging.getLogger('sync_logger')
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   172
        logger.setLevel(logging.DEBUG)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   173
        for h in logger.handlers:
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   174
            logger.removeHandler(h)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   175
        logger.addHandler(handler)
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   176
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   177
        sync_args = {
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   178
            'start_date'   : event_session.start_ts,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   179
            'duration'     : event_session.duration or None,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   180
            'events'       : [event_session.event.code],
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   181
            'channels'     : [utils.AnnotationsSynchronizer.DEFAULT_ANNOTATION_CHANNEL],
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   182
            'project_id'   : event_session.project_id,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   183
            'annot_url'    : url_for('annotationapi0.annotationapi', _external=True),
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   184
            'post_param'   : getattr(config, 'SYNC_POST_PARAM', {}),
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   185
            'base_url'     : getattr(config, 'LDT_PLATFORM_BASE_URL', "http://ldt.iri.centrepompidou.fr/ldtplatform/"),
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   186
            'replace'      : True,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   187
            'merge'        : False,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   188
            'batch_size'   : 500,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   189
            'logger'       : logger,
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   190
        }
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   191
67
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   192
        error = None
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   193
        try:
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   194
            sync = utils.AnnotationsSynchronizer(**sync_args)
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   195
            sync.export_annotations()
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   196
        except Exception as e:
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   197
            error = repr(e) + " - " + str(e)
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   198
66
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   199
        logs = [ line for line in stream.getvalue().split("\n")]
658561ea9e65 export sync code in utils, and add button to synchronize annotation on eventsessionview
ymh <ymh.work@gmail.com>
parents: 64
diff changeset
   200
67
7db9c7ec691a correct getting duration from content. try to improve error management
ymh <ymh.work@gmail.com>
parents: 66
diff changeset
   201
        return self.render('admin/sync_event_session.html', event_session=event_session, sync_args=repr(sync_args), error=error, logs=logs)
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
   202
e27c3c1c57f1 end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
e27c3c1c57f1 end of admin. change the index page and add a redirect to it on the landing page
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
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
   205
admin.add_view(EventSessionView(database.db_session))