Projet

Général

Profil

Télécharger (2,49 ko) Statistiques
| Branche: | Tag: | Révision:

root / auquotidien / modules / strongbox.py @ 8b02623d

1
import os
2
import time
3

    
4
from quixote import get_publisher
5
from qommon.storage import StorableObject
6
from qommon import misc
7

    
8
class StrongboxType(StorableObject):
9
    _names = 'strongbox-types'
10

    
11
    id = None
12
    label = None
13
    validation_months = 0
14

    
15

    
16
class StrongboxFile():
17
    id = None
18
    orig_filename = None
19
    base_filename = None
20
    content_type = None
21
    charset = None
22

    
23
    def __init__(self, id, f):
24
        self.id = id
25
        self.orig_filename = f.orig_filename
26
        self.base_filename = f.base_filename
27
        self.content_type = f.content_type
28
        self.charset = f.charset
29
        self.fp = f.fp
30

    
31
    def __getstate__(self):
32
        odict = self.__dict__.copy()
33
        if not odict.has_key('fp'):
34
            return odict
35

    
36
        # XXX: add some locking
37
        del odict['fp']
38
        dirname = os.path.join(get_publisher().app_dir, 'strongbox-files')
39
        if not os.path.exists(dirname):
40
            os.mkdir(dirname)
41
        odict['filename'] = os.path.join(dirname, str(self.id))
42
        self.fp.seek(0)
43
        fd = file(odict['filename'], 'w')
44
        fd.write(self.fp.read())
45
        fd.close()
46
        return odict
47

    
48

    
49
class StrongboxItem(StorableObject):
50
    _names = 'strongbox-items'
51
    _hashed_indexes = ['user_id']
52

    
53
    user_id = None
54
    description = None
55
    file = None
56
    type_id = None
57
    expiration_time = None
58
    proposed_by = None
59
    proposed_time = None
60
    validated_time = None
61

    
62
    def get_display_name(self):
63
        if self.description:
64
            return self.description
65
        return self.file.base_filename
66

    
67
    def set_file(self, file):
68
        self.file = StrongboxFile(self.id, file)
69

    
70
    def remove_file(self):
71
        os.unlink(self.file.filename)
72

    
73
    def set_expiration_time_from_date(self, value):
74
        if not value:
75
            self.expiration_time = None
76
            return
77
        if not StrongboxType.has_key(self.type_id):
78
            return
79

    
80
        if not StrongboxType.get(self.type_id).validation_months:
81
            self.expiration_time = None
82
            return
83

    
84
        date = time.strptime(value, misc.date_format())
85
        year, month, day = date[:3]
86
        month += StrongboxType.get(self.type_id).validation_months
87
        while month > 12:
88
           year += 1
89
           month -= 12
90
        while True:
91
            try:
92
                self.expiration_time = time.strptime(
93
                       '%04d-%02d-%02d' % (year, month, day), '%Y-%m-%d')
94
            except ValueError:
95
                day -= 1
96
                continue
97
            break
98

    
(25-25/27)