-
Notifications
You must be signed in to change notification settings - Fork 356
Expand file tree
/
Copy pathembargo_registrations.py
More file actions
127 lines (107 loc) · 4.64 KB
/
embargo_registrations.py
File metadata and controls
127 lines (107 loc) · 4.64 KB
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
"""Run nightly, this script will activate any pending embargoes that have
elapsed the pending approval time and make public and registrations whose
embargo end dates have been passed.
"""
import logging
import django
from django.utils import timezone
from django.db import transaction
django.setup()
# init_app must be called before sentry is imported
from website.app import init_app
init_app(routes=False)
from framework import sentry
from framework.celery_tasks import app as celery_app
from website import settings
from osf.models import Embargo, Registration
from scripts import utils as scripts_utils
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def main(dry_run=True):
pending_embargoes = Embargo.objects.pending_embargoes()
for embargo in pending_embargoes:
if embargo.should_be_embargoed:
if dry_run:
logger.warning('Dry run mode')
try:
parent_registration = Registration.objects.get(embargo=embargo)
except Registration.DoesNotExist:
logger.error(
f'Embargo {embargo._id} is not attached to a registration'
)
continue
logger.warning(
'Embargo {} approved. Activating embargo for registration {}'
.format(embargo._id, parent_registration._id)
)
if not dry_run:
if parent_registration.is_deleted:
# Clean up any registration failures during archiving
embargo.forcibly_reject()
embargo.save()
continue
sid = transaction.savepoint()
try:
# Call 'accept' trigger directly. This will terminate the embargo
# if the registration is unmoderated or push it into the moderation
# queue if it is part of a moderated registry.
embargo.accept()
transaction.savepoint_commit(sid)
except Exception as err:
root = parent_registration._dirty_root
embargo = root.embargo
logger.error(
f'Unexpected error raised when activating embargo for '
f'registration {parent_registration._id}. Error: {err}',
exc_info=err,
extra={
'is_public': root.is_public,
'approval_stage': embargo.approval_stage.name,
'is_approved': root.embargo.is_approved
}
)
logger.exception(err)
sentry.log_exception(err)
transaction.savepoint_rollback(sid)
active_embargoes = Embargo.objects.active_embargoes()
for embargo in active_embargoes:
if embargo.should_be_completed:
if dry_run:
logger.warning('Dry run mode')
parent_registration = Registration.objects.get(embargo=embargo)
logger.warning(
'Embargo {} complete. Making registration {} public'
.format(embargo._id, parent_registration._id)
)
if not dry_run:
if parent_registration.is_deleted:
# Clean up any registration failures during archiving
embargo.forcibly_reject()
embargo.save()
continue
sid = transaction.savepoint()
try:
parent_registration.terminate_embargo()
transaction.savepoint_commit(sid)
except Exception as err:
root = parent_registration._dirty_root
embargo = root.embargo
logger.error(
f'Registration {parent_registration._id} could not be made public because {str(err)}',
exc_info=err,
extra={
'is_public': root.is_public,
'approval_stage': embargo.approval_stage.name,
'is_approved': root.embargo.is_approved
}
)
logger.exception(err)
sentry.log_exception(err)
transaction.savepoint_rollback(sid)
@celery_app.task(name='scripts.embargo_registrations')
def run_main(dry_run=True):
if not dry_run:
scripts_utils.add_file_logger(logger, __file__)
main(dry_run=dry_run)
if __name__ == '__main__':
main(False)