def form_valid(self, form): _type = _("à l'article") if self.object.is_tutorial: _type = _("au tutoriel") elif self.object.is_opinion: raise PermissionDenied bot = get_object_or_404(User, username=settings.ZDS_APP["member"]["bot_account"]) all_authors_pk = [author.pk for author in self.object.authors.all()] user = form.cleaned_data["user"] if user.pk in all_authors_pk: messages.error(self.request, _("Un auteur ne peut pas être désigné comme contributeur")) return redirect(self.object.get_absolute_url()) else: contribution_role = form.cleaned_data.get("contribution_role") comment = form.cleaned_data.get("comment") if ContentContribution.objects.filter( user=user, contribution_role=contribution_role, content=self.object ).exists(): messages.error( self.request, _( "Ce membre fait déjà partie des " 'contributeurs {} avec pour rôle "{}"'.format(_type, contribution_role.title) ), ) return redirect(self.object.get_absolute_url()) contribution = ContentContribution( user=user, contribution_role=contribution_role, comment=comment, content=self.object ) contribution.save() url_index = reverse(self.object.type.lower() + ":find-" + self.object.type.lower(), args=[user.pk]) send_mp( bot, [user], format_lazy("{} {}", _("Contribution"), _type), self.versioned_object.title, render_to_string( "tutorialv2/messages/add_contribution_pm.md", { "content": self.object, "type": _type, "url": self.object.get_absolute_url(), "index": url_index, "user": user.username, "role": contribution.contribution_role.title, }, ), send_by_mail=True, direct=False, leave=True, ) self.success_url = self.object.get_absolute_url() return super(AddContributorToContent, self).form_valid(form)
def create_contribution(role, contributor, content): contribution = ContentContribution(contribution_role=role, user=contributor, content=content) contribution.save() return contribution