Skip to content

Commit

Permalink
Merge pull request #216 from elixir-luxembourg/fixes-to-importer
Browse files Browse the repository at this point in the history
Fixes to importer
  • Loading branch information
Pinar Alper authored May 4, 2021
2 parents 5c8ff7b + b2ba58c commit f61ee5c
Show file tree
Hide file tree
Showing 57 changed files with 555 additions and 273 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ Single file mode:

Batch mode:
```bash
./manage.py import_projects -f path/to/dir/with/json/files/
./manage.py import_projects -d path/to/dir/with/json/files/
```

Available commands: `import_projects`, `import_datasets`, `import_partners`.
Expand Down
3 changes: 3 additions & 0 deletions core/fixtures/contact-types.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[
{
"name": "Researcher"
},
{
"name": "Principal_Investigator"
},
Expand Down
3 changes: 2 additions & 1 deletion core/fixtures/elu-dataset.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "object",
"allOf": [
{
"$ref": "https://git-r3lab.uni.lu/pinar.alper/metadata-tools/raw/master/metadata_tools/resources/elu-core.json"
"$ref": "https://raw.githubusercontent.com/elixir-luxembourg/json-schemas/master/schemas/elu-core.json"
}
],
"properties": {
Expand Down Expand Up @@ -145,6 +145,7 @@
"US",
"PS",
"IS",
"IP",
"Other"
]
},
Expand Down
2 changes: 1 addition & 1 deletion core/fixtures/elu-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "object",
"allOf": [
{
"$ref": "https://git-r3lab.uni.lu/pinar.alper/metadata-tools/raw/master/metadata_tools/resources/elu-study.json"
"$ref": "https://raw.githubusercontent.com/elixir-luxembourg/json-schemas/master/schemas/elu-study.json"
}
],
"properties": {
Expand Down
2 changes: 1 addition & 1 deletion core/fixtures/elu-study.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "object",
"allOf": [
{
"$ref": "https://git-r3lab.uni.lu/pinar.alper/metadata-tools/raw/master/metadata_tools/resources/elu-core.json"
"$ref": "https://raw.githubusercontent.com/elixir-luxembourg/json-schemas/master/schemas/elu-core.json"
}
],
"properties": {
Expand Down
5 changes: 3 additions & 2 deletions core/forms/data_declaration.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from django import forms
from django.forms import ValidationError
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy

from core.forms.use_restriction import UseRestrictionForm
from core.models import DataDeclaration, Partner, Contract, GDPRRole
from core.models.contract import PartnerRole
from django.forms import ValidationError
from core.forms.use_restriction import UseRestrictionForm


class DataDeclarationEditForm(forms.ModelForm):

Expand Down
3 changes: 2 additions & 1 deletion core/forms/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def clean(self):
if contract.project:
if str(contract.project.id) != proj:
project_inconsistency = True
self.add_error('project', "Dataset has existing link to Project {} via {}. Please remove link before updating this field.".format(contract.project.acronym, obj))
error_msg = f"Dataset has existing link to Project {contract.project.acronym} via {obj}. Please remove link before updating this field."
self.add_error('project', error_msg)
if project_inconsistency:
errors.append("Unable to update project information.")

Expand Down
14 changes: 10 additions & 4 deletions core/forms/use_restriction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.forms import CharField, ModelForm, Select

from core.models import UseRestriction, RestrictionClass
from core.models.use_restriction import USE_RESTRICTION_CHOICES


class UseRestrictionForm(ModelForm):
Expand All @@ -11,16 +12,19 @@ class UseRestrictionForm(ModelForm):

class Meta:
model = UseRestriction
fields = ('restriction_class', 'notes')
fields = ('use_restriction_rule', 'restriction_class', 'notes', 'use_class_note')


def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class_choices = [(None, "-----------------------")]
class_choices.extend([(d.code, d.name) for d in RestrictionClass.objects.all()])
self.fields['restriction_class'] = CharField(label='Category', help_text= 'Select the category of restrictions. These are \'GA4GH Consent Codes\'', required=True, widget=Select(choices=class_choices, attrs={'class': 'dummy-select'}))
self.fields['restriction_class'] = CharField(label='Use category', help_text= 'Select the category of restrictions. These are \'GA4GH Consent Codes\'', required=True, widget=Select(choices=class_choices, attrs={'class': 'dummy-select'}))
self.fields['notes'].widget.attrs['cols'] = '70'
self.fields['notes'].widget.attrs['rows'] = '1'
self.fields['notes'].widget.attrs['rows'] = '5'
self.fields['use_restriction_rule'] = CharField(label='Use Restriction Rule', help_text= 'Does the rule constraints or forbids?', required=False, widget=Select(choices=USE_RESTRICTION_CHOICES, attrs={'class': 'dummy-select'}))
self.fields['use_class_note'].widget.attrs['cols'] = '70'
self.fields['use_class_note'].widget.attrs['rows'] = '3'


def clean(self):
Expand All @@ -36,4 +40,6 @@ def is_empty(self):
cleaned_data = super().clean()
restriction_class = cleaned_data.get('restriction_class')
notes = cleaned_data.get('notes')
return not restriction_class and not notes
use_class_note = cleaned_data.get('use_class_note')
use_restriction_rule = cleaned_data.get('use_restriction_rule')
return not restriction_class and not notes and not use_class_note and not use_restriction_rule
17 changes: 11 additions & 6 deletions core/importer/JSONSchemaValidator.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import json
import jsonschema
import os
import sys
import urllib.request

from django.conf import settings

from core.exceptions import JSONSchemaValidationError
from core.utils import DaisyLogger


logger = DaisyLogger(__name__)

JSONSCHEMA_BASE_LOCAL_PATH = './core/fixtures/'
JSONSCHEMA_BASE_REMOTE_URL = "https://git-r3lab.uni.lu/pinar.alper/metadata-tools/raw/master/metadata_tools/resources/"
JSONSCHEMA_BASE_LOCAL_PATH = os.path.join(settings.BASE_DIR, 'core', 'fixtures')
JSONSCHEMA_BASE_REMOTE_URL = "https://raw.githubusercontent.com/elixir-luxembourg/json-schemas/master/schemas/"


class BaseJSONSchemaValidator:
Expand Down Expand Up @@ -43,24 +46,26 @@ def _preload_schema(self):
import os
logger.warn("Error (1/2) loading schema from disk for JSON validation...: " + str(e))
logger.warn("Working directory = " + os.getcwd())
logger.warn("File path = " + self.base_path + self.schema_name)
logger.warn("File path = " + os.path.join(self.base_path, self.schema_name))
logger.warn("Will try to load the schema from URL...")

try:
self._cached_schema = self._load_schema_from_url()
return
except:
logger.error("Error (2/2) loading schema from URI for JSON validation...: " + str(e))
logger.error("URL = " + self.base_url + self.schema_name)
logger.error("URL = " + os.path.join(self.base_url, self.schema_name))

raise Exception('Cannot load schema for JSON validation')

def _load_schema_from_disk(self):
with open(self.base_path + self.schema_name, 'r') as opened_file:
file_path = os.path.join(self.base_path, self.schema_name)
with open(file_path, 'r') as opened_file:
return json.load(opened_file)

def _load_schema_from_url(self):
with urllib.request.urlopen(self.base_url + self.schema_name) as url:
file_path = os.path.join(self.base_url, self.schema_name)
with urllib.request.urlopen(file_path) as url:
return json.loads(url.read().decode())

class DatasetJSONSchemaValidator(BaseJSONSchemaValidator):
Expand Down
Loading

0 comments on commit f61ee5c

Please sign in to comment.