File: django_setup.py

package info (click to toggle)
django-cte 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 356 kB
  • sloc: python: 3,245; makefile: 7
file content (76 lines) | stat: -rw-r--r-- 1,882 bytes parent folder | download
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
from django.db import connection

from .models import KeyPair, Region, Order

is_initialized = False


def init_db():
    global is_initialized
    if is_initialized:
        return
    is_initialized = True

    connection.creation.create_test_db(verbosity=0, autoclobber=True)

    setup_data()


def destroy_db():
    connection.creation.destroy_test_db(verbosity=0)


def setup_data():
    regions = {None: None}
    for name, parent in [
        ("sun", None),
        ("mercury", "sun"),
        ("venus", "sun"),
        ("earth", "sun"),
        ("moon", "earth"),
        ("mars", "sun"),
        ("deimos", "mars"),
        ("phobos", "mars"),
        ("proxima centauri", None),
        ("proxima centauri b", "proxima centauri"),
        ("bernard's star", None),
    ]:
        region = Region(name=name, parent=regions[parent])
        region.save()
        regions[name] = region

    for region, amount in [
        ("sun", 1000),
        ("mercury", 10),
        ("mercury", 11),
        ("mercury", 12),
        ("venus", 20),
        ("venus", 21),
        ("venus", 22),
        ("venus", 23),
        ("earth", 30),
        ("earth", 31),
        ("earth", 32),
        ("earth", 33),
        ("moon", 1),
        ("moon", 2),
        ("moon", 3),
        ("mars", 40),
        ("mars", 41),
        ("mars", 42),
        ("proxima centauri", 2000),
        ("proxima centauri b", 10),
        ("proxima centauri b", 11),
        ("proxima centauri b", 12),
    ]:
        order = Order(amount=amount, region=regions[region])
        order.save()

    for key, value, parent in [
        ("level 1", 1, None),
        ("level 2", 1, "level 1"),
        ("level 2", 2, "level 1"),
        ("level 3", 1, "level 2"),
    ]:
        parent = parent and KeyPair.objects.filter(key=parent).first()
        KeyPair.objects.create(key=key, value=value, parent=parent)