Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

converted the post_comment view to a class based view building on top of the generic FormView #170

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

thomst
Copy link

@thomst thomst commented Jul 13, 2021

As discussed in issue #82 a classed based view would ease the task of customize the post_comment view. I took the effort and rebuild the logic of the post_comment view as a class based view. I was anxious to not alter the logic of the routine itself. Just migrated it into the scructure of the generic FormView.

We tried to strictly follow the logic of the original code and keep as much of it as possible while just migrating the code into the structure of the generic FormView.
Instead of passing the CommentPostBadRequest  object we just initialize it within the BadRequest itself.
@thomst thomst changed the title replaced the post_comment view by a class based view building on top of the generic FormView converted the post_comment view to a class based view building on top of the generic FormView Jul 13, 2021
Copy link
Collaborator

@atodorov atodorov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments requested.

I am also not seeing any results reported by Travis CI here so can't judge how well does the new implementation function wrt the existing test suite.

from django.views.generic.edit import FormView
from django.shortcuts import render, resolve_url

from ..compat import url_has_allowed_host_and_scheme
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

everywhere else we use absolute imports, I think we should use an absolute import here too.

"""Return an instance of the form to be used in this view."""
if form_class is None:
form_class = self.get_form_class()
return form_class(self.target_object, data=self.data)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this invocation be modeled after the inherited get_form() implementation and move self.target_object and self.data as part of get_form_kwargs ? The parent implementation is

    def get_form(self, form_class=None):
        """Return an instance of the form to be used in this view."""
        if form_class is None:
            form_class = self.get_form_class()
        return form_class(**self.get_form_kwargs())

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we need the data coming from self.get_form_kwargs() and the target-object in various places in the code I decided to assign them to self.data and self.target_object in the post-method.

In my opinion this is the most comprehensible solution.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing is, that the comment form class takes the target_object as positional argument, which cannot be passed by unpacking the dict returned by self.get_form_kwargs().

form = self.get_form()

# Check security information
if form.security_errors():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain what is your intention here. From what I can see you shouldn't need to override the post method and not call get_form_kwargs() or do form validation manually. All of that is supposed to be handled by FormView under the hood.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I expained above the post method is the first one called. It's the best place to set object attributes that are needed throught out the code, like self.data and self.target_object.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the inherited post method only two cases are differentiated: form-valid and form-invalid. In the logic of the original post_comment view there is a third one: form.security_errors(). Another reason to customize the post-method.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the post method here as the original post_comment method. The original content of the post_comment is split in other methods inherited from the FormView and what is not in other methods remain here in the post method. I think it's a good approach.

The reason why I believe the call to form.security_errors is here, is because the is_valid logic of the CommentForm does not do it. Putting it somewhere else and leaving the class without this post method would leave this new implementation a bit less readable. It could be in the CommentForm, but it would represent more side effects to look after.

What do you think? What do you suggest to do to continue moving forward with this PR? I mean I really like what @thomst did here and it would be great if it landed in the repository.
Thanks for your input :-)

except BadRequest as exc:
return exc.response
else:
return self.form_valid(form)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment. FormView is supposed to be doing this for you and you can simply override form_valid/form_invalid methods.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a question of taste..

@atodorov
Copy link
Collaborator

@claudep it seems Travis CI has migrated from .org to .com for open source repositories and also uses GitHub actions integration (from what I can tell). Can you re-enable this repository on travis-ci.com b/c I don't seem to have permissions to do so.

@thomst
Copy link
Author

thomst commented Jul 13, 2021

Some comments requested.

I am also not seeing any results reported by Travis CI here so can't judge how well does the new implementation function wrt the existing test suite.

tox run well on my changes:

  ...
  py35-django22: commands succeeded
  py36-django22: commands succeeded
  py37-django22: commands succeeded
  py38-django22: commands succeeded
  py39-django22: commands succeeded
  py36-django30: commands succeeded
  py37-django30: commands succeeded
  py38-django30: commands succeeded
  py39-django30: commands succeeded
  py36-django31: commands succeeded
  py37-django31: commands succeeded
  py38-django31: commands succeeded
  py39-django31: commands succeeded
  py36-django32: commands succeeded
  py37-django32: commands succeeded
  py38-django32: commands succeeded
  py39-django32: commands succeeded
  py36-master: commands succeeded
  py37-master: commands succeeded
  py38-master: commands succeeded
  py39-master: commands succeeded
  congratulations :)

@claudep
Copy link
Member

claudep commented Aug 28, 2021

Tests should be runable again now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants