Project

General

Profile

Download (948 Bytes) Statistics
| Branch: | Tag: | Revision:

calebasse / scripts / utils.py @ 38b4bc27

1

    
2
class QuerysetIndex(object):
3
    '''Create simple index of objects in a queryset'''
4
    def __init__(self, qs, *keywords):
5
        for keyword in keywords:
6
            setattr(self, 'by_' + keyword, dict())
7
        for obj in qs:
8
            for keyword in keywords:
9
                getattr(self, 'by_' + keyword)[getattr(obj, keyword)] = obj
10

    
11
def _to_datetime(str_date):
12
    if not str_date:
13
        return None
14
    return datetime.strptime(str_date[:19], "%Y-%m-%d %H:%M:%S")
15

    
16
def _to_date(str_date):
17
    dt = _to_datetime(str_date)
18
    return dt and dt.date()
19

    
20
def _to_time(str_date):
21
    dt = _to_datetime(str_date)
22
    return dt and dt.time()
23

    
24
def _to_duration(str_date):
25
    dt = _to_datetime(str_date)
26
    if dt is None:
27
        return timedelta(minutes=15)
28
    return dt - datetime(1900, 01, 01, 0, 0)
29

    
30
def _get_dict(cols, line):
31
    res = {}
32
    for i, data in enumerate(line):
33
        res[cols[i]] = data.decode('utf-8')
34
    return res
35

    
(42-42/43)