misc: fill qualification select with formdefs from wcs

This commit is contained in:
Frédéric Péters 2015-07-08 15:04:54 +02:00
parent 7fc561b01a
commit a8244d6c78
7 changed files with 128 additions and 21 deletions

29
welco/forms.py Normal file
View File

@ -0,0 +1,29 @@
# welco - multichannel request processing
# Copyright (C) 2015 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django import forms
from django.utils.translation import ugettext_lazy as _
from .utils import get_wcs_options
class QualificationForm(forms.Form):
formdef_reference = forms.CharField(label=_('Associated Form'))
def __init__(self, *args, **kwargs):
super(QualificationForm, self).__init__(*args, **kwargs)
formdef_references = get_wcs_options('json')
self.fields['formdef_reference'].widget = forms.Select(choices=formdef_references)

View File

@ -105,3 +105,8 @@ div#content .cell.qualif form button.done {
bottom: 1ex;
right: 1ex;
}
div#content .cell.qualif select {
max-width: 100%;
}

View File

@ -68,27 +68,7 @@
</form>
</div>
<div class="cell qualif">
<h2>{% trans 'Qualification' %}</h2>
<form>
<div>
<label>{% trans 'Fill new request:' %}</label>
<select>
<option>--</option>
<option>{% trans 'Municipal Room Reservation' %}</option>
</select>
</div>
<div class="sep">
</div>
<div>
<label>{% trans 'Object:' %}</label>
<input type="text"/>
</div>
<div>
<label>{% trans 'Tag:' %}</label>
<input type="text"/>
</div>
<button class="done">{% trans 'Done' %}</button>
</form>
{{ qualification.render }}
</div>
</div>

View File

@ -0,0 +1,18 @@
{% load i18n %}
<h2>{% trans 'Qualification' %}</h2>
<form>
<div>
{{form.as_p}}
</div>
<div class="sep">
</div>
<div>
<label>{% trans 'Object:' %}</label>
<input type="text"/>
</div>
<div>
<label>{% trans 'Tag:' %}</label>
<input type="text"/>
</div>
<button class="done">{% trans 'Done' %}</button>
</form>

View File

@ -22,6 +22,7 @@ from . import apps
urlpatterns = patterns('',
url(r'^$', 'welco.views.home', name='home'),
url(r'^ajax/qualification$', 'welco.views.qualification'),
url(r'^admin/', include(admin.site.urls)),
url(r'^logout/$', 'welco.views.logout', name='auth_logout'),
url(r'^login/$', 'welco.views.login', name='auth_login'),

59
welco/utils.py Normal file
View File

@ -0,0 +1,59 @@
# welco - multichannel request processing
# Copyright (C) 2015 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import requests
from django.conf import settings
from django.core.cache import cache
def get_wcs_services():
return settings.KNOWN_SERVICES.get('wcs')
def get_wcs_json(wcs_url, path):
if not wcs_url.endswith('/'):
wcs_url += '/'
url = wcs_url + path
response_json = cache.get(url)
if response_json is None:
response_json = requests.get(url, headers={'accept': 'application/json'}).json()
cache.set(url, response_json)
return response_json
def get_wcs_options(url):
references = []
categories = {}
for wcs_key, wcs_site in get_wcs_services().iteritems():
site_title = wcs_site.get('title')
response_json = get_wcs_json(wcs_site.get('url'), url)
if type(response_json) is dict:
response_json = response_json.get('data')
for element in response_json:
slug = element.get('slug')
category = element.get('category') or '-'
title = element.get('title')
if len(get_wcs_services()) == 1:
category_key = category
label = title
else:
category_key = '%s : %s' % (site_title, category)
if not category_key in categories:
categories[category_key] = []
reference = '%s:%s' % (wcs_key, slug)
categories[category_key].append((reference, title))
options = []
for category in sorted(categories.keys()):
options.append((category, sorted(categories[category], lambda x, y: cmp(x[1], y[1]))))
return options

View File

@ -18,6 +18,8 @@ from django.contrib.auth import logout as auth_logout
from django.contrib.auth import views as auth_views
from django.http import HttpResponseRedirect
from django.shortcuts import resolve_url
from django import template
from django.template import RequestContext
from django.views.generic import TemplateView
try:
@ -26,6 +28,7 @@ except ImportError:
get_idps = lambda: []
from sources.mail.views import Home as MailHome
from .forms import QualificationForm
def login(request, *args, **kwargs):
@ -47,12 +50,24 @@ def logout(request, next_page=None):
return HttpResponseRedirect(next_page)
class Qualification(object):
def __init__(self, request):
self.request = request
def render(self):
context = RequestContext(self.request)
context['form'] = QualificationForm()
tmpl = template.loader.get_template('welco/qualification.html')
return tmpl.render(context)
class Home(TemplateView):
template_name = 'welco/home.html'
def get_context_data(self, **kwargs):
context = super(Home, self).get_context_data(**kwargs)
context['source'] = MailHome(self.request)
context['qualification'] = Qualification(self.request)
return context
home = Home.as_view()