]> git.parisson.com Git - mezzo.git/commitdiff
Autocomplete : add event info while searching : is_parent + date
authorEmilie <zawadzki@ircam.fr>
Wed, 23 Nov 2016 17:54:41 +0000 (18:54 +0100)
committerEmilie <zawadzki@ircam.fr>
Wed, 23 Nov 2016 17:54:41 +0000 (18:54 +0100)
app/local_settings.py
app/organization/agenda/views.py
app/organization/core/views.py
app/organization/magazine/views.py
app/organization/media/views.py
app/organization/network/views.py
app/organization/pages/views.py
app/organization/projects/views.py
app/static/admin/css/organization.css

index 763359f6a71d73efde30c55e02aac8118edd81ed..0fe59bd76c9437f211cb3bce50b6284338197bd2 100644 (file)
@@ -167,6 +167,7 @@ PAGES_PUBLISHED_INCLUDE_LOGIN_REQUIRED = True
 
 SEARCH_PER_PAGE = 10
 MAX_PAGING_LINKS = 10
+DAL_MAX_RESULTS = 20
 
 RATINGS_ACCOUNT_REQUIRED = True
 
index acaddea1b412f3e3908600a4be673e5f3cf596f2..3482cb68ebeed3de9c0831b7ca54fff404355d1f 100644 (file)
@@ -5,6 +5,7 @@ from dal_select2_queryset_sequence.views import Select2QuerySetSequenceView
 from organization.magazine.models import Article
 from organization.pages.models import CustomPage
 from mezzanine_agenda.models import Event
+from organization.core.views import autocomplete_result_formatting
 
 class ConfirmationView(TemplateView):
 
@@ -18,6 +19,8 @@ class ConfirmationView(TemplateView):
 
 class DynamicContentEventView(Select2QuerySetSequenceView):
 
+    paginate_by = settings.DAL_MAX_RESULTS
+
     def get_queryset(self):
 
         articles = Article.objects.all()
@@ -37,3 +40,7 @@ class DynamicContentEventView(Select2QuerySetSequenceView):
         qs = self.mixup_querysets(qs)
 
         return qs
+
+    def get_results(self, context):
+        results = autocomplete_result_formatting(self, context)
+        return results
index 19b750da79462f1e1203418a413f17cd8d769d4b..785075a0a62f40d63ae215e1444913979e8aa56d 100644 (file)
@@ -3,8 +3,10 @@ from django.http import Http404
 from django.views.generic.base import View
 from django.views.generic import DetailView, ListView, TemplateView
 from django.apps import apps
+from django.utils import six, timezone, formats
 from django.utils.translation import ugettext_lazy as _
 from django.http import QueryDict
+from django.template.defaultfilters import capfirst
 from mezzanine.conf import settings
 from mezzanine.utils.views import paginate
 from organization.core.models import *
@@ -133,3 +135,42 @@ class CustomSearchView(TemplateView):
         context['filter_dict'] = filter_dict
         # context.update(extra_context or {})
         return self.render_to_response(context)
+
+
+def autocomplete_result_formatting(self, context):
+    """
+    Return a list of results usable by Select2.
+    It will render as a list of one <optgroup> per different content type
+    containing a list of one <option> per model.
+    """
+    groups = {}
+
+    for result in context['object_list']:
+        groups.setdefault(type(result), [])
+        groups[type(result)].append(result)
+
+    all_results = []
+    for model, results in groups.items():
+        children = []
+        for result in results:
+            text = six.text_type(result)
+            if model._meta.verbose_name == "Event":
+                event_date = timezone.localtime(result.start)
+                is_parent = ""
+                if not result.parent:
+                    is_parent =  " ♦ -"
+                text = "%s -%s%s" % (six.text_type(result), is_parent, formats.date_format(event_date, "d-m-y H:i"))
+
+            children.append({
+                'id': self.get_result_value(result),
+                'text': text,
+            })
+
+        curr_model_result = {
+             'id': None,
+             'text': capfirst(model._meta.verbose_name),
+             'children': children
+        }
+        all_results.append(curr_model_result)
+
+    return all_results
index d917465f4a1028d121fa4ad4c48c21500c028bc9..f2113eefc8614dad8a5236b0f6941283e77c1a22 100644 (file)
@@ -15,7 +15,7 @@ from mezzanine.conf import settings
 from organization.magazine.models import *
 from organization.network.models import DepartmentPage
 from organization.pages.models import CustomPage, DynamicContentPage
-from organization.core.views import SlugMixin
+from organization.core.views import SlugMixin, autocomplete_result_formatting
 from django.template.defaultfilters import slugify
 
 
@@ -95,6 +95,9 @@ class TopicDetailView(SlugMixin, DetailView):
 
 
 class ObjectAutocomplete(Select2QuerySetSequenceView):
+
+    paginate_by = settings.DAL_MAX_RESULTS
+
     def get_queryset(self):
 
         articles = Article.objects.all()
@@ -118,8 +121,15 @@ class ObjectAutocomplete(Select2QuerySetSequenceView):
 
         return qs
 
