-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/default-sort
- Loading branch information
Showing
29 changed files
with
814 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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,38 @@ | ||
import os | ||
from django.template import Template, Context | ||
from datetime import date | ||
|
||
from django.conf import settings | ||
|
||
from addcorpus.models import CorpusConfiguration | ||
from addcorpus.load_corpus import corpus_dir | ||
|
||
|
||
def render_citation(corpus_name): | ||
raw = citation_template(corpus_name) | ||
return render_citation_context(raw) | ||
|
||
|
||
def citation_template(corpus_name): | ||
conf = CorpusConfiguration.objects.get(corpus__name=corpus_name) | ||
page = conf.citation_page | ||
|
||
if page: | ||
path = os.path.join(corpus_dir(corpus_name), 'citation', page) | ||
with open(path) as f: | ||
content = f.read() | ||
return content | ||
|
||
|
||
def render_citation_context(raw_template): | ||
template = Template(raw_template) | ||
today = date.today() | ||
context = Context({ | ||
'frontend_url': settings.BASE_URL, | ||
'date': { | ||
'year': today.year, | ||
'month': today.strftime('%B'), | ||
'day': today.day | ||
} | ||
}) | ||
return template.render(context) |
This file contains 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,21 @@ | ||
import os | ||
import json | ||
from jsonschema import validate as validate_schema | ||
|
||
here = os.path.dirname(os.path.abspath(__file__)) | ||
schemas_dir = os.path.join(here, '../schemas') | ||
|
||
def corpus_schema(): | ||
path = os.path.join(schemas_dir, 'corpus.schema.json') | ||
with open(path) as f: | ||
return json.load(f) | ||
|
||
def validate(instance): | ||
''' | ||
Validate a JSON corpus instance | ||
Currently, this just checks that it conforms to corpus.schema.json | ||
''' | ||
|
||
schema = corpus_schema() | ||
validate_schema(instance, schema) |
19 changes: 19 additions & 0 deletions
19
backend/addcorpus/migrations/0009_corpusconfiguration_citation_page.py
This file contains 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,19 @@ | ||
# Generated by Django 4.2.7 on 2024-03-05 15:30 | ||
|
||
import addcorpus.validators | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('addcorpus', '0008_alter_field_display_type_geo'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='corpusconfiguration', | ||
name='citation_page', | ||
field=models.CharField(blank=True, help_text='filename of the citation specification (in markdown) for this corpus', max_length=128, validators=[addcorpus.validators.validate_markdown_filename_extension]), | ||
), | ||
] |
This file contains 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 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 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,226 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"$id": "https://github.com/UUDigitalHumanitieslab/I-analyzer/blob/develop/backend/addcorpus/schemas/corpus.schema.json", | ||
"title": "Corpus", | ||
"description": "A corpus on I-analyzer", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"description": "Internal name. Used in the URL and the database." | ||
}, | ||
"meta": { | ||
"type": "object", | ||
"description": "Metadata about the corpus", | ||
"properties": { | ||
"title": { | ||
"type": "string", | ||
"description": "Human-friendly name" | ||
}, | ||
"description": { | ||
"type": "string", | ||
"description": "Longer description" | ||
}, | ||
"languages": { | ||
"type": "array", | ||
"description": "IETF tags of languages used in the content", | ||
"items": { | ||
"type": "string" | ||
}, | ||
"uniqueItems": true, | ||
"minItems": 1 | ||
}, | ||
"category": { | ||
"type": "string", | ||
"description": "nature of the content", | ||
"enum": [ | ||
"parliament", | ||
"periodical", | ||
"finance", | ||
"ruling", | ||
"review", | ||
"inscription", | ||
"oration", | ||
"book", | ||
"informative" | ||
] | ||
}, | ||
"date_range": { | ||
"type": "object", | ||
"description": "the date range of the content", | ||
"properties": { | ||
"min": { | ||
"type": "string", | ||
"format": "date" | ||
}, | ||
"max": { | ||
"type": "string", | ||
"format": "date" | ||
} | ||
} | ||
} | ||
}, | ||
"required": [ | ||
"title", | ||
"description", | ||
"languages", | ||
"category", | ||
"date_range" | ||
] | ||
}, | ||
"source_data": { | ||
"type": "object", | ||
"properties": { | ||
"type": { | ||
"type": "string", | ||
"description": "data type of the source files", | ||
"enum": ["csv"] | ||
}, | ||
"options": { | ||
"type": "object", | ||
"description": "additional options for source files", | ||
"properties": { | ||
"delimiter": { | ||
"type": "string", | ||
"description": "delimiter between values in the source files", | ||
"enum": [",", ";", "\t"] | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"fields": { | ||
"type": "array", | ||
"description": "list of fields", | ||
"items": { | ||
"type": "object", | ||
"description": "A field in a corpus", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"description": "internal name" | ||
}, | ||
"display_name": { | ||
"type": "string", | ||
"description": "human-friendly name" | ||
}, | ||
"description": { | ||
"type": "string", | ||
"description": "longer description for users" | ||
}, | ||
"type": { | ||
"type": "string", | ||
"enum": [ | ||
"text_content", | ||
"text_metadata", | ||
"url", | ||
"integer", | ||
"float", | ||
"date", | ||
"boolean", | ||
"geo_json" | ||
] | ||
}, | ||
"options": { | ||
"type": "object", | ||
"properties": { | ||
"search": { | ||
"type": "boolean", | ||
"description": "whether the field supports full-text search" | ||
}, | ||
"filter": { | ||
"type": "string", | ||
"description": "search filter for the field", | ||
"enum": ["show", "hide", "none"] | ||
}, | ||
"preview": { | ||
"type": "boolean", | ||
"description": "whether the field is included in the preview of a document" | ||
}, | ||
"visualize": { | ||
"type": "boolean", | ||
"description": "whether the field is visualised" | ||
}, | ||
"sort": { | ||
"type": "boolean", | ||
"description": "whether search results can be sorted on this field" | ||
}, | ||
"hidden": { | ||
"type": "boolean", | ||
"description": "whether the field is hidden from the interface" | ||
} | ||
}, | ||
"required": ["search", "filter", "preview", "visualize", "sort", "hidden"] | ||
}, | ||
"language": { | ||
"type": "string", | ||
"description": "language of the field's content. Either an IETF tag, or \"dynamic\"." | ||
}, | ||
"extract": { | ||
"type": "object", | ||
"description": "how to extract this field's value from source files", | ||
"properties": { | ||
"column": { | ||
"type": "string", | ||
"description": "name of the column in the CSV file" | ||
} | ||
}, | ||
"required": ["column"] | ||
} | ||
}, | ||
"required": ["name", "display_name", "type", "options", "extract"] | ||
} | ||
}, | ||
"options": { | ||
"type": "object", | ||
"properties": { | ||
"default_sort": { | ||
"description": "default sort settings for search results", | ||
"$ref": "#sortSetting" | ||
}, | ||
"language_field": { | ||
"type": "string", | ||
"description": "name of the field that contains the IETF tag of the document's content" | ||
}, | ||
"document_context": { | ||
"type": "object", | ||
"description": "description of how documents can be grouped", | ||
"properties": { | ||
"context_field": { | ||
"type": "string", | ||
"description": "name of the field to group by" | ||
}, | ||
"display_name": { | ||
"type": "string", | ||
"description": "display name of a group, ,e.g. 'book'" | ||
}, | ||
"sort": { | ||
"description": "when showing document context, sort them like this", | ||
"$ref": "#sortSetting" | ||
} | ||
}, | ||
"required": ["context_field", "display_name"] | ||
} | ||
} | ||
} | ||
}, | ||
"required": ["name", "meta", "source_data", "fields"], | ||
"$defs": { | ||
"sortSetting": { | ||
"$anchor": "sortSetting", | ||
"type": "object", | ||
"description": "Describes how to sort search results", | ||
"properties": { | ||
"field": { | ||
"type": "string", | ||
"description": "name of on which to sort" | ||
}, | ||
"ascending": { | ||
"type": "boolean", | ||
"description": "whether the sort direction is ascending or descending" | ||
} | ||
}, | ||
"required": ["field", "ascending"] | ||
} | ||
} | ||
} |
This file contains 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 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,7 @@ | ||
## APA | ||
|
||
> Centre for Digital Humanities, Utrecht University (2024). *Example corpus* [data set]. URL: {{ frontend_url }} | ||
## MLA | ||
|
||
> Centre for Digital Humanities, Utrecht University. *Example corpus*, {{ frontend_url }}. Accessed {{ date.day }} {{ date.month }} {{ date.year }}. |
This file contains 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 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,21 @@ | ||
from addcorpus.citation import render_citation | ||
from datetime import date | ||
from unittest.mock import patch | ||
|
||
expected = '''## APA | ||
> Centre for Digital Humanities, Utrecht University (2024). *Example corpus* [data set]. URL: http://localhost:4200 | ||
## MLA | ||
> Centre for Digital Humanities, Utrecht University. *Example corpus*, http://localhost:4200. Accessed 1 January 2024. | ||
''' | ||
|
||
def test_citation_page(mock_corpus): | ||
# monkeypatch.setattr(date, 'today', lambda : date(2024, 1, 1)) | ||
|
||
with patch('addcorpus.citation.date') as mock_date: | ||
mock_date.today.return_value = date(2024, 1, 1) | ||
|
||
result = render_citation(mock_corpus) | ||
assert result == expected |
This file contains 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 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
Oops, something went wrong.