From c2752c6700b2ee24ab1388850c7d5c407e0fd398 Mon Sep 17 00:00:00 2001 From: Patrick Samson Date: Mon, 13 Aug 2012 21:53:12 +0200 Subject: [PATCH] Adjustments for integration with version 1.2.x of django-ajax-selects, in addition to 1.1.x --- postman/fields.py | 7 +- .../autocomplete_postman_multiple.html | 13 ---- .../autocomplete_postman_multiple_as1-1.html | 26 +++++++ .../autocomplete_postman_single.html | 12 ---- .../autocomplete_postman_single_as1-1.html | 23 ++++++ postman/templates/postman/base_write.html | 26 +++++-- postman/test_urls.py | 6 +- postman/tests.py | 71 +++++++++++++++++-- postman/views.py | 6 +- 9 files changed, 146 insertions(+), 44 deletions(-) delete mode 100644 postman/templates/autocomplete_postman_multiple.html create mode 100644 postman/templates/autocomplete_postman_multiple_as1-1.html delete mode 100644 postman/templates/autocomplete_postman_single.html create mode 100644 postman/templates/autocomplete_postman_single_as1-1.html diff --git a/postman/fields.py b/postman/fields.py index c2c70a8..9fd250c 100644 --- a/postman/fields.py +++ b/postman/fields.py @@ -97,10 +97,13 @@ field_name = d.get('field', 'AutoCompleteField') arg_name = d.get('arg_name', 'channel') arg_default = d.get('arg_default') # the minimum to declare to enable the feature +autocompleter_app = {} if app_name in settings.INSTALLED_APPS and arg_default: + autocompleter_app['is_active'] = True + autocompleter_app['name'] = app_name + autocompleter_app['version'] = getattr(__import__(app_name, globals(), locals(), ['__version__']), '__version__', None) # does something like "from ajax_select.fields import AutoCompleteField" auto_complete_field = getattr(__import__(app_name + '.fields', globals(), locals(), [field_name]), field_name) - is_autocompleted = True class CommaSeparatedUserField(BasicCommaSeparatedUserField, auto_complete_field): def __init__(self, *args, **kwargs): @@ -116,5 +119,5 @@ if app_name in settings.INSTALLED_APPS and arg_default: setattr(self.widget, arg_name, value) else: + autocompleter_app['is_active'] = False CommaSeparatedUserField = BasicCommaSeparatedUserField - is_autocompleted = False diff --git a/postman/templates/autocomplete_postman_multiple.html b/postman/templates/autocomplete_postman_multiple.html deleted file mode 100644 index 798c7c4..0000000 --- a/postman/templates/autocomplete_postman_multiple.html +++ /dev/null @@ -1,13 +0,0 @@ -{% extends "autocomplete.html" %} -{% block script %} - $('#{{ html_id }}').autocomplete('{{ lookup_url }}', { - width: 320, - formatItem: function(row) { return row[1]; }, - formatResult: function(row) { return row[2]; }, - multiple: true, - dataType: "text" - }) - $('#{{ html_id }}').result(function(event, data, formatted) { - $('#{{ html_id }}').trigger("added"); - }) -{% endblock %} \ No newline at end of file diff --git a/postman/templates/autocomplete_postman_multiple_as1-1.html b/postman/templates/autocomplete_postman_multiple_as1-1.html new file mode 100644 index 0000000..d9d2930 --- /dev/null +++ b/postman/templates/autocomplete_postman_multiple_as1-1.html @@ -0,0 +1,26 @@ +{% extends "autocomplete.html" %}{% comment %} +This is a custom template for django-ajax-selects version 1.1.4/5 (not for 1.2+). +Channel: postman_multiple_as1-1 +Form Field: AutoCompleteField +Usage: Entering of multiple values. + +There is no such template provided in the django-ajax-selects application. +Differences with the default template: +- it uses the "multiple: true" option of the jquery-plugin-autocomplete +- it fixes the issue http://code.google.com/p/django-ajax-selects/issues/detail?id=57 +Note: this template is also used in the test suite. + +{% endcomment %} +{% block script %} + $('#{{ html_id }}').autocomplete('{{ lookup_url }}', { + width: 320, + formatItem: function(row) { return row[1]; }, + formatResult: function(row) { return row[2]; }, + multiple: true, + dataType: "text" + }) + $('#{{ html_id }}').result(function(event, data, formatted) { + $('#{{ html_id }}').trigger("added"); + }) +{% block extra_script %}{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/postman/templates/autocomplete_postman_single.html b/postman/templates/autocomplete_postman_single.html deleted file mode 100644 index 979e068..0000000 --- a/postman/templates/autocomplete_postman_single.html +++ /dev/null @@ -1,12 +0,0 @@ -{% extends "autocomplete.html" %} -{% block script %} - $('#{{ html_id }}').autocomplete('{{ lookup_url }}', { - width: 320, - formatItem: function(row) { return row[1]; }, - formatResult: function(row) { return row[2]; }, - dataType: "text" - }) - $('#{{ html_id }}').result(function(event, data, formatted) { - $('#{{ html_id }}').trigger("added"); - }) -{% endblock %} \ No newline at end of file diff --git a/postman/templates/autocomplete_postman_single_as1-1.html b/postman/templates/autocomplete_postman_single_as1-1.html new file mode 100644 index 0000000..2b5dc0c --- /dev/null +++ b/postman/templates/autocomplete_postman_single_as1-1.html @@ -0,0 +1,23 @@ +{% extends "autocomplete.html" %}{% comment %} +This is a custom template for django-ajax-selects version 1.1.4/5 (not for 1.2+). +Channel: postman_single_as1-1 +Form Field: AutoCompleteField +Usage: Basic entering of a single value. + +It is the same as the default template, except that it fixes the issue: +http://code.google.com/p/django-ajax-selects/issues/detail?id=57 +Note: this template is also used in the test suite. + +{% endcomment %} +{% block script %} + $('#{{ html_id }}').autocomplete('{{ lookup_url }}', { + width: 320, + formatItem: function(row) { return row[1]; }, + formatResult: function(row) { return row[2]; }, + dataType: "text" + }) + $('#{{ html_id }}').result(function(event, data, formatted) { + $('#{{ html_id }}').trigger("added"); + }) +{% block extra_script %}{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/postman/templates/postman/base_write.html b/postman/templates/postman/base_write.html index e188e48..4a30c38 100644 --- a/postman/templates/postman/base_write.html +++ b/postman/templates/postman/base_write.html @@ -1,13 +1,25 @@ {% extends "postman/base.html" %} {% load i18n %} {% block extrahead %}{{ block.super }} -{% if is_autocompleted %} -{# using the available admin jQuery is enough #} -{# dj v1.4 #}{% load static %} -{# dj v1.3 #}{# #} -{# #} - - +{% if autocompleter_app.is_active %} +{# dj v1.4 #}{# {% load static %} #} + +{% if autocompleter_app.name == "ajax_select" %}{% if autocompleter_app.version == "1.1.4" or autocompleter_app.version == "1.1.5" %} +{# else: for version 1.2.x use AJAX_SELECT_BOOTSTRAP + AJAX_SELECT_INLINES or arrange to include jqueryUI/js/css #}{% endif %}{% endif %} {% endif %} {% endblock %} {% block content %} diff --git a/postman/test_urls.py b/postman/test_urls.py index 611014c..f47bb02 100644 --- a/postman/test_urls.py +++ b/postman/test_urls.py @@ -6,7 +6,7 @@ from django.conf import settings try: from django.conf.urls import patterns, include, url # django 1.4 except ImportError: - from django.conf.urls.defaults import patterns, include, url # django 1.3 + from django.conf.urls.defaults import * # "patterns, include, url" is enough for django 1.3, "*" for django 1.2 from django.forms import ValidationError from django.views.generic.simple import redirect_to @@ -101,8 +101,8 @@ postman_patterns = patterns('postman.views', url(r'^reply_formatters/(?P[\d]+)/$', 'reply', {'formatters': (format_subject,format_body)}, name='postman_reply_formatters'), url(r'^view_formatters/(?P[\d]+)/$', 'view', {'formatters': (format_subject,format_body)}, name='postman_view_formatters'), # auto-complete - url(r'^write_ac/(?:(?P[\w.@+-:]+)/)?$', 'write', {'autocomplete_channels': ('postman_multiple', None)}, name='postman_write_auto_complete'), - url(r'^reply_ac/(?P[\d]+)/$', 'reply', {'autocomplete_channel': 'postman_multiple'}, name='postman_reply_auto_complete'), + url(r'^write_ac/(?:(?P[\w.@+-:]+)/)?$', 'write', {'autocomplete_channels': ('postman_multiple_as1-1', None)}, name='postman_write_auto_complete'), + url(r'^reply_ac/(?P[\d]+)/$', 'reply', {'autocomplete_channel': 'postman_multiple_as1-1'}, name='postman_reply_auto_complete'), # 'template_name' url(r'^inbox_template/(?:(?P