Esempio n. 1
0
    def get_context_data(self, **kwargs):
        context = super(DisplayContent, self).get_context_data(**kwargs)

        # check whether this tuto support js fiddle
        if self.object.js_support:
            is_js = "js"
        else:
            is_js = ""
        context["is_js"] = is_js

        self.get_forms(context)

        context["gallery"] = self.object.gallery
        context["public_content_object"] = self.public_content_object
        if self.object.type.lower() != "opinion":
            context["formAddReviewer"] = ContributionForm(content=self.object)
            context["contributions"] = ContentContribution.objects.filter(
                content=self.object).order_by("contribution_role__position")
            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)})

        return context
Esempio n. 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)

            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