|
| 1 | +# Copyright The IETF Trust 2020, All Rights Reserved |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Generated by Django 1.11.29 on 2020-03-19 13:06 |
| 4 | +from __future__ import unicode_literals |
| 5 | + |
| 6 | +import re |
| 7 | + |
| 8 | +import debug |
| 9 | + |
| 10 | +from django.db import migrations |
| 11 | + |
| 12 | +""" |
| 13 | +This makes me very nervous: |
| 14 | +
|
| 15 | +>>> DocumentURL.objects.filter(desc__icontains='notifications').values_list('tag',flat=True).distinct() |
| 16 | +<QuerySet ['yang-impact-analysis', 'yang-module-metadata']> |
| 17 | +
|
| 18 | +I suspect the wrong thing is happening with the map below wrt the GitHub notificaitons string. |
| 19 | +
|
| 20 | +""" |
| 21 | + |
| 22 | +name_map = { |
| 23 | + "Issue.*": "tracker", |
| 24 | + ".*FAQ.*": "faq", |
| 25 | + ".*Area Web Page": "webpage", |
| 26 | + ".*Wiki": "wiki", |
| 27 | + "Home Page": "webpage", |
| 28 | + "Slack.*": "slack", |
| 29 | + "Additional .* Web Page": "webpage", |
| 30 | + "Additional .* Page": "webpage", |
| 31 | + "Yang catalog entry.*": "yc_entry", |
| 32 | + "Yang impact analysis.*": "yc_impact", |
| 33 | + "GitHub": "github_repo", |
| 34 | + "Github page": "github_repo", |
| 35 | + "GitHub repo.*": "github_repo", |
| 36 | + "Github repository.*": "github_repo", |
| 37 | + "GitHub notifications": "github_notify", |
| 38 | + "GitHub org.*": "github_org", |
| 39 | + "GitHub User.*": "github_username", |
| 40 | + "GitLab User": "gitlab_username", |
| 41 | + "GitLab User Name": "gitlab_username", |
| 42 | +} |
| 43 | + |
| 44 | +def forward(apps, schema_editor): |
| 45 | + DocExtResource = apps.get_model('doc', 'DocExtResource') |
| 46 | + ExtResource = apps.get_model('extresource', 'ExtResource') |
| 47 | + ExtResourceName = apps.get_model('name', 'ExtResourceName') |
| 48 | + DocumentUrl = apps.get_model('doc', 'DocumentUrl') |
| 49 | + |
| 50 | + mapped = 0 |
| 51 | + not_mapped = 0 |
| 52 | + |
| 53 | + for doc_url in DocumentUrl.objects.all(): |
| 54 | + match_found = False |
| 55 | + for regext,slug in name_map.items(): |
| 56 | + if re.match(regext, doc_url.desc): |
| 57 | + match_found = True |
| 58 | + mapped += 1 |
| 59 | + name = ExtResourceName.objects.get(slug=slug) |
| 60 | + ext_res = ExtResource.objects.create(name_id=slug, value= doc_url.url) # TODO: validate this value against name.type |
| 61 | + DocExtResource.objects.create(doc=doc_url.doc, extresource=ext_res) |
| 62 | + break |
| 63 | + if not match_found: |
| 64 | + debug.show('("Not Mapped:",doc_url.desc, doc_url.tag.slug, doc_url.doc.name, doc_url.url)') |
| 65 | + not_mapped += 1 |
| 66 | + debug.show('(mapped, not_mapped)') |
| 67 | + |
| 68 | +def reverse(apps, schema_editor): |
| 69 | + DocExtResource = apps.get_model('doc', 'DocExtResource') |
| 70 | + DocExtResource.objects.all().delete() |
| 71 | + |
| 72 | +class Migration(migrations.Migration): |
| 73 | + |
| 74 | + dependencies = [ |
| 75 | + ('doc', '0032_extres'), |
| 76 | + ('extresource', '0001_extres'), |
| 77 | + ('name', '0011_populate_extres'), |
| 78 | + ] |
| 79 | + |
| 80 | + operations = [ |
| 81 | + migrations.RunPython(forward, reverse) |
| 82 | + ] |
0 commit comments