Debian Codegraph @

tests/mockapp/migrations/0004_load_fixtures.py

raw
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 import os
5
6 from django.core import serializers
7 from django.db import models, migrations
8
9
10 def load_data(apps, schema_editor):
11 """
12 Load fixtures for MockPerson, MockPet and MockLocation
13 """
14
15 fixtures = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, "fixtures"))
16
17 with open(os.path.join(fixtures, "mockperson.json"), "r") as fixture:
18 objects = serializers.deserialize("json", fixture, ignorenonexistent=True)
19 for obj in objects:
20 obj.save()
21
22 with open(os.path.join(fixtures, "mocklocation.json"), "r") as fixture:
23 objects = serializers.deserialize("json", fixture, ignorenonexistent=True)
24 for obj in objects:
25 obj.save()
26
27 with open(os.path.join(fixtures, "mockpet.json"), "r") as fixture:
28 objects = serializers.deserialize("json", fixture, ignorenonexistent=True)
29 for obj in objects:
30 obj.save()
31
32 def unload_data(apps, schema_editor):
33 """
34 Unload fixtures for MockPerson, MockPet and MockLocation
35 """
36
37 MockPerson = apps.get_model("mockapp", "MockPerson")
38 MockLocation = apps.get_model("mockapp", "MockLocation")
39 MockPet = apps.get_model("mockapp", "MockPet")
40
41 MockPerson.objects.all().delete()
42 MockLocation.objects.all().delete()
43 MockPet.objects.all().delete()
44
45
46 class Migration(migrations.Migration):
47
48 dependencies = [
49 ("mockapp", "0001_initial"),
50 ("mockapp", "0002_mockperson"),
51 ("mockapp", "0005_mockperson_birthdate"),
52 ("mockapp", "0003_mockpet"),
53 ]
54
55 operations = [
56 migrations.RunPython(load_data, reverse_code=unload_data)
57 ]