|
| 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 collections import OrderedDict, Counter |
| 11 | + |
| 12 | +from django.db import migrations |
| 13 | + |
| 14 | +from ietf.utils.validators import validate_external_resource_value |
| 15 | +from django.core.exceptions import ValidationError |
| 16 | + |
| 17 | + |
| 18 | +name_map = { |
| 19 | + "Issue.*": "tracker", |
| 20 | + ".*FAQ.*": "faq", |
| 21 | + ".*Area Web Page": "webpage", |
| 22 | + ".*Wiki": "wiki", |
| 23 | + "Home Page": "webpage", |
| 24 | + "Slack.*": "slack", |
| 25 | + "Additional .* Web Page": "webpage", |
| 26 | + "Additional .* Page": "webpage", |
| 27 | + "Yang catalog entry.*": "yc_entry", |
| 28 | + "Yang impact analysis.*": "yc_impact", |
| 29 | + "GitHub": "github_repo", |
| 30 | + "Github page": "github_repo", |
| 31 | + "GitHub repo.*": "github_repo", |
| 32 | + "Github repository.*": "github_repo", |
| 33 | + "GitHub notifications": "github_notify", |
| 34 | + "GitHub org.*": "github_org", |
| 35 | + "GitHub User.*": "github_username", |
| 36 | + "GitLab User": "gitlab_username", |
| 37 | + "GitLab User Name": "gitlab_username", |
| 38 | +} |
| 39 | + |
| 40 | +# TODO: Review all the None values below and make sure ignoring the URLs they match is really the right thing to do. |
| 41 | +url_map = OrderedDict({ |
| 42 | + "https?://github\\.com": "github_repo", |
| 43 | + "https?://trac\\.ietf\\.org/.*/wiki": "wiki", |
| 44 | + "ietf\\.org.*/trac/wiki": "wiki", |
| 45 | + "trac.*wiki": "wiki", |
| 46 | + "www\\.ietf\\.org/mailman" : None, |
| 47 | + "www\\.ietf\\.org/mail-archive" : None, |
| 48 | + "mailarchive\\.ietf\\.org" : None, |
| 49 | + "ietf\\.org/logs": "jabber_log", |
| 50 | + "ietf\\.org/jabber/logs": "jabber_log", |
| 51 | + "xmpp:.*?join": "jabber_room", |
| 52 | + "bell-labs\\.com": None, |
| 53 | + "html\\.charters": None, |
| 54 | + "datatracker\\.ietf\\.org": None, |
| 55 | +}) |
| 56 | + |
| 57 | +def forward(apps, schema_editor): |
| 58 | + DocExtResource = apps.get_model('doc', 'DocExtResource') |
| 59 | + ExtResourceName = apps.get_model('name', 'ExtResourceName') |
| 60 | + DocumentUrl = apps.get_model('doc', 'DocumentUrl') |
| 61 | + |
| 62 | + stats = Counter() |
| 63 | + |
| 64 | + for doc_url in DocumentUrl.objects.all(): |
| 65 | + match_found = False |
| 66 | + for regext,slug in name_map.items(): |
| 67 | + if re.match(regext, doc_url.desc): |
| 68 | + match_found = True |
| 69 | + stats['mapped'] += 1 |
| 70 | + name = ExtResourceName.objects.get(slug=slug) |
| 71 | + DocExtResource.objects.create(doc=doc_url.doc, name_id=slug, value=doc_url.url, display_name=doc_url.desc) # TODO: validate this value against name.type |
| 72 | + break |
| 73 | + if not match_found: |
| 74 | + for regext, slug in url_map.items(): |
| 75 | + doc_url.url = doc_url.url.strip() |
| 76 | + if re.search(regext, doc_url.url): |
| 77 | + match_found = True |
| 78 | + if slug: |
| 79 | + stats['mapped'] +=1 |
| 80 | + name = ExtResourceName.objects.get(slug=slug) |
| 81 | + # Munge the URL if it's the first github repo match |
| 82 | + # Remove "/tree/master" substring if it exists |
| 83 | + # Remove trailing "/issues" substring if it exists |
| 84 | + # Remove "/blob/master/.*" pattern if present |
| 85 | + if regext == "https?://github\\.com": |
| 86 | + doc_url.url = doc_url.url.replace("/tree/master","") |
| 87 | + doc_url.url = re.sub('/issues$', '', doc_url.url) |
| 88 | + doc_url.url = re.sub('/blob/master.*$', '', doc_url.url) |
| 89 | + try: |
| 90 | + validate_external_resource_value(name, doc_url.url) |
| 91 | + DocExtResource.objects.create(doc=doc_url.doc, name=name, value=doc_url.url, display_name=doc_url.desc) # TODO: validate this value against name.type |
| 92 | + except ValidationError as e: # pyflakes:ignore |
| 93 | + debug.show('("Failed validation:", doc_url.url, e)') |
| 94 | + stats['failed_validation'] +=1 |
| 95 | + else: |
| 96 | + stats['ignored'] +=1 |
| 97 | + break |
| 98 | + if not match_found: |
| 99 | + debug.show('("Not Mapped:",doc_url.desc, doc_url.tag.slug, doc_url.doc.name, doc_url.url)') |
| 100 | + stats['not_mapped'] += 1 |
| 101 | + print (stats) |
| 102 | + |
| 103 | +def reverse(apps, schema_editor): |
| 104 | + DocExtResource = apps.get_model('doc', 'DocExtResource') |
| 105 | + DocExtResource.objects.all().delete() |
| 106 | + |
| 107 | +class Migration(migrations.Migration): |
| 108 | + |
| 109 | + dependencies = [ |
| 110 | + ('doc', '0032_extres'), |
| 111 | + ('name', '0013_populate_extres'), |
| 112 | + ] |
| 113 | + |
| 114 | + operations = [ |
| 115 | + migrations.RunPython(forward, reverse) |
| 116 | + ] |
0 commit comments