Skip to content

Commit

Permalink
Simplification for user's schools + section memberships tweaks (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Nov 9, 2023
2 parents cc9c7c3 + 797b661 commit 4174a77
Show file tree
Hide file tree
Showing 82 changed files with 1,252 additions and 496 deletions.
4 changes: 2 additions & 2 deletions fiesta/apps/accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def get_queryset(self, request):
.get_queryset(request)
.select_related(
"user",
"home_university",
"home_faculty__university",
"university",
"faculty__university",
)
)

Expand Down
12 changes: 5 additions & 7 deletions fiesta/apps/accounts/forms/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_form_fields(cls, user: User):
for field_name, conf_field in cls._FIELD_NAMES_TO_CONFIGURATION.items()
if any(conf_field.__get__(c) is not None for c in confs)
)
return cls.Meta.fields + fields_to_include
return fields_to_include + cls.Meta.fields

@classmethod
def get_user_configuration(cls, user: User):
Expand Down Expand Up @@ -82,9 +82,8 @@ class Meta:

fields = (
# TODO: think about limiting the choices by country of section, in which is current membership
"home_university",
"home_faculty",
"guest_faculty",
"university",
"faculty",
"picture",
"facebook",
"instagram",
Expand All @@ -94,9 +93,8 @@ class Meta:
)

widgets = {
"home_university": UniversityWidget,
"home_faculty": FacultyWidget,
"guest_faculty": FacultyWidget,
"university": UniversityWidget,
"faculty": FacultyWidget,
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 4.2.4 on 2023-10-28 17:16

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('universities', '0002_alter_university_name'),
('accounts', '0018_alter_userprofile_instagram'),
]

operations = [
migrations.RemoveConstraint(
model_name='userprofile',
name='home_university_or_faculty',
),
migrations.RemoveField(
model_name='userprofile',
name='guest_faculty',
),
migrations.RemoveField(
model_name='userprofile',
name='home_faculty',
),
migrations.RemoveField(
model_name='userprofile',
name='home_university',
),
migrations.AddField(
model_name='userprofile',
name='faculty',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.RESTRICT, related_name='faculty_user_profiles', to='universities.faculty', verbose_name='faculty'),
),
migrations.AddField(
model_name='userprofile',
name='university',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.RESTRICT, related_name='university_user_profiles', to='universities.university', verbose_name='university'),
),
]
52 changes: 8 additions & 44 deletions fiesta/apps/accounts/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import typing

from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.db import models
from django.db.models import CharField, CheckConstraint, TextChoices
from django.db.models import CharField, TextChoices
from django.utils.translation import gettext_lazy as _
from django_countries.fields import CountryField
from django_lifecycle import AFTER_SAVE, LifecycleModelMixin, hook
Expand All @@ -17,7 +16,6 @@
from apps.files.storage import NamespacedFilesStorage
from apps.utils.models import BaseTimestampedModel
from apps.utils.models.fields import ArrayFieldWithDisplayableChoices
from apps.utils.models.query import Q

if typing.TYPE_CHECKING:
from apps.plugins.middleware.plugin import HttpRequest
Expand Down Expand Up @@ -79,39 +77,25 @@ class Gender(TextChoices):
max_length=16,
)

