# Path: sendinel/backend/models.py
# class AuthenticationCall(models.Model):
#     """
#     Queues all calls that were made to authenticate a user via his 
#     mobile phone number. These calls get deleted once the user has
#     been authenticated successfully.
#     """
#     
#     number = models.CharField(max_length = 20)
#     time = models.DateTimeField(auto_now_add = True)
# 
# Path: sendinel/settings.py
# AUTHENTICATION_ENABLED = False
# 
# AUTHENTICATION_CALL_TIMEOUT = timedelta(minutes = 3)
# 
# COUNTRY_CODE_PHONE = "0049" #"0027" for South Africa
# 
# START_MOBILE_PHONE = "0" # "0" for South Africa (07/08..), "01" for Germany


import re
from datetime import datetime
from django.core.urlresolvers import reverse
from django.core.exceptions import ValidationError
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext as _
from sendinel.backend.models import AuthenticationCall
from sendinel.settings import AUTHENTICATION_ENABLED, \
                              AUTHENTICATION_CALL_TIMEOUT, \
                              COUNTRY_CODE_PHONE, \
                              START_MOBILE_PHONE
    if number.startswith(START_MOBILE_PHONE):
        return number
    else:
        raise ValidationError(_('Please enter a cell phone number.'))

def check_and_delete_authentication_call(number):
    """
    Try to find an AuthenticationCall for the given phone numner.
    The last seven digits are compared to identify the call.
    """
    
    number = format_and_validate_phonenumber(number)
    
    last_seven_digits = number[-7:]
    calls = AuthenticationCall.objects.filter( \
                                    number__endswith = last_seven_digits)
    
    if calls.count() == 1:
        calls.delete()
        return True
    
    return False
    
def delete_timed_out_authentication_calls():
    """
    Find and delete all timed out authentication calls.
    This can be configured through settings.
    """
    
    timeout = datetime.now() - AUTHENTICATION_CALL_TIMEOUT

# Path: sendinel/backend/models.py
# class AuthenticationCall(models.Model):
#     """
#     Queues all calls that were made to authenticate a user via his 
#     mobile phone number. These calls get deleted once the user has
#     been authenticated successfully.
#     """
#     
#     number = models.CharField(max_length = 20)
#     time = models.DateTimeField(auto_now_add = True)
# 
# Path: sendinel/settings.py
# AUTHENTICATION_ENABLED = False
# 
# AUTHENTICATION_CALL_TIMEOUT = timedelta(minutes = 3)
# 
# COUNTRY_CODE_PHONE = "0049" #"0027" for South Africa
# 
# START_MOBILE_PHONE = "0" # "0" for South Africa (07/08..), "01" for Germany


# Path: sendinel/backend/authhelper.py
import re
from datetime import datetime
from django.core.urlresolvers import reverse
from django.core.exceptions import ValidationError
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext as _
from sendinel.backend.models import AuthenticationCall
from sendinel.settings import AUTHENTICATION_ENABLED, \
                              AUTHENTICATION_CALL_TIMEOUT, \
                              COUNTRY_CODE_PHONE, \
                              START_MOBILE_PHONE

    if number.startswith(START_MOBILE_PHONE):
        return number
    else:
        raise ValidationError(_('Please enter a cell phone number.'))

def check_and_delete_authentication_call(number):
    """
    Try to find an AuthenticationCall for the given phone numner.
    The last seven digits are compared to identify the call.
    """
    
    number = format_and_validate_phonenumber(number)
    
    last_seven_digits = number[-7:]
    calls = AuthenticationCall.objects.filter( \
                                    number__endswith = last_seven_digits)
    
    if calls.count() == 1:
        calls.delete()
        return True
    
    return False
    
def delete_timed_out_authentication_calls():
    """
    Find and delete all timed out authentication calls.
    This can be configured through settings.
    """
    
    timeout = datetime.now() - AUTHENTICATION_CALL_TIMEOUT

    AuthenticationCall.objects.filter(time__lt = timeout).delete()