示例#1
0
 def filter_ads(self, request, *args, **kwargs):
     # request.method == 'GET' and request.GET != {} # after removing pages and potential sorting
     self._q = self.request.GET
     self.object_list = self.get_queryset()
     data = {
         'user': self.request.user,
         'search': self.request.GET.copy().urlencode()
     }
     self.ad_search_form = AdSearchForm(data)
     context = self.get_context_data(object_list=self.object_list,
                                     ad_search_form=True)
     self.get_msg()
     return self.render_to_response(context)
示例#2
0
 def update_search(self, request, *args, **kwargs):
     # request.method == 'POST' and search_id is not None
     profile_detail_url = settings.LOGIN_REDIRECT_URL
     self.ad_search = AdSearch.objects.get(id=self.search_id)
     self.ad_search_form = AdSearchForm(request.POST,
                                        instance=self.ad_search)
     if self.ad_search_form.is_valid():
         self.ad_search_form.save()
         messages.add_message(self.request,
                              messages.INFO,
                              _(u'Votre recherche a bien été mise à jour ' +
                                u'dans <a href="%s">votre compte</a>.') %
                              (profile_detail_url),
                              fail_silently=True)
     # need to be sure that self.ad_search.search is well updated
     self._q = QueryDict(self.ad_search.search)
     self.object_list = self.get_queryset()
     context = self.get_context_data(object_list=self.object_list)
     self.get_msg()
     return self.render_to_response(context)
示例#3
0
 def create_search(self, request, *args, **kwargs):
     # request.method == 'POST' and search_id is None
     self.ad_search_form = AdSearchForm(request.POST)
     if self.ad_search_form.is_valid():
         self.ad_search_form.user = request.user
         self.ad_search = self.ad_search_form.save(commit=False)
         self.ad_search.content_type = ContentType.objects.get_for_model(
             self.model)
         self.ad_search.user = request.user
         self.ad_search.public = True
         self.ad_search.save()
         self.search_id = self.ad_search.id
         messages.add_message(
             self.request,
             messages.INFO,
             _(u'Votre recherche a bien été sauvegardée dans votre compte</a>.'
               ),
             fail_silently=True)
         return HttpResponseRedirect(
             reverse('search', kwargs={'search_id': self.search_id}))
示例#4
0
 def filter_ads(self, request, *args, **kwargs):
     # request.method == 'GET' and request.GET != {} # after removing pages and potential sorting
     self._q = self.request.GET
     self.object_list = self.get_queryset()
     data = {'user': self.request.user,
         'search': self.request.GET.copy().urlencode()}
     self.ad_search_form = AdSearchForm(data)
     context = self.get_context_data(object_list=self.object_list,
         ad_search_form=True)
     self.get_msg()
     return self.render_to_response(context)
示例#5
0
 def create_search(self, request, *args, **kwargs):
     # request.method == 'POST' and search_id is None
     self.ad_search_form = AdSearchForm(request.POST)
     if self.ad_search_form.is_valid():
         self.ad_search_form.user = request.user
         self.ad_search = self.ad_search_form.save(commit=False)
         self.ad_search.content_type = ContentType.objects.get_for_model(self.model)
         self.ad_search.user = request.user
         self.ad_search.public = True
         self.ad_search.save()
         self.search_id = self.ad_search.id
         messages.add_message(self.request, messages.INFO,
             _(u'Votre recherche a bien été sauvegardée dans votre compte</a>.'), fail_silently=True)
         return HttpResponseRedirect(reverse('search',
             kwargs={'search_id': self.search_id}))
示例#6
0
 def update_search(self, request, *args, **kwargs):
     # request.method == 'POST' and search_id is not None
     profile_detail_url = account_url(self.request)
     self.ad_search = AdSearch.objects.get(id=self.search_id)
     self.ad_search_form = AdSearchForm(request.POST, instance=self.ad_search)
     if self.ad_search_form.is_valid():
         self.ad_search_form.save()
         messages.add_message(self.request, messages.INFO,
             _(u'Votre recherche a bien été mise à jour ' +
               u'dans <a href="%s">votre compte</a>.')
             % (profile_detail_url), fail_silently=True)
     # need to be sure that self.ad_search.search is well updated
     self._q = QueryDict(self.ad_search.search)
     self.object_list = self.get_queryset()
     context = self.get_context_data(object_list=self.object_list)
     self.get_msg()
     return self.render_to_response(context)
