Project

General

Profile

Download (2.03 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / calebasse / utils.py @ 1f4ae3b3

1
from django.contrib.auth.models import Group
2

    
3
from datetime import timedelta, datetime
4

    
5
__EPOCH = datetime(day=5,month=1,year=1970)
6

    
7
def __date_to_datetime(date):
8
    return datetime(date.year, date.month, date.day)
9

    
10
def weeks_since_epoch(date):
11
    days_since_epoch = (__date_to_datetime(date) - __EPOCH).days
12
    return days_since_epoch // 7
13

    
14
def weekday_ranks(date):
15
    '''Returns n so that if date occurs on a certain weekday, this is the
16
       n-th weekday of the month counting from the first. Also return -n-1 if
17
       this is the n-th weekday of the month counting from the last.
18
    '''
19
    n = 0
20
    month = date.month
21
    i = date - timedelta(days=7)
22
    while i.month == month:
23
        n += 1
24
        i = i - timedelta(days=7)
25
    m = -1
26
    i = date + timedelta(days=7)
27
    while i.month == month:
28
        m -= 1
29
        i = i + timedelta(days=7)
30
    return n, m
31

    
32
def is_super_user(user):
33
    if not user or not user.is_authenticated():
34
        return False
35
    if user.is_superuser:
36
        return True
37
    super_group = None
38
    try:
39
        super_group = Group.objects.get(name='Super utilisateurs')
40
    except:
41
        return False
42
    if super_group in user.groups.all():
43
        return True
44
    return False
45

    
46
def is_validator(user):
47
    if is_super_user(user):
48
        return True
49
    if not user or not user.is_authenticated():
50
        return False
51
    validator_group = None
52
    try:
53
        validator_group = Group.objects.get(name='Administratifs')
54
    except:
55
        return False
56
    if validator_group in user.groups.all():
57
        return True
58
    return False
59

    
60
def get_nir_control_key(nir):
61
    try:
62
        # Corse dpt 2A et 2B
63
        minus = 0
64
        if nir[6] in ('A', 'a'):
65
            nir = [c for c in nir]
66
            nir[6] = '0'
67
            nir = ''.join(nir)
68
            minus = 1000000
69
        elif nir[6] in ('B', 'b'):
70
            nir = [c for c in nir]
71
            nir[6] = '0'
72
            nir = ''.join(nir)
73
            minus = 2000000
74
        nir = int(nir) - minus
75
        return (97 - (nir % 97))
76
    except:
77
        return None
(15-15/17)