Skip to content

Commit

Permalink
load fixture as last migration
Browse files Browse the repository at this point in the history
  • Loading branch information
pat1 committed Oct 6, 2023
1 parent 69a8005 commit dd5121d
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 157 deletions.
79 changes: 0 additions & 79 deletions python/rmap/stations/migrations/0013_auto_20230227_1028.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,6 @@
from django.core import serializers

import os
fixture_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../fixtures'))

def load_fixture(apps, schema_editor):

def load_fixture_from_file(fixture_file):
fixture = open(fixture_file, 'rb')
objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
for obj in objects:
obj.save()
fixture.close()

for fixture_filename in ("site_rmap.cc.json",):
if fixture_filename[-5:] == ".json":

fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_file=fixture_dir+"/sensor_type_01.json"
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

for fixture_filename in os.listdir(fixture_dir):
if fixture_filename[:3] == "sta" and fixture_filename[-5:] == ".json":

fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_02.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_03.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_04.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_05.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_06.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_07.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_08.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)



def unload_fixture(apps, schema_editor):
"Brutally deleting all entries for this model..."

MyModel = apps.get_model("stations", "StationMetadata")
MyModel.objects.all().delete()
MyModel = apps.get_model("stations", "Board")
MyModel.objects.all().delete()
MyModel = apps.get_model("stations", "Sensor")
MyModel.objects.all().delete()

class Migration(migrations.Migration):

Expand All @@ -102,8 +27,4 @@ class Migration(migrations.Migration):
name='address',
field=models.PositiveIntegerField(blank=True, default=None, help_text='I2C address (decimal)', null=True),
),
migrations.RunPython(
code=load_fixture,
reverse_code=unload_fixture,
),
]
38 changes: 0 additions & 38 deletions python/rmap/stations/migrations/0033_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,10 @@

from django.db import models, migrations
from django.core import serializers
import os

fixture_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../fixtures'))
#fixture_filename = 'initial_data.json'

def load_fixture(apps, schema_editor):

fixture_filename = "sensor_type_09.json"

fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)

fixture = open(fixture_file, 'rb')
objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
for obj in objects:
obj.save()
fixture.close()


def unload_fixture(apps, schema_editor):
"Brutally deleting all entries for this model..."

#MyModel = apps.get_model("stations", "StationMetadata")
#MyModel.objects.all().delete()
#MyModel = apps.get_model("stations", "Board")
#MyModel.objects.all().delete()
#MyModel = apps.get_model("stations", "Sensor")
#MyModel.objects.all().delete()

# MyModel = apps.get_model("stations", "UserProfile")
# MyModel.objects.get(user=apps.get_model("auth", "User").objects.get(username="rmap")).delete()
# apps.get_model("auth", "User").objects.get(username="rmap").delete()

pass


class Migration(migrations.Migration):

dependencies = [
('stations', '0014_boardmaintstatus'),
]

operations = [
migrations.RunPython(load_fixture, reverse_code=unload_fixture),
]
40 changes: 0 additions & 40 deletions python/rmap/stations/migrations/0035_fixture.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,10 @@
# -*- coding: utf-8 -*-


from django.db import models, migrations
from django.core import serializers
import os

fixture_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../fixtures'))
#fixture_filename = 'initial_data.json'

def load_fixture(apps, schema_editor):

fixture_filename = "template_stations.json"

fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)

fixture = open(fixture_file, 'rb')
objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
for obj in objects:
obj.save()
fixture.close()


def unload_fixture(apps, schema_editor):
"Brutally deleting all entries for this model..."

#MyModel = apps.get_model("stations", "StationMetadata")
#MyModel.objects.all().delete()
#MyModel = apps.get_model("stations", "Board")
#MyModel.objects.all().delete()
#MyModel = apps.get_model("stations", "Sensor")
#MyModel.objects.all().delete()

# MyModel = apps.get_model("stations", "UserProfile")
# MyModel.objects.get(user=apps.get_model("auth", "User").objects.get(username="rmap")).delete()
# apps.get_model("auth", "User").objects.get(username="rmap").delete()

pass


class Migration(migrations.Migration):

dependencies = [
('stations', '0034_auto_20230530_2148'),
]

operations = [
migrations.RunPython(load_fixture, reverse_code=unload_fixture),
]
106 changes: 106 additions & 0 deletions python/rmap/stations/migrations/0037_stationmetadata_type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,108 @@
# Generated by Django 2.2.17 on 2023-10-06 06:57

from django.db import migrations, models
from django.core import serializers
import os

fixture_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../fixtures'))


fixture_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../fixtures'))
#fixture_filename = 'initial_data.json'


def load_fixture(apps, schema_editor):

def load_fixture_from_file(fixture_file):
fixture = open(fixture_file, 'rb')
objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
for obj in objects:
obj.save()
fixture.close()

fixture_file=fixture_dir+"/sensor_type_01.json"
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

for fixture_filename in os.listdir(fixture_dir):
if fixture_filename[:3] == "sta" and fixture_filename[-5:] == ".json":

fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_02.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_03.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_04.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_05.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_06.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_07.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)

fixture_filename = "sensor_type_08.json"
fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)
load_fixture_from_file(fixture_file)


fixture_filename = "sensor_type_09.json"

fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)

fixture = open(fixture_file, 'rb')
objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
for obj in objects:
obj.save()
fixture.close()

fixture_filename = "template_stations.json"

fixture_file = os.path.join(fixture_dir, fixture_filename)
print("load fixture from file: ",fixture_file)

fixture = open(fixture_file, 'rb')
objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
for obj in objects:
obj.save()
fixture.close()






def unload_fixture(apps, schema_editor):
"Brutally deleting all entries for this model..."

MyModel = apps.get_model("stations", "StationMetadata")
MyModel.objects.all().delete()
MyModel = apps.get_model("stations", "Board")
MyModel.objects.all().delete()
MyModel = apps.get_model("stations", "Sensor")
MyModel.objects.all().delete()


class Migration(migrations.Migration):
Expand All @@ -15,4 +117,8 @@ class Migration(migrations.Migration):
name='type',
field=models.CharField(choices=[('unknown', 'Unknow model'), ('manual', 'Human observer'), ('stimav2', 'Stima version 2'), ('stimav3', 'Stima version 3'), ('stimav4', 'Stima version 4')], default='unknown', help_text='Type of the station', max_length=50),
),
migrations.RunPython(
code=load_fixture,
reverse_code=unload_fixture,
),
]

0 comments on commit dd5121d

Please sign in to comment.