1
|
from django.contrib.auth.models import Group
|
2
|
from django.conf import settings
|
3
|
|
4
|
from datetime import timedelta, datetime
|
5
|
|
6
|
from .middleware.request import get_request
|
7
|
|
8
|
__EPOCH = datetime(day=5,month=1,year=1970)
|
9
|
|
10
|
def __date_to_datetime(date):
|
11
|
return datetime(date.year, date.month, date.day)
|
12
|
|
13
|
def weeks_since_epoch(date):
|
14
|
days_since_epoch = (__date_to_datetime(date) - __EPOCH).days
|
15
|
return days_since_epoch // 7
|
16
|
|
17
|
def weekday_ranks(date):
|
18
|
'''Returns n so that if date occurs on a certain weekday, this is the
|
19
|
n-th weekday of the month counting from the first. Also return -n-1 if
|
20
|
this is the n-th weekday of the month counting from the last.
|
21
|
'''
|
22
|
n = 0
|
23
|
month = date.month
|
24
|
i = date - timedelta(days=7)
|
25
|
while i.month == month:
|
26
|
n += 1
|
27
|
i = i - timedelta(days=7)
|
28
|
m = -1
|
29
|
i = date + timedelta(days=7)
|
30
|
while i.month == month:
|
31
|
m -= 1
|
32
|
i = i + timedelta(days=7)
|
33
|
return n, m
|
34
|
|
35
|
def is_super_user(user):
|
36
|
if not user or not user.is_authenticated():
|
37
|
return False
|
38
|
if user.is_superuser:
|
39
|
return True
|
40
|
super_group = None
|
41
|
try:
|
42
|
super_group = Group.objects.get(name='Super utilisateurs')
|
43
|
except:
|
44
|
return False
|
45
|
if super_group in user.groups.all():
|
46
|
return True
|
47
|
return False
|
48
|
|
49
|
def is_validator(user):
|
50
|
if is_super_user(user):
|
51
|
return True
|
52
|
if not user or not user.is_authenticated():
|
53
|
return False
|
54
|
validator_group = None
|
55
|
try:
|
56
|
validator_group = Group.objects.get(name='Administratifs')
|
57
|
except:
|
58
|
return False
|
59
|
if validator_group in user.groups.all():
|
60
|
return True
|
61
|
return False
|
62
|
|
63
|
def get_nir_control_key(nir):
|
64
|
try:
|
65
|
# Corse dpt 2A et 2B
|
66
|
minus = 0
|
67
|
if nir[6] in ('A', 'a'):
|
68
|
nir = [c for c in nir]
|
69
|
nir[6] = '0'
|
70
|
nir = ''.join(nir)
|
71
|
minus = 1000000
|
72
|
elif nir[6] in ('B', 'b'):
|
73
|
nir = [c for c in nir]
|
74
|
nir[6] = '0'
|
75
|
nir = ''.join(nir)
|
76
|
minus = 2000000
|
77
|
nir = int(nir) - minus
|
78
|
return (97 - (nir % 97))
|
79
|
except:
|
80
|
return None
|
81
|
|
82
|
def get_service_setting(setting_name, default_value=None):
|
83
|
from .cbv import HOME_SERVICE_COOKIE
|
84
|
request = get_request()
|
85
|
if not request:
|
86
|
return None
|
87
|
service = request.COOKIES.get(HOME_SERVICE_COOKIE)
|
88
|
if not service:
|
89
|
return None
|
90
|
if not hasattr(settings, 'SERVICE_SETTINGS'):
|
91
|
return None
|
92
|
return settings.SERVICE_SETTINGS.get(service, {}).get(setting_name) or default_value
|