コード例 #1
0
ファイル: contents.py プロジェクト: Vultik/zds-site
    def get_forms(self, context):
        """get all the auxiliary forms about validation, js fiddle..."""

        validation = Validation.objects.filter(
            content__pk=self.object.pk).order_by("-date_proposition").first()

        form_js = JsFiddleActivationForm(
            initial={"js_support": self.object.js_support})

        context["formAskValidation"] = AskValidationForm(
            content=self.versioned_object,
            initial={
                "source": self.object.source,
                "version": self.sha
            })

        if validation:
            context["formValid"] = AcceptValidationForm(
                validation, initial={"source": self.object.source})
            context["formReject"] = RejectValidationForm(validation)
            context["formCancel"] = CancelValidationForm(validation)

        if self.versioned_object.sha_public:
            context["formRevokeValidation"] = RevokeValidationForm(
                self.versioned_object,
                initial={"version": self.versioned_object.sha_public})
            context["formUnpublication"] = UnpublicationForm(
                self.versioned_object,
                initial={"version": self.versioned_object.sha_public})

        if self.versioned_object.is_beta:
            context["formWarnTypo"] = WarnTypoForm(self.versioned_object,
                                                   self.versioned_object,
                                                   public=False)

        context["validation"] = validation
        context["formJs"] = form_js
        context["form_edit_license"] = EditContentLicenseForm(
            self.versioned_object)
        context["form_edit_tags"] = EditContentTagsForm(
            self.versioned_object, self.object)

        if self.versioned_object.requires_validation:
            context["formPublication"] = PublicationForm(
                self.versioned_object, initial={"source": self.object.source})
        else:
            context["formPublication"] = None
コード例 #2
0
    def get_context_data(self, **kwargs):
        """Show the given tutorial if exists."""
        context = super(DisplayOnlineContent, self).get_context_data(**kwargs)

        if context['is_staff']:
            if self.current_content_type == 'OPINION':
                context['alerts'] = self.object.alerts_on_this_content.all()
            context['formRevokeValidation'] = RevokeValidationForm(
                self.versioned_object,
                initial={'version': self.versioned_object.sha_public})
            context['formUnpublication'] = UnpublicationForm(
                self.versioned_object,
                initial={'version': self.versioned_object.sha_public})

        context['formWarnTypo'] = WarnTypoForm(self.versioned_object,
                                               self.versioned_object)

        queryset_reactions = ContentReaction.objects\
            .select_related('author') \
            .select_related('author__profile') \
            .select_related('hat') \
            .select_related('editor') \
            .prefetch_related('alerts_on_this_comment') \
            .prefetch_related('alerts_on_this_comment__author') \
            .filter(related_content__pk=self.object.pk) \
            .order_by('pubdate')

        # pagination of articles and opinions
        context['previous_content'] = None
        context['next_content'] = None

        if self.current_content_type in ('ARTICLE', 'OPINION'):
            queryset_pagination = PublishedContent.objects.filter(
                content_type=self.current_content_type, must_redirect=False)

            if self.current_content_type == 'OPINION':
                # filter opinions only from the same author
                queryset_pagination = queryset_pagination.filter(
                    authors__in=self.object.authors.all())

            context['previous_content'] = queryset_pagination \
                .filter(publication_date__lt=self.public_content_object.publication_date) \
                .order_by('publication_date').first()
            context['next_content'] = queryset_pagination \
                .filter(publication_date__gt=self.public_content_object.publication_date) \
                .order_by('publication_date').first()

        if self.versioned_object.type == 'OPINION':
            context['formPickOpinion'] = PickOpinionForm(
                self.versioned_object,
                initial={'version': self.versioned_object.sha_public})
            context['formUnpickOpinion'] = UnpickOpinionForm(
                self.versioned_object,
                initial={'version': self.versioned_object.sha_public})
            context['formConvertOpinion'] = PromoteOpinionToArticleForm(
                self.versioned_object,
                initial={'version': self.versioned_object.sha_public})

        # pagination of comments
        make_pagination(context,
                        self.request,
                        queryset_reactions,
                        settings.ZDS_APP['content']['notes_per_page'],
                        context_list_name='reactions',
                        with_previous_item=True)

        # is JS activated ?
        context['is_js'] = True
        if not self.object.js_support:
            context['is_js'] = False

        # optimize requests:
        votes = CommentVote.objects.filter(
            user_id=self.request.user.id,
            comment__in=queryset_reactions).all()
        context['user_like'] = [
            vote.comment_id for vote in votes if vote.positive
        ]
        context['user_dislike'] = [
            vote.comment_id for vote in votes if not vote.positive
        ]

        if self.request.user.has_perm('tutorialv2.change_contentreaction'):
            context['user_can_modify'] = [
                reaction.pk for reaction in queryset_reactions
            ]
        else:
            context['user_can_modify'] = [
                reaction.pk for reaction in queryset_reactions
                if reaction.author == self.request.user
            ]

        context['is_antispam'] = self.object.antispam()
        context['pm_link'] = self.object.get_absolute_contact_url(
            _('À propos de'))
        context[
            'subscriber_count'] = ContentReactionAnswerSubscription.objects.get_subscriptions(
                self.object).count()
        # We need reading time expressed in minutes
        try:
            context['reading_time'] = int(
                self.versioned_object.get_tree_level() *
                self.object.public_version.char_count /
                settings.ZDS_APP['content']['characters_per_minute'])
        except ZeroDivisionError as e:
            logger.warning(
                'could not compute reading time: setting characters_per_minute is set to zero (error=%s)',
                e)

        if self.request.user.is_authenticated():
            for reaction in context['reactions']:
                signals.content_read.send(sender=reaction.__class__,
                                          instance=reaction,
                                          user=self.request.user)
            signals.content_read.send(sender=self.object.__class__,
                                      instance=self.object,
                                      user=self.request.user,
                                      target=PublishableContent)
        if last_participation_is_old(self.object, self.request.user):
            mark_read(self.object, self.request.user)

        return context
