-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make minimum age check more flexible
- Loading branch information
Showing
7 changed files
with
111 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
from django.contrib import admin | ||
from django.contrib.admin import register | ||
|
||
# Register your models here. | ||
from age.models import AgeRegistration | ||
|
||
|
||
@register(AgeRegistration) | ||
class AgeRegistrationAdmin(admin.ModelAdmin): | ||
"""Admin interface for the age registration model.""" | ||
|
||
list_display = ("user", "name", "minimum_age", "created_at") | ||
search_fields = ("user__username",) | ||
ordering = ("created_at",) | ||
|
||
def name(self, instance): | ||
"""Name of the user.""" | ||
return instance.user.__str__() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
import uuid | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.db import models | ||
|
||
|
||
User = get_user_model() | ||
|
||
|
||
class Is18YearsOld(models.Model): | ||
"""Class to check whether someone has a legal drinking age.""" | ||
class AgeRegistration(models.Model): | ||
"""Class to save an age registration.""" | ||
|
||
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="is_18_years_old") | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
updated_at = models.DateTimeField(auto_now=True) | ||
minimum_age = models.PositiveIntegerField() | ||
|
||
def __str__(self): | ||
"""Convert this object to string.""" | ||
return f"{self.user} ({self.created_at})" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import json | ||
|
||
from django.conf import settings | ||
from django.contrib.auth import get_user_model | ||
|
||
from age import models | ||
|
||
User = get_user_model() | ||
|
||
|
||
def verify_minimum_age(user: User, minimum_age: int = 18) -> bool: | ||
"""Verify whether someone has a certain minimum age.""" | ||
try: | ||
age_registration = models.AgeRegistration.objects.get(user=user) | ||
except models.AgeRegistration.DoesNotExist: | ||
return False | ||
|
||
return age_registration.minimum_age >= minimum_age | ||
|
||
|
||
def construct_disclose_tree(): | ||
"""Construct the disclose tree.""" | ||
yivi_disclose_tree = [] | ||
yivi_age_disclose_tree = [] | ||
for yivi_key in settings.AGE_VERIFICATION_MINIMUM_AGE_MAPPING.keys(): | ||
yivi_age_disclose_tree.append([yivi_key]) | ||
|
||
yivi_disclose_tree.append(yivi_age_disclose_tree) | ||
if settings.AGE_VERIFICATION_USERNAME_ATTRIBUTE is not None: | ||
yivi_disclose_tree.append([[settings.AGE_VERIFICATION_USERNAME_ATTRIBUTE]]) | ||
return yivi_disclose_tree | ||
|
||
|
||
def get_proven_attributes_from_proof_tree(proof_tree): | ||
"""Get the proven attributes from a proof tree.""" | ||
proven_attributes = [] | ||
for attribute_conjuction_clause in proof_tree: | ||
for possibly_proven_attribute in attribute_conjuction_clause: | ||
if possibly_proven_attribute["status"] == "PRESENT": | ||
attribute_id = possibly_proven_attribute["id"] | ||
proven_attributes.append(attribute_id) | ||
return proven_attributes | ||
|
||
|
||
def get_highest_proven_age_from_proven_attributes(proven_attributes: [str]): | ||
"""Get the highest minimum age from the proven attributes.""" | ||
highest_age = None | ||
for proven_attribute in proven_attributes: | ||
if proven_attribute in settings.AGE_VERIFICATION_MINIMUM_AGE_MAPPING.keys(): | ||
proven_attribute_minimum_age = settings.AGE_VERIFICATION_MINIMUM_AGE_MAPPING[proven_attribute] | ||
if highest_age is None or highest_age < proven_attribute_minimum_age: | ||
highest_age = proven_attribute_minimum_age | ||
return highest_age |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters