1 | from uuid import uuid1 |
---|
2 | from datetime import datetime, timedelta, timezone |
---|
3 | |
---|
4 | from repoze.folder import Folder |
---|
5 | from pyramid.security import Allow, Deny, Everyone, ALL_PERMISSIONS |
---|
6 | |
---|
7 | import pytz |
---|
8 | |
---|
9 | |
---|
10 | class BulkFile(Folder): |
---|
11 | |
---|
12 | """ |
---|
13 | Object that maps to a compressed file uploaded by a user to upload several |
---|
14 | workout tracking files at once. |
---|
15 | """ |
---|
16 | |
---|
17 | __parent__ = __name__ = None |
---|
18 | |
---|
19 | def __acl__(self): |
---|
20 | """ |
---|
21 | Owner of the compressed file has full permissions to the file, as well |
---|
22 | as system admins. Everybody else has no permissions. |
---|
23 | """ |
---|
24 | permissions = [ |
---|
25 | (Allow, str(self.uid), 'view'), |
---|
26 | (Allow, str(self.uid), 'edit'), |
---|
27 | (Allow, str(self.uid), 'delete'), |
---|
28 | (Deny, Everyone, ALL_PERMISSIONS) |
---|
29 | ] |
---|
30 | return permissions |
---|
31 | |
---|
32 | def __init__(self, **kw): |
---|
33 | super(BulkFile, self).__init__() |
---|
34 | self.bfid = uuid1() |
---|
35 | self.uid = kw['uid'] # required, so let it blow if none is given |
---|
36 | self.uploaded = kw.get('uploaded', datetime.now(timezone.utc)) |
---|
37 | self.compressed_file = kw.get('compressed_file', None) # Blob |
---|
38 | self.file_name = '' # unicode string |
---|
39 | self.file_type = '' # unicode string |
---|
40 | self.loaded = False # datetime when workouts have been loaded |
---|
41 | self.workout_ids = [] # ids of the workouts loaded from this file |
---|
42 | |
---|
43 | def _in_timezone(self, timezone, value): |
---|
44 | """ |
---|
45 | Return a string representation of the given value date and time, |
---|
46 | localized into the given timezone |
---|
47 | """ |
---|
48 | _value = value.astimezone(pytz.timezone(timezone)) |
---|
49 | return _value.strftime('%d/%m/%Y %H:%M (%Z)') |
---|
50 | |
---|
51 | def uploaded_in_timezone(self, timezone): |
---|
52 | return self._in_timezone(timezone, self.uploaded) |
---|
53 | |
---|
54 | def loaded_in_timezone(self, timezone): |
---|
55 | if self.loaded: |
---|
56 | return self._in_timezone(timezone, self.loaded) |
---|
57 | return '' |
---|
58 | |
---|
59 | |
---|
60 | class BulkFiles(Folder): |
---|
61 | |
---|
62 | """ |
---|
63 | Container for bulk upload compressed files |
---|
64 | """ |
---|
65 | |
---|
66 | __parent__ = __name__ = None |
---|
67 | |
---|
68 | def __acl__(self): |
---|
69 | """ |
---|
70 | Everybody can view, super users can edit |
---|
71 | """ |
---|
72 | permissions = [ |
---|
73 | (Allow, Everyone, 'view'), |
---|
74 | (Allow, 'admins', 'edit'), |
---|
75 | (Deny, Everyone, ALL_PERMISSIONS) |
---|
76 | ] |
---|
77 | return permissions |
---|
78 | |
---|
79 | def add_bulk_file(self, bulk_file): |
---|
80 | self[str(bulk_file.bfid)] = bulk_file |
---|
81 | |
---|
82 | def get_by_uid(self, uid): |
---|
83 | """ |
---|
84 | Return bulk files owned by the given uid |
---|
85 | """ |
---|
86 | return [self[bf] for bf in self if self[bf].uid == str(uid)] |
---|