src/blog_admin/views.py
def form_valid(self, form):
        """Process and save form data and return back an RedirectResponse."""
        # ToDo: Add unit tests
        # ToDo: Refactor (extract method)
        # Convert received hashtags from json format to the list[str]
        hashtags = PostFormView.parse_hashtags(
            form.cleaned_data['hashtags']
        )
        self.stored_tags = PostFormView.retrieve_stored_tags()

        # Filter new tags, validate them and save them to db
        new_tags = self.filter_new_tags(hashtags)

        if not self.new_tags_are_valid(new_tags):
            form.add_error('hashtags', 'Hashtag(s) doesn\'t comply')
            return self.form_invalid(form)

        new_tags = self.save_new_tags(new_tags)

        # Save post and add author to it.
        # Posts needs to be saved before linking hashtags objects in the db
        post = self.save_post(form)

        # Select all tags, which are linked with current post
        post_tags = [
            Hashtags(text=tag) for tag in hashtags
            if tag in self.stored_tags
        ]
        post_tags.extend(new_tags)

        # Link created hashtags with the post object in the db
        for tag in post_tags:
            post.hashtags.add(tag)

        # Return successful HttpResponse
        return super().form_valid(form)
Process and save form data and return back an RedirectResponse.
Process and save form data and return back an RedirectResponse.
Process and save form data and return back an RedirectResponse .
def form_valid(self, form):
        hashtags = PostFormView.parse_hashtags(
            form.cleaned_data['hashtags']
        )
        self.stored_tags = PostFormView.retrieve_stored_tags()
        new_tags = self.filter_new_tags(hashtags)
        if not self.new_tags_are_valid(new_tags):
            form.add_error('hashtags', 'Hashtag(s) doesn\'t comply')
            return self.form_invalid(form)
        new_tags = self.save_new_tags(new_tags)
        post = self.save_post(form)
        post_tags = [
            Hashtags(text=tag) for tag in hashtags
            if tag in self.stored_tags
        ]
        post_tags.extend(new_tags)
        for tag in post_tags:
            post.hashtags.add(tag)
        return super().form_valid(form)
def form_valid ( self , form ) : hashtags = PostFormView . parse_hashtags ( form . cleaned_data [ 'hashtags' ] ) self . stored_tags = PostFormView . retrieve_stored_tags ( ) new_tags = self . filter_new_tags ( hashtags ) if not self . new_tags_are_valid ( new_tags ) : form . add_error ( 'hashtags' , 'Hashtag(s) doesn\'t comply' ) return self . form_invalid ( form ) new_tags = self . save_new_tags ( new_tags ) post = self . save_post ( form ) post_tags = [ Hashtags ( text = tag ) for tag in hashtags if tag in self . stored_tags ] post_tags . extend ( new_tags ) for tag in post_tags : post . hashtags . add ( tag ) return super ( ) . form_valid ( form )
Process and save form data and return back an RedirectResponse.
Process and save form data and return back an RedirectResponse .
"""Process and save form data and return back an RedirectResponse.""" # ToDo: Add unit tests # ToDo: Refactor (extract method) # Convert received hashtags from json format to the list[str] # Filter new tags, validate them and save them to db # Save post and add author to it. # Posts needs to be saved before linking hashtags objects in the db # Select all tags, which are linked with current post # Link created hashtags with the post object in the db # Return successful HttpResponse
self
form