def _prepare(cls, create, *, light=True, **kwargs): auths = [] if 'author_list' in kwargs: auths = kwargs.pop('author_list') given_licence = None if 'licence' in kwargs: given_licence = kwargs.pop('licence', None) or Licence.objects.first() if isinstance(given_licence, str) and given_licence: given_licence = Licence.objects.filter( title=given_licence).first() or Licence.objects.first() licence = given_licence or LicenceFactory() text = text_content if not light: text = tricky_text_content publishable_content = super(PublishableContentFactory, cls)._prepare(create, **kwargs) publishable_content.gallery = GalleryFactory() publishable_content.licence = licence for auth in auths: publishable_content.authors.add(auth) publishable_content.save() for author in publishable_content.authors.all(): UserGalleryFactory(user=author, gallery=publishable_content.gallery, mode='W') init_new_repo(publishable_content, text, text) return publishable_content
def _prepare(cls, create, **kwargs): auths = [] if 'author_list' in kwargs: auths = kwargs.pop('author_list') light = True if 'light' in kwargs: light = kwargs.pop('light') text = text_content if not light: text = tricky_text_content publishable_content = super(PublishableContentFactory, cls)._prepare(create, **kwargs) publishable_content.gallery = GalleryFactory() for auth in auths: publishable_content.authors.add(auth) publishable_content.save() for author in publishable_content.authors.all(): UserGalleryFactory(user=author, gallery=publishable_content.gallery, mode='W') init_new_repo(publishable_content, text, text) return publishable_content
def form_valid(self, form): # create the object: self.content = PublishableContent() self.content.title = form.cleaned_data["title"] self.content.description = form.cleaned_data["description"] self.content.type = form.cleaned_data["type"] self.content.licence = self.request.user.profile.licence # Use the preferred license of the user if it exists self.content.source = form.cleaned_data["source"] self.content.creation_date = datetime.now() # Creating the gallery gal = Gallery() gal.title = form.cleaned_data["title"] gal.slug = slugify(form.cleaned_data["title"]) gal.pubdate = datetime.now() gal.save() self.content.gallery = gal self.content.save() # create image: if "image" in self.request.FILES: img = Image() img.physical = self.request.FILES["image"] img.gallery = gal img.title = self.request.FILES["image"] img.slug = slugify(self.request.FILES["image"].name) img.pubdate = datetime.now() img.save() self.content.image = img self.content.save() # We need to save the content before changing its author list since it's a many-to-many relationship self.content.authors.add(self.request.user) self.content.ensure_author_gallery() self.content.save() # Add subcategories on tutorial for subcat in form.cleaned_data["subcategory"]: self.content.subcategory.add(subcat) self.content.save() # create a new repo : init_new_repo( self.content, form.cleaned_data["introduction"], form.cleaned_data["conclusion"], form.cleaned_data["msg_commit"], ) return super(CreateContent, self).form_valid(form)
def form_valid(self, form): # create the object: self.content = PublishableContent() self.content.title = form.cleaned_data["title"] self.content.description = form.cleaned_data["description"] self.content.type = form.cleaned_data["type"] self.content.licence = self.request.user.profile.licence # Use the preferred license of the user if it exists self.content.source = form.cleaned_data["source"] self.content.creation_date = datetime.now() # Creating the gallery gal = create_content_gallery(form) # create image: if "image" in self.request.FILES: mixin = ImageCreateMixin() mixin.gallery = gal try: img = mixin.perform_create(str(_("Icône du contenu")), self.request.FILES["image"]) except NotAnImage: form.add_error("image", _("Image invalide")) return super().form_invalid(form) img.pubdate = datetime.now() self.content.gallery = gal self.content.save() if "image" in self.request.FILES: self.content.image = img self.content.save() # We need to save the content before changing its author list since it's a many-to-many relationship self.content.authors.add(self.request.user) self.content.ensure_author_gallery() self.content.save() # Add subcategories on tutorial for subcat in form.cleaned_data["subcategory"]: self.content.subcategory.add(subcat) self.content.save() # create a new repo : init_new_repo( self.content, form.cleaned_data["introduction"], form.cleaned_data["conclusion"], form.cleaned_data["msg_commit"], ) return super().form_valid(form)
def _generate(cls, create, attrs): # These parameters are only used inside _generate() and won't be saved in the database, # which is why we use attrs.pop() (they are removed from attrs). light = attrs.pop("light", True) author_list = attrs.pop("author_list", None) add_license = attrs.pop("add_license", True) add_category = attrs.pop("add_category", True) # This parameter will be saved in the database, # which is why we use attrs.get() (it stays in attrs). licence = attrs.get("licence", None) auths = author_list or [] if add_license: given_licence = licence or Licence.objects.first() if isinstance(given_licence, str) and given_licence: given_licence = Licence.objects.filter( title=given_licence).first() or Licence.objects.first() licence = given_licence or LicenceFactory() text = text_content if not light: text = tricky_text_content intro_content = attrs.pop("intro", text) conclusion_content = attrs.pop("conclusion", text) publishable_content = super()._generate(create, attrs) publishable_content.gallery = GalleryFactory() publishable_content.licence = licence for auth in auths: publishable_content.authors.add(auth) if add_category: publishable_content.subcategory.add(SubCategoryFactory()) publishable_content.save() for author in publishable_content.authors.all(): UserGalleryFactory(user=author, gallery=publishable_content.gallery, mode="W") init_new_repo(publishable_content, intro_content, conclusion_content) return publishable_content
def _prepare(cls, create, *, light=True, author_list=None, licence: Licence = None, add_license=True, add_category=True, **kwargs): auths = author_list or [] if add_license: given_licence = licence or Licence.objects.first() if isinstance(given_licence, str) and given_licence: given_licence = Licence.objects.filter( title=given_licence).first() or Licence.objects.first() licence = given_licence or LicenceFactory() text = text_content if not light: text = tricky_text_content publishable_content = super(PublishableContentFactory, cls)._prepare(create, **kwargs) publishable_content.gallery = GalleryFactory() publishable_content.licence = licence for auth in auths: publishable_content.authors.add(auth) if add_category: publishable_content.subcategory.add(SubCategoryFactory()) publishable_content.save() for author in publishable_content.authors.all(): UserGalleryFactory(user=author, gallery=publishable_content.gallery, mode="W") init_new_repo(publishable_content, text, text) return publishable_content
def form_valid(self, form): if self.request.FILES["archive"]: try: zfile = zipfile.ZipFile(self.request.FILES["archive"], "r") except zipfile.BadZipfile: messages.error(self.request, _("Cette archive n'est pas au format ZIP.")) return self.form_invalid(form) try: new_content = UpdateContentWithArchive.extract_content_from_zip( zfile) except BadArchiveError as e: messages.error(self.request, e.message) return super(CreateContentFromArchive, self).form_invalid(form) except KeyError as e: messages.error( self.request, _(e.message + " n'est pas correctement renseigné.")) return super(CreateContentFromArchive, self).form_invalid(form) else: # Warn the user if the license has been changed manifest = json_handler.loads( str(zfile.read("manifest.json"), "utf-8")) if new_content.licence and "licence" in manifest and manifest[ "licence"] != new_content.licence.code: messages.info( self.request, _("la licence « {} » a été appliquée.".format( new_content.licence.code))) # first, create DB object (in order to get a slug) self.object = PublishableContent() self.object.title = new_content.title self.object.description = new_content.description self.object.licence = new_content.licence self.object.type = new_content.type # change of type is then allowed !! self.object.creation_date = datetime.now() self.object.save() new_content.slug = self.object.slug # new slug (choosen via DB) # Creating the gallery gal = Gallery() gal.title = new_content.title gal.slug = slugify(new_content.title) gal.pubdate = datetime.now() gal.save() # Attach user to gallery self.object.gallery = gal self.object.save() # Add subcategories on tutorial for subcat in form.cleaned_data["subcategory"]: self.object.subcategory.add(subcat) # We need to save the tutorial before changing its author list since it's a many-to-many relationship self.object.authors.add(self.request.user) self.object.save() self.object.ensure_author_gallery() # ok, now we can import introduction = "" conclusion = "" if new_content.introduction: introduction = str(zfile.read(new_content.introduction), "utf-8") if new_content.conclusion: conclusion = str(zfile.read(new_content.conclusion), "utf-8") commit_message = _("Création de « {} »").format( new_content.title) init_new_repo(self.object, introduction, conclusion, commit_message=commit_message) # copy all: versioned = self.object.load_version() try: UpdateContentWithArchive.update_from_new_version_in_zip( versioned, new_content, zfile) except BadArchiveError as e: self.object.delete() # abort content creation messages.error(self.request, e.message) return super(CreateContentFromArchive, self).form_invalid(form) # and end up by a commit !! commit_message = form.cleaned_data["msg_commit"] if not commit_message: commit_message = _( "Importation d'une archive contenant « {} »").format( new_content.title) versioned.slug = self.object.slug # force slug to ensure path resolution sha = versioned.repo_update( versioned.title, versioned.get_introduction(), versioned.get_conclusion(), commit_message, update_slug=True, ) # This HAVE TO happen after commiting files (if not, content are None) if "image_archive" in self.request.FILES: try: zfile = zipfile.ZipFile( self.request.FILES["image_archive"], "r") except zipfile.BadZipfile: messages.error( self.request, _("L'archive contenant les images n'est pas au format ZIP." )) return self.form_invalid(form) UpdateContentWithArchive.use_images_from_archive( self.request, zfile, versioned, self.object.gallery) commit_message = _( "Utilisation des images de l'archive pour « {} »" ).format(new_content.title) sha = versioned.commit_changes( commit_message) # another commit # of course, need to update sha self.object.sha_draft = sha self.object.update_date = datetime.now() self.object.save() self.success_url = reverse("content:view", args=[versioned.pk, versioned.slug]) return super(CreateContentFromArchive, self).form_valid(form)