コード例 #3
0
    def get_context_data(self, **kwargs):
        """Show the given tutorial if exists."""
        context = super(DisplayOnlineContent, self).get_context_data(**kwargs)

        if context["is_staff"]:
            if self.current_content_type == "OPINION":
                context["alerts"] = self.object.alerts_on_this_content.all()
            context["formRevokeValidation"] = RevokeValidationForm(
                self.versioned_object,
                initial={"version": self.versioned_object.sha_public})
            context["formUnpublication"] = UnpublicationForm(
                self.versioned_object,
                initial={"version": self.versioned_object.sha_public})

        context["formWarnTypo"] = WarnTypoForm(self.versioned_object,
                                               self.versioned_object)

        queryset_reactions = (
            ContentReaction.objects.select_related("author").select_related(
                "author__profile").select_related("hat").select_related(
                    "editor").prefetch_related("alerts_on_this_comment").
            prefetch_related("alerts_on_this_comment__author").filter(
                related_content__pk=self.object.pk).order_by("pubdate"))

        # pagination of articles and opinions
        context["previous_content"] = None
        context["next_content"] = None

        if self.current_content_type in ("ARTICLE", "OPINION"):
            queryset_pagination = PublishedContent.objects.filter(
                content_type=self.current_content_type, must_redirect=False)

            context["previous_content"] = (queryset_pagination.filter(
                publication_date__lt=self.public_content_object.
                publication_date).order_by("-publication_date").first())
            context["next_content"] = (queryset_pagination.filter(
                publication_date__gt=self.public_content_object.
                publication_date).order_by("publication_date").first())

        if self.versioned_object.type == "OPINION":
            context["formPickOpinion"] = PickOpinionForm(
                self.versioned_object,
                initial={"version": self.versioned_object.sha_public})
            context["formUnpickOpinion"] = UnpickOpinionForm(
                self.versioned_object,
                initial={"version": self.versioned_object.sha_public})
            context["formConvertOpinion"] = PromoteOpinionToArticleForm(
                self.versioned_object,
                initial={"version": self.versioned_object.sha_public})
        else:
            context["content_suggestions"] = ContentSuggestion.objects.filter(
                publication=self.object)
            excluded_for_search = [
                str(x.suggestion.pk) for x in context["content_suggestions"]
            ]
            excluded_for_search.append(str(self.object.pk))
            context["formAddSuggestion"] = SearchSuggestionForm(
                content=self.object,
                initial={"excluded_pk": ",".join(excluded_for_search)})

        context["form_edit_tags"] = EditContentTagsForm(
            self.versioned_object, self.object)

        # pagination of comments
        make_pagination(
            context,
            self.request,
            queryset_reactions,
            settings.ZDS_APP["content"]["notes_per_page"],
            context_list_name="reactions",
            with_previous_item=True,
        )

        # is JS activated ?
        context["is_js"] = True
        if not self.object.js_support:
            context["is_js"] = False

        # optimize requests:
        votes = CommentVote.objects.filter(
            user_id=self.request.user.id,
            comment__in=queryset_reactions).all()
        context["user_like"] = [
            vote.comment_id for vote in votes if vote.positive
        ]
        context["user_dislike"] = [
            vote.comment_id for vote in votes if not vote.positive
        ]

        if self.request.user.has_perm("tutorialv2.change_contentreaction"):
            context["user_can_modify"] = [
                reaction.pk for reaction in queryset_reactions
            ]
        else:
            context["user_can_modify"] = [
                reaction.pk for reaction in queryset_reactions
                if reaction.author == self.request.user
            ]

        context["is_antispam"] = self.object.antispam()
        context["pm_link"] = self.object.get_absolute_contact_url(
            _("À propos de"))
        context[
            "subscriber_count"] = ContentReactionAnswerSubscription.objects.get_subscriptions(
                self.object).count()
        # We need reading time expressed in minutes
        try:
            char_count = self.object.public_version.char_count
            if char_count:
                context["reading_time"] = int(
                    self.versioned_object.get_tree_level() * char_count /
                    settings.ZDS_APP["content"]["characters_per_minute"])
        except ZeroDivisionError as e:
            logger.warning(
                "could not compute reading time: setting characters_per_minute is set to zero (error=%s)",
                e)

        if self.request.user.is_authenticated:
            for reaction in context["reactions"]:
                signals.content_read.send(sender=reaction.__class__,
                                          instance=reaction,
                                          user=self.request.user)
            signals.content_read.send(sender=self.object.__class__,
                                      instance=self.object,
                                      user=self.request.user,
                                      target=PublishableContent)
        if last_participation_is_old(self.object, self.request.user):
            mark_read(self.object, self.request.user)

        context["contributions"] = ContentContribution.objects.filter(
            content=self.object).order_by("contribution_role__position")
        context[
            "content_suggestions_random"] = ContentSuggestion.objects.filter(
                publication=self.object).order_by(
                    "?")[:settings.ZDS_APP["content"]["suggestions_per_page"]]

        return context
