Exemplo n.º 1
0
 def get(self, request, *args, **kwargs):
     self.query = request.GET.get('q')
     self.max_articles = request.GET.get('max_articles', 0)
     self.edit_mode = (request.toolbar
                       and toolbar_edit_mode_active(request))
     return super(ArticleSearchResultsList,
                  self).get(request, *args, **kwargs)
Exemplo n.º 2
0
    def get_tags(self, request, namespace):
        """
        Get tags with articles count for given namespace string.

        Return list of Tag objects ordered by custom 'num_articles' attribute.
        """
        if (request and hasattr(request, 'toolbar') and  # noqa: #W504
                request.toolbar and toolbar_edit_mode_active(request)):
            articles = self.namespace(namespace)
        else:
            articles = self.published().namespace(namespace)
        if not articles:
            # return empty iterable early not to perform useless requests
            return []
        kwargs = TaggedItem.bulk_lookup_kwargs(articles)

        # aggregate and sort
        counted_tags = dict(TaggedItem.objects
                            .filter(**kwargs)
                            .values('tag')
                            .annotate(tag_count=models.Count('tag'))
                            .values_list('tag', 'tag_count'))

        # and finally get the results
        tags = Tag.objects.filter(pk__in=counted_tags.keys())
        for tag in tags:
            tag.num_articles = counted_tags[tag.pk]
        return sorted(tags, key=attrgetter('num_articles'), reverse=True)
Exemplo n.º 3
0
 def get_edit_mode(self, request):
     """
     Returns True only if an operator is logged-into the CMS and is in
     edit mode.
     """
     return (hasattr(request, 'toolbar') and request.toolbar
             and  # noqa: W504
             toolbar_edit_mode_active(request))
Exemplo n.º 4
0
 def get(self, request, *args, **kwargs):
     self.query = request.GET.get('q')
     self.max_articles = request.GET.get('max_articles', 0)
     self.edit_mode = (
         request.toolbar and toolbar_edit_mode_active(request)
     )  #(request.toolbar and request.toolbar.edit_mode) original code tha gives error:'CMSToolbar' object has no attribute 'edit_mode'
     return super(ArticleSearchResultsList,
                  self).get(request, *args, **kwargs)
Exemplo n.º 5
0
    def get_months(self, request, namespace):
        """
        Get months and years with articles count for given request and namespace
        string. This means how many articles there are in each month.

        The request is required, because logged-in content managers may get
        different counts.

        Return list of dictionaries ordered by article publishing date of the
        following format:
        [
            {
                'date': date(YEAR, MONTH, ARBITRARY_DAY),
                'num_articles': NUM_ARTICLES
            },
            ...
        ]
        """

        # TODO: check if this limitation still exists in Django 1.6+
        # This is done in a naive way as Django is having tough time while
        # aggregating on date fields
        if (request and hasattr(request, 'toolbar') and  # noqa: #W504
                request.toolbar and toolbar_edit_mode_active(request)):
            articles = self.namespace(namespace)
        else:
            articles = self.published().namespace(namespace)
        dates = articles.values_list('publishing_date', flat=True)
        dates = [(x.year, x.month) for x in dates]
        date_counter = Counter(dates)
        dates = set(dates)
        dates = sorted(dates, reverse=True)
        months = [
            # Use day=3 to make sure timezone won't affect this hacks'
            # month value. There are UTC+14 and UTC-12 timezones!
            {
                'date': datetime.date(year=year, month=month, day=3),
                'num_articles': date_counter[(year, month)]
            } for year, month in dates
        ]
        return months
Exemplo n.º 6
0
    def get_tags(self, request, namespace):
        """
        Get tags with articles count for given namespace string.

        Return list of Tag objects ordered by custom 'num_articles' attribute.
        """
        from aldryn_newsblog.models import ArticleTag

        if (request and hasattr(request, 'toolbar') and  # noqa: #W504
                request.toolbar and toolbar_edit_mode_active(request)):
            articles = self.namespace(namespace)
        else:
            articles = self.published().namespace(namespace)
        if not articles:
            # return empty iterable early not to perform useless requests
            return []

        return ArticleTag.objects.annotate(
            article_count=Count('article', filter=Q(
                article__in=articles))).filter(
                    article_count__gt=0).order_by('-article_count')
Exemplo n.º 7
0
    def get_months(self, request, namespace):
        """
        Get months and years with articles count for given request and namespace
        string. This means how many articles there are in each month.

        The request is required, because logged-in content managers may get
        different counts.

        Return list of dictionaries ordered by article publishing date of the
        following format:
        [
            {
                'date': date(YEAR, MONTH, ARBITRARY_DAY),
                'num_articles': NUM_ARTICLES
            },
            ...
        ]
        """

        # TODO: check if this limitation still exists in Django 1.6+
        # This is done in a naive way as Django is having tough time while
        # aggregating on date fields
        if (request and hasattr(request, 'toolbar') and  # noqa: #W504
                request.toolbar and toolbar_edit_mode_active(request)):
            articles = self.namespace(namespace)
        else:
            articles = self.published().namespace(namespace)
        dates = articles.values_list('publishing_date', flat=True)
        dates = [(x.year, x.month) for x in dates]
        date_counter = Counter(dates)
        dates = set(dates)
        dates = sorted(dates, reverse=True)
        months = [
            # Use day=3 to make sure timezone won't affect this hacks'
            # month value. There are UTC+14 and UTC-12 timezones!
            {'date': datetime.date(year=year, month=month, day=3),
             'num_articles': date_counter[(year, month)]}
            for year, month in dates]
        return months
Exemplo n.º 8
0
 def dispatch(self, request, *args, **kwargs):
     self.edit_mode = (self.request.toolbar
                       and toolbar_edit_mode_active(self.request))
     return super(EditModeMixin, self).dispatch(request, *args, **kwargs)
Exemplo n.º 9
0
 def get_queryset(self, request):
     """Returns base queryset with support for preview-mode."""
     queryset = Article.objects
     if not (request.toolbar and toolbar_edit_mode_active(request)):
         queryset = queryset.published()
     return queryset
Exemplo n.º 10
0
 def get_queryset(self, request):
     """Returns base queryset with support for preview-mode."""
     queryset = Article.objects
     if not (request.toolbar and toolbar_edit_mode_active(request)):
         queryset = queryset.published()
     return queryset
Exemplo n.º 11
0
 def dispatch(self, request, *args, **kwargs):
     self.edit_mode = (
         self.request.toolbar and toolbar_edit_mode_active(self.request))
     return super(EditModeMixin, self).dispatch(request, *args, **kwargs)
Exemplo n.º 12
0
 def get(self, request, *args, **kwargs):
     self.query = request.GET.get('q')
     self.max_articles = request.GET.get('max_articles', 0)
     self.edit_mode = (request.toolbar and toolbar_edit_mode_active(request))
     return super(ArticleSearchResultsList, self).get(request, *args, **kwargs)