Skip to content

Commit f2cef48

Browse files
authored
Merge pull request #8024 from CenterForOpenScience/revert-8023-revert-8000-citation
Revert "[REVERT][OSF-3622] Add migration file to automatically parse citations""
2 parents 17e97d0 + a3fda3f commit f2cef48

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# This migration port `scripts/parse_citation_styles` to automatically parse citation styles.
2+
# Additionally, this set the corresponding `has_bibliography` field to `False` for all citation formats whose CSL files do not
3+
# include a bibliography section. As a result, all such citation formats would not show up in OSF
4+
# citation widgets for users to choose.
5+
#
6+
# NOTE:
7+
# As of December 6th, 2017, there are however THREE EXCEPTIONS:
8+
# "Bluebook Law Review", "Bluebook Law Review(2)" and "Bluebook Inline" shares a
9+
# special CSL file ('website/static/bluebook.cls'), in which a bibliography section is defined,
10+
# in order to render bibliographies even though their official CSL files (located in CenterForOpenScience/styles repo)
11+
# do not contain a bibliography section. Therefore, This migration also automatically set `has_bibliography` to `True` for all styles whose titles contain "Bluebook"
12+
13+
import logging
14+
import os
15+
16+
from django.db import migrations
17+
from lxml import etree
18+
19+
from osf.models.citation import CitationStyle
20+
from website import settings
21+
22+
logger = logging.getLogger(__file__)
23+
24+
def get_style_files(path):
25+
files = (os.path.join(path, x) for x in os.listdir(path))
26+
return (f for f in files if os.path.isfile(f))
27+
28+
def parse_citation_styles(*args):
29+
# drop all styles
30+
CitationStyle.remove()
31+
32+
for style_file in get_style_files(settings.CITATION_STYLES_PATH):
33+
with open(style_file, 'r') as f:
34+
try:
35+
root = etree.parse(f).getroot()
36+
except etree.XMLSyntaxError:
37+
continue
38+
39+
namespace = root.nsmap.get(None)
40+
selector = '{{{ns}}}info/{{{ns}}}'.format(ns=namespace)
41+
42+
title = root.find(selector + 'title').text
43+
# `has_bibliography` is set to `True` for Bluebook citation formats due to the special way we handle them.
44+
has_bibliography = root.find('{{{ns}}}{tag}'.format(ns=namespace, tag='bibliography')) is not None or 'Bluebook' in title
45+
# Required
46+
fields = {
47+
'_id': os.path.splitext(os.path.basename(style_file))[0],
48+
'title': title,
49+
'has_bibliography': has_bibliography,
50+
}
51+
52+
# Optional
53+
try:
54+
fields['short_title'] = root.find(selector + "title-short").text
55+
except AttributeError:
56+
pass
57+
58+
try:
59+
fields['summary'] = root.find(selector + 'summary').text
60+
except AttributeError:
61+
pass
62+
63+
style = CitationStyle(**fields)
64+
style.save()
65+
66+
def revert(*args):
67+
# The revert of this migration simply removes all CitationStyle instances.
68+
CitationStyle.remove()
69+
70+
class Migration(migrations.Migration):
71+
72+
dependencies = [
73+
('osf', '0073_citationstyle_has_bibliography'),
74+
]
75+
76+
operations = [
77+
migrations.RunPython(parse_citation_styles, revert),
78+
]

0 commit comments

Comments
 (0)