Project

General

Profile

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

root / extra / modules / strongbox.py @ 2750dbae

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.get(self.type_id).validation_months:
78
            self.expiration_time = None
79
            return
80

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

    
(24-24/26)