コード例 #4
0
    def get_context_data(self, **kwargs):
        """Show the given tutorial if exists."""
        context = super(DisplayOnlineContent, self).get_context_data(**kwargs)

        if context['is_staff']:
            context['formRevokeValidation'] = RevokeValidationForm(
                self.versioned_object,
                initial={'version': self.versioned_object.sha_public})

        context['formWarnTypo'] = WarnTypoForm(self.versioned_object,
                                               self.versioned_object)

        queryset_reactions = ContentReaction.objects\
            .select_related('author')\
            .select_related('author__profile')\
            .select_related('editor')\
            .prefetch_related('alerts')\
            .prefetch_related('alerts__author')\
            .filter(related_content__pk=self.object.pk)\
            .order_by("pubdate")

        # pagination of articles
        context['paginate_articles'] = False

        if self.object.type == 'ARTICLE':
            # fetch all articles in order to find the previous and the next one
            all_articles = \
                [a for a in PublishedContent.objects
                    .filter(content_type="ARTICLE", must_redirect=False)
                    .order_by('publication_date')
                    .all()]
            articles_count = len(all_articles)

            try:
                position = all_articles.index(self.public_content_object)
            except ValueError:
                pass  # for an unknown reason, the article is not in the list. This should not happen
            else:
                context['paginate_articles'] = True
                context['previous_article'] = None
                context['next_article'] = None

                if position > 0:
                    context['previous_article'] = all_articles[position - 1]
                if position < articles_count - 1:
                    context['next_article'] = all_articles[position + 1]

        # pagination of comments
        make_pagination(context,
                        self.request,
                        queryset_reactions,
                        settings.ZDS_APP['content']['notes_per_page'],
                        context_list_name='reactions',
                        with_previous_item=True)

        # is JS activated ?
        context["is_js"] = True
        if not self.object.js_support:
            context["is_js"] = False

        # optimize requests:

        context["user_dislike"] = CommentDislike.objects\
            .select_related('note')\
            .filter(user__pk=self.request.user.pk, comments__in=context['reactions'])\
            .values_list('comments__pk', flat=True)
        context["user_like"] = CommentLike.objects\
            .select_related('note')\
            .filter(user__pk=self.request.user.pk, comments__in=context['reactions'])\
            .values_list('comments__pk', flat=True)

        if self.request.user.has_perm('tutorialv2.change_contentreaction'):
            context["user_can_modify"] = [
                reaction.pk for reaction in context['reactions']
            ]
        else:
            queryset_reactions_user = ContentReaction.objects\
                .filter(author__pk=self.request.user.pk, related_content__pk=self.object.pk)\
                .values_list('id', flat=True)
            context["user_can_modify"] = queryset_reactions_user

        context['isantispam'] = self.object.antispam()

        # handle reactions:
        if last_participation_is_old(self.object, self.request.user):
            mark_read(self.object)

        return context
