Changeset - 001f20e494f4
[Not reviewed]
default
0 1 1
Dennis Fink - 3 years ago 2022-05-10 23:36:08
dennis.fink@c3l.lu
Move age calculation to own module
2 files changed with 18 insertions and 13 deletions:
0 comments (0 inline, 0 general)
c3l_membership/utils.py
Show inline comments
 
new file 100644
 
from datetime import date
 

	
 

	
 
def calculate_age(birthday):
 
    today = date.today()
 
    born = birthday
 
    try:
 
        birthday = born.replace(year=today.year)
 
    except ValueError:
 
        birthday = born.replace(year=today.year, month=born.month + 1, day=1)
 

	
 
    age = today.year - born.year
 
    if birthday > today:
 
        age -= 1
 
    return age
c3l_membership/views.py
Show inline comments
 
@@ -8,6 +8,7 @@ from flask_babel import gettext
 
from flask_weasyprint import HTML, render_pdf
 

	
 
from .forms import MembershipForm
 
from .utils import calculate_age
 

	
 
root_page = Blueprint("root", __name__, url_prefix="/<lang_code>")
 

	
 
@@ -53,20 +54,8 @@ def index():
 

	
 
    if form.validate_on_submit():
 

	
 
        today = date.today()
 
        if form.birthday.data:
 
            born = form.birthday.data
 
            try:
 
                birthday = born.replace(year=today.year)
 
            except ValueError:
 
                birthday = born.replace(year=today.year, month=born.month + 1, day=1)
 

	
 
            age = today.year - born.year
 

	
 
            if birthday > today:
 
                age -= 1
 

	
 
            if age < 18:
 
            if calculate_age(form.birthday.data) < 18:
 
                form.minor_member.data = True
 
                form.student.data = True
 
            else:
 
@@ -109,6 +98,7 @@ def index():
 
        elif form.payment.data in ("digicash", "satispay"):
 
            price = price * 100
 

	
 
        today = date.today()
 
        xml_data = {
 
            "status": status,
 
            "name": form.fullname.data,
0 comments (0 inline, 0 general)