Skip to content

Commit

Permalink
Moved code around + bugfix
Browse files Browse the repository at this point in the history
Fixed a bug with dispatch order where successful links were redirected to the fail page.

Ensured that last_updated_by is set when an account is linked.

Moved some code to the utils module.
  • Loading branch information
EricTRL committed Sep 17, 2023
1 parent 3196a78 commit f334413
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 251 deletions.
47 changes: 47 additions & 0 deletions core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,53 @@ def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
return context


class LinkedLoginView(LoginView):
"""
A variant of the standard LoginView that shows that some data will be linked to
the Squire account when logging in. This does not actually link any data itself;
subclasses should implement that sort of behaviour.
"""

image_source = None
image_alt = None
link_title = None
link_description = None
link_extra = None

def get_image_source(self):
"""The image for the data to be linked. Defaults to Squire's logo."""
return self.image_source

def get_image_alt(self):
"""Alt text for the image."""
return self.image_alt

def get_link_title(self):
"""Title for the data to be linked."""
return self.link_title

def get_link_description(self):
"""A description of the data to be linked."""
return self.link_description

def get_link_extra(self):
"""Any extra information for the data to be linked."""
return self.link_extra

def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
context = super().get_context_data(**kwargs)
context.update(
{
"image_source": self.get_image_source(),
"image_alt": self.get_image_alt(),
"link_title": self.get_link_title(),
"link_description": self.get_link_description(),
"link_extra": self.get_link_extra(),
}
)
return context


class LogoutSuccessView(TemplateView):
"""View that is displayed when a user is logged out."""

Expand Down
62 changes: 7 additions & 55 deletions membership_file/forms.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import copy
from typing import Any, Dict
from django import forms
from django.conf import settings
from django.contrib import messages
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.core.mail import EmailMultiAlternatives, send_mail
from django.forms.models import ModelFormMetaclass
from django.core.mail import EmailMultiAlternatives
from django.http import HttpRequest
from django.template import loader
from django.utils.encoding import force_bytes
Expand All @@ -18,7 +14,7 @@
from core.forms import LoginForm, RegisterForm
from membership_file.models import Member, Room, MemberYear, Membership
from membership_file.util import LinkAccountTokenGenerator
from utils.forms import UpdatingUserFormMixin
from utils.forms import FieldsetAdminFormMixin, UpdatingUserFormMixin
from utils.widgets import OtherRadioSelect

##################################################################################
Expand Down Expand Up @@ -116,51 +112,6 @@ def save(self):
)


class FieldsetModelFormMetaclass(ModelFormMetaclass):
"""Sets the `_meta.fieldsets` attribute that is required by the admin panel."""

def __new__(mcs, name, bases, attrs):
new_class = super().__new__(mcs, name, bases, attrs)
new_class._meta.fieldsets = None
meta_class = getattr(new_class, "Meta", None)
if meta_class is not None:
new_class._meta.fieldsets = getattr(meta_class, "fieldsets", None)
return new_class


class FieldsetAdminFormMixin(metaclass=FieldsetModelFormMetaclass):
"""
This mixin allows a form to be used in the admin panel. Notably allows using fieldsets
and default admin widgets (e.g. the datetime picker)
"""

required_css_class = "required"

# ModelAdmin media
@property
def media(self):
extra = "" if settings.DEBUG else ".min"
js = [
"vendor/jquery/jquery%s.js" % extra,
"jquery.init.js",
"core.js",
"admin/RelatedObjectLookups.js",
"actions.js",
"urlify.js",
"prepopulate.js",
"vendor/xregexp/xregexp%s.js" % extra,
]
return forms.Media(js=["admin/js/%s" % url for url in js]) + super().media

def get_fieldsets(self, request, obj=None):
"""
Hook for specifying fieldsets.
"""
if self._meta.fieldsets:
return copy.deepcopy(self._meta.fieldsets)
return [(None, {"fields": self.fields})]


class RegisterMemberForm(UpdatingUserFormMixin, FieldsetAdminFormMixin, forms.ModelForm):
"""
Registers a member in the membership file, and optionally sends them an email to link or register a Squire account.
Expand Down Expand Up @@ -404,9 +355,7 @@ def send_mail(


class ConfirmLinkMembershipRegisterForm(RegisterForm):
"""
TODO: RequestingUserMixin
"""
"""A RegisterForm that, when saved, also links a predetermined member to the newly registered user."""

def __init__(self, member: Member, *args, **kwargs):
# Member should not already have an attached user
Expand All @@ -418,12 +367,14 @@ def _save_m2m(self):
super()._save_m2m()
# Attach new user to predetermined member
self.member.user = self.instance
self.member.last_updated_by = self.instance
self.member.save()


class ConfirmLinkMembershipLoginForm(LoginForm):
"""
TODO: RequestingUserMixin
A LoginForm that, when saved, also links a predetermined member to the logged in user.
Also sets the user's email and name to match that of the linked member.
"""

def __init__(self, member: Member, *args, **kwargs):
Expand All @@ -436,6 +387,7 @@ def save(self):
# Attach new user to predetermined member
user = self.get_user()
self.member.user = user
self.member.last_updated_by = user
self.member.save()

# Update user email and real name
Expand Down
Loading

0 comments on commit f334413

Please sign in to comment.