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

WIP: Try adding guest user library #3

Open
wants to merge 31 commits into
base: main
Choose a base branch
from

Conversation

partizipation
Copy link
Contributor

@partizipation partizipation commented Feb 12, 2025

we decided to fork django-guest-user and add email and password for the guest authentication. See here

Couldn't get around the circular reference, tried a bunch of things. Code has a bunch of commented out attempts so it's a little messy but maybe this PR helps review.

Tried:

  • all possible order of guest stuff, auth, and apps.users in INSTALLED_APPS
  • importing in different ways from within users/models.py
  • registering in users/apps.py in a couple different ways

@partizipation partizipation requested a review from m4ra February 12, 2025 16:10
@m4ra m4ra force-pushed the jp-2502-guest-accounts branch from c3e2c5d to dc4a661 Compare February 13, 2025 14:02
@m4ra
Copy link
Contributor

m4ra commented Feb 13, 2025

@partizipation @goapunk to enable the guest user from the registration form, I would add the maybe_make_guest_user in the a+ DefaultSingUpForm.save().
In that way we don't need a middleware. and the guest user will behave as a regular one. In the Account Settings we will need a check in the request.user if they have a user.geust relation then we should add the conversion form component instead of regular profile. Or something down this line.

@m4ra m4ra requested a review from goapunk February 13, 2025 14:11
@m4ra
Copy link
Contributor

m4ra commented Feb 13, 2025

@partizipation the signup template to add the login as guest button is here. But I think the registration form is in a separate story.


GUEST_USER = {
"NAME_GENERATOR": "guest_user.functions.generate_uuid_username", # Default option
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I changed this in the fork of the django-guest, because the long uuid appears on the menu tab and it's ugly. So I switched to the numbered version. But maybe we deploy this one, and PM can let us know.

{% url 'guest_create' as guest_create_url %}
{% with next_param=request.GET.next %}
{% with guest_url=guest_create_url|add:"?next="|add:next_param %}
<p>{% blocktranslate %}Sie wollen als Gast fortfahren? Dann klicken Sie <a href="{{ guest_url }}">hier</a>.{% endblocktranslate %}</p>
Copy link
Contributor

Choose a reason for hiding this comment

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

We need the english version here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Asked Janek for english text yesterday, so waiting to add the content of these pages until I have that and can just do everything at once

{% with next_param=request.GET.next %}
{% with guest_url=guest_create_url|add:"?next="|add:next_param %}
<p>
{% blocktranslate %}Sie wollen als Gast fortfahren? Dann klicken Sie
Copy link
Contributor

Choose a reason for hiding this comment

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

same

@partizipation partizipation force-pushed the jp-2502-guest-accounts branch from 391ad52 to 2ed7317 Compare March 3, 2025 17:42
Copy link
Contributor

@m4ra m4ra left a comment

Choose a reason for hiding this comment

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

Overall good progress. Needs a bit of cleaning, and some last restructuring of the DefaultSingUp and Guest related forms. And three extra tests:
a) public project with a topic module can be viewed by guest user
b) upon guest user conversion we are sending email with the appropriate subject
c) upon email confirmation, guest user is deleted and its associated user has an email and is verified.

return {
"username": self.cleaned_data["username"],
"password": self.cleaned_data["password1"],
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is only for the GuestCreateForm, move the method get_credentials in the respective class GuestConvertForm

# perform_login(self.request, user, email_verification='optional')

# Complete the signup process (sends confirmation email if required)
response = complete_signup(self.request, user, email_verification='mandatory', success_url="/")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you don't need to add the email_verification parameter neither the success_url here, as we are adding it in the settings/base.py. A new test in converting an account and a) checking the mailbox has 1 email and b) the subject of the email sent is the expected one, will help figuring out that part. You can add it in the testsaccount/test_views.py`. See lines 132 - 137 for how the mailbox and redirect testing is working.

Now, about the redirection, I don't think we need one here, since the conversion takes place in the dashboard of the profile, I would say we don't redirect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you don't need to add the email_verification parameter neither the success_url here

It's gives an error without these parameters, so I'm guessing they're needed since we are using this allauth function outside of the library. I've added a reference to these vars in the settings though.

the redirection, I don't think we need one here

The current behavior on signup for regular users is a redirect - in other words, you are not logged in. If for guest user convert we want to keep them logged in, we could do that but it would be more effort. I had some code in that direction earlier though, before I realized the redirect is probably desired.

response = client.get(url)
assert response.status_code == 403


Copy link
Contributor

Choose a reason for hiding this comment

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

would be good to have an extra test with a public project and asserting 200 as response status.

"""
user = email_address.user
Guest.objects.filter(user=user).delete()
print("guest deleted")
Copy link
Contributor

Choose a reason for hiding this comment

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

would be great to have a test for this signal too.


def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user", None) # Accept an existing user instance
Copy link
Contributor

Choose a reason for hiding this comment

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

we shouldn't have a user at this point.

self.fields["username"].required = True
self.fields["password1"].required = True
self.fields["password2"].required = True

Copy link
Contributor

Choose a reason for hiding this comment

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

Move all related guest user form attributes to the relevant class below, so we keep each form cleaner, even if there are few repetitions.

@partizipation partizipation requested review from arc64 and hom3mad3 March 25, 2025 18:29
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.

2 participants