-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Taxonomy import templates [FC-0036] (#89)
feat: adds view to download CSV and JSON taxonomy import template files to demonstrate their format and usage. Also alters the import code to ignore extra fields in import files. This allows us to add comments to our template import files, but more importantly, allows users to import tags from an external source which may have extra data we don't care about.
- Loading branch information
1 parent
0934450
commit b2767fa
Showing
10 changed files
with
394 additions
and
8 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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
""" | ||
Open edX Learning ("Learning Core"). | ||
""" | ||
__version__ = "0.2.1" | ||
__version__ = "0.2.2" |
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,30 @@ | ||
id,value,parent_id,comments | ||
WINDS,Wind instruments,,"This is an example Tag Import file, in CSV format." | ||
PERCUSS,Percussion instruments,,"Only the 'id' and 'value' fields are required. They can be anything you like, but they must must be unique within the taxonomy. Existing tags matching 'id' will be updated on import." | ||
ELECTRIC,Electronic instruments,,"Top-level tags have no 'parent_id', and you can have as many top-level tags as you wish." | ||
STRINGS,String instruments,,"All other fields (like these 'comments') are ignored on import, and will not be included in subsequent tag exports." | ||
BELLS,Idiophone,PERCUSS,"Providing a 'parent_id' creates a tag hierarchy." | ||
DRUMS,Membranophone,PERCUSS,"The 'parent_id' must match an 'id' found earlier in the import file." | ||
CAJÓN,Cajón,DRUMS,"Tag values may contain unicode characters." | ||
PYLE,Pyle Stringed Jam Cajón,CAJÓN,"A tag hierarchy may contain as many as 3 levels. This tag is at level 4, and so it will not be shown to users." | ||
THERAMIN,Theramin,ELECTRIC,"A tag hierarchy may contain uneven levels. Here, the Electronic branch has only 2 levels, while Percussion has 3." | ||
CHORD,Chordophone,PERCUSS, | ||
BRASS,Brass,WINDS, | ||
WOODS,Woodwinds,WINDS, | ||
FLUTE,Flute,WOODS, | ||
PLUCK,Plucked strings,STRINGS, | ||
MANDOLIN,Mandolin,PLUCK, | ||
HARP,Harp,PLUCK, | ||
BANJO,Banjo,PLUCK, | ||
BOW,Bowed strings,STRINGS, | ||
VIOLIN,Violin,BOW, | ||
CELLO,Cello,BOW, | ||
CLARINET,Clarinet,WOODS, | ||
OBOE,Oboe,WOODS, | ||
TRUMPET,Trumpet,BRASS, | ||
TUBA,Tuba,BRASS, | ||
SYNTH,Synthesizer,ELECTRIC, | ||
CELESTA,Celesta,BELLS, | ||
HI-HAT,Hi-hat,BELLS, | ||
TABLA,Tabla,DRUMS, | ||
PIANO,Piano,CHORD, |
158 changes: 158 additions & 0 deletions
158
openedx_tagging/core/tagging/import_export/template.json
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,158 @@ | ||
{ | ||
"tags": [ | ||
{ | ||
"id": "WINDS", | ||
"value": "Wind instruments", | ||
"parent_id": "", | ||
"comments": "This is an example Tag Import file, in JSON format." | ||
}, | ||
{ | ||
"id": "PERCUSS", | ||
"value": "Percussion instruments", | ||
"parent_id": "", | ||
"comments": "Only the 'id' and 'value' fields are required. They can be anything you like, but they must must be unique within the taxonomy. Existing tags matching 'id' will be updated on import." | ||
}, | ||
{ | ||
"id": "ELECTRIC", | ||
"value": "Electronic instruments", | ||
"parent_id": "", | ||
"comments": "Top-level tags have no 'parent_id', and you can have as many top-level tags as you wish." | ||
}, | ||
{ | ||
"id": "STRINGS", | ||
"value": "String instruments", | ||
"parent_id": "", | ||
"comments": "All other fields (like these 'comments') are ignored on import, and will not be included in subsequent tag exports." | ||
}, | ||
{ | ||
"id": "BELLS", | ||
"value": "Idiophone", | ||
"parent_id": "PERCUSS", | ||
"comments": "Providing a 'parent_id' creates a tag hierarchy." | ||
}, | ||
{ | ||
"id": "DRUMS", | ||
"value": "Membranophone", | ||
"parent_id": "PERCUSS", | ||
"comments": "The 'parent_id' must match an 'id' found earlier in the import file." | ||
}, | ||
{ | ||
"id": "CAJÓN", | ||
"value": "Cajón", | ||
"parent_id": "DRUMS", | ||
"comments": "Tag values may contain unicode characters." | ||
}, | ||
{ | ||
"id": "PYLE", | ||
"value": "Pyle Stringed Jam Cajón", | ||
"parent_id": "CAJÓN", | ||
"comments": "A tag hierarchy may contain as many as 3 levels. This tag is at level 4, and so it will not be shown to users." | ||
}, | ||
{ | ||
"id": "THERAMIN", | ||
"value": "Theramin", | ||
"parent_id": "ELECTRIC", | ||
"comments": "A tag hierarchy may contain uneven levels. Here, the Electronic branch has only 2 levels, while Percussion has 3." | ||
}, | ||
{ | ||
"id": "CHORD", | ||
"value": "Chordophone", | ||
"parent_id": "PERCUSS" | ||
}, | ||
{ | ||
"id": "BRASS", | ||
"value": "Brass", | ||
"parent_id": "WINDS" | ||
}, | ||
{ | ||
"id": "WOODS", | ||
"value": "Woodwinds", | ||
"parent_id": "WINDS" | ||
}, | ||
{ | ||
"id": "FLUTE", | ||
"value": "Flute", | ||
"parent_id": "WOODS" | ||
}, | ||
{ | ||
"id": "PLUCK", | ||
"value": "Plucked strings", | ||
"parent_id": "STRINGS" | ||
}, | ||
{ | ||
"id": "MANDOLIN", | ||
"value": "Mandolin", | ||
"parent_id": "PLUCK" | ||
}, | ||
{ | ||
"id": "HARP", | ||
"value": "Harp", | ||
"parent_id": "PLUCK" | ||
}, | ||
{ | ||
"id": "BANJO", | ||
"value": "Banjo", | ||
"parent_id": "PLUCK" | ||
}, | ||
{ | ||
"id": "BOW", | ||
"value": "Bowed strings", | ||
"parent_id": "STRINGS" | ||
}, | ||
{ | ||
"id": "VIOLIN", | ||
"value": "Violin", | ||
"parent_id": "BOW" | ||
}, | ||
{ | ||
"id": "CELLO", | ||
"value": "Cello", | ||
"parent_id": "BOW" | ||
}, | ||
{ | ||
"id": "CLARINET", | ||
"value": "Clarinet", | ||
"parent_id": "WOODS" | ||
}, | ||
{ | ||
"id": "OBOE", | ||
"value": "Oboe", | ||
"parent_id": "WOODS" | ||
}, | ||
{ | ||
"id": "TRUMPET", | ||
"value": "Trumpet", | ||
"parent_id": "BRASS" | ||
}, | ||
{ | ||
"id": "TUBA", | ||
"value": "Tuba", | ||
"parent_id": "BRASS" | ||
}, | ||
{ | ||
"id": "SYNTH", | ||
"value": "Synthesizer", | ||
"parent_id": "ELECTRIC" | ||
}, | ||
{ | ||
"id": "CELESTA", | ||
"value": "Celesta", | ||
"parent_id": "BELLS" | ||
}, | ||
{ | ||
"id": "HI-HAT", | ||
"value": "Hi-hat", | ||
"parent_id": "BELLS" | ||
}, | ||
{ | ||
"id": "TABLA", | ||
"value": "Tabla", | ||
"parent_id": "DRUMS" | ||
}, | ||
{ | ||
"id": "PIANO", | ||
"value": "Piano", | ||
"parent_id": "CHORD" | ||
} | ||
] | ||
} |
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,51 @@ | ||
""" | ||
Taxonomy Import views | ||
""" | ||
from __future__ import annotations | ||
|
||
import os | ||
|
||
from django.http import FileResponse, Http404 | ||
from rest_framework.request import Request | ||
from rest_framework.views import APIView | ||
|
||
|
||
class TemplateView(APIView): | ||
""" | ||
View which serves the static Taxonomy Import template files. | ||
**Example Requests** | ||
GET /tagging/rest_api/v1/import/template.csv | ||
GET /tagging/rest_api/v1/import/template.json | ||
**Query Returns** | ||
* 200 - Success | ||
* 404 - Template file not found | ||
* 405 - Method not allowed | ||
""" | ||
http_method_names = ['get'] | ||
|
||
template_dir = os.path.join( | ||
os.path.dirname(__file__), | ||
"../../import_export/", | ||
) | ||
allowed_ext_to_content_type = { | ||
"csv": "text/csv", | ||
"json": "application/json", | ||
} | ||
|
||
def get(self, request: Request, file_ext: str, *args, **kwargs) -> FileResponse: | ||
""" | ||
Downloads the requested file as an attachment, | ||
or raises 404 if not found. | ||
""" | ||
content_type = self.allowed_ext_to_content_type.get(file_ext) | ||
if not content_type: | ||
raise Http404 | ||
|
||
filename = f"template.{file_ext}" | ||
content_disposition = f'attachment; filename="{filename}"' | ||
fh = open(os.path.join(self.template_dir, filename), "rb") | ||
response = FileResponse(fh, content_type=content_type) | ||
response['Content-Disposition'] = content_disposition | ||
return response |
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.