-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit_db.py
82 lines (70 loc) · 2.77 KB
/
init_db.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
# Initialize database.
from cit import create_app
from cit.db import db
from mixer.backend.sqlalchemy import Mixer
from mixer.backend.flask import mixer
from cit.auth.models import User
from cit.organizations.models import Organization
from cit.issues.models import Issue, Photo
from cit.comments.models import Comment
import sys
import argparse
class MyOwnMixer(Mixer):
def populate_target(self, values):
target = self.__scheme(**values)
return target
mixer = MyOwnMixer()
class InitDB():
def __init__(self, app):
self.app = app
def create_parser(self):
parser = argparse.ArgumentParser()
parser.add_argument('--make-admin', action='store', default='')
args = parser.parse_args()
return parser
def generate_test_data(self):
with self.app.app_context():
organization = mixer.blend(Organization,
name=mixer.RANDOM,
address='POINT(49.836134 24.023151)')
db.session.add(organization)
user = mixer.blend(User,
fb_first_name=mixer.RANDOM,
fb_last_name=mixer.RANDOM,
fb_id=mixer.RANDOM,
email=mixer.RANDOM,
about_me=mixer.RANDOM)
user = User(user.fb_first_name, user.fb_last_name, user.fb_id,
user.email, user.about_me)
user.organizations.append(organization)
db.session.add(user)
db.session.commit()
issue = mixer.blend(Issue,
reporter='1',
description=mixer.RANDOM,
coordinates='POINT(49.839357 24.028398)')
db.session.add(issue)
comment = mixer.blend(Comment,
author=user,
issue=issue,
message=mixer.RANDOM)
db.session.add(comment)
photo = mixer.blend(Photo,
issue=issue,
file_path=mixer.RANDOM)
db.session.add(photo)
db.session.commit()
def make_user_as_admin(self, user_id):
with self.app.app_context():
db.session.query(User).filter(User.fb_id == user_id).\
update({'is_superuser': True})
db.session.commit()
if __name__ == '__main__':
app = create_app()
init_db = InitDB(app)
parser = init_db.create_parser()
namespace = parser.parse_args(sys.argv[1:])
if namespace.make_admin:
init_db.make_user_as_admin(namespace.make_admin)
else:
init_db.generate_test_data()