forked from haiwen/seahub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
178 lines (146 loc) · 5.62 KB
/
handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Copyright (c) 2012-2016 Seafile Ltd.
import logging
from . import settings
import datetime
from seaserv import seafile_api, get_org_id_by_repo_id
logger = logging.getLogger(__name__)
try:
from seafevents import seafevents_api
def repo_created_cb(sender, **kwargs):
org_id = kwargs['org_id']
creator = kwargs['creator']
repo_id = kwargs['repo_id']
repo_name = kwargs['repo_name']
# Move here to avoid model import during Django setup.
# TODO: Don't register signal/handlers during Seahub start.
if org_id and org_id > 0:
related_users = seafile_api.org_get_shared_users_by_repo(org_id, repo_id)
else:
related_users = seafile_api.get_shared_users_by_repo(repo_id)
org_id = -1
if creator not in related_users:
related_users.append(creator)
record = {
'op_type': 'create',
'obj_type': 'repo',
'timestamp': datetime.datetime.utcnow(),
'repo_id': repo_id,
'repo_name': repo_name,
'path': '/',
'op_user': creator,
'related_users': related_users,
'org_id': org_id,
}
from .utils import SeafEventsSession
session = SeafEventsSession()
seafevents_api.save_user_activity(session, record)
session.close()
LIBRARY_TEMPLATES = getattr(settings, 'LIBRARY_TEMPLATES', {})
library_template = kwargs['library_template']
if LIBRARY_TEMPLATES and library_template:
try:
dir_path_list = LIBRARY_TEMPLATES[library_template]
for dir_path in dir_path_list:
seafile_api.mkdir_with_parents(repo_id, '/', dir_path.strip('/'), creator)
except Exception as e:
logger.error(e)
def repo_deleted_cb(sender, **kwargs):
"""When a repo is deleted, an event would be added to every user in all
groups to which this repo is shared.
"""
org_id = kwargs['org_id']
operator = kwargs['operator']
repo_owner = kwargs['repo_owner']
repo_id = kwargs['repo_id']
repo_name = kwargs['repo_name']
if org_id and org_id > 0:
related_users = seafile_api.org_get_shared_users_by_repo(org_id, repo_id)
else:
related_users = seafile_api.get_shared_users_by_repo(repo_id)
org_id = -1
if repo_owner not in related_users:
related_users.append(repo_owner)
record = {
'op_type': 'delete',
'obj_type': 'repo',
'timestamp': datetime.datetime.utcnow(),
'repo_id': repo_id,
'repo_name': repo_name,
'path': '/',
'op_user': operator,
'related_users': related_users,
'org_id': org_id if org_id and org_id > 0 else -1,
}
from .utils import SeafEventsSession
session = SeafEventsSession()
seafevents_api.save_user_activity(session, record)
session.close()
def clean_up_repo_trash_cb(sender, **kwargs):
"""When a repo trash is deleted, the operator will be recorded.
"""
org_id = kwargs['org_id']
operator = kwargs['operator']
repo_id = kwargs['repo_id']
days = kwargs.get('days', None)
repo_name = kwargs['repo_name']
repo_owner = kwargs['repo_owner']
if org_id and org_id > 0:
related_users = seafile_api.org_get_shared_users_by_repo(org_id, repo_id)
else:
related_users = seafile_api.get_shared_users_by_repo(repo_id)
org_id = -1
if repo_owner not in related_users:
related_users.append(repo_owner)
record = {
'op_type': 'clean-up-trash',
'obj_type': 'repo',
'timestamp': datetime.datetime.utcnow(),
'repo_id': repo_id,
'repo_name': repo_name,
'path': '/',
'days': days,
'op_user': operator,
'related_users': related_users,
'org_id': org_id,
}
from .utils import SeafEventsSession
session = SeafEventsSession()
seafevents_api.save_user_activity(session, record)
session.close()
def repo_restored_cb(sender, **kwargs):
repo_id = kwargs['repo_id']
operator = kwargs['operator']
repo = seafile_api.get_repo(repo_id)
org_id = get_org_id_by_repo_id(repo_id)
if org_id and org_id > 0:
related_users = seafile_api.org_get_shared_users_by_repo(org_id, repo_id)
repo_owner = seafile_api.get_org_repo_owner(repo_id)
else:
related_users = seafile_api.get_shared_users_by_repo(repo_id)
repo_owner = seafile_api.get_repo_owner(repo_id)
if repo_owner not in related_users:
related_users.append(repo_owner)
record = {
'op_type': 'recover',
'obj_type': 'repo',
'timestamp': datetime.datetime.utcnow(),
'repo_id': repo_id,
'repo_name': repo.repo_name,
'path': '/',
'op_user': operator,
'related_users': related_users,
'org_id': org_id,
}
from .utils import SeafEventsSession
session = SeafEventsSession()
seafevents_api.save_user_activity(session, record)
session.close()
except ImportError:
def repo_created_cb(sender, **kwargs):
pass
def repo_restored_cb(sender, **kwargs):
pass
def repo_deleted_cb(sender, **kwargs):
pass
def clean_up_repo_trash_cb(sender, **kwargs):
pass