Skip to content
Snippets Groups Projects
Commit db04d75d authored by Carlos Vega's avatar Carlos Vega
Browse files

moved function is_valid_social_security_number to utils so it can be used in...

moved function is_valid_social_security_number to utils so it can be used in other python files such as import_file.py
parent 0020e804
No related branches found
No related tags found
1 merge request!178Feature/import data pdp
......@@ -3,10 +3,10 @@ import logging
from django import forms
from django.forms import ModelForm
from web.algorithm import VerhoeffAlgorithm, LuhnAlgorithm
from web.forms.forms import DATEPICKER_DATE_ATTRS
from web.models import Subject
from web.models.constants import COUNTRY_OTHER_ID
from web.utils import is_valid_social_security_number
logger = logging.getLogger(__name__)
......@@ -18,22 +18,8 @@ def validate_subject_country(self, cleaned_data):
def validate_social_security_number(self, number):
if not is_valid_social_security_number(number):
self.add_error('social_security_number', "Social security number is invalid")
def is_valid_social_security_number(number):
if number is not None and number != '':
if len(number) != 13:
return False
if not number.isdigit():
return False
if not LuhnAlgorithm.is_luhn_valid(number[:12]):
return False
if not VerhoeffAlgorithm.is_valid_verhoeff(number[:11] + number[12]):
return False
return True
self.add_error('social_security_number',
"Social security number is invalid")
FIELD_ORDER = ["first_name", "last_name", "sex", "date_born", "social_security_number",
"default_written_communication_language", "languages", "phone_number", "phone_number_2",
......@@ -42,7 +28,8 @@ FIELD_ORDER = ["first_name", "last_name", "sex", "date_born", "social_security_n
class SubjectAddForm(ModelForm):
date_born = forms.DateField(label="Date of birth",
widget=forms.DateInput(DATEPICKER_DATE_ATTRS, "%Y-%m-%d"),
widget=forms.DateInput(
DATEPICKER_DATE_ATTRS, "%Y-%m-%d"),
required=False
)
......@@ -56,13 +43,15 @@ class SubjectAddForm(ModelForm):
def clean(self):
cleaned_data = super(SubjectAddForm, self).clean()
validate_subject_country(self, cleaned_data)
validate_social_security_number(self, cleaned_data["social_security_number"])
validate_social_security_number(
self, cleaned_data["social_security_number"])
return cleaned_data
class SubjectEditForm(ModelForm):
date_born = forms.DateField(label="Date of birth",
widget=forms.DateInput(DATEPICKER_DATE_ATTRS, "%Y-%m-%d"),
widget=forms.DateInput(
DATEPICKER_DATE_ATTRS, "%Y-%m-%d"),
required=False
)
......@@ -78,7 +67,8 @@ class SubjectEditForm(ModelForm):
def clean(self):
validate_subject_country(self, self.cleaned_data)
validate_social_security_number(self, self.cleaned_data["social_security_number"])
validate_social_security_number(
self, self.cleaned_data["social_security_number"])
class Meta:
model = Subject
......
......@@ -3,15 +3,34 @@ from django.utils import timezone
import datetime
from datetime import timedelta
from web.algorithm import VerhoeffAlgorithm, LuhnAlgorithm
def is_valid_social_security_number(number):
if number is not None and number != '':
if len(number) != 13:
return False
if not number.isdigit():
return False
if not LuhnAlgorithm.is_luhn_valid(number[:12]):
return False
if not VerhoeffAlgorithm.is_valid_verhoeff(number[:11] + number[12]):
return False
return True
def get_today_midnight_date():
today = timezone.now()
today_midnight = datetime.datetime(today.year, today.month, today.day, tzinfo=today.tzinfo)
return today_midnight
today = timezone.now()
today_midnight = datetime.datetime(
today.year, today.month, today.day, tzinfo=today.tzinfo)
return today_midnight
def get_weekdays_in_period(fromdate, todate):
'''
fromdate and todate must be generated using datetime.date or datetime.datetime like:
from datetime import date
fromdate = date(2010,1,1)
todate = date(2010,3,31)
......@@ -21,11 +40,12 @@ def get_weekdays_in_period(fromdate, todate):
but both dates must have the same format !
todate is not included in the range
Weekdays are returned as isoweekdays like the form described in week_choices from constants.py (starting at 1)
'''
if todate < fromdate:
return set([])
day_generator = (fromdate + timedelta(day) for day in xrange((todate - fromdate).days))
day_generator = (fromdate + timedelta(day)
for day in xrange((todate - fromdate).days))
weekdays = set([date.isoweekday() for date in day_generator])
return weekdays
\ No newline at end of file
return weekdays
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment