diff options
Diffstat (limited to 'ishtar_common/views.py')
| -rw-r--r-- | ishtar_common/views.py | 46 | 
1 files changed, 39 insertions, 7 deletions
diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 586e1f0a1..1f8b46fd6 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -33,7 +33,7 @@ from django.core.urlresolvers import reverse, NoReverseMatch  from django.db.models import Q  from django.forms.models import modelformset_factory  from django.http import HttpResponse, Http404, HttpResponseRedirect, \ -    HttpResponseBadRequest +    HttpResponseBadRequest, JsonResponse  from django.shortcuts import redirect, render  from django.utils.decorators import method_decorator  from django.utils.translation import ugettext, ugettext_lazy as _ @@ -839,6 +839,20 @@ class IshtarMixin(object):          return context +class JSONResponseMixin: +    """ +    Render a JSON response. +    """ +    def render_to_response(self, context, **response_kwargs): +        return JsonResponse( +            self.get_data(context), +            **response_kwargs +        ) + +    def get_data(self, context): +        return context + +  class LoginRequiredMixin(object):      @method_decorator(login_required)      def dispatch(self, request, *args, **kwargs): @@ -1662,10 +1676,10 @@ document_deletion_wizard = wizards.DocumentDeletionWizard.as_view(      url_name='document_deletion',) -class SearchQueryEdit(LoginRequiredMixin, FormView): -    template_name = 'ishtar/forms/search_query.html' -    form_class = forms.SearchQueryForm - +class SearchQueryMixin(object): +    """ +    Manage content type and profile init +    """      def dispatch(self, request, *args, **kwargs):          if not request.user.pk:              raise Http404() @@ -1684,7 +1698,12 @@ class SearchQueryEdit(LoginRequiredMixin, FormView):              )          except ContentType.DoesNotExist:              raise Http404() -        return super(SearchQueryEdit, self).dispatch(request, *args, **kwargs) +        return super(SearchQueryMixin, self).dispatch(request, *args, **kwargs) + + +class SearchQueryEdit(SearchQueryMixin, LoginRequiredMixin, FormView): +    template_name = 'ishtar/forms/search_query.html' +    form_class = forms.SearchQueryForm      def get_form_kwargs(self):          kwargs = super(SearchQueryEdit, self).get_form_kwargs() @@ -1703,4 +1722,17 @@ class SearchQueryEdit(LoginRequiredMixin, FormView):          return HttpResponseRedirect(self.get_success_url())      def get_success_url(self): -        return reverse('success') +        return reverse('success', args=['bookmark']) + + +class BookmarkList(SearchQueryMixin, JSONResponseMixin, LoginRequiredMixin, +                   TemplateView): +    def get_data(self, context): +        q = models.SearchQuery.objects.filter( +            content_type=self.content_type, +            profile=self.profile +        ) +        return { +            'bookmarks': [ +                {'label': sq.label, 'query': sq.query} for sq in q.all()] +        }  | 
