From ef4a13bd49ea9afa286381bb11510efb1b0d76e9 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 29 Dec 2010 20:04:51 +0100 Subject: Creation of archaelogical file (refs #14) - integration of an AJAX autocomplete field --- docs/src/INSTALL.t2t | 1 + ishtar/furnitures/context_processors.py | 1 + ishtar/furnitures/forms.py | 9 +++- ishtar/furnitures/models.py | 21 +++++---- ishtar/furnitures/urls.py | 6 ++- ishtar/furnitures/views.py | 58 +++++++++++-------------- ishtar/furnitures/widgets.py | 77 +++++++++++++++++++++++++++++++++ ishtar/settings.py.example | 1 + ishtar/templates/base.html | 2 + ishtar/templates/file_wizard.html | 4 +- 10 files changed, 134 insertions(+), 46 deletions(-) create mode 100644 ishtar/furnitures/widgets.py diff --git a/docs/src/INSTALL.t2t b/docs/src/INSTALL.t2t index 68dcc462f..9e147bcb2 100644 --- a/docs/src/INSTALL.t2t +++ b/docs/src/INSTALL.t2t @@ -17,6 +17,7 @@ Last update: %%date(%m-%d-%Y) - [django-simple-history https://bitbucket.org/q/django-simple-history/src] version 1.0 - registration - libjs-jquery +- libjs-jquery-ui To install django-simple-history: ``` diff --git a/ishtar/furnitures/context_processors.py b/ishtar/furnitures/context_processors.py index 5711e5341..92e957af0 100644 --- a/ishtar/furnitures/context_processors.py +++ b/ishtar/furnitures/context_processors.py @@ -33,5 +33,6 @@ def get_base_context(request): dct['CURRENT_ACTION'] = request.session['CURRENT_ACTION'] dct['MENU'] = request.session['MENU'] dct['JQUERY_URL'] = settings.JQUERY_URL + dct['JQUERY_UI_URL'] = settings.JQUERY_UI_URL return dct diff --git a/ishtar/furnitures/forms.py b/ishtar/furnitures/forms.py index e547c6ffb..367e97cb1 100644 --- a/ishtar/furnitures/forms.py +++ b/ishtar/furnitures/forms.py @@ -22,6 +22,7 @@ Forms definition """ import datetime +from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _ from django.template import Context from django.shortcuts import render_to_response @@ -31,6 +32,11 @@ from merlin.wizards.utils import Step as BasicStep from merlin.wizards.session import SessionWizard import models +import widgets + +from django.utils.functional import lazy + +reverse_lazy = lazy(reverse, unicode) class Step(BasicStep): """ @@ -61,7 +67,8 @@ class FileWizard(Wizard): }) class FileForm1(forms.Form): - in_charge = forms.IntegerField(label=_("Person in charge")) + in_charge = forms.IntegerField(label=_("Person in charge"), + widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-person'))) year = forms.IntegerField(label=_("Year"), initial=lambda:datetime.datetime.now().year) internal_reference = forms.CharField(label=_(u"Internal reference"), diff --git a/ishtar/furnitures/models.py b/ishtar/furnitures/models.py index f770fc6c6..d3b942102 100644 --- a/ishtar/furnitures/models.py +++ b/ishtar/furnitures/models.py @@ -85,13 +85,17 @@ class Departement(models.Model): return unicode(self.number) + u" - " + self.label class Address(BaseHistorizedItem): - address = models.TextField(_(u"Address")) - address_complement = models.TextField(_(u"Address complement")) - postal_code = models.CharField(_(u"Postal code"), max_length=10) - town = models.CharField(_(u"Town"), max_length=30) - country = models.CharField(_(u"Country"), max_length=30) - phone = models.CharField(_(u"Phone"), max_length=18) - mobile_phone = models.CharField(_(u"Mobile phone"), max_length=18) + address = models.TextField(_(u"Address"), null=True, blank=True) + address_complement = models.TextField(_(u"Address complement"), null=True, + blank=True) + postal_code = models.CharField(_(u"Postal code"), max_length=10, null=True, + blank=True) + town = models.CharField(_(u"Town"), max_length=30, null=True, blank=True) + country = models.CharField(_(u"Country"), max_length=30, null=True, + blank=True) + phone = models.CharField(_(u"Phone"), max_length=18, null=True, blank=True) + mobile_phone = models.CharField(_(u"Mobile phone"), max_length=18, + null=True, blank=True) history = HistoricalRecords() class Meta: @@ -134,7 +138,7 @@ class Person(Address, OwnPerms) : email = models.CharField(_(u"Email"), max_length=40) person_type = models.ForeignKey(PersonType, verbose_name=_(u"Type")) attached_to = models.ForeignKey('Organization', - verbose_name=_(u"Is attached to")) + verbose_name=_(u"Is attached to"), blank=True, null=True) is_author = models.NullBooleanField(_(u"Is an author?"), blank=True, null=True) in_charge_storage = models.NullBooleanField(_(u"In charge of a storage?"), @@ -144,6 +148,7 @@ class Person(Address, OwnPerms) : verbose_name = _(u"Person") verbose_name_plural = _(u"Persons") permissions = ( + ("view_person", ugettext(u"Can view Person")), ("view_own_person", ugettext(u"Can view own Person")), ("add_own_person", ugettext(u"Can add own Person")), ("change_own_person", ugettext(u"Can change own Person")), diff --git a/ishtar/furnitures/urls.py b/ishtar/furnitures/urls.py index 0b3cd6c16..ab347e548 100644 --- a/ishtar/furnitures/urls.py +++ b/ishtar/furnitures/urls.py @@ -31,8 +31,10 @@ actions = r"|".join(actions) urlpatterns += patterns('ishtar.furnitures.views', url(BASE_URL + r'(?P' + actions + r')/(?P[A-Za-z0-9_-]+)/' +\ - r'(?P\d)/$', 'action', name='action_bounded_form'), + r'(?P\d)/$', 'action', name='action-bounded-form'), url(BASE_URL + r'(?P' + actions + r')/(?P[A-Za-z0-9_-]+)/$', - 'action', name='action_form'), + 'action', name='action-form'), url(BASE_URL + r'(?P' + actions + r')/$', 'action', name='action'), + url(BASE_URL + r'autocomplete/$', 'autocomplete_person', + name='autocomplete-person'), ) diff --git a/ishtar/furnitures/views.py b/ishtar/furnitures/views.py index 5ea71cc86..f9157d9d6 100644 --- a/ishtar/furnitures/views.py +++ b/ishtar/furnitures/views.py @@ -21,14 +21,18 @@ Furnitures views """ +import json from django.http import HttpResponse from django.template import RequestContext from django.shortcuts import render_to_response, redirect from django.utils.translation import ugettext, ugettext_lazy as _ +from django.db.models import Q +from django.core import serializers from ishtar import settings from menus import menu from forms import Step, FileForm1, FileForm2, FileWizard +import models def index(request): """ @@ -43,38 +47,26 @@ def check_permission(request, action_slug, obj_id=None): return menu.items[action_slug].is_available(request.user, obj_id) return menu.items[action_slug].can_be_available(request.user) -''' -def base_action(request, action_slug, obj_id=None, *args, **kwargs): - """ - Basic action management - """ - if not check_permission(request, action_slug, obj_id): - not_permitted_msg = ugettext(u"Operation not permitted.") - return HttpResponse(not_permitted_msg) - request.session['CURRENT_ACTION'] = action_slug - associated_wizard = action_slug + '_wizard' - dct = {} - print base_action - globals_dct = globals() - if associated_wizard in globals_dct: - wizard = globals_dct[associated_wizard] - current_step = None - if wizard.id in request.session \ - and 'current_step' in request.session[wizard.id] \ - and request.session[wizard.id]['current_step']: - current_step = request.session[wizard.id]['current_step'].slug - else: - current_step = wizard.base_steps[0].slug - return action(request, action_slug, obj_id=obj_id, *args, **kwargs) - if obj_id: - return redirect('action', action_slug, current_step, obj_id) - return redirect('action', action_slug, current_step) - - if action_slug in globals_dct: - return globals_dct[action](request, dct, obj_id, *args, **kwargs) - return render_to_response('index.html', dct, - context_instance=RequestContext(request)) -''' +def autocomplete_person(request): + if not request.user.has_perm('furnitures.view_person'): + return HttpResponse(mimetype='text/plain') + if not request.GET.get('term'): + return HttpResponse(mimetype='text/plain') + q = request.GET.get('term') + limit = request.GET.get('limit', 15) + try: + limit = int(limit) + except ValueError: + return HttpResponseBadRequest() + query = Q() + for q in q.split(' '): + query = query | Q(name__istartswith=q) | Q(surname__istartswith=q) | \ + Q(email__icontains=q) + persons = models.Person.objects.filter(query)[:limit] + data = json.dumps([{'id':person.pk, + 'value':"%s %s - %s" % (person.name, person.surname, person.email)} + for person in persons]) + return HttpResponse(data, mimetype='text/plain') def action(request, action_slug, obj_id=None, *args, **kwargs): """ @@ -97,7 +89,7 @@ def action(request, action_slug, obj_id=None, *args, **kwargs): current_step = request.session[wizard.id]['current_step'].slug else: current_step = wizard.base_steps[0].slug - return redirect('action_form', action_slug, current_step) + return redirect('action-form', action_slug, current_step) elif wizard.id in request.session: for step in wizard.base_steps: if step.slug == kwargs['slug']: diff --git a/ishtar/furnitures/widgets.py b/ishtar/furnitures/widgets.py new file mode 100644 index 000000000..bbcb58423 --- /dev/null +++ b/ishtar/furnitures/widgets.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (C) 2010 Étienne Loks +# Copyright (C) 2007 skam +# (http://djangosnippets.org/snippets/233/) + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU 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 General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# See the file COPYING for details. +from django import forms +from django.forms.widgets import flatatt +from django.utils.encoding import smart_unicode +from django.utils.html import escape +from django.utils.simplejson import JSONEncoder + +class JQueryAutoComplete(forms.TextInput): + def __init__(self, source, options={}, attrs={}): + """ + Source can be a list containing the autocomplete values or a + string containing the url used for the request. + """ + self.options = None + self.attrs = {} + self.source = source + if len(options) > 0: + self.options = JSONEncoder().encode(options) + self.attrs.update(attrs) + + def render_js(self, field_id): + if isinstance(self.source, list): + source = JSONEncoder().encode(self.source) + elif isinstance(self.source, str) or isinstance(self.source, unicode): + source = "'%s'" % escape(self.source) + else: + try: + source = "'" + unicode(self.source) +"'" + except: + raise ValueError('source type is not valid') + options = 'source : ' + source + options += ''', select: function( event, ui ) { + $("#result").html(ui.item ? + "Selected: " + ui.item.value + " aka " + ui.item.id : + "Nothing selected, input was " + this.value ); + },minLength: 2 + + ''' + if self.options: + options += ',%s' % self.options + + return u'$(\'#%s\').autocomplete({%s});' % (field_id, options) + + def render(self, name, value=None, attrs=None): + final_attrs = self.build_attrs(attrs, name=name) + if value: + final_attrs['value'] = escape(smart_unicode(value)) + + if not self.attrs.has_key('id'): + final_attrs['id'] = 'id_%s' % name + return u'''

+ + ''' % { + 'attrs' : flatatt(final_attrs), + 'js' : self.render_js(final_attrs['id']), + } + diff --git a/ishtar/settings.py.example b/ishtar/settings.py.example index facc76ff2..4cb1a710c 100644 --- a/ishtar/settings.py.example +++ b/ishtar/settings.py.example @@ -7,6 +7,7 @@ APP_NAME = "" ROOT_PATH = "/var/local/webapp/ishtar/ishtar/" URL_PATH = "" JQUERY_URL = "/javascript/jquery/jquery.js" +JQUERY_UI_URL = "/javascript/jquery-ui/" LOGIN_REDIRECT_URL = "/" + URL_PATH DEBUG = True diff --git a/ishtar/templates/base.html b/ishtar/templates/base.html index 12954a70d..726520c54 100644 --- a/ishtar/templates/base.html +++ b/ishtar/templates/base.html @@ -9,7 +9,9 @@ {% block title %}Ishtar{% if APP_NAME %} - {{APP_NAME}}{%endif%}{% endblock %} + + diff --git a/ishtar/templates/file_wizard.html b/ishtar/templates/file_wizard.html index aa19420e2..4bb6a59c8 100644 --- a/ishtar/templates/file_wizard.html +++ b/ishtar/templates/file_wizard.html @@ -4,9 +4,9 @@ {% block content %}
{% csrf_token %} -- cgit v1.2.3