home_university = models.ForeignKey(
university = models.ForeignKey(
"universities.University",
on_delete=models.RESTRICT,
verbose_name=_("home university"),
help_text=_("home university for all users"),
related_name="home_university_user_profiles",
verbose_name=_("university"),
related_name="university_user_profiles",
null=True,
blank=True,
db_index=True,
)
home_faculty = models.ForeignKey(
faculty = models.ForeignKey(
"universities.Faculty",
on_delete=models.RESTRICT,
verbose_name=_("home faculty"),
# TODO: help, not description
help_text=_("home faculty for members, empty for internationals"),
related_name="home_faculty_user_profiles",
verbose_name=_("faculty"),
related_name="faculty_user_profiles",
null=True,
blank=True,
db_index=True,
)

guest_faculty = models.ForeignKey(
"universities.Faculty",
on_delete=models.RESTRICT,
verbose_name=_("guest faculty"),
help_text=_("guest faculty for international students, empty for members"),
related_name="guest_user_profiles",
blank=True,
null=True,
db_index=True,
)

picture = models.ImageField(
storage=user_profile_picture_storage,
upload_to=user_profile_picture_storage.upload_to,
Expand Down Expand Up @@ -173,26 +157,6 @@ class Preferences(enum.Flag):
class Meta:
verbose_name = _("user profile")
verbose_name_plural = _("user profiles")
constraints = (
CheckConstraint(
# home university XOR home faculty
check=Q(state=UserProfileState.INCOMPLETE.value) | (Q(home_university=None) ^ Q(home_faculty=None)),
name="home_university_or_faculty",
),
)

def clean(self):
super().clean()

if not (bool(self.home_university) ^ bool(self.home_faculty)):
raise ValidationError(
{
"home_university": _(
# TODO: weird, basically it's exactly one of these
"At least one from home university/faculty has to be set."
)
}
)

@hook(AFTER_SAVE)
def on_save(self):
Expand All @@ -203,7 +167,7 @@ def on_save(self):
def __str__(self):
return (
f"{self.user} {self.nationality} "
f"{self.home_university or (self.home_faculty.university if self.home_faculty else None) or ''} "
f"{self.university or (self.faculty.university if self.faculty else None) or ''} "
)

def is_esn_card_holder(self):
Expand Down
117 changes: 60 additions & 57 deletions fiesta/apps/accounts/templates/account/email.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,65 +15,68 @@
<h1 class="card-title">{% trans "E-mail Addresses" %}</h1>
{% if user.emailaddress_set.all %}
<p>{% trans 'The following e-mail addresses are associated with your account:' %}</p>
<form action="{% url 'account_email' %}"
class="{{ form|get_form_classes }}"
method="post">
<form hx-post="{% url 'account_email' %}"
class="{{ form|get_form_classes }}">
{% csrf_token %}
<table class="table table-compact mx-auto">
<ul class="max-w-md mx-auto">
{% for emailaddress in user.emailaddress_set.all %}
<tr>
<td>
<label for="email_radio_{{ forloop.counter }}"
class="flex flex-row items-center gap-x-2
{% if emailaddress.primary %}text-secondary{% endif %}">
<input id="email_radio_{{ forloop.counter }}"
type="radio"
name="email"
class="radio radio-xs"
{% if emailaddress.primary or user.emailaddress_set.count == 1 %}checked="checked"{% endif %}
value="{{ emailaddress.email }}" />
<li class="w-full inline-flex flex-col md:flex-row justify-between mb-2">
<label for="email_radio_{{ forloop.counter }}"
class="flex flex-row items-center gap-x-2
{% if emailaddress.primary %}text-primary{% endif %}">
<input id="email_radio_{{ forloop.counter }}"
type="radio"
name="email"
class="radio"
{# {% if emailaddress.primary or user.emailaddress_set.count == 1 %}checked="checked"{% endif %}#}
value="{{ emailaddress.email }}"/>
{{ emailaddress.email }}
</label>
</td>
<td>
{% if emailaddress.verified %}
<span class="badge badge-success">{% trans "Verified" %}</span>
{% else %}
<span class="badge badge-warning">{% trans "Unverified" %}</span>
{% endif %}
{% if emailaddress.primary %}
<span class="badge badge-secondary">{% trans "Primary" %}</span>
{% endif %}
</td>
</tr>
{% endfor %}
</table>
<div class="Forms__field Forms__field--buttons btn-group">
<button class="btn btn-sm btn-secondary" type="submit" name="action_primary">{% trans 'Make Primary' %}</button>
<button class="btn btn-sm btn-warning" type="submit" name="action_send">{% trans 'Re-send Verification' %}</button>
<button class="btn btn-sm btn-primary" type="submit" name="action_remove">{% trans 'Remove' %}</button>
</div>
</form>
{% else %}
<p class="my-2">
<strong>{% trans 'Warning:' %}</strong> {% trans "You currently do not have any e-mail address set up. You should really add an e-mail address so you can receive notifications, reset your password, etc." %}
</p>
{% endif %}
{% if can_add_email %}
<hr>
<h2 class="card-title">{% trans "Add New E-mail Address" %}</h2>
<form method="post"
action="{% url 'account_email' %}"
class="{{ form|get_form_classes }}">
{% csrf_token %}
{% include "fiestaforms/parts/errors.html" with errors=form.non_field_errors %}
{% include "fiestaforms/parts/field.html" with bf=form.email errors=form.errors.email %}
<div class="Forms__field Forms__field--buttons">
<button name="action_add" type="submit" class="btn btn-primary">{% trans "Add E-mail" %}</button>
</div>
</form>
{% endif %}
<script type="text/javascript">
<span>
{% if emailaddress.primary %}
<span class="badge badge-primary">{% trans "Primary" %}</span>
{% endif %}
{% if emailaddress.verified %}
<span class="badge badge-success">{% trans "Verified" %}</span>
{% else %}
<span class="badge badge-warning">{% trans "Unverified" %}</span>
{% endif %}
</span>
</li>
{% endfor %}
</ul>
<div class="Forms__field Forms__field--buttons btn-group flex-wrap">
<button class="btn btn-sm btn-primary btn-outline"
type="submit"
name="action_primary">{% trans 'Make Primary' %}</button>
<button class="btn btn-sm btn-warning btn-outline"
type="submit"
name="action_send">{% trans 'Re-send Verification' %}</button>
<button class="btn btn-sm btn-error btn-outline"
type="submit"
name="action_remove">{% trans 'Remove' %}</button>
</div>
</form>
{% else %}
<p class="my-2">
<strong>{% trans 'Warning:' %}</strong> {% trans "You currently do not have any e-mail address set up. You should really add an e-mail address so you can receive notifications, reset your password, etc." %}
</p>
{% endif %}

{% if can_add_email %}
<h2 class="card-title mt-6">{% trans "Add New E-mail Address" %}</h2>
<form method="post"
action="{% url 'account_email' %}"
class="{{ form|get_form_classes }}">
{% csrf_token %}
{% include "fiestaforms/parts/errors.html" with errors=form.non_field_errors %}
{% include "fiestaforms/parts/field.html" with bf=form.email errors=form.errors.email %}
<div class="Forms__field Forms__field--buttons">
<button name="action_add" type="submit" class="btn btn-primary">{% trans "Add E-mail" %}</button>
</div>
</form>
{% endif %}
<script type="text/javascript">
(function () {
var message = "{% trans 'Do you really want to remove the selected e-mail address?' %}";
var actions = document.getElementsByName('action_remove');
Expand All @@ -85,5 +88,5 @@ <h2 class="card-title">{% trans "Add New E-mail Address" %}</h2>
});
}
})();
</script>
{% endblock %}
</script>
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ <h1 class="card-title">My Memberships</h1>
</svg>
{% elif membership.state == membership.State.ACTIVE %}
{% section_membership_activation_url membership as activation_url %}
{# TODO: the ?next may be set here, and we're resetting to home url of section -- maybe the option is to respect ?next to keep the same page before section switch #}
<a href="{{ activation_url }}" type="button" class="btn btn-success">jump in</a>
{% endif %}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div class="max-w-xl flex flex-col mx-auto">
<span class="self-center py-4">
{# TODO: show CTA to entry the section space #}
<span class="alert alert-info">We are sorry, but you are not a member of any section.</span>
<span class="alert alert-warning">We are sorry: you have no memberships yet.</span>
</span>
<div class="flex flex-row justify-end items-center space-x-4">
<a class="btn btn-primary" href="{% url "accounts:membership-new" %}">Request new membership</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h1 class="card-title">
Request for New Section Membership
{% endif %}
</h1>
<h2 class="text-center text-lg title-font pb-4">Who you are?</h2>
<h2 class="text-center text-lg pb-4">Who you are?</h2>
<form method="POST"
id="membership-form"
x-data="{roleInput: document.querySelector('#membership-form input[name=role]'), role: ''}"
Expand All @@ -26,7 +26,7 @@ <h2 class="text-center text-lg title-font pb-4">Who you are?</h2>
</svg>
</div>
<div class="flex-grow">
<h2 class="test-base-content text-lg title-font font-medium mb-3">International Student</h2>
<h2 class="test-base-content text-lg font-medium mb-3">International Student</h2>
<p class="leading-relaxed text-base">Are you ready for your Erasmus experience? Buddy, ESNcard, events?</p>
</div>
</div>
Expand Down
Loading

0 comments on commit 4174a77

Please sign in to comment.