示例#7
0
 def create_search(self, request, *args, **kwargs):
     # request.method == 'POST' and search_id is None
     # save the search
     profile_detail_url = account_url(self.request)
     # return the results
     self.ad_search_form = AdSearchForm(request.POST)
     if self.ad_search_form.is_valid():
         logger.info('ad search form valid')
         self.ad_search_form.user = request.user
         self.ad_search = self.ad_search_form.save(commit=False)
         self.ad_search.content_type = ContentType.objects.get_for_model(self.model)
         self.ad_search.user = request.user
         self.ad_search.public = True
         self.ad_search.save()
         self.search_id = self.ad_search.id
         messages.add_message(self.request, messages.INFO,
             _(u'Votre recherche a bien été sauvegardée ' +
             u'dans <a href="%s">votre compte</a>.')
             % (profile_detail_url), fail_silently=True)
             # when creation, we need to save related ads to ad_search_results
         #self.update_ad_search_results()
         return HttpResponseRedirect(reverse('search',
             kwargs={'search_id': self.search_id}))
示例#8
0
class AdSearchView(ListView):
    """
    Class based ad search view

    GET method for searching: filtering, ordering, and browsing by page results
    POST method for saving a search
    """
    model = Ad
    search_id = None
    template_name = 'geoads/search.html'
    context_object_name = 'filter'
    no_results_msg = None  # Message when there is no results
    results_msg = None  # Message when number of results > 0
    ad_search_form = None
    ad_search = None

    #BUG: paginate_by = 14 doesn't work, I use django-paginator

    def dispatch(self, request, *args, **kwargs):
        # Dispatch according to the request.method and url args/kwargs

        if 'search_id' in kwargs:
            self.search_id = kwargs['search_id']

        self.request = request
        self.args = args
        self.kwargs = kwargs

        if request.method == 'POST':
            if self.search_id:
                return self.update_search(request, *args, **kwargs)
            else:
                return self.create_search(request, *args, **kwargs)
        else:
            if self.search_id:
                return self.read_search(request, *args, **kwargs)
            elif request.GET != {}:
                return self.filter_ads(request, *args, **kwargs)
            else:
                return self.home(request, *args, **kwargs)

    def home(self, request, *args, **kwargs):
        # request.method == 'GET' and request.GET only contains pages and sorting
        self._q = None
        self.object_list = self.get_queryset()
        context = self.get_context_data(object_list=self.object_list,
                                        initial_ads=True)
        return self.render_to_response(context)

    def filter_ads(self, request, *args, **kwargs):
        # request.method == 'GET' and request.GET != {} # after removing pages and potential sorting
        self._q = self.request.GET
        self.object_list = self.get_queryset()
        data = {
            'user': self.request.user,
            'search': self.request.GET.copy().urlencode()
        }
        self.ad_search_form = AdSearchForm(data)
        context = self.get_context_data(object_list=self.object_list,
                                        ad_search_form=True)
        self.get_msg()
        return self.render_to_response(context)

    def read_search(self, request, *args, **kwargs):
        # request.method == 'GET' and search_id is not None
        self.ad_search = AdSearch.objects.get(id=self.search_id)
        if self.ad_search.user != self.request.user:
            raise Http404
        self._q = QueryDict(self.ad_search.search)
        self.object_list = self.get_queryset()
        context = self.get_context_data(object_list=self.object_list)
        self.get_msg()
        return self.render_to_response(context)

    @method_decorator(login_required)
    def create_search(self, request, *args, **kwargs):
        # request.method == 'POST' and search_id is None
        self.ad_search_form = AdSearchForm(request.POST)
        if self.ad_search_form.is_valid():
            self.ad_search_form.user = request.user
            self.ad_search = self.ad_search_form.save(commit=False)
            self.ad_search.content_type = ContentType.objects.get_for_model(
                self.model)
            self.ad_search.user = request.user
            self.ad_search.public = True
            self.ad_search.save()
            self.search_id = self.ad_search.id
            messages.add_message(
                self.request,
                messages.INFO,
                _(u'Votre recherche a bien été sauvegardée dans votre compte</a>.'
                  ),
                fail_silently=True)
            return HttpResponseRedirect(
                reverse('search', kwargs={'search_id': self.search_id}))

    @method_decorator(login_required)
    def update_search(self, request, *args, **kwargs):
        # request.method == 'POST' and search_id is not None
        profile_detail_url = settings.LOGIN_REDIRECT_URL
        self.ad_search = AdSearch.objects.get(id=self.search_id)
        self.ad_search_form = AdSearchForm(request.POST,
                                           instance=self.ad_search)
        if self.ad_search_form.is_valid():
            self.ad_search_form.save()
            messages.add_message(self.request,
                                 messages.INFO,
                                 _(u'Votre recherche a bien été mise à jour ' +
                                   u'dans <a href="%s">votre compte</a>.') %
                                 (profile_detail_url),
                                 fail_silently=True)
        # need to be sure that self.ad_search.search is well updated
        self._q = QueryDict(self.ad_search.search)
        self.object_list = self.get_queryset()
        context = self.get_context_data(object_list=self.object_list)
        self.get_msg()
        return self.render_to_response(context)

    def get_queryset(self):
        filter = self.model.filterset()(self._q)
        return filter

    def get_context_data(self,
                         initial_ads=None,
                         ad_search_form=None,
                         **kwargs):
        context = super(AdSearchView, self).get_context_data(**kwargs)
        if initial_ads == True:
            context['initial_ads'] = self.model.objects.select_related()\
                .order_by('-create_date')[0:10]
        if ad_search_form == True:
            context[
                'ad_search_form'] = self.ad_search_form  # need to be filled with current search
        context['search_id'] = self.search_id  # what to do with that
        return context

    def get_msg(self):
        """
        Search result default message
        """
        if len(self.object_list.qs) == 0:
            messages.add_message(self.request,
                                 messages.INFO,
                                 self.get_no_results_msg(),
                                 fail_silently=True)
        else:
            messages.add_message(self.request,
                                 messages.INFO,
                                 self.get_results_msg(),
                                 fail_silently=True)

    def get_no_results_msg(self):
        """
        Message for search that give 0 results
        """
        if self.no_results_msg is None:
            return _(u'Aucune annonce ne correspond à votre recherche. ' +\
                     u'Elargissez votre zone de recherche ou modifiez les critères.')
        return self.no_results_msg

    def get_results_msg(self):
        """
        Message for search that give 1 or more results
        """
        #TODO: should have information if search come from a saved search
        if self.results_msg is None:
            msg = ungettext(u'%s annonce correspondant à votre recherche. ',
                    u'%s annonces correspondant à votre recherche. ',
                    len(self.object_list.qs)) \
                            % (len(self.object_list.qs))
            return msg
        return self.results_msg
