Skip to content

Commit

Permalink
Merge pull request #785 from hackerspace-ntnu/783-force-stud-address
Browse files Browse the repository at this point in the history
783 force stud address
  • Loading branch information
michaelbrusegard authored Mar 13, 2024
2 parents 435b2ed + f8e9fcb commit 8e44fa8
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 14 deletions.
3 changes: 2 additions & 1 deletion applications/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.template.loader import render_to_string
from django.urls import reverse

from authentication.views import convert_to_stud_email
from committees.models import Committee

from .models import Application, ApplicationGroupChoice
Expand Down Expand Up @@ -94,6 +95,6 @@ def send_email(self):
"[Hackerspace NTNU] Ny søknad!",
new_application_message,
"Hackerspace NTNU",
emails,
convert_to_stud_email(*emails),
fail_silently=False,
)
47 changes: 46 additions & 1 deletion authentication/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from social_django.utils import load_backend, load_strategy

from authentication.apps import AuthenticationConfig
from authentication.views import associate_by_email, save_profile
from authentication.views import associate_by_email, convert_to_stud_email, save_profile


class AuthenticationConfigTest(TestCase):
Expand Down Expand Up @@ -126,3 +126,48 @@ def test_existing_user_profiles(self):
def test_assoc_existing_user(self):
associate = associate_by_email(self.backend, {}, user=self.testuser)
self.assertIsNone(associate)

def test_convert_stud_emails(self):
emails = [
"[email protected]",
"[email protected]",
]
new_emails = convert_to_stud_email(*emails)

self.assertEqual(emails, new_emails)

def test_convert_ntnu_emails(self):
emails = [
"[email protected]",
"[email protected]",
]

new_emails = convert_to_stud_email(*emails)

self.assertEqual(len(new_emails), 2)
self.assertNotEqual(emails, new_emails)
self.assertEqual(new_emails[0], "[email protected]")
self.assertEqual(new_emails[1], "[email protected]")

def test_convert_ntnu_and_stud_emails(self):
emails = [
"[email protected]",
"[email protected]",
]

new_emails = convert_to_stud_email(*emails)

self.assertEqual(len(new_emails), 2)
self.assertNotEqual(emails, new_emails)
self.assertEqual(new_emails[0], "[email protected]")
self.assertEqual(new_emails[0], emails[0])
self.assertEqual(new_emails[1], "[email protected]")

def test_convert_empty_email(self):
emails = ["", "ntnu", "@ntnu.", "@ntnu.no"]
new_emails = convert_to_stud_email(*emails)
self.assertEqual(new_emails, ["ntnu", "@stud.ntnu.no", "@stud.ntnu.no"])

def test_convert_none_values(self):
new_emails = convert_to_stud_email("[email protected]", None, None)
self.assertEqual(new_emails, ["[email protected]"])
16 changes: 16 additions & 0 deletions authentication/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List, Union

from django.contrib.auth import get_user_model, logout
from django.shortcuts import redirect
from django.urls import reverse
Expand Down Expand Up @@ -94,3 +96,17 @@ def get_user_by_stud_or_ntnu_email(email: str):
user = User.objects.filter(email=query_email).first()
if user:
return user


def convert_to_stud_email(*user_emails: Union[str, None]) -> List[str]:
new_emails = []
for email in user_emails:
if not email:
continue
split_email = email.split("@ntnu")
if len(split_email) > 1:
new_email = split_email[0] + "@stud.ntnu.no"
new_emails.append(new_email)
else:
new_emails.append(email)
return new_emails
4 changes: 2 additions & 2 deletions internalportal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from django.views.generic.edit import BaseDetailView

from applications.models import Application, ApplicationGroup, ApplicationGroupChoice
from authentication.views import get_user_by_stud_or_ntnu_email
from authentication.views import convert_to_stud_email, get_user_by_stud_or_ntnu_email
from committees.models import Committee
from internalportal.forms import InterviewEmailForm
from inventory.models.item_loan import ItemLoan
Expand Down Expand Up @@ -170,7 +170,7 @@ def get(self, request, *args, **kwargs):
_("Søknad sendt videre"),
new_application_message,
"Hackerspace NTNU",
emails,
convert_to_stud_email(*emails),
fail_silently=False,
)

Expand Down
15 changes: 7 additions & 8 deletions news/templates/news/skill_missing_email.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Hei,

Denne mailen blir sendt til deg fordi du mangler

{% for skill in skills %}{{ skill }} {% endfor %}
{% for skill in skills %}
{{ skill }}{% endfor %}

Det har nå blitt åpnet et arrangement som gir denne skillen så meld deg på.
Husk at for å kunne pange må man ha oppnådd Hackerspace grunnkurs.
Expand All @@ -18,19 +18,18 @@ LabOps Hackerspace NTNU

-----------------------------------


Hello,

This email is being sent to you because you are missing the following skill(s):

{% for skill in skills %}{{ skill }} {% endfor %}
This email is being sent to you because you have not acquired the following skill(s):
{% for skill in skills %}
{{ skill }}{% endfor %}

An event has now been opened that offers this skill, so sign up.
Please remember that to participate, one must have completed the Hackerspace basic course.
Please remember that to retire (pang), one must have completed the Hackerspace basic course.

Access the event here: hackerspace-ntnu.no/events/{{ event.id }}

If this is not accurate, you can contact LabOps.

Best regards,
LabOps Hackerspace NTNU
LabOps Hackerspace NTNU
5 changes: 3 additions & 2 deletions news/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
UpdateView,
)

from authentication.views import convert_to_stud_email
from committees.models import Committee

from .forms import (
Expand Down Expand Up @@ -400,7 +401,6 @@ def form_valid(self, form):
upload_form.save()

skills = form.cleaned_data["skills"]
print(skills)
if len(skills) > 0 and not form.cleaned_data["draft"]:
self._send_mail_to_members_without_skill(skills, self.object)

Expand All @@ -414,6 +414,7 @@ def _send_mail_to_members_without_skill(self, skills, event):
get_user_model()
.objects.filter(groups__name__in=list(committee_array))
.exclude(profile__skills__in=skills)
.values_list("email", flat=True)
)

plain_message = render_to_string(
Expand All @@ -425,7 +426,7 @@ def _send_mail_to_members_without_skill(self, skills, event):
"[Hackerspace NTNU] Arrangement gir skill du mangler!",
plain_message,
"Hackerspace NTNU",
list(members_without_skill),
convert_to_stud_email(*members_without_skill),
fail_silently=False,
)
pass
Expand Down

0 comments on commit 8e44fa8

Please sign in to comment.