Skip to content

Commit

Permalink
fix(buddy-system): quick match if matcher faculty has not been set
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Nov 11, 2023
1 parent 0f20403 commit 84add1e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
2 changes: 2 additions & 0 deletions fiesta/apps/accounts/forms/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class UserProfileForm(BaseModelForm):
FIELDS_TO_CONFIGURATION = {
UserProfile.university: SectionsConfiguration.required_university,
UserProfile.faculty: SectionsConfiguration.required_faculty,
UserProfile.nationality: SectionsConfiguration.required_nationality,
UserProfile.gender: SectionsConfiguration.required_gender,
UserProfile.picture: SectionsConfiguration.required_picture,
Expand Down
14 changes: 11 additions & 3 deletions fiesta/apps/buddy_system/forms.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

from django.core.exceptions import ValidationError
from django.forms import BooleanField, HiddenInput, fields_for_model
from django.template.loader import render_to_string
from django.utils.functional import lazy
from django.utils.translation import gettext_lazy as _

from apps.accounts.models import UserProfile
from apps.accounts.models import User, UserProfile
from apps.buddy_system.models import BuddyRequest, BuddyRequestMatch
from apps.fiestaforms.fields.array import ChoicedArrayField
from apps.fiestaforms.forms import BaseModelForm
Expand Down Expand Up @@ -100,15 +101,22 @@ class Meta:

class QuickBuddyMatchForm(BaseModelForm):
submit_text = _("Match")
instance: BuddyRequestMatch

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# self.fields["issuer"].disabled = True

class Meta:
model = BuddyRequestMatch
fields = ("matcher",)
widgets = {
"matcher": ActiveLocalMembersFromSectionWidget,
}

def clean_matcher(self):
matcher: User = self.cleaned_data["matcher"]

if not matcher.profile_or_none.faculty:
raise ValidationError(_("This user has not set their faculty. Please ask them to do so or do it yourself."))

return matcher
14 changes: 11 additions & 3 deletions fiesta/apps/buddy_system/views/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django_tables2 import Column, TemplateColumn, tables
from django_tables2.utils import Accessor

from apps.accounts.models import User, UserProfile
from apps.buddy_system.forms import BuddyRequestEditorForm, QuickBuddyMatchForm
from apps.buddy_system.models import BuddyRequest, BuddyRequestMatch
from apps.fiestaforms.views.htmx import HtmxFormMixin
Expand Down Expand Up @@ -175,15 +176,19 @@ class QuickBuddyMatchView(

def get_initial(self):
try:
matcher: User = self.get_object().match.matcher
profile: UserProfile = matcher.profile_or_none
return {
"matcher": self.get_object().match.matcher,
"matcher": matcher,
# SectionPluginsValidator ensures that faculty is required if BuddySystem is enabled
"matcher_faculty": profile.faculty if profile else None,
}
except BuddyRequestMatch.DoesNotExist:
return {}

@transaction.atomic
def form_valid(self, form):
br: BuddyRequest = self.get_object()
br: BuddyRequest = form.instance

try:
if br.match:
Expand All @@ -192,9 +197,12 @@ def form_valid(self, form):
except BuddyRequestMatch.DoesNotExist:
pass

matcher: User = form.cleaned_data.get("matcher")

match = BuddyRequestMatch(
request=br,
matcher=form.cleaned_data.get("matcher"),
matcher=matcher,
matcher_faculty=matcher.profile_or_none.faculty,
)

match.save()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2023-11-10 22:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('sections', '0016_alter_sectionmembership_state'),
]

operations = [
migrations.AddField(
model_name='sectionsconfiguration',
name='required_faculty',
field=models.BooleanField(blank=True, default=None, help_text='Flag if field is needed to fill in user profile: True=field is required, False=field is optional, None=field is not available', null=True, verbose_name='required faculty'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2023-11-10 23:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('sections', '0017_sectionsconfiguration_required_faculty'),
]

operations = [
migrations.AddField(
model_name='sectionsconfiguration',
name='required_university',
field=models.BooleanField(blank=True, default=None, help_text='Flag if field is needed to fill in user profile: True=field is required, False=field is optional, None=field is not available', null=True, verbose_name='required university'),
),
]
14 changes: 14 additions & 0 deletions fiesta/apps/sections/models/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@


class SectionsConfiguration(BasePluginConfiguration):
required_university = BooleanField(
verbose_name=_("required university"),
default=None,
null=True,
blank=True,
help_text=FLAG_HELP_TEXT,
)
required_faculty = BooleanField(
verbose_name=_("required faculty"),
default=None,
null=True,
blank=True,
help_text=FLAG_HELP_TEXT,
)
required_nationality = BooleanField(
verbose_name=_("required nationality"),
default=None,
Expand Down

0 comments on commit 84add1e

Please sign in to comment.