-
Notifications
You must be signed in to change notification settings - Fork 31
/
manage.py
146 lines (110 loc) · 4.16 KB
/
manage.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
#!/usr/bin/env python
from flask_script import Manager
from superdesk import get_resource_service
from newsroom.web import NewsroomWebApp
from newsroom.elastic_utils import rebuild_elastic_index
from newsroom.mongo_utils import index_elastic_from_mongo, index_elastic_from_mongo_from_timestamp
from newsroom.auth import get_user_by_email
from newsroom.company_expiry_alerts import CompanyExpiryAlerts
from newsroom.monitoring .email_alerts import MonitoringEmailAlerts
from newsroom.data_updates import GenerateUpdate, Upgrade, get_data_updates_files, Downgrade
import content_api
app = NewsroomWebApp()
manager = Manager(app)
@manager.command
def create_user(email, password, first_name, last_name, is_admin):
new_user = {
'email': email,
'password': password,
'first_name': first_name,
'last_name': last_name,
'user_type': 'administrator' if is_admin else 'public',
'is_enabled': True,
'is_approved': True
}
with app.test_request_context('/users', method='POST'):
user = get_user_by_email(email)
if user:
print('user already exists %s' % str(new_user))
else:
print('creating user %s' % str(new_user))
get_resource_service('users').post([new_user])
print('user saved %s' % (new_user))
return new_user
@manager.command
def elastic_rebuild():
rebuild_elastic_index()
@manager.command
def elastic_init():
app.data.init_elastic(app)
@manager.option('-h', '--hours', dest='hours', default=None)
@manager.option('-c', '--collection', dest='collection', default=None)
@manager.option('-t', '--timestamp', dest='timestamp', default=None)
@manager.option('-d', '--direction', dest='direction', choices=['older', 'newer'], default='older')
def index_from_mongo(hours, collection, timestamp, direction):
print('Checking if elastic index exists, a new one will be created if not')
app.data.init_elastic(app)
print('Elastic index check has been completed')
if timestamp:
index_elastic_from_mongo_from_timestamp(collection, timestamp, direction)
else:
index_elastic_from_mongo(hours=hours, collection=collection)
@manager.command
def content_reset():
app.data.remove('items')
app.data.remove('items_versions')
@manager.command
def send_company_expiry_alerts():
CompanyExpiryAlerts().send_alerts()
@manager.command
def send_monitoring_schedule_alerts():
MonitoringEmailAlerts().run()
@manager.command
def send_monitoring_immediate_alerts():
MonitoringEmailAlerts().run(True)
@manager.option('-m', '--expiry', dest='expiry_days', required=False)
def remove_expired(expiry_days):
exp = content_api.RemoveExpiredItems()
exp.run(expiry_days)
@manager.option('-r', '--resource', dest='resource', required=True)
def data_generate_update(resource):
cmd = GenerateUpdate()
cmd.run(resource)
@manager.option(
'-i', '--id', dest='data_update_id',
required=False, choices=get_data_updates_files(strip_file_extension=True),
help='Data update id to run last'
)
@manager.option(
'-f', '--fake-init', dest='fake',
required=False, action='store_true',
help='Mark data updates as run without actually running them'
)
@manager.option(
'-d', '--dry-run', dest='dry',
required=False, action='store_true',
help='Does not mark data updates as done. This can be useful for development.'
)
def data_upgrade(data_update_id=None, fake=False, dry=False):
cmd = Upgrade()
cmd.run(data_update_id, fake, dry)
@manager.option(
'-i', '--id', dest='data_update_id',
required=False, choices=get_data_updates_files(strip_file_extension=True),
help='Data update id to run last'
)
@manager.option(
'-f', '--fake-init', dest='fake',
required=False, action='store_true',
help='Mark data updates as run without actually running them'
)
@manager.option(
'-d', '--dry-run', dest='dry',
required=False, action='store_true',
help='Does not mark data updates as done. This can be useful for development.'
)
def data_downgrade(data_update_id=None, fake=False, dry=False):
cmd = Downgrade()
cmd.run(data_update_id, fake, dry)
if __name__ == "__main__":
manager.run()