Skip to content

Bugfix/name validators #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
install: bin/python

bin/python:
virtualenv -p /usr/bin/python3 .

.PHONY: install migrate checkmigrations serve deploy clean

install: bin/python requirements.txt
bin/pip install -r requirements.txt

migrate: bin/python
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ django-bootstrap4~=2.3.1
djangorestframework~=3.12.1
djangorestframework-csv~=2.1.0
pep8~=1.7.1
requests~=2.24.0
regex~=2024.9.11
requests~=2.31.0
psycopg2~=2.8.6
git+https://github.com/Work4Labs/django-test-pep8
71 changes: 71 additions & 0 deletions teknologr/members/tests_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from django.test import TestCase
from django.core.exceptions import ValidationError
from .validators import CommonValidators

class Validator(CommonValidators):
cleaned_data = {}

class ValidatorTest(TestCase):
def setUp(self):
self.validator = Validator()

def setData(self, prop, value):
self.validator.cleaned_data[prop] = value

def test_clean_given_names_ok(self):
for name in [
"Svakar",
"Svakar Svante",
"Svakar-Svante",
" Svakar ",
"Pär",
"Áìñö",
]:
self.setData("given_names", name)
self.validator.clean_given_names()

def test_clean_given_names_error(self):
for c in "3_@<>=?!#%/\\*'\"^":
self.setData("given_names", f"Svakar{c}")
with self.assertRaises(ValidationError):
self.validator.clean_given_names()

def test_clean_surname_ok(self):
for name in [
"Teknolog",
" af Teknolog ",
"von Teknolog-Teekkari",
"Ingenjör",
"Áìñö",
]:
self.setData("surname", name)
self.validator.clean_surname()

def test_clean_surname_error(self):
for c in "3_@<>=?!#%/\\*'\"^":
self.setData("surname", f"Teknolog{c}")
with self.assertRaises(ValidationError):
self.validator.clean_surname()

def test_clean_phone_ok(self):
for number in [
"040123456",
"040-123 234",
"123 - 456 - 789",
" 040123456 ",
"+358 40 - 123",
]:
self.setData("phone", number)
self.validator.clean_phone()

def test_clean_phone_error(self):
for number in [
"040123456g",
"040123456,331",
"+ 358123",
"-040123456",
"040123456-",
]:
self.setData("phone", number)
with self.assertRaises(ValidationError):
self.validator.clean_phone()
14 changes: 7 additions & 7 deletions teknologr/members/validators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from datetime import datetime
import re
import regex as re

def in_range(value, a, b):
if value is None:
Expand All @@ -20,21 +20,21 @@ class CommonValidators:
# XXX: Validate student id?

def clean_given_names(self):
given_names = self.cleaned_data.get('given_names')
regex = re.compile('^[a-zA-Z -]*$')
given_names = self.cleaned_data.get('given_names').strip()
regex = re.compile(r'^[\p{L} -]*$')
if not regex.match(given_names):
raise ValidationError('Ogiltigt förnamn')
return given_names

def clean_surname(self):
surname = self.cleaned_data.get('surname')
regex = re.compile('^[a-zA-Z -]*$')
surname = self.cleaned_data.get('surname').strip()
regex = re.compile(r'^[\p{L} -]*$')
if not regex.match(surname):
raise ValidationError('Ogiltigt efternamn')
return surname

def clean_email(self):
email = self.cleaned_data.get('email')
email = self.cleaned_data.get('email').strip()
if not email:
return ''
try:
Expand All @@ -44,7 +44,7 @@ def clean_email(self):
return email

def clean_phone(self):
phone = self.cleaned_data.get('phone')
phone = self.cleaned_data.get('phone').strip()
if not phone:
return ''
regex = re.compile('^\+?[0-9][0-9- ]*[0-9]$')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.20 on 2024-10-24 22:02

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('registration', '0009_applicant_username'),
]

operations = [
migrations.AlterField(
model_name='applicant',
name='enrolment_year',
field=models.IntegerField(choices=[(1872, 1872), (1873, 1873), (1874, 1874), (1875, 1875), (1876, 1876), (1877, 1877), (1878, 1878), (1879, 1879), (1880, 1880), (1881, 1881), (1882, 1882), (1883, 1883), (1884, 1884), (1885, 1885), (1886, 1886), (1887, 1887), (1888, 1888), (1889, 1889), (1890, 1890), (1891, 1891), (1892, 1892), (1893, 1893), (1894, 1894), (1895, 1895), (1896, 1896), (1897, 1897), (1898, 1898), (1899, 1899), (1900, 1900), (1901, 1901), (1902, 1902), (1903, 1903), (1904, 1904), (1905, 1905), (1906, 1906), (1907, 1907), (1908, 1908), (1909, 1909), (1910, 1910), (1911, 1911), (1912, 1912), (1913, 1913), (1914, 1914), (1915, 1915), (1916, 1916), (1917, 1917), (1918, 1918), (1919, 1919), (1920, 1920), (1921, 1921), (1922, 1922), (1923, 1923), (1924, 1924), (1925, 1925), (1926, 1926), (1927, 1927), (1928, 1928), (1929, 1929), (1930, 1930), (1931, 1931), (1932, 1932), (1933, 1933), (1934, 1934), (1935, 1935), (1936, 1936), (1937, 1937), (1938, 1938), (1939, 1939), (1940, 1940), (1941, 1941), (1942, 1942), (1943, 1943), (1944, 1944), (1945, 1945), (1946, 1946), (1947, 1947), (1948, 1948), (1949, 1949), (1950, 1950), (1951, 1951), (1952, 1952), (1953, 1953), (1954, 1954), (1955, 1955), (1956, 1956), (1957, 1957), (1958, 1958), (1959, 1959), (1960, 1960), (1961, 1961), (1962, 1962), (1963, 1963), (1964, 1964), (1965, 1965), (1966, 1966), (1967, 1967), (1968, 1968), (1969, 1969), (1970, 1970), (1971, 1971), (1972, 1972), (1973, 1973), (1974, 1974), (1975, 1975), (1976, 1976), (1977, 1977), (1978, 1978), (1979, 1979), (1980, 1980), (1981, 1981), (1982, 1982), (1983, 1983), (1984, 1984), (1985, 1985), (1986, 1986), (1987, 1987), (1988, 1988), (1989, 1989), (1990, 1990), (1991, 1991), (1992, 1992), (1993, 1993), (1994, 1994), (1995, 1995), (1996, 1996), (1997, 1997), (1998, 1998), (1999, 1999), (2000, 2000), (2001, 2001), (2002, 2002), (2003, 2003), (2004, 2004), (2005, 2005), (2006, 2006), (2007, 2007), (2008, 2008), (2009, 2009), (2010, 2010), (2011, 2011), (2012, 2012), (2013, 2013), (2014, 2014), (2015, 2015), (2016, 2016), (2017, 2017), (2018, 2018), (2019, 2019), (2020, 2020), (2021, 2021), (2022, 2022), (2023, 2023), (2024, 2024)], default=2024),
),
]
Loading