|
| 1 | +# etna:allowAlterField |
| 2 | + |
| 3 | + |
| 4 | +from django.db import migrations |
| 5 | + |
| 6 | +KEY_STAGES = [ |
| 7 | + ("key-stage-1", "Key stage 1", 1, "5-7"), |
| 8 | + ("key-stage-2", "Key stage 2", 2, "7-11"), |
| 9 | + ("key-stage-3", "Key stage 3", 3, "11-14"), |
| 10 | + ("key-stage-4", "Key stage 4", 4, "14-16"), |
| 11 | + ("key-stage-5", "Key stage 5", 5, "16-18"), |
| 12 | +] |
| 13 | + |
| 14 | +THEMES = [ |
| 15 | + ("power-politics-and-reform", "Power, politics and reform"), |
| 16 | + ("war-and-revolution", "War and revolution"), |
| 17 | + ("migration-and-identity", "Migration and identity"), |
| 18 | + ("empire-and-decolonisation", "Empire and decolonisation"), |
| 19 | + ("crime-and-punishment", "Crime and punishment"), |
| 20 | + ("medicine-welfare-and-society", "Medicine, welfare and society"), |
| 21 | + ("family-community-and-everyday-life", "Family, community and everyday life"), |
| 22 | + ("economy-trade-and-industry", "Economy, trade and industry"), |
| 23 | + ("archive-skills", "Archive skills"), |
| 24 | + ("local-history", "Local history"), |
| 25 | + ("womens-history", "Women's history"), |
| 26 | + ("black-asian-and-global-majority-history", "Black, Asian and global majority history"), |
| 27 | + ("lgbtq-history", "LGBTQ+ history"), |
| 28 | + ("disability-history", "Disability history"), |
| 29 | + ("significant-people-places-and-events", "Significant people, places and events"), |
| 30 | +] |
| 31 | + |
| 32 | +# Tuple: (slug, name, year_from, year_to, available_for_resources) |
| 33 | +TIME_PERIODS = [ |
| 34 | + ("early-civilisations", "Early civilisations", None, 900, False), |
| 35 | + ("medieval", "Medieval", 900, 1485, True), |
| 36 | + ("early-modern", "Early modern", 1485, 1750, True), |
| 37 | + ("industrial-revolution", "Industrial revolution", 1750, 1901, True), |
| 38 | + ("early-twentieth-century", "Early Twentieth Century", 1901, 1945, True), |
| 39 | + ("post-war-and-modern", "Post-War and modern", 1945, None, True), |
| 40 | + ("cross-period", "Cross period", None, None, True), |
| 41 | +] |
| 42 | + |
| 43 | + |
| 44 | +def seed_key_stages(apps, schema_editor): |
| 45 | + KeyStage = apps.get_model("education", "KeyStage") |
| 46 | + key_stage_field_names = {field.name for field in KeyStage._meta.fields} |
| 47 | + |
| 48 | + for slug, name, stage, age_range in KEY_STAGES: |
| 49 | + defaults = {"name": name} |
| 50 | + defaults["stage"] = stage |
| 51 | + defaults["age_range"] = age_range |
| 52 | + |
| 53 | + KeyStage.objects.update_or_create( |
| 54 | + slug=slug, |
| 55 | + defaults=defaults, |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def seed_themes(apps, schema_editor): |
| 60 | + Theme = apps.get_model("education", "Theme") |
| 61 | + |
| 62 | + for slug, name in THEMES: |
| 63 | + Theme.objects.update_or_create( |
| 64 | + slug=slug, |
| 65 | + defaults={"name": name}, |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def seed_time_periods(apps, schema_editor): |
| 70 | + TimePeriod = apps.get_model("education", "TimePeriod") |
| 71 | + time_period_field_names = {field.name for field in TimePeriod._meta.fields} |
| 72 | + |
| 73 | + for slug, name, year_from, year_to, available_for_resources in TIME_PERIODS: |
| 74 | + defaults = {"name": name} |
| 75 | + |
| 76 | + if "year_from" in time_period_field_names: |
| 77 | + defaults["year_from"] = year_from |
| 78 | + |
| 79 | + if "year_to" in time_period_field_names: |
| 80 | + defaults["year_to"] = year_to |
| 81 | + |
| 82 | + if "available_for_resources" in time_period_field_names: |
| 83 | + defaults["available_for_resources"] = available_for_resources |
| 84 | + |
| 85 | + TimePeriod.objects.update_or_create( |
| 86 | + slug=slug, |
| 87 | + defaults=defaults, |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +def seed_education_taxonomies(apps, schema_editor): |
| 92 | + seed_key_stages(apps, schema_editor) |
| 93 | + seed_themes(apps, schema_editor) |
| 94 | + seed_time_periods(apps, schema_editor) |
| 95 | + |
| 96 | + |
| 97 | +def noop_reverse(apps, schema_editor): |
| 98 | + pass |
| 99 | + |
| 100 | + |
| 101 | +class Migration(migrations.Migration): |
| 102 | + dependencies = [ |
| 103 | + ("education", "0001_initial"), |
| 104 | + ] |
| 105 | + |
| 106 | + operations = [ |
| 107 | + migrations.RunPython(seed_education_taxonomies, noop_reverse), |
| 108 | + ] |
0 commit comments