コード例 #5
0
ファイル: views_published.py プロジェクト: BertrandD/zds-site
    def get_context_data(self, **kwargs):
        """Show the given tutorial if exists."""
        context = super(DisplayOnlineContent, self).get_context_data(**kwargs)

        if context['is_staff']:
            context['formRevokeValidation'] = RevokeValidationForm(
                self.versioned_object, initial={'version': self.versioned_object.sha_public})

        context['formWarnTypo'] = WarnTypoForm(self.versioned_object, self.versioned_object)

        queryset_reactions = ContentReaction.objects\
            .select_related('author')\
            .select_related('author__profile')\
            .select_related('editor')\
            .prefetch_related('alerts')\
            .prefetch_related('alerts__author')\
            .filter(related_content__pk=self.object.pk)\
            .order_by("pubdate")

        # pagination of articles
        context['paginate_articles'] = False

        if self.object.type == 'ARTICLE':
            # fetch all articles in order to find the previous and the next one
            all_articles = \
                [a for a in PublishedContent.objects
                    .filter(content_type="ARTICLE", must_redirect=False)
                    .order_by('publication_date')
                    .all()]
            articles_count = len(all_articles)

            try:
                position = all_articles.index(self.public_content_object)
            except ValueError:
                pass  # for an unknown reason, the article is not in the list. This should not happen
            else:
                context['paginate_articles'] = True
                context['previous_article'] = None
                context['next_article'] = None

                if position > 0:
                    context['previous_article'] = all_articles[position - 1]
                if position < articles_count - 1:
                    context['next_article'] = all_articles[position + 1]

        # pagination of comments
        make_pagination(context,
                        self.request,
                        queryset_reactions,
                        settings.ZDS_APP['content']['notes_per_page'],
                        context_list_name='reactions',
                        with_previous_item=True)

        # is JS activated ?
        context["is_js"] = True
        if not self.object.js_support:
            context["is_js"] = False

        # optimize requests:
        votes = CommentVote.objects.filter(user_id=self.request.user.id, comment__in=queryset_reactions).all()
        context["user_like"] = [vote.comment_id for vote in votes if vote.positive]
        context["user_dislike"] = [vote.comment_id for vote in votes if not vote.positive]

        if self.request.user.has_perm('tutorialv2.change_contentreaction'):
            context["user_can_modify"] = [reaction.pk for reaction in queryset_reactions]
        else:
            context["user_can_modify"] = [reaction.pk for reaction in queryset_reactions
                                          if reaction.author == self.request.user]

        context['isantispam'] = self.object.antispam()
        context['pm_link'] = self.object.get_absolute_contact_url(_(u'À propos de'))
        context['subscriber_count'] = ContentReactionAnswerSubscription.objects.get_subscriptions(self.object).count()

        if self.request.user.is_authenticated():
            for reaction in context['reactions']:
                signals.content_read.send(sender=reaction.__class__, instance=reaction, user=self.request.user)
            signals.content_read.send(
                sender=self.object.__class__, instance=self.object, user=self.request.user, target=PublishableContent)
        if last_participation_is_old(self.object, self.request.user):
            mark_read(self.object, self.request.user)

        return context