Projet

Général

Profil

Télécharger (3,82 ko) Statistiques
| Branche: | Tag: | Révision:

calebasse / calebasse / utils.py @ 6f6221a2

1
import os
2

    
3
from django.contrib.auth.models import Group
4
from django.conf import settings
5

    
6
from datetime import timedelta, datetime
7

    
8
from .middleware.request import get_request
9

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

    
12
def __date_to_datetime(date):
13
    return datetime(date.year, date.month, date.day)
14

    
15
def weeks_since_epoch(date):
16
    days_since_epoch = (__date_to_datetime(date) - __EPOCH).days
17
    return days_since_epoch // 7
18

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

    
37
def is_super_user(user):
38
    if not user or not user.is_authenticated():
39
        return False
40
    if user.is_superuser:
41
        return True
42
    super_group = None
43
    try:
44
        super_group = Group.objects.get(name='Super utilisateurs')
45
    except:
46
        return False
47
    if super_group in user.groups.all():
48
        return True
49
    return False
50

    
51
def is_validator(user):
52
    if is_super_user(user):
53
        return True
54
    if not user or not user.is_authenticated():
55
        return False
56
    validator_group = None
57
    try:
58
        validator_group = Group.objects.get(name='Administratifs')
59
    except:
60
        return False
61
    if validator_group in user.groups.all():
62
        return True
63
    return False
64

    
65
def get_nir_control_key(nir):
66
    try:
67
        # Corse dpt 2A et 2B
68
        minus = 0
69
        if nir[6] in ('A', 'a'):
70
            nir = [c for c in nir]
71
            nir[6] = '0'
72
            nir = ''.join(nir)
73
            minus = 1000000
74
        elif nir[6] in ('B', 'b'):
75
            nir = [c for c in nir]
76
            nir[6] = '0'
77
            nir = ''.join(nir)
78
            minus = 2000000
79
        nir = int(nir) - minus
80
        return (97 - (nir % 97))
81
    except:
82
        return None
83

    
84
def get_service_setting(setting_name, default_value=None):
85
    from .cbv import HOME_SERVICE_COOKIE
86
    request = get_request()
87
    if not request:
88
        return None
89
    service = request.COOKIES.get(HOME_SERVICE_COOKIE)
90
    if not service:
91
        return None
92
    if not hasattr(settings, 'SERVICE_SETTINGS'):
93
        return None
94
    return settings.SERVICE_SETTINGS.get(service, {}).get(setting_name) or default_value
95

    
96
def get_last_file(path, prefix=None, suffix=None):
97
    '''
98
    A filename is of format year-mont-day-hour-min-sec.xml or
99
    prefix_year-mont-day-hour-min-secsuffix
100

    
101
    If suffix is None, only file *_year-mont-day-hour-min-sec are treated.
102
    If prefix is None, all files *_year-mont-day-hour-min-secsuffix are
103
    treated.
104
    '''
105
    if not path or not os.path.isdir(path):
106
        return None
107
    cv_files = [(f, f) for f in os.listdir(path) \
108
            if os.path.isfile(os.path.join(path, f)) ]
109
    if prefix:
110
        cv_files = [(f1, f2[len(prefix) + 1:])
111
                for f1, f2 in cv_files if f2.startswith(prefix)]
112
    else:
113
        cv_files = [(f1, f2.rsplit('_', 1)[-1])
114
                for f1, f2 in cv_files]
115
    if suffix:
116
        cv_files = [(f1, f2[:-len(suffix)])
117
                for f1, f2 in cv_files if f2.endswith(suffix)]
118
    fmt = '%Y-%d-%m-%H-%M-%S'
119
    cv_files_and_dates = list()
120
    for f1, f2 in cv_files:
121
        try:
122
            d = datetime.strptime(f2, fmt)
123
        except:
124
            pass
125
        else:
126
            cv_files_and_dates.append((f1, d))
127
    if len(cv_files_and_dates):
128
        cv_files_and_dates = sorted(cv_files_and_dates,
129
                key=lambda x:x[1], reverse=True)
130
        return os.path.join(path, cv_files_and_dates[0][0])
131
    else:
132
        return None
(15-15/17)