1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
import typing |
3
|
|
|
import datetime as datetime_root |
4
|
|
|
import json |
5
|
|
|
import os |
6
|
|
|
from datetime import datetime |
|
|
|
|
7
|
|
|
|
8
|
|
|
from babel.dates import format_timedelta |
9
|
|
|
from bs4 import BeautifulSoup |
10
|
|
|
from sqlalchemy import Column, inspect, Index |
11
|
|
|
from sqlalchemy import ForeignKey |
12
|
|
|
from sqlalchemy import Sequence |
13
|
|
|
from sqlalchemy.ext.associationproxy import association_proxy |
14
|
|
|
from sqlalchemy.ext.hybrid import hybrid_property |
15
|
|
|
from sqlalchemy.orm import backref |
16
|
|
|
from sqlalchemy.orm import relationship |
17
|
|
|
from sqlalchemy.orm.attributes import InstrumentedAttribute |
18
|
|
|
from sqlalchemy.orm.collections import attribute_mapped_collection |
19
|
|
|
from sqlalchemy.types import Boolean |
20
|
|
|
from sqlalchemy.types import DateTime |
21
|
|
|
from sqlalchemy.types import Integer |
22
|
|
|
from sqlalchemy.types import Text |
23
|
|
|
from sqlalchemy.types import Unicode |
24
|
|
|
from depot.fields.sqlalchemy import UploadedFileField |
25
|
|
|
from depot.fields.upload import UploadedFile |
26
|
|
|
from depot.io.utils import FileIntent |
27
|
|
|
|
28
|
|
|
from tracim.lib.utils.translation import fake_translator as l_ |
29
|
|
|
from tracim.lib.utils.translation import get_locale |
30
|
|
|
from tracim.exceptions import ContentRevisionUpdateError |
31
|
|
|
from tracim.models.meta import DeclarativeBase |
32
|
|
|
from tracim.models.auth import User |
33
|
|
|
|
34
|
|
|
DEFAULT_PROPERTIES = dict( |
35
|
|
|
allowed_content=dict( |
36
|
|
|
folder=True, |
37
|
|
|
file=True, |
38
|
|
|
page=True, |
39
|
|
|
thread=True, |
40
|
|
|
), |
41
|
|
|
) |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
class Workspace(DeclarativeBase): |
|
|
|
|
45
|
|
|
|
46
|
|
|
__tablename__ = 'workspaces' |
47
|
|
|
|
48
|
|
|
workspace_id = Column(Integer, Sequence('seq__workspaces__workspace_id'), autoincrement=True, primary_key=True) |
|
|
|
|
49
|
|
|
|
50
|
|
|
label = Column(Unicode(1024), unique=False, nullable=False, default='') |
51
|
|
|
description = Column(Text(), unique=False, nullable=False, default='') |
52
|
|
|
calendar_enabled = Column(Boolean, unique=False, nullable=False, default=False) |
53
|
|
|
|
54
|
|
|
# Default value datetime.utcnow, |
55
|
|
|
# see: http://stackoverflow.com/a/13370382/801924 (or http://pastebin.com/VLyWktUn) |
56
|
|
|
created = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow) |
57
|
|
|
# Default value datetime.utcnow, |
58
|
|
|
# see: http://stackoverflow.com/a/13370382/801924 (or http://pastebin.com/VLyWktUn) |
59
|
|
|
updated = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow) |
60
|
|
|
|
61
|
|
|
is_deleted = Column(Boolean, unique=False, nullable=False, default=False) |
62
|
|
|
|
63
|
|
|
revisions = relationship("ContentRevisionRO") |
64
|
|
|
|
65
|
|
|
@hybrid_property |
66
|
|
|
def contents(self) -> ['Content']: |
|
|
|
|
67
|
|
|
# Return a list of unique revisions parent content |
68
|
|
|
contents = [] |
69
|
|
|
for revision in self.revisions: |
70
|
|
|
# TODO BS 20161209: This ``revision.node.workspace`` make a lot |
|
|
|
|
71
|
|
|
# of SQL queries ! |
72
|
|
|
if revision.node.workspace == self and revision.node not in contents: |
73
|
|
|
contents.append(revision.node) |
74
|
|
|
|
75
|
|
|
return contents |
76
|
|
|
|
77
|
|
|
# TODO - G-M - 27-03-2018 - [Calendar] Check about calendar code |
|
|
|
|
78
|
|
|
# @property |
79
|
|
|
# def calendar_url(self) -> str: |
80
|
|
|
# # TODO - 20160531 - Bastien: Cyclic import if import in top of file |
|
|
|
|
81
|
|
|
# from tracim.lib.calendar import CalendarManager |
82
|
|
|
# calendar_manager = CalendarManager(None) |
83
|
|
|
# |
84
|
|
|
# return calendar_manager.get_workspace_calendar_url(self.workspace_id) |
85
|
|
|
|
86
|
|
|
def get_user_role(self, user: User) -> int: |
|
|
|
|
87
|
|
|
for role in user.roles: |
88
|
|
|
if role.workspace.workspace_id==self.workspace_id: |
|
|
|
|
89
|
|
|
return role.role |
90
|
|
|
return UserRoleInWorkspace.NOT_APPLICABLE |
91
|
|
|
|
92
|
|
|
def get_label(self): |
93
|
|
|
""" this method is for interoperability with Content class""" |
94
|
|
|
return self.label |
95
|
|
|
|
96
|
|
|
def get_allowed_content_types(self): |
|
|
|
|
97
|
|
|
# @see Content.get_allowed_content_types() |
98
|
|
|
return [ContentType('folder')] |
99
|
|
|
|
100
|
|
|
def get_valid_children( |
|
|
|
|
101
|
|
|
self, |
102
|
|
|
content_types: list=None, |
|
|
|
|
103
|
|
|
show_deleted: bool=False, |
|
|
|
|
104
|
|
|
show_archived: bool=False, |
|
|
|
|
105
|
|
|
): |
106
|
|
|
for child in self.contents: |
107
|
|
|
# we search only direct children |
108
|
|
|
if not child.parent \ |
109
|
|
|
and (show_deleted or not child.is_deleted) \ |
110
|
|
|
and (show_archived or not child.is_archived): |
111
|
|
|
if not content_types or child.type in content_types: |
112
|
|
|
yield child |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
class UserRoleInWorkspace(DeclarativeBase): |
|
|
|
|
116
|
|
|
|
117
|
|
|
__tablename__ = 'user_workspace' |
118
|
|
|
|
119
|
|
|
user_id = Column(Integer, ForeignKey('users.user_id'), nullable=False, default=None, primary_key=True) |
|
|
|
|
120
|
|
|
workspace_id = Column(Integer, ForeignKey('workspaces.workspace_id'), nullable=False, default=None, primary_key=True) |
|
|
|
|
121
|
|
|
role = Column(Integer, nullable=False, default=0, primary_key=False) |
122
|
|
|
do_notify = Column(Boolean, unique=False, nullable=False, default=False) |
123
|
|
|
|
124
|
|
|
workspace = relationship('Workspace', remote_side=[Workspace.workspace_id], backref='roles', lazy='joined') |
|
|
|
|
125
|
|
|
user = relationship('User', remote_side=[User.user_id], backref='roles') |
126
|
|
|
|
127
|
|
|
NOT_APPLICABLE = 0 |
128
|
|
|
READER = 1 |
129
|
|
|
CONTRIBUTOR = 2 |
130
|
|
|
CONTENT_MANAGER = 4 |
131
|
|
|
WORKSPACE_MANAGER = 8 |
132
|
|
|
|
133
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
134
|
|
|
# LABEL = dict() |
135
|
|
|
# LABEL[0] = l_('N/A') |
136
|
|
|
# LABEL[1] = l_('Reader') |
137
|
|
|
# LABEL[2] = l_('Contributor') |
138
|
|
|
# LABEL[4] = l_('Content Manager') |
139
|
|
|
# LABEL[8] = l_('Workspace Manager') |
140
|
|
|
# |
141
|
|
|
# STYLE = dict() |
142
|
|
|
# STYLE[0] = '' |
143
|
|
|
# STYLE[1] = 'color: #1fdb11;' |
144
|
|
|
# STYLE[2] = 'color: #759ac5;' |
145
|
|
|
# STYLE[4] = 'color: #ea983d;' |
146
|
|
|
# STYLE[8] = 'color: #F00;' |
147
|
|
|
# |
148
|
|
|
# ICON = dict() |
149
|
|
|
# ICON[0] = '' |
150
|
|
|
# ICON[1] = 'fa-eye' |
151
|
|
|
# ICON[2] = 'fa-pencil' |
152
|
|
|
# ICON[4] = 'fa-graduation-cap' |
153
|
|
|
# ICON[8] = 'fa-legal' |
154
|
|
|
# |
155
|
|
|
# |
156
|
|
|
# @property |
157
|
|
|
# def icon(self): |
158
|
|
|
# return UserRoleInWorkspace.ICON[self.role] |
159
|
|
|
# |
160
|
|
|
# @property |
161
|
|
|
# def style(self): |
162
|
|
|
# return UserRoleInWorkspace.STYLE[self.role] |
163
|
|
|
# |
164
|
|
|
# def role_as_label(self): |
165
|
|
|
# return UserRoleInWorkspace.LABEL[self.role] |
166
|
|
|
|
167
|
|
|
@classmethod |
168
|
|
|
def get_all_role_values(self): |
|
|
|
|
169
|
|
|
return [ |
170
|
|
|
UserRoleInWorkspace.READER, |
171
|
|
|
UserRoleInWorkspace.CONTRIBUTOR, |
172
|
|
|
UserRoleInWorkspace.CONTENT_MANAGER, |
173
|
|
|
UserRoleInWorkspace.WORKSPACE_MANAGER |
174
|
|
|
] |
175
|
|
|
|
176
|
|
|
class RoleType(object): |
|
|
|
|
177
|
|
|
def __init__(self, role_id): |
178
|
|
|
self.role_type_id = role_id |
179
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
180
|
|
|
# self.icon = UserRoleInWorkspace.ICON[role_id] |
181
|
|
|
# self.role_label = UserRoleInWorkspace.LABEL[role_id] |
182
|
|
|
# self.css_style = UserRoleInWorkspace.STYLE[role_id] |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
# TODO - G.M - 09-04-2018 [Cleanup] It this items really needed ? |
|
|
|
|
186
|
|
|
# class LinkItem(object): |
187
|
|
|
# def __init__(self, href, label): |
188
|
|
|
# self.href = href |
189
|
|
|
# self.label = label |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
class ActionDescription(object): |
|
|
|
|
193
|
|
|
""" |
194
|
|
|
Allowed status are: |
195
|
|
|
- open |
196
|
|
|
- closed-validated |
197
|
|
|
- closed-invalidated |
198
|
|
|
- closed-deprecated |
199
|
|
|
""" |
200
|
|
|
|
201
|
|
|
COPY = 'copy' |
202
|
|
|
ARCHIVING = 'archiving' |
203
|
|
|
COMMENT = 'content-comment' |
204
|
|
|
CREATION = 'creation' |
205
|
|
|
DELETION = 'deletion' |
206
|
|
|
EDITION = 'edition' # Default action if unknow |
207
|
|
|
REVISION = 'revision' |
208
|
|
|
STATUS_UPDATE = 'status-update' |
209
|
|
|
UNARCHIVING = 'unarchiving' |
210
|
|
|
UNDELETION = 'undeletion' |
211
|
|
|
MOVE = 'move' |
212
|
|
|
|
213
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
214
|
|
|
# _ICONS = { |
215
|
|
|
# 'archiving': 'fa fa-archive', |
216
|
|
|
# 'content-comment': 'fa-comment-o', |
217
|
|
|
# 'creation': 'fa-magic', |
218
|
|
|
# 'deletion': 'fa-trash', |
219
|
|
|
# 'edition': 'fa-edit', |
220
|
|
|
# 'revision': 'fa-history', |
221
|
|
|
# 'status-update': 'fa-random', |
222
|
|
|
# 'unarchiving': 'fa-file-archive-o', |
223
|
|
|
# 'undeletion': 'fa-trash-o', |
224
|
|
|
# 'move': 'fa-arrows', |
225
|
|
|
# 'copy': 'fa-files-o', |
226
|
|
|
# } |
227
|
|
|
# |
228
|
|
|
# _LABELS = { |
229
|
|
|
# 'archiving': l_('archive'), |
230
|
|
|
# 'content-comment': l_('Item commented'), |
231
|
|
|
# 'creation': l_('Item created'), |
232
|
|
|
# 'deletion': l_('Item deleted'), |
233
|
|
|
# 'edition': l_('item modified'), |
234
|
|
|
# 'revision': l_('New revision'), |
235
|
|
|
# 'status-update': l_('New status'), |
236
|
|
|
# 'unarchiving': l_('Item unarchived'), |
237
|
|
|
# 'undeletion': l_('Item undeleted'), |
238
|
|
|
# 'move': l_('Item moved'), |
239
|
|
|
# 'copy': l_('Item copied'), |
240
|
|
|
# } |
241
|
|
|
|
242
|
|
|
def __init__(self, id): |
|
|
|
|
243
|
|
|
assert id in ActionDescription.allowed_values() |
244
|
|
|
self.id = id |
|
|
|
|
245
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
246
|
|
|
# self.label = ActionDescription._LABELS[id] |
247
|
|
|
# self.icon = ActionDescription._ICONS[id] |
248
|
|
|
# self.css = '' |
249
|
|
|
|
250
|
|
|
@classmethod |
251
|
|
|
def allowed_values(cls): |
|
|
|
|
252
|
|
|
return [cls.ARCHIVING, |
253
|
|
|
cls.COMMENT, |
254
|
|
|
cls.CREATION, |
255
|
|
|
cls.DELETION, |
256
|
|
|
cls.EDITION, |
257
|
|
|
cls.REVISION, |
258
|
|
|
cls.STATUS_UPDATE, |
259
|
|
|
cls.UNARCHIVING, |
260
|
|
|
cls.UNDELETION, |
261
|
|
|
cls.MOVE, |
262
|
|
|
cls.COPY, |
263
|
|
|
] |
|
|
|
|
264
|
|
|
|
265
|
|
|
|
266
|
|
|
class ContentStatus(object): |
|
|
|
|
267
|
|
|
""" |
268
|
|
|
Allowed status are: |
269
|
|
|
- open |
270
|
|
|
- closed-validated |
271
|
|
|
- closed-invalidated |
272
|
|
|
- closed-deprecated |
273
|
|
|
""" |
274
|
|
|
|
275
|
|
|
OPEN = 'open' |
276
|
|
|
CLOSED_VALIDATED = 'closed-validated' |
277
|
|
|
CLOSED_UNVALIDATED = 'closed-unvalidated' |
278
|
|
|
CLOSED_DEPRECATED = 'closed-deprecated' |
279
|
|
|
|
280
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
281
|
|
|
# _LABELS = {'open': l_('work in progress'), |
282
|
|
|
# 'closed-validated': l_('closed — validated'), |
283
|
|
|
# 'closed-unvalidated': l_('closed — cancelled'), |
284
|
|
|
# 'closed-deprecated': l_('deprecated')} |
285
|
|
|
# |
286
|
|
|
# _LABELS_THREAD = {'open': l_('subject in progress'), |
287
|
|
|
# 'closed-validated': l_('subject closed — resolved'), |
288
|
|
|
# 'closed-unvalidated': l_('subject closed — cancelled'), |
289
|
|
|
# 'closed-deprecated': l_('deprecated')} |
290
|
|
|
# |
291
|
|
|
# _LABELS_FILE = {'open': l_('work in progress'), |
292
|
|
|
# 'closed-validated': l_('closed — validated'), |
293
|
|
|
# 'closed-unvalidated': l_('closed — cancelled'), |
294
|
|
|
# 'closed-deprecated': l_('deprecated')} |
295
|
|
|
# |
296
|
|
|
# _ICONS = { |
297
|
|
|
# 'open': 'fa fa-square-o', |
298
|
|
|
# 'closed-validated': 'fa fa-check-square-o', |
299
|
|
|
# 'closed-unvalidated': 'fa fa-close', |
300
|
|
|
# 'closed-deprecated': 'fa fa-warning', |
301
|
|
|
# } |
302
|
|
|
# |
303
|
|
|
# _CSS = { |
304
|
|
|
# 'open': 'tracim-status-open', |
305
|
|
|
# 'closed-validated': 'tracim-status-closed-validated', |
306
|
|
|
# 'closed-unvalidated': 'tracim-status-closed-unvalidated', |
307
|
|
|
# 'closed-deprecated': 'tracim-status-closed-deprecated', |
308
|
|
|
# } |
309
|
|
|
|
310
|
|
|
def __init__(self, |
311
|
|
|
id, |
|
|
|
|
312
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
313
|
|
|
# type='' |
314
|
|
|
): |
|
|
|
|
315
|
|
|
self.id = id |
|
|
|
|
316
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
317
|
|
|
# self.icon = ContentStatus._ICONS[id] |
318
|
|
|
# self.css = ContentStatus._CSS[id] |
319
|
|
|
# |
320
|
|
|
# if type==ContentType.Thread: |
321
|
|
|
# self.label = ContentStatus._LABELS_THREAD[id] |
322
|
|
|
# elif type==ContentType.File: |
323
|
|
|
# self.label = ContentStatus._LABELS_FILE[id] |
324
|
|
|
# else: |
325
|
|
|
# self.label = ContentStatus._LABELS[id] |
326
|
|
|
|
327
|
|
|
|
328
|
|
|
@classmethod |
329
|
|
|
def all(cls, type='') -> ['ContentStatus']: |
|
|
|
|
330
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
331
|
|
|
# all = [] |
332
|
|
|
# all.append(ContentStatus('open', type)) |
333
|
|
|
# all.append(ContentStatus('closed-validated', type)) |
334
|
|
|
# all.append(ContentStatus('closed-unvalidated', type)) |
335
|
|
|
# all.append(ContentStatus('closed-deprecated', type)) |
336
|
|
|
# return all |
337
|
|
|
status_list = list() |
338
|
|
|
for elem in cls.allowed_values(): |
339
|
|
|
status_list.append(ContentStatus(elem)) |
340
|
|
|
return status_list |
341
|
|
|
|
342
|
|
|
@classmethod |
343
|
|
|
def allowed_values(cls): |
|
|
|
|
344
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
345
|
|
|
# return ContentStatus._LABELS.keys() |
346
|
|
|
return [ |
347
|
|
|
ContentStatus.OPEN, |
348
|
|
|
ContentStatus.CLOSED_UNVALIDATED, |
349
|
|
|
ContentStatus.CLOSED_VALIDATED, |
350
|
|
|
ContentStatus.CLOSED_DEPRECATED |
351
|
|
|
] |
352
|
|
|
|
353
|
|
|
|
354
|
|
|
class ContentType(object): |
|
|
|
|
355
|
|
|
Any = 'any' |
356
|
|
|
|
357
|
|
|
Folder = 'folder' |
358
|
|
|
File = 'file' |
359
|
|
|
Comment = 'comment' |
360
|
|
|
Thread = 'thread' |
361
|
|
|
Page = 'page' |
362
|
|
|
Event = 'event' |
363
|
|
|
|
364
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Do we really need this ? |
|
|
|
|
365
|
|
|
# _STRING_LIST_SEPARATOR = ',' |
366
|
|
|
|
367
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
368
|
|
|
# _ICONS = { # Deprecated |
369
|
|
|
# 'dashboard': 'fa-home', |
370
|
|
|
# 'workspace': 'fa-bank', |
371
|
|
|
# 'folder': 'fa fa-folder-open-o', |
372
|
|
|
# 'file': 'fa fa-paperclip', |
373
|
|
|
# 'page': 'fa fa-file-text-o', |
374
|
|
|
# 'thread': 'fa fa-comments-o', |
375
|
|
|
# 'comment': 'fa fa-comment-o', |
376
|
|
|
# 'event': 'fa fa-calendar-o', |
377
|
|
|
# } |
378
|
|
|
# |
379
|
|
|
# _CSS_ICONS = { |
380
|
|
|
# 'dashboard': 'fa fa-home', |
381
|
|
|
# 'workspace': 'fa fa-bank', |
382
|
|
|
# 'folder': 'fa fa-folder-open-o', |
383
|
|
|
# 'file': 'fa fa-paperclip', |
384
|
|
|
# 'page': 'fa fa-file-text-o', |
385
|
|
|
# 'thread': 'fa fa-comments-o', |
386
|
|
|
# 'comment': 'fa fa-comment-o', |
387
|
|
|
# 'event': 'fa fa-calendar-o', |
388
|
|
|
# } |
389
|
|
|
# |
390
|
|
|
# _CSS_COLORS = { |
391
|
|
|
# 'dashboard': 't-dashboard-color', |
392
|
|
|
# 'workspace': 't-less-visible', |
393
|
|
|
# 'folder': 't-folder-color', |
394
|
|
|
# 'file': 't-file-color', |
395
|
|
|
# 'page': 't-page-color', |
396
|
|
|
# 'thread': 't-thread-color', |
397
|
|
|
# 'comment': 't-thread-color', |
398
|
|
|
# 'event': 't-event-color', |
399
|
|
|
# } |
400
|
|
|
|
401
|
|
|
_ORDER_WEIGHT = { |
402
|
|
|
'folder': 0, |
403
|
|
|
'page': 1, |
404
|
|
|
'thread': 2, |
405
|
|
|
'file': 3, |
406
|
|
|
'comment': 4, |
407
|
|
|
'event': 5, |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
411
|
|
|
# _LABEL = { |
412
|
|
|
# 'dashboard': '', |
413
|
|
|
# 'workspace': l_('workspace'), |
414
|
|
|
# 'folder': l_('folder'), |
415
|
|
|
# 'file': l_('file'), |
416
|
|
|
# 'page': l_('page'), |
417
|
|
|
# 'thread': l_('thread'), |
418
|
|
|
# 'comment': l_('comment'), |
419
|
|
|
# 'event': l_('event'), |
420
|
|
|
# } |
421
|
|
|
# |
422
|
|
|
# _DELETE_LABEL = { |
423
|
|
|
# 'dashboard': '', |
424
|
|
|
# 'workspace': l_('Delete this workspace'), |
425
|
|
|
# 'folder': l_('Delete this folder'), |
426
|
|
|
# 'file': l_('Delete this file'), |
427
|
|
|
# 'page': l_('Delete this page'), |
428
|
|
|
# 'thread': l_('Delete this thread'), |
429
|
|
|
# 'comment': l_('Delete this comment'), |
430
|
|
|
# 'event': l_('Delete this event'), |
431
|
|
|
# } |
432
|
|
|
# |
433
|
|
|
# @classmethod |
434
|
|
|
# def get_icon(cls, type: str): |
435
|
|
|
# assert(type in ContentType._ICONS) # DYN_REMOVE |
436
|
|
|
# return ContentType._ICONS[type] |
437
|
|
|
|
438
|
|
|
@classmethod |
439
|
|
|
def all(cls): |
|
|
|
|
440
|
|
|
return cls.allowed_types() |
441
|
|
|
|
442
|
|
|
@classmethod |
443
|
|
|
def allowed_types(cls): |
|
|
|
|
444
|
|
|
return [cls.Folder, cls.File, cls.Comment, cls.Thread, cls.Page, |
445
|
|
|
cls.Event] |
446
|
|
|
|
447
|
|
|
@classmethod |
448
|
|
|
def allowed_types_for_folding(cls): |
|
|
|
|
449
|
|
|
# This method is used for showing only "main" |
450
|
|
|
# types in the left-side treeview |
451
|
|
|
return [cls.Folder, cls.File, cls.Thread, cls.Page] |
452
|
|
|
|
453
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
454
|
|
|
# @classmethod |
455
|
|
|
# def allowed_types_from_str(cls, allowed_types_as_string: str): |
456
|
|
|
# allowed_types = [] |
457
|
|
|
# # HACK - THIS |
458
|
|
|
# for item in allowed_types_as_string.split(ContentType._STRING_LIST_SEPARATOR): |
459
|
|
|
# if item and item in ContentType.allowed_types_for_folding(): |
460
|
|
|
# allowed_types.append(item) |
461
|
|
|
# return allowed_types |
462
|
|
|
# |
463
|
|
|
# @classmethod |
464
|
|
|
# def fill_url(cls, content: 'Content'): |
465
|
|
|
# # TODO - DYNDATATYPE - D.A. - 2014-12-02 |
|
|
|
|
466
|
|
|
# # Make this code dynamic loading data types |
467
|
|
|
# |
468
|
|
|
# if content.type==ContentType.Folder: |
469
|
|
|
# return '/workspaces/{}/folders/{}'.format(content.workspace_id, content.content_id) |
470
|
|
|
# elif content.type==ContentType.File: |
471
|
|
|
# return '/workspaces/{}/folders/{}/files/{}'.format(content.workspace_id, content.parent_id, content.content_id) |
|
|
|
|
472
|
|
|
# elif content.type==ContentType.Thread: |
473
|
|
|
# return '/workspaces/{}/folders/{}/threads/{}'.format(content.workspace_id, content.parent_id, content.content_id) |
|
|
|
|
474
|
|
|
# elif content.type==ContentType.Page: |
475
|
|
|
# return '/workspaces/{}/folders/{}/pages/{}'.format(content.workspace_id, content.parent_id, content.content_id) |
|
|
|
|
476
|
|
|
# |
477
|
|
|
# @classmethod |
478
|
|
|
# def fill_url_for_workspace(cls, workspace: Workspace): |
479
|
|
|
# # TODO - DYNDATATYPE - D.A. - 2014-12-02 |
|
|
|
|
480
|
|
|
# # Make this code dynamic loading data types |
481
|
|
|
# return '/workspaces/{}'.format(workspace.workspace_id) |
482
|
|
|
|
483
|
|
|
@classmethod |
484
|
|
|
def sorted(cls, types: ['ContentType']) -> ['ContentType']: |
|
|
|
|
485
|
|
|
return sorted(types, key=lambda content_type: content_type.priority) |
486
|
|
|
|
487
|
|
|
@property |
488
|
|
|
def type(self): |
|
|
|
|
489
|
|
|
return self.id |
490
|
|
|
|
491
|
|
|
def __init__(self, type): |
|
|
|
|
492
|
|
|
self.id = type |
|
|
|
|
493
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
494
|
|
|
# self.icon = ContentType._CSS_ICONS[type] |
495
|
|
|
# self.color = ContentType._CSS_COLORS[type] # deprecated |
496
|
|
|
# self.css = ContentType._CSS_COLORS[type] |
497
|
|
|
# self.label = ContentType._LABEL[type] |
498
|
|
|
self.priority = ContentType._ORDER_WEIGHT[type] |
499
|
|
|
|
500
|
|
|
def toDict(self): |
|
|
|
|
501
|
|
|
return dict(id=self.type, |
502
|
|
|
type=self.type, |
503
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
504
|
|
|
# icon=self.icon, |
505
|
|
|
# color=self.color, |
506
|
|
|
# label=self.label, |
507
|
|
|
priority=self.priority) |
508
|
|
|
|
509
|
|
|
|
510
|
|
|
class ContentChecker(object): |
|
|
|
|
511
|
|
|
|
512
|
|
|
@classmethod |
513
|
|
|
def check_properties(cls, item): |
|
|
|
|
514
|
|
|
if item.type == ContentType.Folder: |
515
|
|
|
properties = item.properties |
516
|
|
|
if 'allowed_content' not in properties.keys(): |
517
|
|
|
return False |
518
|
|
|
if 'folders' not in properties['allowed_content']: |
519
|
|
|
return False |
520
|
|
|
if 'files' not in properties['allowed_content']: |
521
|
|
|
return False |
522
|
|
|
if 'pages' not in properties['allowed_content']: |
523
|
|
|
return False |
524
|
|
|
if 'threads' not in properties['allowed_content']: |
525
|
|
|
return False |
526
|
|
|
return True |
527
|
|
|
|
528
|
|
|
if item.type == ContentType.Event: |
529
|
|
|
properties = item.properties |
530
|
|
|
if 'name' not in properties.keys(): |
531
|
|
|
return False |
532
|
|
|
if 'raw' not in properties.keys(): |
533
|
|
|
return False |
534
|
|
|
if 'start' not in properties.keys(): |
535
|
|
|
return False |
536
|
|
|
if 'end' not in properties.keys(): |
537
|
|
|
return False |
538
|
|
|
return True |
539
|
|
|
|
540
|
|
|
# TODO - G.M - 15-03-2018 - Choose only correct Content-type for origin |
|
|
|
|
541
|
|
|
# Only content who can be copied need this |
542
|
|
|
if item.type == ContentType.Any: |
543
|
|
|
properties = item.properties |
544
|
|
|
if 'origin' in properties.keys(): |
545
|
|
|
return True |
546
|
|
|
raise NotImplementedError |
547
|
|
|
|
548
|
|
|
@classmethod |
549
|
|
|
def reset_properties(cls, item): |
|
|
|
|
550
|
|
|
if item.type == ContentType.Folder: |
551
|
|
|
item.properties = DEFAULT_PROPERTIES |
552
|
|
|
return |
553
|
|
|
|
554
|
|
|
raise NotImplementedError |
555
|
|
|
|
556
|
|
|
|
557
|
|
|
class ContentRevisionRO(DeclarativeBase): |
|
|
|
|
558
|
|
|
""" |
559
|
|
|
Revision of Content. It's immutable, update or delete an existing ContentRevisionRO will throw |
560
|
|
|
ContentRevisionUpdateError errors. |
561
|
|
|
""" |
562
|
|
|
|
563
|
|
|
__tablename__ = 'content_revisions' |
564
|
|
|
|
565
|
|
|
revision_id = Column(Integer, primary_key=True) |
566
|
|
|
content_id = Column(Integer, ForeignKey('content.id'), nullable=False) |
567
|
|
|
owner_id = Column(Integer, ForeignKey('users.user_id'), nullable=True) |
568
|
|
|
|
569
|
|
|
label = Column(Unicode(1024), unique=False, nullable=False) |
570
|
|
|
description = Column(Text(), unique=False, nullable=False, default='') |
571
|
|
|
file_extension = Column( |
572
|
|
|
Unicode(255), |
573
|
|
|
unique=False, |
574
|
|
|
nullable=False, |
575
|
|
|
server_default='', |
576
|
|
|
) |
577
|
|
|
file_mimetype = Column(Unicode(255), unique=False, nullable=False, default='') |
|
|
|
|
578
|
|
|
# INFO - A.P - 2017-07-03 - Depot Doc |
579
|
|
|
# http://depot.readthedocs.io/en/latest/#attaching-files-to-models |
580
|
|
|
# http://depot.readthedocs.io/en/latest/api.html#module-depot.fields |
581
|
|
|
depot_file = Column(UploadedFileField, unique=False, nullable=True) |
582
|
|
|
properties = Column('properties', Text(), unique=False, nullable=False, default='') |
583
|
|
|
|
584
|
|
|
type = Column(Unicode(32), unique=False, nullable=False) |
585
|
|
|
status = Column(Unicode(32), unique=False, nullable=False, default=ContentStatus.OPEN) |
586
|
|
|
created = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow) |
587
|
|
|
updated = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow) |
588
|
|
|
is_deleted = Column(Boolean, unique=False, nullable=False, default=False) |
589
|
|
|
is_archived = Column(Boolean, unique=False, nullable=False, default=False) |
590
|
|
|
is_temporary = Column(Boolean, unique=False, nullable=False, default=False) |
591
|
|
|
revision_type = Column(Unicode(32), unique=False, nullable=False, default='') |
592
|
|
|
|
593
|
|
|
workspace_id = Column(Integer, ForeignKey('workspaces.workspace_id'), unique=False, nullable=True) |
|
|
|
|
594
|
|
|
workspace = relationship('Workspace', remote_side=[Workspace.workspace_id]) |
595
|
|
|
|
596
|
|
|
parent_id = Column(Integer, ForeignKey('content.id'), nullable=True, default=None) |
597
|
|
|
parent = relationship("Content", foreign_keys=[parent_id], back_populates="children_revisions") |
598
|
|
|
|
599
|
|
|
node = relationship("Content", foreign_keys=[content_id], back_populates="revisions") |
600
|
|
|
owner = relationship('User', remote_side=[User.user_id]) |
601
|
|
|
|
602
|
|
|
""" List of column copied when make a new revision from another """ |
603
|
|
|
_cloned_columns = ( |
604
|
|
|
'content_id', |
605
|
|
|
'created', |
606
|
|
|
'description', |
607
|
|
|
'file_mimetype', |
608
|
|
|
'file_extension', |
609
|
|
|
'is_archived', |
610
|
|
|
'is_deleted', |
611
|
|
|
'label', |
612
|
|
|
'owner', |
613
|
|
|
'owner_id', |
614
|
|
|
'parent', |
615
|
|
|
'parent_id', |
616
|
|
|
'properties', |
617
|
|
|
'revision_type', |
618
|
|
|
'status', |
619
|
|
|
'type', |
620
|
|
|
'updated', |
621
|
|
|
'workspace', |
622
|
|
|
'workspace_id', |
623
|
|
|
'is_temporary', |
624
|
|
|
) |
625
|
|
|
|
626
|
|
|
# Read by must be used like this: |
627
|
|
|
# read_datetime = revision.ready_by[<User instance>] |
628
|
|
|
# if user did not read the content, then a key error is raised |
629
|
|
|
read_by = association_proxy( |
630
|
|
|
'revision_read_statuses', # name of the attribute |
631
|
|
|
'view_datetime', # attribute the value is taken from |
632
|
|
|
creator=lambda k, v: \ |
633
|
|
|
RevisionReadStatus(user=k, view_datetime=v) |
634
|
|
|
) |
635
|
|
|
|
636
|
|
|
@property |
637
|
|
|
def file_name(self): |
|
|
|
|
638
|
|
|
return '{0}{1}'.format( |
639
|
|
|
self.label, |
640
|
|
|
self.file_extension, |
641
|
|
|
) |
642
|
|
|
|
643
|
|
|
@classmethod |
644
|
|
|
def new_from(cls, revision: 'ContentRevisionRO') -> 'ContentRevisionRO': |
645
|
|
|
""" |
646
|
|
|
|
647
|
|
|
Return new instance of ContentRevisionRO where properties are copied from revision parameter. |
|
|
|
|
648
|
|
|
Look at ContentRevisionRO._cloned_columns to see what columns are copieds. |
649
|
|
|
|
650
|
|
|
:param revision: revision to copy |
651
|
|
|
:type revision: ContentRevisionRO |
652
|
|
|
:return: new revision from revision parameter |
653
|
|
|
:rtype: ContentRevisionRO |
654
|
|
|
""" |
655
|
|
|
new_rev = cls() |
656
|
|
|
|
657
|
|
|
for column_name in cls._cloned_columns: |
658
|
|
|
column_value = getattr(revision, column_name) |
659
|
|
|
setattr(new_rev, column_name, column_value) |
660
|
|
|
|
661
|
|
|
new_rev.updated = datetime.utcnow() |
662
|
|
|
if revision.depot_file: |
663
|
|
|
new_rev.depot_file = FileIntent( |
664
|
|
|
revision.depot_file.file.read(), |
665
|
|
|
revision.file_name, |
666
|
|
|
revision.file_mimetype, |
667
|
|
|
) |
668
|
|
|
|
669
|
|
|
return new_rev |
670
|
|
|
|
671
|
|
|
@classmethod |
672
|
|
|
def copy( |
|
|
|
|
673
|
|
|
cls, |
674
|
|
|
revision: 'ContentRevisionRO', |
675
|
|
|
parent: 'Content' |
676
|
|
|
) -> 'ContentRevisionRO': |
677
|
|
|
|
678
|
|
|
copy_rev = cls() |
679
|
|
|
import copy |
680
|
|
|
copy_columns = cls._cloned_columns |
681
|
|
|
for column_name in copy_columns: |
682
|
|
|
# INFO - G-M - 15-03-2018 - set correct parent |
683
|
|
|
if column_name == 'parent_id': |
684
|
|
|
column_value = copy.copy(parent.id) |
685
|
|
|
elif column_name == 'parent': |
686
|
|
|
column_value = copy.copy(parent) |
687
|
|
|
else: |
688
|
|
|
column_value = copy.copy(getattr(revision, column_name)) |
689
|
|
|
setattr(copy_rev, column_name, column_value) |
690
|
|
|
|
691
|
|
|
# copy attached_file |
692
|
|
|
if revision.depot_file: |
693
|
|
|
copy_rev.depot_file = FileIntent( |
694
|
|
|
revision.depot_file.file.read(), |
695
|
|
|
revision.file_name, |
696
|
|
|
revision.file_mimetype, |
697
|
|
|
) |
698
|
|
|
return copy_rev |
699
|
|
|
|
700
|
|
|
def __setattr__(self, key: str, value: 'mixed'): |
701
|
|
|
""" |
702
|
|
|
ContentRevisionUpdateError is raised if tried to update column and revision own identity |
703
|
|
|
:param key: attribute name |
704
|
|
|
:param value: attribute value |
705
|
|
|
:return: |
706
|
|
|
""" |
707
|
|
|
if key in ('_sa_instance_state', ): # Prevent infinite loop from SQLAlchemy code and altered set |
|
|
|
|
708
|
|
|
return super().__setattr__(key, value) |
709
|
|
|
|
710
|
|
|
# FIXME - G.M - 28-03-2018 - Cycling Import |
|
|
|
|
711
|
|
|
from tracim.models.revision_protection import RevisionsIntegrity |
712
|
|
|
if inspect(self).has_identity \ |
713
|
|
|
and key in self._cloned_columns \ |
714
|
|
|
and not RevisionsIntegrity.is_updatable(self): |
715
|
|
|
raise ContentRevisionUpdateError( |
|
|
|
|
716
|
|
|
"Can't modify revision. To work on new revision use tracim.model.new_revision " + |
|
|
|
|
717
|
|
|
"context manager.") |
718
|
|
|
|
719
|
|
|
super().__setattr__(key, value) |
720
|
|
|
|
721
|
|
|
def get_status(self) -> ContentStatus: |
|
|
|
|
722
|
|
|
return ContentStatus(self.status) |
723
|
|
|
|
724
|
|
|
def get_label(self) -> str: |
|
|
|
|
725
|
|
|
return self.label or self.file_name or '' |
726
|
|
|
|
727
|
|
|
def get_last_action(self) -> ActionDescription: |
|
|
|
|
728
|
|
|
return ActionDescription(self.revision_type) |
729
|
|
|
|
730
|
|
|
def has_new_information_for(self, user: User) -> bool: |
731
|
|
|
""" |
732
|
|
|
:param user: the _session current user |
733
|
|
|
:return: bool, True if there is new information for given user else False |
734
|
|
|
False if the user is None |
735
|
|
|
""" |
736
|
|
|
if not user: |
737
|
|
|
return False |
738
|
|
|
|
739
|
|
|
if user not in self.read_by.keys(): |
740
|
|
|
return True |
741
|
|
|
|
742
|
|
|
return False |
743
|
|
|
|
744
|
|
|
def get_label_as_file(self): |
|
|
|
|
745
|
|
|
file_extension = self.file_extension or '' |
746
|
|
|
|
747
|
|
|
if self.type == ContentType.Thread: |
748
|
|
|
file_extension = '.html' |
749
|
|
|
elif self.type == ContentType.Page: |
750
|
|
|
file_extension = '.html' |
751
|
|
|
|
752
|
|
|
return '{0}{1}'.format( |
753
|
|
|
self.label, |
754
|
|
|
file_extension, |
755
|
|
|
) |
756
|
|
|
|
757
|
|
|
|
758
|
|
|
Index('idx__content_revisions__owner_id', ContentRevisionRO.owner_id) |
759
|
|
|
Index('idx__content_revisions__parent_id', ContentRevisionRO.parent_id) |
760
|
|
|
|
761
|
|
|
|
762
|
|
|
class Content(DeclarativeBase): |
|
|
|
|
763
|
|
|
""" |
764
|
|
|
Content is used as a virtual representation of ContentRevisionRO. |
765
|
|
|
content.PROPERTY (except for content.id, content.revisions, content.children_revisions) will return |
|
|
|
|
766
|
|
|
value of most recent revision of content. |
767
|
|
|
|
768
|
|
|
# UPDATE A CONTENT |
769
|
|
|
|
770
|
|
|
To update an existing Content, you must use tracim.model.new_revision context manager: |
771
|
|
|
content = my_sontent_getter_method() |
772
|
|
|
with new_revision(content): |
773
|
|
|
content.description = 'foo bar baz' |
774
|
|
|
DBSession.flush() |
775
|
|
|
|
776
|
|
|
# QUERY CONTENTS |
777
|
|
|
|
778
|
|
|
To query contents you will need to join your content query with ContentRevisionRO. Join |
779
|
|
|
condition is available at tracim.lib.content.ContentApi#get_revision_join: |
780
|
|
|
|
781
|
|
|
content = DBSession.query(Content).join(ContentRevisionRO, ContentApi.get_revision_join()) |
782
|
|
|
.filter(Content.label == 'foo') |
783
|
|
|
.one() |
784
|
|
|
|
785
|
|
|
ContentApi provide also prepared Content at tracim.lib.content.ContentApi#get_canonical_query: |
786
|
|
|
|
787
|
|
|
content = ContentApi.get_canonical_query() |
788
|
|
|
.filter(Content.label == 'foo') |
789
|
|
|
.one() |
790
|
|
|
""" |
791
|
|
|
|
792
|
|
|
__tablename__ = 'content' |
793
|
|
|
|
794
|
|
|
revision_to_serialize = -0 # This flag allow to serialize a given revision if required by the user |
|
|
|
|
795
|
|
|
|
796
|
|
|
id = Column(Integer, primary_key=True) |
|
|
|
|
797
|
|
|
# TODO - A.P - 2017-09-05 - revisions default sorting |
|
|
|
|
798
|
|
|
# The only sorting that makes sens is ordering by "updated" field. But: |
799
|
|
|
# - its content will soon replace the one of "created", |
800
|
|
|
# - this "updated" field will then be dropped. |
801
|
|
|
# So for now, we order by "revision_id" explicitly, but remember to switch |
802
|
|
|
# to "created" once "updated" removed. |
803
|
|
|
# https://github.com/tracim/tracim/issues/336 |
804
|
|
|
revisions = relationship("ContentRevisionRO", |
805
|
|
|
foreign_keys=[ContentRevisionRO.content_id], |
806
|
|
|
back_populates="node", |
807
|
|
|
order_by="ContentRevisionRO.revision_id") |
808
|
|
|
children_revisions = relationship("ContentRevisionRO", |
809
|
|
|
foreign_keys=[ContentRevisionRO.parent_id], |
810
|
|
|
back_populates="parent") |
811
|
|
|
|
812
|
|
|
@hybrid_property |
813
|
|
|
def content_id(self) -> int: |
|
|
|
|
814
|
|
|
return self.revision.content_id |
815
|
|
|
|
816
|
|
|
@content_id.setter |
817
|
|
|
def content_id(self, value: int) -> None: |
818
|
|
|
self.revision.content_id = value |
819
|
|
|
|
820
|
|
|
@content_id.expression |
821
|
|
|
def content_id(cls) -> InstrumentedAttribute: |
|
|
|
|
822
|
|
|
return ContentRevisionRO.content_id |
823
|
|
|
|
824
|
|
|
@hybrid_property |
825
|
|
|
def revision_id(self) -> int: |
|
|
|
|
826
|
|
|
return self.revision.revision_id |
827
|
|
|
|
828
|
|
|
@revision_id.setter |
829
|
|
|
def revision_id(self, value: int): |
830
|
|
|
self.revision.revision_id = value |
831
|
|
|
|
832
|
|
|
@revision_id.expression |
833
|
|
|
def revision_id(cls) -> InstrumentedAttribute: |
|
|
|
|
834
|
|
|
return ContentRevisionRO.revision_id |
835
|
|
|
|
836
|
|
|
@hybrid_property |
837
|
|
|
def owner_id(self) -> int: |
|
|
|
|
838
|
|
|
return self.revision.owner_id |
839
|
|
|
|
840
|
|
|
@owner_id.setter |
841
|
|
|
def owner_id(self, value: int) -> None: |
842
|
|
|
self.revision.owner_id = value |
843
|
|
|
|
844
|
|
|
@owner_id.expression |
845
|
|
|
def owner_id(cls) -> InstrumentedAttribute: |
|
|
|
|
846
|
|
|
return ContentRevisionRO.owner_id |
847
|
|
|
|
848
|
|
|
@hybrid_property |
849
|
|
|
def label(self) -> str: |
|
|
|
|
850
|
|
|
return self.revision.label |
851
|
|
|
|
852
|
|
|
@label.setter |
853
|
|
|
def label(self, value: str) -> None: |
854
|
|
|
self.revision.label = value |
855
|
|
|
|
856
|
|
|
@label.expression |
857
|
|
|
def label(cls) -> InstrumentedAttribute: |
|
|
|
|
858
|
|
|
return ContentRevisionRO.label |
859
|
|
|
|
860
|
|
|
@hybrid_property |
861
|
|
|
def description(self) -> str: |
|
|
|
|
862
|
|
|
return self.revision.description |
863
|
|
|
|
864
|
|
|
@description.setter |
865
|
|
|
def description(self, value: str) -> None: |
866
|
|
|
self.revision.description = value |
867
|
|
|
|
868
|
|
|
@description.expression |
869
|
|
|
def description(cls) -> InstrumentedAttribute: |
|
|
|
|
870
|
|
|
return ContentRevisionRO.description |
871
|
|
|
|
872
|
|
|
@hybrid_property |
873
|
|
|
def file_name(self) -> str: |
|
|
|
|
874
|
|
|
return '{0}{1}'.format( |
875
|
|
|
self.revision.label, |
876
|
|
|
self.revision.file_extension, |
877
|
|
|
) |
878
|
|
|
|
879
|
|
|
@file_name.setter |
880
|
|
|
def file_name(self, value: str) -> None: |
881
|
|
|
file_name, file_extension = os.path.splitext(value) |
882
|
|
|
if not self.revision.label: |
883
|
|
|
self.revision.label = file_name |
884
|
|
|
self.revision.file_extension = file_extension |
885
|
|
|
|
886
|
|
|
@file_name.expression |
887
|
|
|
def file_name(cls) -> InstrumentedAttribute: |
|
|
|
|
888
|
|
|
return ContentRevisionRO.file_name + ContentRevisionRO.file_extension |
889
|
|
|
|
890
|
|
|
@hybrid_property |
891
|
|
|
def file_extension(self) -> str: |
|
|
|
|
892
|
|
|
return self.revision.file_extension |
893
|
|
|
|
894
|
|
|
@file_extension.setter |
895
|
|
|
def file_extension(self, value: str) -> None: |
896
|
|
|
self.revision.file_extension = value |
897
|
|
|
|
898
|
|
|
@file_extension.expression |
899
|
|
|
def file_extension(cls) -> InstrumentedAttribute: |
|
|
|
|
900
|
|
|
return ContentRevisionRO.file_extension |
901
|
|
|
|
902
|
|
|
@hybrid_property |
903
|
|
|
def file_mimetype(self) -> str: |
|
|
|
|
904
|
|
|
return self.revision.file_mimetype |
905
|
|
|
|
906
|
|
|
@file_mimetype.setter |
907
|
|
|
def file_mimetype(self, value: str) -> None: |
908
|
|
|
self.revision.file_mimetype = value |
909
|
|
|
|
910
|
|
|
@file_mimetype.expression |
911
|
|
|
def file_mimetype(cls) -> InstrumentedAttribute: |
|
|
|
|
912
|
|
|
return ContentRevisionRO.file_mimetype |
913
|
|
|
|
914
|
|
|
@hybrid_property |
915
|
|
|
def _properties(self) -> str: |
|
|
|
|
916
|
|
|
return self.revision.properties |
917
|
|
|
|
918
|
|
|
@_properties.setter |
919
|
|
|
def _properties(self, value: str) -> None: |
920
|
|
|
self.revision.properties = value |
921
|
|
|
|
922
|
|
|
@_properties.expression |
923
|
|
|
def _properties(cls) -> InstrumentedAttribute: |
|
|
|
|
924
|
|
|
return ContentRevisionRO.properties |
925
|
|
|
|
926
|
|
|
@hybrid_property |
927
|
|
|
def type(self) -> str: |
|
|
|
|
928
|
|
|
return self.revision.type |
929
|
|
|
|
930
|
|
|
@type.setter |
931
|
|
|
def type(self, value: str) -> None: |
932
|
|
|
self.revision.type = value |
933
|
|
|
|
934
|
|
|
@type.expression |
935
|
|
|
def type(cls) -> InstrumentedAttribute: |
|
|
|
|
936
|
|
|
return ContentRevisionRO.type |
937
|
|
|
|
938
|
|
|
@hybrid_property |
939
|
|
|
def status(self) -> str: |
|
|
|
|
940
|
|
|
return self.revision.status |
941
|
|
|
|
942
|
|
|
@status.setter |
943
|
|
|
def status(self, value: str) -> None: |
944
|
|
|
self.revision.status = value |
945
|
|
|
|
946
|
|
|
@status.expression |
947
|
|
|
def status(cls) -> InstrumentedAttribute: |
|
|
|
|
948
|
|
|
return ContentRevisionRO.status |
949
|
|
|
|
950
|
|
|
@hybrid_property |
951
|
|
|
def created(self) -> datetime: |
|
|
|
|
952
|
|
|
return self.revision.created |
953
|
|
|
|
954
|
|
|
@created.setter |
955
|
|
|
def created(self, value: datetime) -> None: |
956
|
|
|
self.revision.created = value |
957
|
|
|
|
958
|
|
|
@created.expression |
959
|
|
|
def created(cls) -> InstrumentedAttribute: |
|
|
|
|
960
|
|
|
return ContentRevisionRO.created |
961
|
|
|
|
962
|
|
|
@hybrid_property |
963
|
|
|
def updated(self) -> datetime: |
|
|
|
|
964
|
|
|
return self.revision.updated |
965
|
|
|
|
966
|
|
|
@updated.setter |
967
|
|
|
def updated(self, value: datetime) -> None: |
968
|
|
|
self.revision.updated = value |
969
|
|
|
|
970
|
|
|
@updated.expression |
971
|
|
|
def updated(cls) -> InstrumentedAttribute: |
|
|
|
|
972
|
|
|
return ContentRevisionRO.updated |
973
|
|
|
|
974
|
|
|
@hybrid_property |
975
|
|
|
def is_deleted(self) -> bool: |
|
|
|
|
976
|
|
|
return self.revision.is_deleted |
977
|
|
|
|
978
|
|
|
@is_deleted.setter |
979
|
|
|
def is_deleted(self, value: bool) -> None: |
980
|
|
|
self.revision.is_deleted = value |
981
|
|
|
|
982
|
|
|
@is_deleted.expression |
983
|
|
|
def is_deleted(cls) -> InstrumentedAttribute: |
|
|
|
|
984
|
|
|
return ContentRevisionRO.is_deleted |
985
|
|
|
|
986
|
|
|
@hybrid_property |
987
|
|
|
def is_archived(self) -> bool: |
|
|
|
|
988
|
|
|
return self.revision.is_archived |
989
|
|
|
|
990
|
|
|
@is_archived.setter |
991
|
|
|
def is_archived(self, value: bool) -> None: |
992
|
|
|
self.revision.is_archived = value |
993
|
|
|
|
994
|
|
|
@is_archived.expression |
995
|
|
|
def is_archived(cls) -> InstrumentedAttribute: |
|
|
|
|
996
|
|
|
return ContentRevisionRO.is_archived |
997
|
|
|
|
998
|
|
|
@hybrid_property |
999
|
|
|
def is_temporary(self) -> bool: |
|
|
|
|
1000
|
|
|
return self.revision.is_temporary |
1001
|
|
|
|
1002
|
|
|
@is_temporary.setter |
1003
|
|
|
def is_temporary(self, value: bool) -> None: |
1004
|
|
|
self.revision.is_temporary = value |
1005
|
|
|
|
1006
|
|
|
@is_temporary.expression |
1007
|
|
|
def is_temporary(cls) -> InstrumentedAttribute: |
|
|
|
|
1008
|
|
|
return ContentRevisionRO.is_temporary |
1009
|
|
|
|
1010
|
|
|
@hybrid_property |
1011
|
|
|
def revision_type(self) -> str: |
|
|
|
|
1012
|
|
|
return self.revision.revision_type |
1013
|
|
|
|
1014
|
|
|
@revision_type.setter |
1015
|
|
|
def revision_type(self, value: str) -> None: |
1016
|
|
|
self.revision.revision_type = value |
1017
|
|
|
|
1018
|
|
|
@revision_type.expression |
1019
|
|
|
def revision_type(cls) -> InstrumentedAttribute: |
|
|
|
|
1020
|
|
|
return ContentRevisionRO.revision_type |
1021
|
|
|
|
1022
|
|
|
@hybrid_property |
1023
|
|
|
def workspace_id(self) -> int: |
|
|
|
|
1024
|
|
|
return self.revision.workspace_id |
1025
|
|
|
|
1026
|
|
|
@workspace_id.setter |
1027
|
|
|
def workspace_id(self, value: int) -> None: |
1028
|
|
|
self.revision.workspace_id = value |
1029
|
|
|
|
1030
|
|
|
@workspace_id.expression |
1031
|
|
|
def workspace_id(cls) -> InstrumentedAttribute: |
|
|
|
|
1032
|
|
|
return ContentRevisionRO.workspace_id |
1033
|
|
|
|
1034
|
|
|
@hybrid_property |
1035
|
|
|
def workspace(self) -> Workspace: |
|
|
|
|
1036
|
|
|
return self.revision.workspace |
1037
|
|
|
|
1038
|
|
|
@workspace.setter |
1039
|
|
|
def workspace(self, value: Workspace) -> None: |
1040
|
|
|
self.revision.workspace = value |
1041
|
|
|
|
1042
|
|
|
@workspace.expression |
1043
|
|
|
def workspace(cls) -> InstrumentedAttribute: |
|
|
|
|
1044
|
|
|
return ContentRevisionRO.workspace |
1045
|
|
|
|
1046
|
|
|
@hybrid_property |
1047
|
|
|
def parent_id(self) -> int: |
|
|
|
|
1048
|
|
|
return self.revision.parent_id |
1049
|
|
|
|
1050
|
|
|
@parent_id.setter |
1051
|
|
|
def parent_id(self, value: int) -> None: |
1052
|
|
|
self.revision.parent_id = value |
1053
|
|
|
|
1054
|
|
|
@parent_id.expression |
1055
|
|
|
def parent_id(cls) -> InstrumentedAttribute: |
|
|
|
|
1056
|
|
|
return ContentRevisionRO.parent_id |
1057
|
|
|
|
1058
|
|
|
@hybrid_property |
1059
|
|
|
def parent(self) -> 'Content': |
|
|
|
|
1060
|
|
|
return self.revision.parent |
1061
|
|
|
|
1062
|
|
|
@parent.setter |
1063
|
|
|
def parent(self, value: 'Content') -> None: |
1064
|
|
|
self.revision.parent = value |
1065
|
|
|
|
1066
|
|
|
@parent.expression |
1067
|
|
|
def parent(cls) -> InstrumentedAttribute: |
|
|
|
|
1068
|
|
|
return ContentRevisionRO.parent |
1069
|
|
|
|
1070
|
|
|
@hybrid_property |
1071
|
|
|
def node(self) -> 'Content': |
|
|
|
|
1072
|
|
|
return self.revision.node |
1073
|
|
|
|
1074
|
|
|
@node.setter |
1075
|
|
|
def node(self, value: 'Content') -> None: |
1076
|
|
|
self.revision.node = value |
1077
|
|
|
|
1078
|
|
|
@node.expression |
1079
|
|
|
def node(cls) -> InstrumentedAttribute: |
|
|
|
|
1080
|
|
|
return ContentRevisionRO.node |
1081
|
|
|
|
1082
|
|
|
@hybrid_property |
1083
|
|
|
def owner(self) -> User: |
|
|
|
|
1084
|
|
|
return self.revision.owner |
1085
|
|
|
|
1086
|
|
|
@owner.setter |
1087
|
|
|
def owner(self, value: User) -> None: |
1088
|
|
|
self.revision.owner = value |
1089
|
|
|
|
1090
|
|
|
@owner.expression |
1091
|
|
|
def owner(cls) -> InstrumentedAttribute: |
|
|
|
|
1092
|
|
|
return ContentRevisionRO.owner |
1093
|
|
|
|
1094
|
|
|
@hybrid_property |
1095
|
|
|
def children(self) -> ['Content']: |
1096
|
|
|
""" |
1097
|
|
|
:return: list of children Content |
1098
|
|
|
:rtype Content |
1099
|
|
|
""" |
1100
|
|
|
# Return a list of unique revisions parent content |
1101
|
|
|
return list(set([revision.node for revision in self.children_revisions])) |
1102
|
|
|
|
1103
|
|
|
@property |
1104
|
|
|
def revision(self) -> ContentRevisionRO: |
|
|
|
|
1105
|
|
|
return self.get_current_revision() |
1106
|
|
|
|
1107
|
|
|
@property |
1108
|
|
|
def first_revision(self) -> ContentRevisionRO: |
|
|
|
|
1109
|
|
|
return self.revisions[0] # FIXME |
|
|
|
|
1110
|
|
|
|
1111
|
|
|
@property |
1112
|
|
|
def last_revision(self) -> ContentRevisionRO: |
|
|
|
|
1113
|
|
|
return self.revisions[-1] |
1114
|
|
|
|
1115
|
|
|
@property |
1116
|
|
|
def is_editable(self) -> bool: |
|
|
|
|
1117
|
|
|
return not self.is_archived and not self.is_deleted |
1118
|
|
|
|
1119
|
|
|
@property |
1120
|
|
|
def depot_file(self) -> UploadedFile: |
|
|
|
|
1121
|
|
|
return self.revision.depot_file |
1122
|
|
|
|
1123
|
|
|
@depot_file.setter |
1124
|
|
|
def depot_file(self, value): |
1125
|
|
|
self.revision.depot_file = value |
1126
|
|
|
|
1127
|
|
|
def get_current_revision(self) -> ContentRevisionRO: |
|
|
|
|
1128
|
|
|
if not self.revisions: |
1129
|
|
|
return self.new_revision() |
1130
|
|
|
|
1131
|
|
|
# If last revisions revision don't have revision_id, return it we just add it. |
1132
|
|
|
if self.revisions[-1].revision_id is None: |
1133
|
|
|
return self.revisions[-1] |
1134
|
|
|
|
1135
|
|
|
# Revisions should be ordred by revision_id but we ensure that here |
1136
|
|
|
revisions = sorted(self.revisions, key=lambda revision: revision.revision_id) |
1137
|
|
|
return revisions[-1] |
1138
|
|
|
|
1139
|
|
|
def new_revision(self) -> ContentRevisionRO: |
1140
|
|
|
""" |
1141
|
|
|
Return and assign to this content a new revision. |
1142
|
|
|
If it's a new content, revision is totally new. |
1143
|
|
|
If this content already own revision, revision is build from last revision. |
1144
|
|
|
:return: |
1145
|
|
|
""" |
1146
|
|
|
if not self.revisions: |
1147
|
|
|
self.revisions.append(ContentRevisionRO()) |
1148
|
|
|
return self.revisions[0] |
1149
|
|
|
|
1150
|
|
|
new_rev = ContentRevisionRO.new_from(self.get_current_revision()) |
1151
|
|
|
self.revisions.append(new_rev) |
1152
|
|
|
return new_rev |
1153
|
|
|
|
1154
|
|
|
def get_valid_children(self, content_types: list=None) -> ['Content']: |
|
|
|
|
1155
|
|
|
for child in self.children: |
1156
|
|
|
if not child.is_deleted and not child.is_archived: |
1157
|
|
|
if not content_types or child.type in content_types: |
1158
|
|
|
yield child.node |
1159
|
|
|
|
1160
|
|
|
@hybrid_property |
1161
|
|
|
def properties(self) -> dict: |
1162
|
|
|
""" return a structure decoded from json content of _properties """ |
1163
|
|
|
if not self._properties: |
1164
|
|
|
return DEFAULT_PROPERTIES |
1165
|
|
|
return json.loads(self._properties) |
1166
|
|
|
|
1167
|
|
|
@properties.setter |
1168
|
|
|
def properties(self, properties_struct: dict) -> None: |
1169
|
|
|
""" encode a given structure into json and store it in _properties attribute""" |
1170
|
|
|
self._properties = json.dumps(properties_struct) |
1171
|
|
|
ContentChecker.check_properties(self) |
1172
|
|
|
|
1173
|
|
|
def created_as_delta(self, delta_from_datetime:datetime=None): |
|
|
|
|
1174
|
|
|
if not delta_from_datetime: |
1175
|
|
|
delta_from_datetime = datetime.utcnow() |
1176
|
|
|
|
1177
|
|
|
return format_timedelta(delta_from_datetime - self.created, |
1178
|
|
|
locale=get_locale()) |
1179
|
|
|
|
1180
|
|
|
def datetime_as_delta(self, datetime_object, |
|
|
|
|
1181
|
|
|
delta_from_datetime:datetime=None): |
|
|
|
|
1182
|
|
|
if not delta_from_datetime: |
1183
|
|
|
delta_from_datetime = datetime.utcnow() |
1184
|
|
|
return format_timedelta(delta_from_datetime - datetime_object, |
1185
|
|
|
locale=get_locale()) |
1186
|
|
|
|
1187
|
|
|
def get_child_nb(self, content_type: ContentType, content_status = ''): |
|
|
|
|
1188
|
|
|
child_nb = 0 |
1189
|
|
|
for child in self.get_valid_children(): |
1190
|
|
|
if child.type == content_type or content_type == ContentType.Any: |
1191
|
|
|
if not content_status: |
1192
|
|
|
child_nb = child_nb+1 |
1193
|
|
|
elif content_status==child.status: |
|
|
|
|
1194
|
|
|
child_nb = child_nb+1 |
1195
|
|
|
return child_nb |
1196
|
|
|
|
1197
|
|
|
def get_label(self): |
|
|
|
|
1198
|
|
|
return self.label or self.file_name or '' |
1199
|
|
|
|
1200
|
|
|
def get_label_as_file(self) -> str: |
1201
|
|
|
""" |
1202
|
|
|
:return: Return content label in file representation context |
1203
|
|
|
""" |
1204
|
|
|
return self.revision.get_label_as_file() |
1205
|
|
|
|
1206
|
|
|
def get_status(self) -> ContentStatus: |
|
|
|
|
1207
|
|
|
return ContentStatus( |
1208
|
|
|
self.status, |
1209
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
1210
|
|
|
# self.type.__str__() |
1211
|
|
|
) |
1212
|
|
|
|
1213
|
|
|
def get_last_action(self) -> ActionDescription: |
|
|
|
|
1214
|
|
|
return ActionDescription(self.revision_type) |
1215
|
|
|
|
1216
|
|
|
def get_last_activity_date(self) -> datetime_root.datetime: |
|
|
|
|
1217
|
|
|
last_revision_date = self.updated |
1218
|
|
|
for revision in self.revisions: |
1219
|
|
|
if revision.updated > last_revision_date: |
1220
|
|
|
last_revision_date = revision.updated |
1221
|
|
|
|
1222
|
|
|
for child in self.children: |
1223
|
|
|
if child.updated > last_revision_date: |
1224
|
|
|
last_revision_date = child.updated |
1225
|
|
|
return last_revision_date |
1226
|
|
|
|
1227
|
|
|
def has_new_information_for(self, user: User) -> bool: |
1228
|
|
|
""" |
1229
|
|
|
:param user: the _session current user |
1230
|
|
|
:return: bool, True if there is new information for given user else False |
1231
|
|
|
False if the user is None |
1232
|
|
|
""" |
1233
|
|
|
revision = self.get_current_revision() |
1234
|
|
|
|
1235
|
|
|
if not user: |
1236
|
|
|
return False |
1237
|
|
|
|
1238
|
|
|
if user not in revision.read_by.keys(): |
1239
|
|
|
# The user did not read this item, so yes! |
1240
|
|
|
return True |
1241
|
|
|
|
1242
|
|
|
for child in self.get_valid_children(): |
1243
|
|
|
if child.has_new_information_for(user): |
1244
|
|
|
return True |
1245
|
|
|
|
1246
|
|
|
return False |
1247
|
|
|
|
1248
|
|
|
def get_comments(self): |
|
|
|
|
1249
|
|
|
children = [] |
1250
|
|
|
for child in self.children: |
1251
|
|
|
if ContentType.Comment==child.type and not child.is_deleted and not child.is_archived: |
|
|
|
|
1252
|
|
|
children.append(child.node) |
1253
|
|
|
return children |
1254
|
|
|
|
1255
|
|
|
def get_last_comment_from(self, user: User) -> 'Content': |
|
|
|
|
1256
|
|
|
# TODO - Make this more efficient |
|
|
|
|
1257
|
|
|
last_comment_updated = None |
1258
|
|
|
last_comment = None |
1259
|
|
|
for comment in self.get_comments(): |
1260
|
|
|
if user.user_id == comment.owner.user_id: |
1261
|
|
|
if not last_comment or last_comment_updated<comment.updated: |
|
|
|
|
1262
|
|
|
# take only the latest comment ! |
1263
|
|
|
last_comment = comment |
1264
|
|
|
last_comment_updated = comment.updated |
1265
|
|
|
|
1266
|
|
|
return last_comment |
1267
|
|
|
|
1268
|
|
|
def get_previous_revision(self) -> 'ContentRevisionRO': |
|
|
|
|
1269
|
|
|
rev_ids = [revision.revision_id for revision in self.revisions] |
1270
|
|
|
rev_ids.sort() |
1271
|
|
|
|
1272
|
|
|
if len(rev_ids)>=2: |
|
|
|
|
1273
|
|
|
revision_rev_id = rev_ids[-2] |
1274
|
|
|
|
1275
|
|
|
for revision in self.revisions: |
1276
|
|
|
if revision.revision_id == revision_rev_id: |
1277
|
|
|
return revision |
1278
|
|
|
|
1279
|
|
|
return None |
1280
|
|
|
|
1281
|
|
|
def description_as_raw_text(self): |
|
|
|
|
1282
|
|
|
# 'html.parser' fixes a hanging bug |
1283
|
|
|
# see http://stackoverflow.com/questions/12618567/problems-running-beautifulsoup4-within-apache-mod-python-django |
|
|
|
|
1284
|
|
|
return BeautifulSoup(self.description, 'html.parser').text |
1285
|
|
|
|
1286
|
|
|
def get_allowed_content_types(self): |
|
|
|
|
1287
|
|
|
types = [] |
1288
|
|
|
try: |
1289
|
|
|
allowed_types = self.properties['allowed_content'] |
1290
|
|
|
for type_label, is_allowed in allowed_types.items(): |
1291
|
|
|
if is_allowed: |
1292
|
|
|
types.append(ContentType(type_label)) |
1293
|
|
|
except Exception as e: |
|
|
|
|
1294
|
|
|
print(e.__str__()) |
1295
|
|
|
print('----- /*\ *****') |
|
|
|
|
1296
|
|
|
raise ValueError('Not allowed content property') |
1297
|
|
|
|
1298
|
|
|
return ContentType.sorted(types) |
1299
|
|
|
|
1300
|
|
|
def get_history(self, drop_empty_revision=False) -> '[VirtualEvent]': |
|
|
|
|
1301
|
|
|
events = [] |
1302
|
|
|
for comment in self.get_comments(): |
1303
|
|
|
events.append(VirtualEvent.create_from_content(comment)) |
1304
|
|
|
|
1305
|
|
|
revisions = sorted(self.revisions, key=lambda rev: rev.revision_id) |
1306
|
|
|
for revision in revisions: |
1307
|
|
|
# INFO - G.M - 09-03-2018 - Do not show file revision with empty |
1308
|
|
|
# file to have a more clear view of revision. |
1309
|
|
|
# Some webdav client create empty file before uploading, we must |
1310
|
|
|
# have possibility to not show the related revision |
1311
|
|
|
if drop_empty_revision: |
1312
|
|
|
if revision.depot_file and revision.depot_file.file.content_length == 0: # nopep8 |
1313
|
|
|
# INFO - G.M - 12-03-2018 -Always show the last and |
1314
|
|
|
# first revision. |
1315
|
|
|
if revision != revisions[-1] and revision != revisions[0]: |
1316
|
|
|
continue |
1317
|
|
|
|
1318
|
|
|
events.append(VirtualEvent.create_from_content_revision(revision)) |
1319
|
|
|
|
1320
|
|
|
sorted_events = sorted(events, |
1321
|
|
|
key=lambda event: event.created, reverse=True) |
1322
|
|
|
return sorted_events |
1323
|
|
|
|
1324
|
|
|
@classmethod |
1325
|
|
|
def format_path(cls, url_template: str, content: 'Content') -> str: |
|
|
|
|
1326
|
|
|
wid = content.workspace.workspace_id |
1327
|
|
|
fid = content.parent_id # May be None if no parent |
1328
|
|
|
ctype = content.type |
1329
|
|
|
cid = content.content_id |
1330
|
|
|
return url_template.format(wid=wid, fid=fid, ctype=ctype, cid=cid) |
1331
|
|
|
|
1332
|
|
|
def copy(self, parent): |
|
|
|
|
1333
|
|
|
cpy_content = Content() |
1334
|
|
|
for rev in self.revisions: |
1335
|
|
|
cpy_rev = ContentRevisionRO.copy(rev, parent) |
1336
|
|
|
cpy_content.revisions.append(cpy_rev) |
1337
|
|
|
return cpy_content |
1338
|
|
|
|
1339
|
|
|
|
1340
|
|
|
class RevisionReadStatus(DeclarativeBase): |
|
|
|
|
1341
|
|
|
|
1342
|
|
|
__tablename__ = 'revision_read_status' |
1343
|
|
|
|
1344
|
|
|
revision_id = Column(Integer, ForeignKey('content_revisions.revision_id', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True) |
|
|
|
|
1345
|
|
|
user_id = Column(Integer, ForeignKey('users.user_id', ondelete='CASCADE', onupdate='CASCADE'), primary_key=True) |
|
|
|
|
1346
|
|
|
# Default value datetime.utcnow, see: http://stackoverflow.com/a/13370382/801924 (or http://pastebin.com/VLyWktUn) |
|
|
|
|
1347
|
|
|
view_datetime = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow) |
1348
|
|
|
|
1349
|
|
|
content_revision = relationship( |
1350
|
|
|
'ContentRevisionRO', |
1351
|
|
|
backref=backref( |
1352
|
|
|
'revision_read_statuses', |
1353
|
|
|
collection_class=attribute_mapped_collection('user'), |
1354
|
|
|
cascade='all, delete-orphan' |
1355
|
|
|
)) |
1356
|
|
|
|
1357
|
|
|
user = relationship('User', backref=backref( |
1358
|
|
|
'revision_readers', |
1359
|
|
|
collection_class=attribute_mapped_collection('view_datetime'), |
1360
|
|
|
cascade='all, delete-orphan' |
1361
|
|
|
)) |
1362
|
|
|
|
1363
|
|
|
|
1364
|
|
|
class NodeTreeItem(object): |
|
|
|
|
1365
|
|
|
""" |
1366
|
|
|
This class implements a model that allow to simply represents |
1367
|
|
|
the left-panel menu items. This model is used by dbapi but |
1368
|
|
|
is not directly related to sqlalchemy and database |
1369
|
|
|
""" |
1370
|
|
|
def __init__( |
1371
|
|
|
self, |
|
|
|
|
1372
|
|
|
node: Content, |
|
|
|
|
1373
|
|
|
children: typing.List['NodeTreeItem'], |
|
|
|
|
1374
|
|
|
is_selected: bool = False, |
|
|
|
|
1375
|
|
|
): |
1376
|
|
|
self.node = node |
1377
|
|
|
self.children = children |
1378
|
|
|
self.is_selected = is_selected |
1379
|
|
|
|
1380
|
|
|
|
1381
|
|
|
class VirtualEvent(object): |
|
|
|
|
1382
|
|
|
@classmethod |
1383
|
|
|
def create_from(cls, object): |
|
|
|
|
1384
|
|
|
if Content == object.__class__: |
1385
|
|
|
return cls.create_from_content(object) |
1386
|
|
|
elif ContentRevisionRO == object.__class__: |
1387
|
|
|
return cls.create_from_content_revision(object) |
1388
|
|
|
|
1389
|
|
|
@classmethod |
1390
|
|
|
def create_from_content(cls, content: Content): |
|
|
|
|
1391
|
|
|
content_type = ContentType(content.type) |
1392
|
|
|
|
1393
|
|
|
label = content.get_label() |
1394
|
|
|
if content.type == ContentType.Comment: |
1395
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Remove label param |
|
|
|
|
1396
|
|
|
# from this object ? |
1397
|
|
|
label = l_('<strong>{}</strong> wrote:').format(content.owner.get_display_name()) |
1398
|
|
|
|
1399
|
|
|
return VirtualEvent(id=content.content_id, |
1400
|
|
|
created=content.created, |
1401
|
|
|
owner=content.owner, |
1402
|
|
|
type=content_type, |
1403
|
|
|
label=label, |
1404
|
|
|
content=content.description, |
1405
|
|
|
ref_object=content) |
1406
|
|
|
|
1407
|
|
|
@classmethod |
1408
|
|
|
def create_from_content_revision(cls, revision: ContentRevisionRO): |
|
|
|
|
1409
|
|
|
action_description = ActionDescription(revision.revision_type) |
1410
|
|
|
|
1411
|
|
|
return VirtualEvent(id=revision.revision_id, |
1412
|
|
|
created=revision.updated, |
1413
|
|
|
owner=revision.owner, |
1414
|
|
|
type=action_description, |
1415
|
|
|
label=action_description.label, |
|
|
|
|
1416
|
|
|
content='', |
1417
|
|
|
ref_object=revision) |
1418
|
|
|
|
1419
|
|
|
def __init__(self, id, created, owner, type, label, content, ref_object): |
|
|
|
|
1420
|
|
|
self.id = id |
|
|
|
|
1421
|
|
|
self.created = created |
1422
|
|
|
self.owner = owner |
1423
|
|
|
self.type = type |
1424
|
|
|
self.label = label |
1425
|
|
|
self.content = content |
1426
|
|
|
self.ref_object = ref_object |
1427
|
|
|
|
1428
|
|
|
assert hasattr(type, 'id') |
1429
|
|
|
# TODO - G.M - 10-04-2018 - [Cleanup] Drop this |
|
|
|
|
1430
|
|
|
# assert hasattr(type, 'css') |
1431
|
|
|
# assert hasattr(type, 'icon') |
1432
|
|
|
# assert hasattr(type, 'label') |
1433
|
|
|
|
1434
|
|
|
def created_as_delta(self, delta_from_datetime:datetime=None): |
|
|
|
|
1435
|
|
|
if not delta_from_datetime: |
1436
|
|
|
delta_from_datetime = datetime.utcnow() |
1437
|
|
|
return format_timedelta(delta_from_datetime - self.created, |
1438
|
|
|
locale=get_locale()) |
1439
|
|
|
|
1440
|
|
|
def create_readable_date(self, delta_from_datetime:datetime=None): |
|
|
|
|
1441
|
|
|
aff = '' |
1442
|
|
|
|
1443
|
|
|
if not delta_from_datetime: |
1444
|
|
|
delta_from_datetime = datetime.utcnow() |
1445
|
|
|
|
1446
|
|
|
delta = delta_from_datetime - self.created |
1447
|
|
|
|
|
|
|
|
1448
|
|
|
if delta.days > 0: |
1449
|
|
|
if delta.days >= 365: |
1450
|
|
|
aff = '%d year%s ago' % (delta.days/365, 's' if delta.days/365>=2 else '') |
|
|
|
|
1451
|
|
|
elif delta.days >= 30: |
1452
|
|
|
aff = '%d month%s ago' % (delta.days/30, 's' if delta.days/30>=2 else '') |
|
|
|
|
1453
|
|
|
else: |
1454
|
|
|
aff = '%d day%s ago' % (delta.days, 's' if delta.days>=2 else '') |
|
|
|
|
1455
|
|
|
else: |
1456
|
|
|
if delta.seconds < 60: |
1457
|
|
|
aff = '%d second%s ago' % (delta.seconds, 's' if delta.seconds>1 else '') |
|
|
|
|
1458
|
|
|
elif delta.seconds/60 < 60: |
1459
|
|
|
aff = '%d minute%s ago' % (delta.seconds/60, 's' if delta.seconds/60>=2 else '') |
|
|
|
|
1460
|
|
|
else: |
1461
|
|
|
aff = '%d hour%s ago' % (delta.seconds/3600, 's' if delta.seconds/3600>=2 else '') |
|
|
|
|
1462
|
|
|
|
1463
|
|
|
return aff |
1464
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.