示例#9
0
class AdSearchView(ListView):
    """
    Class based ad search view

    GET method for searching: filtering, ordering, and browsing by page results
    POST method for saving a search

    """
    model = Ad
    search_id = None
    template_name = 'geoads/search.html'
    context_object_name = 'filter'
    no_results_msg = None  # Message when there is no results
    results_msg = None  # Message when number of results > 0
    ad_search_form = None
    ad_search = None
    #BUG: paginate_by = 14 doesn't work, I use django-paginator

    def dispatch(self, request, *args, **kwargs):
        # here, dispatch according to the request.method and url args/kwargs

        if 'search_id' in kwargs:
            self.search_id = kwargs['search_id']

        self.request = request
        self.args = args
        self.kwargs = kwargs

        if request.method == 'POST':
            if self.search_id:
                logger.info('update_search')
                return self.update_search(request, *args, **kwargs)
            else:
                logger.info('create_search')
                return self.create_search(request, *args, **kwargs)
        else:
            if self.search_id:
                logger.info('read_search')
                return self.read_search(request, *args, **kwargs)
            elif request.GET != {}:
                logger.info('filter_search')
                return self.filter_ads(request, *args, **kwargs)
            else:
                logger.info('home')
                return self.home(request, *args, **kwargs)

        #return super(AdSearchView, self).dispatch(request, *args, **kwargs)

    def home(self, request, *args, **kwargs):
        # request.method == 'GET' and request.GET only contains pages and sorting
        self._q = None
        self.object_list = self.get_queryset()
        context = self.get_context_data(object_list=self.object_list,
            initial_ads=True)
        return self.render_to_response(context)

    def filter_ads(self, request, *args, **kwargs):
        # request.method == 'GET' and request.GET != {} # after removing pages and potential sorting
        self._q = self.request.GET
        self.object_list = self.get_queryset()
        data = {'user': self.request.user,
            'search': self.request.GET.copy().urlencode()}
        self.ad_search_form = AdSearchForm(data)
        context = self.get_context_data(object_list=self.object_list,
            ad_search_form=True)
        self.get_msg()
        return self.render_to_response(context)

    def read_search(self, request, *args, **kwargs):
        # request.method == 'GET' and search_id is not None
        self.ad_search = AdSearch.objects.get(id=self.search_id)
        if self.ad_search.user != self.request.user:
                raise Http404
        self._q = QueryDict(self.ad_search.search)
        self.object_list = self.get_queryset()
        context = self.get_context_data(object_list=self.object_list)
        self.get_msg()
        return self.render_to_response(context)

    @method_decorator(login_required)
    def create_search(self, request, *args, **kwargs):
        # request.method == 'POST' and search_id is None
        # save the search
        profile_detail_url = account_url(self.request)
        # return the results
        self.ad_search_form = AdSearchForm(request.POST)
        if self.ad_search_form.is_valid():
            logger.info('ad search form valid')
            self.ad_search_form.user = request.user
            self.ad_search = self.ad_search_form.save(commit=False)
            self.ad_search.content_type = ContentType.objects.get_for_model(self.model)
            self.ad_search.user = request.user
            self.ad_search.public = True
            self.ad_search.save()
            self.search_id = self.ad_search.id
            messages.add_message(self.request, messages.INFO,
                _(u'Votre recherche a bien été sauvegardée ' +
                u'dans <a href="%s">votre compte</a>.')
                % (profile_detail_url), fail_silently=True)
                # when creation, we need to save related ads to ad_search_results
            #self.update_ad_search_results()
            return HttpResponseRedirect(reverse('search',
                kwargs={'search_id': self.search_id}))
        # this would be better no ?
        #self._q = QueryDict(self.ad_search.search)
        #self.object_list = self._get_queryset()
        #context = self.get_context_data(object_list=self.object_list)
        #return self.render_to_response(context)

    @method_decorator(login_required)
    def update_search(self, request, *args, **kwargs):
        # request.method == 'POST' and search_id is not None
        profile_detail_url = account_url(self.request)
        self.ad_search = AdSearch.objects.get(id=self.search_id)
        self.ad_search_form = AdSearchForm(request.POST, instance=self.ad_search)
        if self.ad_search_form.is_valid():
            self.ad_search_form.save()
            messages.add_message(self.request, messages.INFO,
                _(u'Votre recherche a bien été mise à jour ' +
                  u'dans <a href="%s">votre compte</a>.')
                % (profile_detail_url), fail_silently=True)
        # need to be sure that self.ad_search.search is well updated
        self._q = QueryDict(self.ad_search.search)
        self.object_list = self.get_queryset()
        context = self.get_context_data(object_list=self.object_list)
        self.get_msg()
        return self.render_to_response(context)

    def get_queryset(self):
        #filter = self.model.filterset(self._q)
        filter = self.model.objects.filterset(self._q)
        return filter

    def get_context_data(self, initial_ads=None, ad_search_form=None, **kwargs):
        context = super(AdSearchView, self).get_context_data(**kwargs)
        if initial_ads == True:
            context['initial_ads'] = self.model.objects.select_related()\
                .order_by('-create_date')[0:10]
        if ad_search_form == True:
            context['ad_search_form'] = self.ad_search_form  # need to be filled with current search
        context['search_id'] = self.search_id  # what to do with that
        return context

    def get_msg(self):
        """
        Search result default message

        """
        if len(self.object_list.qs) == 0:
            messages.add_message(self.request, messages.INFO,
                self.get_no_results_msg(), fail_silently=True)
        else:
            messages.add_message(self.request, messages.INFO,
                self.get_results_msg(), fail_silently=True)

    def get_no_results_msg(self):
        """
        Message for search that give 0 results

        """
        if self.no_results_msg is None:
            return _(u'Aucune annonce ne correspond à votre recherche. ' +\
                     u'Elargissez votre zone de recherche ou modifiez les critères.')
        return self.no_results_msg

    def get_results_msg(self):
        """
        Message for search that give 1 or more results

        """
        #TODO: should have information if search come from a saved search
        if self.results_msg is None:
            msg = ungettext(u'%s annonce correspondant à votre recherche. ',
                    u'%s annonces correspondant à votre recherche. ',
                    len(self.object_list.qs)) \
                            % (len(self.object_list.qs))
            return msg
        return self.results_msg