-
Notifications
You must be signed in to change notification settings - Fork 358
[ENG-3866] Move citation style population out of migration stream #9966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cslzchen
merged 10 commits into
CenterForOpenScience:feature/django_upgrade
from
Johnetordoff:signals-citation-styles
Jul 15, 2022
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b8e3766
Move loading of citation styles to post-migration signal
5a1f7da
Peg repo to commit SHA
cb1819e
Attempt to speed things up by loading fixture
b1b8e62
Move update_citation_styles to management command
b0939e3
Remove old test like thing
88852ea
Clean up UR0 setting and docstring
17d5da3
remove install bower from CI setup
0ceaf46
reorder imports
1a89214
reintroduce tests, but test skip them
3be3cdb
Trivial clean-up
cslzchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| #!/usr/bin/env python3 | ||
| # -*- coding: utf-8 -*- | ||
| import io | ||
| import logging | ||
| import os | ||
| import re | ||
| import requests | ||
| import zipfile | ||
|
|
||
| from django.apps import apps | ||
| from django.core.management.base import BaseCommand | ||
| from django.db import transaction | ||
| from lxml import etree | ||
| from urllib.parse import urlparse | ||
|
|
||
| from api.base import settings | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def sync_citation_styles(dry_run=False): | ||
| CitationStyle = apps.get_model('osf', 'citationstyle') | ||
| zip_data = io.BytesIO(requests.get(settings.CITATION_STYLES_REPO_URL).content) | ||
|
cslzchen marked this conversation as resolved.
|
||
| with transaction.atomic(): | ||
| with zipfile.ZipFile(zip_data) as zip_file: | ||
| for file_name in [name for name in zip_file.namelist() if name.endswith('.zip') and not name.startswith('dependent')]: | ||
| root = etree.fromstring(zip_file.read(file_name)) | ||
|
|
||
| namespace = re.search(r'\{.*\}', root.tag).group() | ||
| title = root.find('{namespace}info/{namespace}title'.format(namespace=f'{namespace}')).text | ||
| has_bibliography = 'Bluebook' in title | ||
|
|
||
| if not has_bibliography: | ||
| bib = root.find(f'{{{namespace}}}bibliography') | ||
| layout = bib.find(f'{{{namespace}}}layout') if bib is not None else None | ||
| has_bibliography = True | ||
| if layout is not None and len(layout.getchildren()) == 1 and 'choose' in layout.getchildren()[0].tag: | ||
| choose = layout.find(f'{{{namespace}}}choose') | ||
| else_tag = choose.find(f'{{{namespace}}}else') | ||
| if else_tag is None: | ||
| supported_types = [] | ||
| match_none = False | ||
| for child in choose.getchildren(): | ||
| types = child.get('type', None) | ||
| match_none = child.get('match', 'any') == 'none' | ||
| if types is not None: | ||
| types = types.split(' ') | ||
| supported_types.extend(types) | ||
| if 'webpage' in types: | ||
| break | ||
| else: | ||
| if len(supported_types) and not match_none: | ||
| # has_bibliography set to False now means that either bibliography tag is absent | ||
| # or our type (webpage) is not supported by the current version of this style. | ||
| has_bibliography = False | ||
|
|
||
| summary = getattr( | ||
| root.find('{namespace}info/{namespace}summary'.format(namespace=f'{namespace}')), 'text', None | ||
| ) | ||
| short_title = getattr( | ||
| root.find('{namespace}info/{namespace}title-short'.format(namespace=f'{namespace}')), 'text', None | ||
| ) | ||
| # Required | ||
| fields = { | ||
| '_id': os.path.splitext(os.path.basename(file_name))[0], | ||
| 'title': title, | ||
| 'has_bibliography': has_bibliography, 'parent_style': None, | ||
| 'short_title': short_title, | ||
| 'summary': summary | ||
| } | ||
|
|
||
| CitationStyle.objects.update_or_create(**fields) | ||
|
|
||
| for file_name in [name for name in zip_file.namelist() if name.endswith('.zip') and name.startswith('dependent')]: | ||
| root = etree.fromstring(zip_file.read(file_name)) | ||
|
|
||
| namespace = re.search(r'\{.*\}', root.tag).group() | ||
| title = root.find('{namespace}info/{namespace}title'.format(namespace=f'{namespace}')).text | ||
| has_bibliography = root.find(f'{{{namespace}}}bibliography') is not None or 'Bluebook' in title | ||
|
|
||
| style_id = os.path.splitext(os.path.basename(file_name))[0] | ||
| links = root.findall('{namespace}info/{namespace}link'.format(namespace=f'{namespace}')) | ||
| for link in links: | ||
| if link.get('rel') == 'independent-parent': | ||
| parent_style_id = urlparse(link.get('href')).path.split('/')[-1] | ||
| parent_style = CitationStyle.objects.get(_id=parent_style_id) | ||
|
|
||
| if parent_style is not None: | ||
| summary = getattr( | ||
| root.find('{namespace}info/{namespace}summary'.format(namespace=f'{namespace}')), | ||
| 'text', None | ||
| ) | ||
| short_title = getattr( | ||
| root.find('{namespace}info/{namespace}title-short'.format(namespace=f'{namespace}')), | ||
| 'text', None | ||
| ) | ||
|
|
||
| parent_has_bibliography = parent_style.has_bibliography | ||
| fields = { | ||
| '_id': style_id, | ||
| 'title': title, | ||
| 'has_bibliography': parent_has_bibliography, | ||
| 'parent_style': parent_style_id, | ||
| 'short_title': short_title, | ||
| 'summary': summary | ||
| } | ||
| CitationStyle.objects.update_or_create(**fields) | ||
| break | ||
| else: | ||
| logger.debug('Unable to load parent_style object: parent {}, dependent style {}'.format(parent_style_id, style_id)) | ||
| else: | ||
| fields = { | ||
| '_id': style_id, | ||
| 'title': title, | ||
| 'has_bibliography': has_bibliography, | ||
| 'parent_style': None | ||
| } | ||
| CitationStyle.objects.update_or_create(**fields) | ||
|
|
||
| if dry_run: | ||
| raise RuntimeError('This is a dry run rolling back transaction.') | ||
|
cslzchen marked this conversation as resolved.
|
||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| """Updates citation styles to its current repo URL.""" | ||
| def add_arguments(self, parser): | ||
| super().add_arguments(parser) | ||
| parser.add_argument( | ||
| '--dry', | ||
| action='store_true', | ||
| dest='dry_run', | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| dry_run = options.get('dry_run') | ||
| sync_citation_styles(dry_run=dry_run) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.