members/models: normalize usernames, use username as association name if available

pull/73/head
Christian Merten 1 year ago
parent 8633c41b8a
commit 3d60f7e9bf
Signed by: christian.merten
GPG Key ID: D953D69721B948B3

@ -1,6 +1,7 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
import uuid import uuid
import pytz import pytz
import unicodedata
import re import re
import csv import csv
from django.db import models from django.db import models
@ -368,11 +369,19 @@ class Member(Person):
"""A synonym for the email field.""" """A synonym for the email field."""
return self.email return self.email
@property
def username(self):
"""Return the username. Either this the name of the linked user, or
it is the suggested username."""
if not self.user:
return self.suggested_username()
else:
return self.user.username
@property @property
def association_email(self): def association_email(self):
"""Returning the association email of the member""" """Returning the association email of the member"""
raw = "{0}.{1}@{2}".format(self.prename.lower(), self.lastname.lower(), settings.DOMAIN) return "{username}@{domain}".format(username=self.username, domain=settings.DOMAIN)
return raw.replace('ö', 'oe').replace('ä', 'ae').replace('ü', 'ue')
def registration_complete(self): def registration_complete(self):
"""Check if all necessary fields are set.""" """Check if all necessary fields are set."""
@ -666,7 +675,7 @@ class Member(Person):
def suggested_username(self): def suggested_username(self):
"""Returns a suggested username given by {prename}.{lastname}.""" """Returns a suggested username given by {prename}.{lastname}."""
raw = "{0}.{1}".format(self.prename.lower(), self.lastname.lower()) raw = "{0}.{1}".format(self.prename.lower(), self.lastname.lower())
return raw.replace('ö', 'oe').replace('ä', 'ae').replace('ü', 'ue') return normalize_name(raw)
def has_internal_email(self): def has_internal_email(self):
"""Returns if the configured e-mail address is a DAV360 email address.""" """Returns if the configured e-mail address is a DAV360 email address."""
@ -1749,3 +1758,8 @@ def import_from_csv_waitinglist(path):
for row in rows: for row in rows:
transform_row(row) transform_row(row)
def normalize_name(raw):
noumlaut = raw.replace('ö', 'oe').replace('ä', 'ae').replace('ü', 'ue').replace(' ', '_')
return unicodedata.normalize('NFKD', noumlaut).encode('ascii', 'ignore').decode('ascii')

Loading…
Cancel
Save