Skip to content

Commit

Permalink
all set for #37
Browse files Browse the repository at this point in the history
  • Loading branch information
csae8092 committed Jul 20, 2022
1 parent 7db528e commit 21db088
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 71 deletions.
5 changes: 4 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
myenv
env
db.sqlite3
db.sqlite3
cover
media/uploads/*.rdf
media/uploads/*.ttl
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ s_wsgy.py
.env_dev
vocabseditor/settings/pg_local.py
.env_secret
media/uploads/*.rdf
media/uploads/*.ttl
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ REDMINE_ID=12345
MIGRATE=yes
```

`docker run -it -p 8020:8020 --rm --env-file .env vocabseditor`
`docker run -it -p 8020:8020 --rm --name vocabseditor --env-file .env vocabseditor`

### docker-compose (using external database)

Expand Down
4 changes: 4 additions & 0 deletions build_and_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#/bin/bash

docker build -t vocabseditor:latest .
docker run -it -p 8020:8020 --rm --name vocabseditor --env-file .env vocabseditor
Empty file added media/uploads/.gitkeep
Empty file.
18 changes: 0 additions & 18 deletions vocabs/export_views.py

This file was deleted.

47 changes: 47 additions & 0 deletions vocabs/import_export_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django.contrib import messages
from django.shortcuts import redirect, render
from django.contrib.auth.decorators import login_required
from django.views.generic.list import ListView
from django_celery_results.models import TaskResult

from vocabs.forms import UploadFileForm
from vocabs.tasks import export_concept_schema, import_concept_schema
from vocabs.utils import handle_uploaded_file


def export_async(request):
get_format = request.GET.get('format', default='pretty-xml')
schema_id = request.GET.get('schema-id')
export_concept_schema.delay(schema_id, get_format)
return redirect('vocabs:job-status')


@login_required
def import_async(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
file = request.FILES['file']
file_format = file.name.split('.')[-1]
if file_format in ['ttl', 'rdf']:
file_format = file.name.split('.')[-1]
full_path = handle_uploaded_file(file)
import_concept_schema.delay(
full_path,
request.user.username,
file_format=file_format,
language=form.cleaned_data['language']
)
messages.info(request, f"Started Import of {file.name}")
else:
messages.error(request, "Upload rdf or ttl file")
return redirect('vocabs:job-status')
else:
form = UploadFileForm()
return render(request, 'vocabs/upload.html', {'form': form})


class TaskResultListView(ListView):
model = TaskResult
paginate_by = 100
template_name = "vocabs/taskresult_list.j2"
3 changes: 2 additions & 1 deletion vocabs/skos_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,8 @@ def upload_data(self, user):
logging.info(e)
else:
pass
return SkosConcept.objects.rebuild()
SkosConcept.objects.rebuild()
return concept_scheme.get_absolute_url()
else:
pass
return
24 changes: 21 additions & 3 deletions vocabs/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from django.conf import settings
from django.utils.text import slugify

from vocabs.rdf_utils import graph_construct_qs, RDF_FORMATS
from vocabs.models import SkosConceptScheme, SkosConcept
from vocabs.rdf_utils import graph_construct_qs, RDF_FORMATS
from vocabs.skos_import import SkosImporter


@shared_task
@shared_task(name="Export")
def export_concept_schema(schema_id, export_format):
schema = SkosConceptScheme.objects.get(id=schema_id)
qs = SkosConcept.objects.filter(scheme=schema)
Expand All @@ -16,4 +17,21 @@ def export_concept_schema(schema_id, export_format):
export_path = os.path.join(settings.MEDIA_ROOT, file_name)
g.serialize(export_path, format=export_format)
os.chmod(export_path, 0o0755) # this is needed because I don't get docker permission/user things
return file_name
return f"/media/{file_name}"


@shared_task(name="Import")
def import_concept_schema(full_path, user_name, file_format=None, language=None):
if file_format == 'ttl':
skos_vocab = SkosImporter(
file=full_path,
file_format="ttl",
language=language
)
else:
skos_vocab = SkosImporter(
file=full_path,
language=language
)
result = skos_vocab.upload_data(user=user_name)
return result
6 changes: 3 additions & 3 deletions vocabs/templates/vocabs/taskresult_list.j2
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Job</th>
<th scope="col">Status</th>
<th scope="col">Start</th>
<th scope="col">End</th>
Expand All @@ -20,7 +20,7 @@
</thead>
<tbody>{% for x in object_list %}
<tr>
<th scope="row">{{ x.task_id }}</th>
<th scope="row"><abbr title="{{ x.task_id }}">{{ x.task_name }}</a></th>
<td>{% if x.status == "SUCCESS" %}
<span class="badge badge-success">{{ x.status }}</span>
{% elif x.status == "FAILURE" %}
Expand All @@ -32,7 +32,7 @@
<td>{{ x.date_created }}</td>
<td>{{ x.date_done }}</td>
<td>{% if x.status == "SUCCESS" %}
<a href="/media/{{ x.result|cut:'"' }}">{{ x.result |cut:'"' }}</a>
<a href="{{ x.result|cut:'"' }}">{{ x.result |cut:'"' }}</a>
{% else %}
{{ x.result }}
{% endif %}
Expand Down
6 changes: 3 additions & 3 deletions vocabs/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import path
from . import views
from vocabs.export_views import export_async, TaskResultListView
from vocabs.import_export_views import import_async, export_async, TaskResultListView


app_name = 'vocabs'
Expand Down Expand Up @@ -45,7 +45,7 @@
'collection/delete/<int:pk>', views.SkosCollectionDelete.as_view(),
name='skoscollection_delete',
),
path('import/', views.file_upload, name='import'),
path('import/', import_async, name='import'),
path('export/', export_async, name='export'),
path('export-status/', TaskResultListView.as_view(), name='export-status'),
path('job-status/', TaskResultListView.as_view(), name='job-status'),
]
16 changes: 16 additions & 0 deletions vocabs/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
from django.conf import settings

RDF_FORMATS = {
"xml": "rdf",
"n3": "n3",
Expand All @@ -9,3 +12,16 @@
"nquads": "nq",
"json-ld": ".jsonld",
}


def handle_uploaded_file(file):
file_name = file.name
full_file_name = os.path.join(
settings.MEDIA_ROOT,
'uploads',
file_name
)
with open(full_file_name, 'wb+') as destination:
for chunk in file.chunks():
destination.write(chunk)
return full_file_name
49 changes: 8 additions & 41 deletions vocabs/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import time
import datetime
from django.shortcuts import render
from guardian.shortcuts import get_objects_for_user
from django.contrib.auth.decorators import login_required
from reversion.models import Version
from django.db import transaction
from browsing.browsing_utils import GenericListView, BaseCreateView, BaseUpdateView
from django.http import HttpResponse
from django.views.generic.detail import DetailView
from django.views.generic.edit import DeleteView
from django.utils.decorators import method_decorator
from django.urls import reverse_lazy
from django_tables2 import RequestConfig
from .models import SkosConcept, SkosConceptScheme, SkosCollection
from .forms import (
UploadFileForm,

from vocabs.models import SkosConcept, SkosConceptScheme, SkosCollection
from vocabs.forms import (
SkosConceptSchemeFormHelper,
SkosConceptSchemeForm,
SkosCollectionFormHelper,
Expand All @@ -36,17 +40,8 @@
SkosConceptSchemeListFilter,
SkosCollectionListFilter
)
from browsing.browsing_utils import GenericListView, BaseCreateView, BaseUpdateView
from vocabs.rdf_utils import graph_construct_qs, RDF_FORMATS

from guardian.shortcuts import get_objects_for_user
from django.contrib.auth.decorators import login_required
from reversion.models import Version
from django.db import transaction
from django.shortcuts import redirect
from vocabs.skos_import import SkosImporter
from django.contrib import messages


class BaseDetailView(DetailView):

Expand Down Expand Up @@ -597,31 +592,3 @@ def render_to_response(self, context):
g = graph_construct_qs(qs)
g.serialize(destination=response, format=get_format)
return response


###################################################
# SKOS vocabulary upload
###################################################

@login_required
def file_upload(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
file = request.FILES['file']
file_format = file.name.split('.')[-1]
if file_format in ['ttl', 'rdf']:
if file_format == 'ttl':
skos_vocab = SkosImporter(file=file, file_format="ttl", language=form.cleaned_data['language'])
else:
skos_vocab = SkosImporter(file=file, language=form.cleaned_data['language'])
try:
skos_vocab.upload_data(user=request.user.username)
return redirect('vocabs:browse_schemes')
except Exception as error:
messages.error(request, error)
else:
messages.error(request, "Upload rdf or ttl file")
else:
form = UploadFileForm()
return render(request, 'vocabs/upload.html', {'form': form})
1 change: 1 addition & 0 deletions vocabseditor/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
CELERY_RESULT_BACKEND = 'django-db'
CELERY_BROKER_URL = os.environ.get('amqp://')
CELERY_TASK_TRACK_STARTED = True
CELERY_RESULT_EXTENDED = True


# Django guardian settings
Expand Down

0 comments on commit 21db088

Please sign in to comment.