-
Notifications
You must be signed in to change notification settings - Fork 2
/
manage.py
172 lines (139 loc) · 4.61 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
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
import csv
import os
import uuid
from flask import current_app
from flask_script import Manager, Shell
from flask_migrate import Migrate, MigrateCommand
from getpass import getpass
from app import create_app, db
from app.models import (
Roles,
Users,
Stories,
Tags,
Comments,
Events,
Modules,
Flags,
Feedback,
)
from app.constants.user_type_auth import AGENCY_USER
from app.constants.module import FEATURED
from app.db_utils import create_object
from app.constants.event_type import EDIT_FEATURED_STORY
from sqlalchemy.orm.exc import NoResultFound
app = create_app(os.getenv("FLASK_CONFIG") or "default")
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context():
return dict(
app=app,
db=db,
Roles=Roles,
Users=Users,
Stories=Stories,
Tags=Tags,
Comments=Comments,
Events=Events,
Modules=Modules,
Flags=Flags,
Feedback=Feedback,
)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command("db", MigrateCommand)
@manager.command
def create_user():
"""
Command line tool to create a user in the database.
:return: message indicating either error or success
"""
email = input("Enter your email: ")
password = getpass("Enter your desired password: ")
confirm_password = getpass("Re-enter your password: ")
if password != confirm_password:
return print("Passwords are not the same. Please try again.")
user = Users(str(uuid.uuid4()), AGENCY_USER, email=email, is_admin=True)
user.set_password(password)
create_object(user)
return print("Successfully created user, " + email)
@manager.command
def deploy():
"""Run deployment tasks"""
from flask_migrate import upgrade
from app.models import Tags
# migrate database to latest revision
upgrade()
# pre-populate
Tags.populate()
es_recreate()
@manager.command
def es_recreate():
"""Recreate elasticsearch index and request docs."""
from app.search.utils import recreate
recreate()
@manager.command
def test():
"""Run the unit tests."""
import unittest
tests = unittest.TestLoader().discover("tests")
unittest.TextTestRunner(verbosity=2).run(tests)
@manager.option(
"-f",
"--featured",
help="Create featured story module.",
action="store_true",
dest="featured",
)
def modules(featured=False):
"""
Manage function modules that inserts a single featured story entry to the modules table.
Takes in a csv file with a story_id and a quote; delimiting by a semicolon.
If current featured story module is_active, set it to false and set new featured story to true.
"""
if featured:
# open csv file
with open(current_app.config["FEATURED_DATA"], "r") as csvfile:
featured_csv = csv.reader(csvfile, delimiter=";")
next(featured_csv) # skip the header
row = next(featured_csv)
story_id = row[0]
quote = row[1]
# disable current featured story
try:
current_featured = Modules.query.filter(
Modules.type == FEATURED, Modules.is_active == True
).one()
except NoResultFound:
current_featured = None
print("No featured module set")
previous_value = None
if current_featured:
current_featured.is_active = False
previous_value = current_featured.val_for_events
db.session.commit()
# create and store object into Modules table
new_featured_story = Stories.query.filter(
Stories.id == story_id, Stories.is_visible == True
).one()
featured_module = Modules(
story_id=story_id,
type=FEATURED,
activist_first=new_featured_story.activist_first,
activist_last=new_featured_story.activist_last,
content=quote,
media_url=new_featured_story.image_url,
is_active=True,
)
create_object(featured_module)
# create and store Events object
edit_featured_event = Events(
_type=EDIT_FEATURED_STORY,
module_id=featured_module.id,
previous_value=previous_value,
new_value=featured_module.val_for_events,
)
create_object(edit_featured_event)
print("New featured story module set")
csvfile.close()
if __name__ == "__main__":
manager.run()