Commit 1935cf7f authored by Samuel GAIST's avatar Samuel GAIST Committed by Flavio TARSETTI
Browse files

[databases][all] Pre-commit cleanup

parent 9c7eba95
Pipeline #42578 passed with stage
in 15 minutes and 29 seconds
......@@ -25,11 +25,18 @@
# #
###############################################################################
import simplejson as json
from django import forms
from django.contrib import admin
from django.core.files.base import ContentFile
from ..common.admin import notify_by_email
from ..common.models import Shareable
from ..common.texts import Messages
from ..ui.forms import CodeMirrorJSONFileField
from ..ui.forms import CodeMirrorPythonFileField
from ..ui.forms import CodeMirrorRSTFileField
from ..ui.forms import NameField
from .models import Database as DatabaseModel
from .models import DatabaseProtocol as DatabaseProtocolModel
from .models import DatabaseSet as DatabaseSetModel
......@@ -38,20 +45,6 @@ from .models import DatabaseSetTemplate as DatabaseSetTemplateModel
from .models import DatabaseSetTemplateOutput as DatabaseSetTemplateOutputModel
from .models import validate_database
from ..ui.forms import (
CodeMirrorJSONFileField,
CodeMirrorRSTFileField,
CodeMirrorPythonFileField,
NameField,
)
from ..common.texts import Messages
from ..common.admin import notify_by_email
from ..common.models import Shareable
import simplejson as json
# ------------------------------------------------
......
......@@ -25,26 +25,24 @@
# #
###############################################################################
import os
import json
import logging
import os
from rest_framework.response import Response
from rest_framework import permissions as drf_permissions
from rest_framework import exceptions as drf_exceptions
from rest_framework import permissions as drf_permissions
from rest_framework import views
from .models import Database
from .models import DatabaseSetTemplate
from .serializers import DatabaseSerializer, DatabaseCreationSerializer
from rest_framework.response import Response
from ..common import is_true
from ..common import permissions as beat_permissions
from ..common.api import ListCreateBaseView
from ..common.utils import ensure_html
from ..dataformats.serializers import ReferencedDataFormatSerializer
from .models import Database
from .models import DatabaseSetTemplate
from .serializers import DatabaseCreationSerializer
from .serializers import DatabaseSerializer
logger = logging.getLogger(__name__)
......
......@@ -25,13 +25,16 @@
# #
###############################################################################
from ..common.apps import CommonAppConfig
from django.utils.translation import ugettext_lazy as _
from ..common.apps import CommonAppConfig
class DatabasesConfig(CommonAppConfig):
name = 'beat.web.databases'
verbose_name = _('Databases')
name = "beat.web.databases"
verbose_name = _("Databases")
def ready(self):
super(DatabasesConfig, self).ready()
from . import signals
from . import signals # noqa: F401
......@@ -28,8 +28,8 @@
"""
Database specific exceptions
"""
from ..common.exceptions import BaseCreationError
class DatabaseCreationError(BaseCreationError):
pass
......@@ -26,38 +26,32 @@
###############################################################################
import os
import simplejson
from datetime import datetime
from django.db import models
import simplejson
from django.conf import settings
from django.urls import reverse
from django.db import models
from django.db.models import Q
from django.urls import reverse
import beat.core.database
from beat.backend.python.hash import hashDataset
from ..dataformats.models import DataFormat
from ..code.models import get_contribution_source_code_filename
from ..code.models import get_source_code
from ..code.models import set_source_code
from ..common import storage
from ..common.models import Versionable
from ..common.models import VersionableManager
from ..common.storage import OverwriteStorage
from ..common import storage
from ..common.models import get_contribution_declaration_filename
from ..common.models import get_contribution_description_filename
from ..code.models import get_contribution_source_code_filename
from ..common.models import get_description
from ..common.models import set_description
from ..common.models import get_declaration
from ..common.models import set_declaration
from ..common.models import get_declaration_string
from ..code.models import get_source_code
from ..code.models import set_source_code
from ..common.models import get_description
from ..common.models import set_declaration
from ..common.models import set_description
from ..common.storage import OverwriteStorage
from ..dataformats.models import DataFormat
# ----------------------------------------------------------
......
......@@ -26,16 +26,14 @@
###############################################################################
from django.conf import settings
from rest_framework import serializers
from ..common.serializers import VersionableSerializer
from ..common import fields as beat_fields
import beat.core.database
from .models import Database
from ..common import fields as beat_fields
from ..common.serializers import VersionableSerializer
from .exceptions import DatabaseCreationError
import beat.core.database
from .models import Database
class DatabaseSerializer(VersionableSerializer):
......
......@@ -30,11 +30,12 @@ from django.db import models
from django.dispatch import receiver
from ..dataformats.models import DataFormat
from .models import Database, DatabaseProtocol, DatabaseSet
from .models import DatabaseSetTemplate, DatabaseSetTemplateOutput
from .models import Database
from .models import DatabaseProtocol
from .models import DatabaseSet
from .models import DatabaseSetOutput
from .models import DatabaseSetTemplate
from .models import DatabaseSetTemplateOutput
from .models import validate_database
......@@ -94,18 +95,17 @@ def refresh_protocols(sender, instance, **kwargs):
core.name = instance.fullname()
protocols = DatabaseProtocol.objects.filter(
database__name=instance.name,
database__version=instance.version,
database__name=instance.name, database__version=instance.version,
)
existing = set((k.name, k.set_template_basename()) for k in protocols)
new_objects = set([(k,v['template']) for k,v in core.protocols.items()])
new_objects = set([(k, v["template"]) for k, v in core.protocols.items()])
for protocol_name, template in existing - new_objects:
# notice: no need to worry, this will clean-up all the rest
protocols.get(name__iexact=protocol_name).delete()
json_protocols = dict([(k,v) for k,v in core.protocols.items()])
json_protocols = dict([(k, v) for k, v in core.protocols.items()])
for protocol_name, template in new_objects - existing:
protocol = DatabaseProtocol(name=protocol_name, database=instance)
......@@ -114,12 +114,12 @@ def refresh_protocols(sender, instance, **kwargs):
json_protocol = json_protocols[protocol_name]
# creates all the template sets, outputs, etc for the first time
for set_attr in json_protocol['sets']:
for set_attr in json_protocol["sets"]:
tset_name = json_protocol['template'] + '__' + set_attr['template']
tset_name = json_protocol["template"] + "__" + set_attr["template"]
dataset_template = DatabaseSetTemplate.objects.filter(name=tset_name)
if not dataset_template: #create
if not dataset_template: # create
dataset_template = DatabaseSetTemplate(name=tset_name)
dataset_template.save()
else:
......@@ -127,78 +127,70 @@ def refresh_protocols(sender, instance, **kwargs):
# Create the database set
dataset = DatabaseSet.objects.filter(
name = set_attr['name'],
template = dataset_template,
protocol = protocol,
name=set_attr["name"], template=dataset_template, protocol=protocol,
)
if not dataset: #create
if not dataset: # create
dataset = DatabaseSet.objects.create(
name = set_attr['name'],
template = dataset_template,
protocol = protocol,
name=set_attr["name"],
template=dataset_template,
protocol=protocol,
)
# Create the database set template output
for output_name, format_name in set_attr['outputs'].items():
if len(format_name.split('/')) != 3:
for output_name, format_name in set_attr["outputs"].items():
if len(format_name.split("/")) != 3:
raise SyntaxError(
"Dataformat should be named following the " \
"style `<username>/<format>/<version>', the " \
"Dataformat should be named following the "
"style `<username>/<format>/<version>', the "
"value `%s' is not valid" % (format_name,)
)
(author, name, version) = format_name.split('/')
(author, name, version) = format_name.split("/")
dataformats = DataFormat.objects.filter(
author__username=author,
name=name,
version=version,
author__username=author, name=name, version=version,
)
# TODO: Remove this when validation works (see comments)
if len(dataformats) != 1:
raise SyntaxError(
"Could not find dataformat named `%s' to set" \
"output `%s' of template `%s' for protocol" \
"`%s' of database `%s'", (
"Could not find dataformat named `%s' to set"
"output `%s' of template `%s' for protocol"
"`%s' of database `%s'",
(
format_name,
output_name,
dataset_template.name,
protocol_name,
instance.name,
)
output_name,
dataset_template.name,
protocol_name,
instance.name,
),
)
return
database_template_output = \
DatabaseSetTemplateOutput.objects.filter(
database_template_output = DatabaseSetTemplateOutput.objects.filter(
name=output_name,
template=dataset_template,
dataformat=dataformats[0],
)
if not database_template_output: # create
database_template_output = DatabaseSetTemplateOutput(
name=output_name,
template=dataset_template,
dataformat=dataformats[0],
)
if not database_template_output: # create
database_template_output = \
DatabaseSetTemplateOutput(
name=output_name,
template=dataset_template,
dataformat=dataformats[0],
)
database_template_output.save()
else:
database_template_output = \
database_template_output[0]
database_template_output = database_template_output[0]
# Create the database set output
dataset_output = \
DatabaseSetOutput.objects.filter(template=database_template_output,
set=dataset,
)
dataset_output = DatabaseSetOutput.objects.filter(
template=database_template_output, set=dataset,
)
if not dataset_output: # create
if not dataset_output: # create
dataset_output = DatabaseSetOutput(
template=database_template_output,
set=dataset,
template=database_template_output, set=dataset,
)
dataset_output.save()
......@@ -210,8 +202,9 @@ def refresh_protocols(sender, instance, **kwargs):
@receiver(models.signals.post_delete, sender=DatabaseSet)
def delete_empty_template_sets(sender, **kwargs):
instance = kwargs['instance']
instance = kwargs["instance"]
try:
if not instance.template.sets.all(): instance.template.delete()
except:
if not instance.template.sets.all():
instance.template.delete()
except Exception: # nosec
pass
......@@ -26,20 +26,17 @@
###############################################################################
import json
from datetime import datetime
from datetime import timedelta
from datetime import datetime, timedelta
from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.auth.models import User
from django.urls import reverse
from .models import Database
from ..dataformats.models import DataFormat
from ..common.testutils import BaseTestCase
from ..common.testutils import tearDownModule # noqa test runner will call it
from ..dataformats.models import DataFormat
from .models import Database
TEST_PWD = "1234" # nosec
......
......@@ -39,30 +39,31 @@ def view(request, name, version=None):
if version is not None:
version = int(version)
database = Database.objects.for_user(request.user, True).get(name__iexact=name,
version=version)
database = Database.objects.for_user(request.user, True).get(
name__iexact=name, version=version
)
else:
database = Database.objects.for_user(request.user, True).filter(name__iexact=name).order_by('-version')[0]
except:
database = (
Database.objects.for_user(request.user, True)
.filter(name__iexact=name)
.order_by("-version")[0]
)
except Database.DoesNotExist:
raise Http404
# Render the page
return render(request,
'databases/view.html',
{'database': database})
return render(request, "databases/view.html", {"database": database})
def ls(request):
'''List all accessible databases to the request user'''
"""List all accessible databases to the request user"""
# orders databases so that the latest information is displayed first
objects = Database.objects.for_user(request.user, True).order_by('-creation_date')
objects = Database.objects.for_user(request.user, True).order_by("-creation_date")
objects = Database.filter_latest_versions(objects)
return render(request,
'databases/list.html',
dict(
objects=objects,
author=request.user,
owner=True,
))
return render(
request,
"databases/list.html",
dict(objects=objects, author=request.user, owner=True,),
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment