Commit 1aff0440 authored by Samuel GAIST's avatar Samuel GAIST Committed by Flavio TARSETTI
Browse files

[plotters][all] Pre-commit cleanup

parent f993c530
......@@ -25,94 +25,81 @@
# #
###############################################################################
'''Administrative add-on for the extended Django User model'''
from django.contrib import admin
"""Administrative add-on for the extended Django User model"""
import simplejson as json
from django import forms
from django.contrib import admin
from django.core.files.base import ContentFile
from ..common.texts import Messages
from ..ui.forms import CodeMirrorJSONCharField
from ..ui.forms import CodeMirrorJSONFileField
from ..ui.forms import CodeMirrorPythonFileField
from ..ui.forms import CodeMirrorRSTFileField
from ..ui.forms import NameField
from .models import DefaultPlotter
from .models import Plotter
from .models import PlotterParameter
from .models import DefaultPlotter
from .models import validate_plotter
from ..ui.forms import CodeMirrorRSTFileField, CodeMirrorJSONFileField, \
CodeMirrorPythonFileField, CodeMirrorJSONCharField
from ..ui.forms import NameField
import simplejson as json
#----------------------------------------------------------
# ----------------------------------------------------------
def rehash_plotter(modeladmin, request, queryset):
"""Recalculates the hash of a plotter"""
for q in queryset: q.save()
for q in queryset:
q.save()
rehash_plotter.short_description = 'Rehash selected plotters'
rehash_plotter.short_description = "Rehash selected plotters"
#----------------------------------------------------------
# ----------------------------------------------------------
class PlotterModelForm(forms.ModelForm):
name = NameField(
widget=forms.TextInput(attrs=dict(size=80)),
help_text=Messages['algo_name'],
widget=forms.TextInput(attrs=dict(size=80)), help_text=Messages["algo_name"],
)
declaration_file = CodeMirrorJSONFileField(
label='Declaration',
help_text=Messages['json'],
label="Declaration", help_text=Messages["json"],
)
source_code_file = CodeMirrorPythonFileField(
label='Source code',
help_text=Messages['code'],
label="Source code", help_text=Messages["code"],
)
description_file = CodeMirrorRSTFileField(
label='Description',
label="Description",
required=False,
allow_empty_file=True,
help_text=Messages['description'],
help_text=Messages["description"],
)
parameters = CodeMirrorJSONCharField(
readonly=True,
required=False,
)
parameters = CodeMirrorJSONCharField(readonly=True, required=False,)
sample_data = CodeMirrorJSONCharField(
readonly=True,
required=False,
)
sample_data = CodeMirrorJSONCharField(readonly=True, required=False,)
class Meta:
model = Plotter
exclude = []
widgets = {
'short_description': forms.TextInput(
attrs=dict(size=100),
),
"short_description": forms.TextInput(attrs=dict(size=100),),
}
def clean_declaration_file(self):
"""Cleans-up the clean_declaration_file data, make sure it is really new"""
new_declaration = self.cleaned_data['declaration_file'].read()
old_declaration = ''
new_declaration = self.cleaned_data["declaration_file"].read()
old_declaration = ""
if self.instance and self.instance.declaration_file.name is not None:
old_declaration = self.instance.declaration_string
if new_declaration == old_declaration:
self.changed_data.remove('declaration_file')
self.changed_data.remove("declaration_file")
content_file = ContentFile(old_declaration)
content_file.name = self.instance.declaration_file.name
return content_file
......@@ -125,42 +112,57 @@ class PlotterModelForm(forms.ModelForm):
raise forms.ValidationError(str(e))
# if that works out, then we return the passed file
self.cleaned_data['declaration_file'].seek(0) #reset ContentFile readout
return self.cleaned_data['declaration_file']
self.cleaned_data["declaration_file"].seek(0) # reset ContentFile readout
return self.cleaned_data["declaration_file"]
def clean(self):
"""Cleans-up the input data, make sure it overall validates"""
if 'declaration_file' in self.data and \
isinstance(self.data['declaration_file'], str):
if "declaration_file" in self.data and isinstance(
self.data["declaration_file"], str
):
mutable_data = self.data.copy()
mutable_data['declaration_file'] = ContentFile(self.data['declaration_file'], name='unsaved')
mutable_data["declaration_file"] = ContentFile(
self.data["declaration_file"], name="unsaved"
)
self.data = mutable_data
#----------------------------------------------------------
# ----------------------------------------------------------
class PlotterAdmin(admin.ModelAdmin):
list_display = ('id', 'author', 'name', 'version', 'language', 'dataformat', 'short_description')
search_fields = [
'name',
'dataformat__name',
'author__username',
'name',
'short_description',
'parameters',
'previous_version__author__username',
'previous_version__name',
'fork_of__author__username',
'fork_of__name',
list_display = (
"id",
"author",
"name",
"version",
"language",
"dataformat",
"short_description",
)
search_fields = [
"name",
"dataformat__name",
"author__username",
"name",
"short_description",
"parameters",
"previous_version__author__username",
"previous_version__name",
"fork_of__author__username",
"fork_of__name",
]
list_display_links = ('id', 'name')
list_filter = ('author',)
readonly_fields = ('hash', 'short_description', 'dataformat',
'referenced_libraries')
list_display_links = ("id", "name")
list_filter = ("author",)
readonly_fields = (
"hash",
"short_description",
"dataformat",
"referenced_libraries",
)
actions = [
rehash_plotter,
......@@ -168,152 +170,140 @@ class PlotterAdmin(admin.ModelAdmin):
form = PlotterModelForm
filter_horizontal = [
'shared_with',
'shared_with_team',
'referenced_libraries'
]
filter_horizontal = ["shared_with", "shared_with_team", "referenced_libraries"]
fieldsets = (
(None,
dict(
fields=('name', 'author'),
),
),
('Documentation',
dict(
classes=('collapse',),
fields=('short_description', 'description_file',),
),
),
('Versioning',
dict(
classes=('collapse',),
fields=('version', 'previous_version', 'fork_of'),
),
),
('Sharing',
dict(
classes=('collapse',),
fields=('sharing', 'shared_with', 'shared_with_team', 'usable_by'),
),
),
('Cached Information (read-only)',
dict(
classes=('collapse',),
fields=('dataformat', 'parameters', 'referenced_libraries',
'sample_data'),
),
),
('Definition',
dict(
fields=('hash', 'declaration_file', 'language', 'source_code_file'),
),
),
(None, dict(fields=("name", "author"),),),
(
"Documentation",
dict(
classes=("collapse",),
fields=("short_description", "description_file",),
),
),
(
"Versioning",
dict(
classes=("collapse",),
fields=("version", "previous_version", "fork_of"),
),
),
(
"Sharing",
dict(
classes=("collapse",),
fields=("sharing", "shared_with", "shared_with_team", "usable_by"),
),
),
(
"Cached Information (read-only)",
dict(
classes=("collapse",),
fields=(
"dataformat",
"parameters",
"referenced_libraries",
"sample_data",
),
),
),
(
"Definition",
dict(fields=("hash", "declaration_file", "language", "source_code_file"),),
),
)
admin.site.register(Plotter, PlotterAdmin)
class PlotterParameterModelForm(forms.ModelForm):
name = NameField(
widget=forms.TextInput(attrs=dict(size=80)),
help_text=Messages['name'],
widget=forms.TextInput(attrs=dict(size=80)), help_text=Messages["name"],
)
data = CodeMirrorJSONCharField(
help_text=Messages['json'],
)
data = CodeMirrorJSONCharField(help_text=Messages["json"],)
description = CodeMirrorRSTFileField(
required=False,
help_text=Messages['description'],
required=False, help_text=Messages["description"],
)
class Meta:
model = PlotterParameter
exclude = []
widgets = {
'short_description': forms.TextInput(
attrs=dict(size=100),
),
"short_description": forms.TextInput(attrs=dict(size=100),),
}
class PlotterParameterAdmin(admin.ModelAdmin):
list_display = ('id',
'author',
'name',
'version',
'plotter',
'creation_date',
'sharing',
'short_description',
'previous_version',
'fork_of',
)
search_fields = ['author__username',
'name',
'short_description',
'plotter__name',
'previous_version__author__username',
'previous_version__name',
'fork_of__author__username',
'fork_of__name',
]
list_display_links = ('id', 'name')
list_filter = ('sharing', )
list_display = (
"id",
"author",
"name",
"version",
"plotter",
"creation_date",
"sharing",
"short_description",
"previous_version",
"fork_of",
)
search_fields = [
"author__username",
"name",
"short_description",
"plotter__name",
"previous_version__author__username",
"previous_version__name",
"fork_of__author__username",
"fork_of__name",
]
list_display_links = ("id", "name")
list_filter = ("sharing",)
form = PlotterParameterModelForm
filter_horizontal = [
'shared_with',
'shared_with_team'
]
filter_horizontal = ["shared_with", "shared_with_team"]
fieldsets = (
(None,
dict(
fields=('name', 'author'),
),
),
('Documentation',
dict(
classes=('collapse',),
fields=('short_description', 'description'),
),
),
('Versioning',
dict(
classes=('collapse',),
fields=('version', 'previous_version', 'fork_of'),
),
),
('Sharing',
dict(
classes=('collapse',),
fields=('sharing', 'shared_with', 'shared_with_team'),
),
),
('Plotter Parameters',
dict(
fields=('plotter', 'data',),
),
),
(None, dict(fields=("name", "author"),),),
(
"Documentation",
dict(classes=("collapse",), fields=("short_description", "description"),),
),
(
"Versioning",
dict(
classes=("collapse",),
fields=("version", "previous_version", "fork_of"),
),
),
(
"Sharing",
dict(
classes=("collapse",),
fields=("sharing", "shared_with", "shared_with_team"),
),
),
("Plotter Parameters", dict(fields=("plotter", "data",),),),
)
admin.site.register(PlotterParameter, PlotterParameterAdmin)
class DefaultPlotterAdmin(admin.ModelAdmin):
list_display = ('id', 'dataformat', 'plotter', 'parameter')
search_fields = ['dataformat__name',
'authors__username',
'name',
]
list_display_links = ('id',)
list_display = ("id", "dataformat", "plotter", "parameter")
search_fields = [
"dataformat__name",
"authors__username",
"name",
]
list_display_links = ("id",)
admin.site.register(DefaultPlotter, DefaultPlotterAdmin)
......@@ -25,38 +25,43 @@
# #
###############################################################################
from ..common.api import ListContributionView
from .models import Plotter, PlotterParameter, DefaultPlotter
from .serializers import PlotterSerializer, PlotterParameterSerializer, DefaultPlotterSerializer
from .serializers import PlotterAllSerializer, PlotterCreationSerializer, FullPlotterSerializer
from .serializers import PlotterParameterAllSerializer, PlotterParameterCreationSerializer, FullPlotterParameterSerializer
from django.db.models import Q
from ..code.api import ShareCodeView, RetrieveUpdateDestroyCodeView
from ..code.serializers import CodeDiffSerializer
from ..code.api import DiffView
from ..common.api import (CheckContributionNameView, ShareView,
ListContributionView, ListCreateContributionView, RetrieveUpdateDestroyContributionView)
from ..common.utils import validate_restructuredtext, ensure_html
from ..common.responses import BadRequestResponse
import json
from django.core.exceptions import ValidationError
from django.shortcuts import get_object_or_404
from rest_framework import exceptions as drf_exceptions
from rest_framework import generics
from rest_framework import views
from rest_framework import permissions
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework import status
from django.shortcuts import get_object_or_404
from django.core.exceptions import ValidationError
from ..code.api import RetrieveUpdateDestroyCodeView
from ..code.api import ShareCodeView
from ..common.api import CheckContributionNameView
from ..common.api import ListContributionView
from ..common.api import ListCreateContributionView
from ..common.api import RetrieveUpdateDestroyContributionView
from ..common.api import ShareView
from ..common.responses import BadRequestResponse
from ..common.utils import ensure_html
from ..common.utils import validate_restructuredtext
from .models import DefaultPlotter
from .models import Plotter
from .models import PlotterParameter
from .serializers import DefaultPlotterSerializer
from .serializers import FullPlotterParameterSerializer
from .serializers import FullPlotterSerializer
from .serializers import PlotterAllSerializer
from .serializers import PlotterCreationSerializer
from .serializers import PlotterParameterCreationSerializer
from .serializers import PlotterParameterSerializer
from .serializers import PlotterSerializer
import json
class ListPlotterView(ListContributionView):
"""
List all available plotters
"""
model = Plotter
serializer_class = PlotterSerializer
......@@ -65,53 +70,73 @@ class ListFormatPlotterView(ListContributionView):
"""
List all plotters corresponding to the given dataformat
"""
model = Plotter
serializer_class = PlotterSerializer
def get_queryset(self):
return self.model.objects.filter(
dataformat__author__username = self.kwargs.get('author_name'),
dataformat__name = self.kwargs.get('dataformat_name'),
dataformat__version = self.kwargs.get('version'))
dataformat__author__username=self.kwargs.get("author_name"),
dataformat__name=self.kwargs.get("dataformat_name"),
dataformat__version=self.kwargs.get("version"),
)
class ListPlotterParameterView(ListContributionView):
"""
List all available plotters parameters
"""
model = PlotterParameter
serializer_class = PlotterParameterSerializer
def get_queryset(self):
username = self.kwargs.get('author_name')
name = self.kwargs.get('dataformat_name')
version = self.kwargs.get('version')
username = self.kwargs.get("author_name")
name = self.kwargs.get("dataformat_name")
version = self.kwargs.get("version")
if username and name and version:
return self.model.objects.filter(
dataformat__author__username = username,
dataformat__name = name,
dataformat__version = version)
dataformat__author__username=username,
dataformat__name=name,
dataformat__version=version,
)
else:
author_name = 'plot' if self.request.user.is_anonymous else self.request.user.username
#return self.model.objects.all()
#from author and public and get latest version only
objects = self.model.objects.from_author_and_public(self.request.user, author_name).order_by('-version')
author_name = (
"plot" if self.request.user.is_anonymous else self.request.user.username
)
# return self.model.objects.all()
# from author and public and get latest version only
objects = self.model.objects.from_author_and_public(
self.request.user, author_name
).order_by("-version")
filtered_list = []
filtered_list_id = []
for the_item in objects:
check = False
for filtered_item in filtered_list:
if the_item.author == filtered_item.author and the_item.name == filtered_item.name:
if (
the_item.author == filtered_item.author
and the_item.name == filtered_item.name
):
check = True
if check == False:
if not check:
filtered_list.append(the_item)
filtered_list_id.append(the_item.id)
objects = self.model.objects.from_author_and_public(self.request.user, author_name).order_by('-version').filter(id__in=filtered_list_id)
objects = (
self.model.objects.from_author_and_public(
self.request.user, author_name
)
.order_by("-version")
.filter(id__in=filtered_list_id)
)
return objects
class ListDefaultPlotterView(generics.ListAPIView):
"""
List all available plotters
"""
model = DefaultPlotter
serializer_class = DefaultPlotterSerializer
......@@ -119,7 +144,7 @@ class ListDefaultPlotterView(generics.ListAPIView):
return self.model.objects.all()
#----------------------------------------------------------
# ----------------------------------------------------------
class CheckPlotterNameView(CheckContributionNameView):
......@@ -127,21 +152,23 @@ class CheckPlotterNameView(CheckContributionNameView):
This view sanitizes a Plotter name and
checks whether it is already used.
"""
model = Plotter