Project

General

Profile

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

calebasse / calebasse / utils.py @ 0cbf950f

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 0. Also return 4 if this the
17
    last suck weekday of the month. '''
18
    n = 0
19
    month = date.month
20
    i = date - timedelta(days=7)
21
    while i.month == month:
22
        n += 1
23
        i = i - timedelta(days=7)
24
    if (date+timedelta(days=7)).month != month and n != 4:
25
        return n, 4
26
    else:
27
        return n,
28

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

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

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