[docs]classPostWizardForm(forms.ModelForm):""" Form for the post wizard. It is used to create a new post with the required fields. """default_appconfig=Noneapp_config=forms.IntegerField(label=_("app. config"),required=True,widget=forms.HiddenInput,)
[docs]defsave(self,commit=True):ifself.errors:returnsuper().save(commit)post=Post(app_config=self.cleaned_data["app_config"],)user=get_current_user()post._set_default_author(user)post.save()autocreate_plugin=commitandself.cleaned_data["app_config"].use_placeholderpost_text=self.instance.post_textinstance_dict=forms.model_to_dict(self.instance)instance_dict["post"]=postinstance_dict["language"]=self.language_codeinstance_dict["slug"]=self.create_slug()ifautocreate_plugin:delinstance_dict["post_text"]# Create a plugin laterself.instance=self.Meta.model.objects.with_user(user).create(**instance_dict)ifautocreate_plugin:self.add_plugin(post_text)# Create plugin nowreturnself.instance
[docs]defcreate_slug(self):""" Generate a valid slug, in case the given one is taken """source=self.cleaned_data.get("slug","")lang_choice=self.language_codeifnotsource:source=slugify(self.cleaned_data.get("title",""))qs=PostContent.admin_manager.latest_content(language=lang_choice)used=list(qs.values_list("slug",flat=True))slug=sourcei=2whilesluginused:slug=f"{source}-{i}"i+=1returnslug
[docs]defclean_app_config(self):try:pk=int(self.cleaned_data.get("app_config",self.default_appconfig))config=StoriesConfig.objects.get(pk=pk)except(ValueError,TypeError,ObjectDoesNotExist):self.add_error(None,_("Selected story not available any more. Close wizard."))return0# Invalid PKreturnconfig
[docs]defadd_plugin(self,text:str)->None:""" Add text field content as text plugin to the post. """iftext:plugin_type=get_setting("WIZARD_CONTENT_PLUGIN")plugin_body=get_setting("WIZARD_CONTENT_PLUGIN_BODY")opts={"placeholder":self.instance.content,"plugin_type":plugin_type,"language":self.language_code,plugin_body:text,}add_plugin(**opts)