-
Notifications
You must be signed in to change notification settings - Fork 9
/
fixtures.py
75 lines (56 loc) · 1.97 KB
/
fixtures.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
import os
from flask import (
Blueprint,
json
)
from flask_login import login_user as ext_login_user
from social_flask_mongoengine.models import FlaskStorage
from models import (Cluster, Tags, TaggingSpeed, User, Pages, Shred,
ShredsDistances)
from utils import handle_exception_as_json
mod = Blueprint('fixtures', __name__, url_prefix='/fixtures')
FIXTURES_DIR = os.path.join(os.path.dirname(__file__), 'fixtures')
def _import_from_file(fname, model):
id_fieldname = model.id.name
with open(os.path.join(FIXTURES_DIR, fname), "r") as f:
objs = json.load(f)
for obj in objs:
if id_fieldname in obj:
try:
model.objects(pk=obj[id_fieldname]).get()
except model.DoesNotExist:
model.objects.create(**obj)
else:
model.objects.create(**obj)
@mod.route("/reset_db", methods=["POST"])
@handle_exception_as_json()
def reset_db():
Tags.objects.delete()
TaggingSpeed.objects.delete()
Cluster.objects.delete()
Shred.objects.delete()
ShredsDistances.objects.delete()
User.objects.delete()
Pages.objects.delete()
FlaskStorage.user.objects.delete()
FlaskStorage.nonce.objects.delete()
FlaskStorage.association.objects.delete()
FlaskStorage.code.objects.delete()
@mod.route("/create_users", methods=["POST"])
@handle_exception_as_json()
def create_users():
_import_from_file("users.json", User)
@mod.route("/create_base_tags", methods=["POST"])
@handle_exception_as_json()
def create_base_tags():
_import_from_file("base_tags.json", Tags)
@mod.route("/create_shreds", methods=["POST"])
@handle_exception_as_json()
def create_shreds():
_import_from_file("shreds.json", Shred)
_import_from_file("clusters.json", Cluster)
@mod.route("/login_user/<string:username>", methods=["POST"])
@handle_exception_as_json()
def login_user(username):
usr = User.objects.get(username=username)
ext_login_user(usr)