def save_validation_state(self, db_object, form, is_update, published, validation, versioned): # save in database db_object.sha_public = validation.version db_object.source = form.cleaned_data['source'] db_object.sha_validation = None db_object.public_version = published if form.cleaned_data['is_major'] or not is_update or db_object.pubdate is None: db_object.pubdate = datetime.now() db_object.is_obsolete = False # close beta if is an article if db_object.type == 'ARTICLE': db_object.sha_beta = None topic = db_object.beta_topic if topic is not None and not topic.is_locked: msg_post = render_to_string( 'tutorialv2/messages/beta_desactivate.md', {'content': versioned} ) send_post(self.request, topic, self.request.user, msg_post) lock_topic(topic) db_object.save() # save validation object validation.comment_validator = form.cleaned_data['text'] validation.status = 'ACCEPT' validation.date_validation = datetime.now() validation.save()
def save_validation_state(self, db_object, form, is_update, published, validation, versioned): # save in database db_object.sha_public = validation.version db_object.source = form.cleaned_data['source'] db_object.sha_validation = None db_object.public_version = published if form.cleaned_data[ 'is_major'] or not is_update or db_object.pubdate is None: db_object.pubdate = datetime.now() db_object.is_obsolete = False # close beta if is an article if db_object.type == 'ARTICLE': db_object.sha_beta = None topic = db_object.beta_topic if topic is not None and not topic.is_locked: msg_post = render_to_string( 'tutorialv2/messages/beta_desactivate.md', {'content': versioned}) send_post(self.request, topic, self.request.user, msg_post) lock_topic(topic) db_object.save() # save validation object validation.comment_validator = form.cleaned_data['text'] validation.status = 'ACCEPT' validation.date_validation = datetime.now() validation.save()
def close_article_beta(db_object, versioned, user, request=None): """ Close forum topic of an article if the artcle was in beta. :param db_object: the article :type db_object: zds.tutorialv2.models.database.PublishableContent :param versioned: the public version of article, used to pupulate closing post :type versioned: zds.tutorialv2.models.versioned.VersionedContent :param user: the current user :param request: the current request """ if db_object.type == "ARTICLE": db_object.sha_beta = None topic = db_object.beta_topic if topic is not None and not topic.is_locked: msg_post = render_to_string("tutorialv2/messages/beta_desactivate.md", {"content": versioned}) send_post(request, topic, user, msg_post) lock_topic(topic)
def form_valid(self, form): beta_version = self.versioned_object sha_beta = beta_version.current_version # topic of the beta version: topic = self.object.beta_topic if topic: if topic.forum_id != settings.ZDS_APP["forum"]["beta_forum_id"]: # if the topic is moved from the beta forum, then a new one is created instead topic = None _type = self.object.type.lower() if _type == "tutorial": _type = _("tutoriel") elif _type == "opinion": raise PermissionDenied # perform actions: if self.action == "inactive": self.object.sha_beta = None msg_post = render_to_string( "tutorialv2/messages/beta_desactivate.md", { "content": beta_version, "type": _type }) send_post(self.request, topic, self.request.user, msg_post) lock_topic(topic) elif self.action == "set": already_in_beta = self.object.in_beta() all_tags = [] if not already_in_beta or self.object.sha_beta != sha_beta: self.object.sha_beta = sha_beta self.versioned_object.in_beta = True self.versioned_object.sha_beta = sha_beta msg = render_to_string( "tutorialv2/messages/beta_activate_topic.md", { "content": beta_version, "type": _type, "url": settings.ZDS_APP["site"]["url"] + self.versioned_object.get_absolute_url_beta(), }, ) if not topic: # if first time putting the content in beta, send a message on the forum and a PM # find tags all_tags = self._get_all_tags() topic = self._create_beta_topic(msg, beta_version, _type, all_tags) bot = get_object_or_404( User, username=settings.ZDS_APP["member"]["bot_account"]) msg_pm = render_to_string( "tutorialv2/messages/beta_activate_pm.md", { "content": beta_version, "type": _type, "url": settings.ZDS_APP["site"]["url"] + topic.get_absolute_url(), "user": self.request.user, }, ) if not self.object.validation_private_message: self.object.validation_private_message = send_mp( bot, self.object.authors.all(), self.object.validation_message_title, beta_version.title, msg_pm, send_by_mail=False, leave=True, hat=get_hat_from_settings("validation"), ) self.object.save() else: send_message_mp( bot, self.object.validation_private_message, msg, hat=get_hat_from_settings("validation")) # When the anti-spam triggers (because the author of the # message posted themselves within the last 15 minutes), # it is likely that we want to avoid to generate a duplicated # post that couldn't be deleted. We hence avoid to add another # message to the topic. else: all_tags = self._get_all_tags() if not already_in_beta: unlock_topic(topic) msg_post = render_to_string( "tutorialv2/messages/beta_reactivate.md", { "content": beta_version, "type": _type, "url": settings.ZDS_APP["site"]["url"] + self.versioned_object.get_absolute_url_beta(), }, ) topic = send_post(self.request, topic, self.request.user, msg_post) elif not topic.antispam(): msg_post = render_to_string( "tutorialv2/messages/beta_update.md", { "content": beta_version, "type": _type, "url": settings.ZDS_APP["site"]["url"] + self.versioned_object.get_absolute_url_beta(), }, ) topic = send_post(self.request, topic, self.request.user, msg_post) # make sure that all authors follow the topic: for author in self.object.authors.all(): TopicAnswerSubscription.objects.get_or_create_active( author, topic) mark_read(topic, author) # finally set the tags on the topic if topic: topic.tags.clear() for tag in all_tags: topic.tags.add(tag) topic.save() self.object.save( ) # we should prefer .update but it needs a huge refactoring self.success_url = self.versioned_object.get_absolute_url( version=sha_beta) if self.object.is_beta(sha_beta): self.success_url = self.versioned_object.get_absolute_url_beta() return super(ManageBetaContent, self).form_valid(form)
def form_valid(self, form): user = self.request.user validation = Validation.objects.filter(pk=self.kwargs['pk']).last() if not validation: raise PermissionDenied if validation.validator != user: raise PermissionDenied if validation.status != 'PENDING_V': raise PermissionDenied # get database representation and validated version db_object = validation.content versioned = db_object.load_version(sha=validation.version) self.success_url = versioned.get_absolute_url( version=validation.version) is_update = db_object.sha_public try: published = publish_content( db_object, versioned, is_major_update=form.cleaned_data['is_major']) except FailureDuringPublication as e: messages.error(self.request, e.message) else: # save in database db_object.sha_public = validation.version db_object.source = form.cleaned_data['source'] db_object.sha_validation = None db_object.public_version = published if form.cleaned_data[ 'is_major'] or not is_update or db_object.pubdate is None: db_object.pubdate = datetime.now() db_object.is_obsolete = False # close beta if is an article if db_object.type == 'ARTICLE': db_object.sha_beta = None topic = db_object.beta_topic if topic is not None and not topic.is_locked: msg_post = render_to_string( 'tutorialv2/messages/beta_desactivate.md', {'content': versioned}) send_post(self.request, topic, self.request.user, msg_post) lock_topic(topic) db_object.save() # save validation object validation.comment_validator = form.cleaned_data['text'] validation.status = 'ACCEPT' validation.date_validation = datetime.now() validation.save() # Follow signals.new_content.send(sender=db_object.__class__, instance=db_object, by_email=False) messages.success(self.request, _('Le contenu a bien été validé.')) self.success_url = published.get_absolute_url_online() return super(AcceptValidation, self).form_valid(form)