+    def get_results(self, context):
+        results = autocomplete_result_formatting(self, context)
+        return results
+
 
 class DynamicContentArticleView(Select2QuerySetSequenceView):
+
+    paginate_by = settings.DAL_MAX_RESULTS
+
     def get_queryset(self):
 
         articles = Article.objects.all()
@@ -142,3 +152,7 @@ class DynamicContentArticleView(Select2QuerySetSequenceView):
         qs = self.mixup_querysets(qs)
 
         return qs
+
+    def get_results(self, context):
+        results = autocomplete_result_formatting(self, context)
+        return results
index 6a4ebcef113190599ecdb9fd3cad005debcdc3b6..f4c7ffb2766e304984d57cca4af345ae33f18d1d 100644 (file)
@@ -3,7 +3,6 @@ from collections import defaultdict
 from organization.media.models import *
 from organization.core.views import *
 from dal import autocomplete
-from dal_select2_queryset_sequence.views import Select2QuerySetSequenceView
 from django.core.exceptions import FieldDoesNotExist
 
 # temporarily excluse not ready models
index 271b3e48c0f1fbd8de51de04bf27c349ae691ea6..819ac924cd59032f7b13ea1e2c8c011df47c0156 100644 (file)
@@ -1,6 +1,5 @@
 from django.shortcuts import render
 from dal import autocomplete
-from dal_select2_queryset_sequence.views import Select2QuerySetSequenceView
 from organization.network.models import *
 from organization.core.views import *
 
index 4e089bd1d83dd36fe23dc01101d15fce2e616c0c..ae1a313864a9559f2eedf3abef75e8651c9f6687 100644 (file)
@@ -6,14 +6,14 @@ from dal import autocomplete
 from dal_select2_queryset_sequence.views import Select2QuerySetSequenceView
 from django.core.urlresolvers import reverse, reverse_lazy
 from django.utils.translation import ugettext_lazy as _
+from mezzanine.conf import settings
 from organization.pages.models import CustomPage
-from organization.core.views import SlugMixin
+from organization.core.views import SlugMixin, autocomplete_result_formatting
 from organization.magazine.models import Article, Topic, Brief
 from organization.pages.models import Home
 from organization.agenda.models import Event
 from organization.media.models import Playlist
 
-
 class HomeView(SlugMixin, ListView):
 
     model = Home
@@ -58,9 +58,15 @@ class DynamicContentHomeSliderView(Select2QuerySetSequenceView):
 
         return qs
 
+    def get_results(self, context):
+        results = autocomplete_result_formatting(self, context)
+        return results
+
 
 class DynamicContentHomeBodyView(Select2QuerySetSequenceView):
 
+    paginate_by = settings.DAL_MAX_RESULTS
+
     def get_queryset(self):
 
         articles = Article.objects.all()
@@ -87,6 +93,11 @@ class DynamicContentHomeBodyView(Select2QuerySetSequenceView):
         return qs
 
 
+    def get_results(self, context):
+        results = autocomplete_result_formatting(self, context)
+        return results
+
+
 class DynamicContentHomeMediaView(Select2QuerySetSequenceView):
 
     def get_queryset(self):
@@ -131,3 +142,7 @@ class DynamicContentPageView(Select2QuerySetSequenceView):
         qs = self.mixup_querysets(qs)
 
         return qs
+
+    def get_results(self, context):
+        results = autocomplete_result_formatting(self, context)
+        return results    
index b58a1d6eeb9547ab083f3e680e7f36e181b3e611..89567b93db8e39babb1af5664448df3ca9bcf487 100644 (file)
@@ -1,13 +1,13 @@
 from django.shortcuts import render
 from dal import autocomplete
 from dal_select2_queryset_sequence.views import Select2QuerySetSequenceView
+from mezzanine_agenda.models import Event
+from mezzanine.conf import settings
 from organization.projects.models import *
 from organization.core.views import *
 from organization.magazine.views import Article
-from mezzanine_agenda.models import Event
 from organization.pages.models import CustomPage
 
-
 class ProjectDetailView(SlugMixin, DetailView):
 
     model = Project
@@ -38,6 +38,8 @@ class ProjectDetailView(SlugMixin, DetailView):
 
 class DynamicContentProjectView(Select2QuerySetSequenceView):
 
+    paginate_by = settings.DAL_MAX_RESULTS
+
     def get_queryset(self):
 
         articles = Article.objects.all()
@@ -58,6 +60,10 @@ class DynamicContentProjectView(Select2QuerySetSequenceView):
 
         return qs
 
+    def get_results(self, context):
+        results = autocomplete_result_formatting(self, context)
+        return results
+
 
 class ProjectDemoDetailView(SlugMixin, DetailView):
 
index be6b6c1334dccb4ad4eb20e2f70c793e298bbfc7..8d5a827c429bb02eefff843ee3d2ea171f34dbc1 100644 (file)
@@ -10,3 +10,7 @@
     padding: 20px;
     background: yellow;
 }
+
+.select2-container {
+    min-width: 70em;
+}