Source code for beat.web.plotters.admin

#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

###############################################################################
#                                                                             #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/           #
# Contact: beat.support@idiap.ch                                              #
#                                                                             #
# This file is part of the beat.web module of the BEAT platform.              #
#                                                                             #
# Commercial License Usage                                                    #
# Licensees holding valid commercial BEAT licenses may use this file in       #
# accordance with the terms contained in a written agreement between you      #
# and Idiap. For further information contact tto@idiap.ch                     #
#                                                                             #
# Alternatively, this file may be used under the terms of the GNU Affero      #
# Public License version 3 as published by the Free Software and appearing    #
# in the file LICENSE.AGPL included in the packaging of this file.            #
# The BEAT platform is distributed in the hope that it will be useful, but    #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY  #
# or FITNESS FOR A PARTICULAR PURPOSE.                                        #
#                                                                             #
# You should have received a copy of the GNU Affero Public License along      #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/.           #
#                                                                             #
###############################################################################

"""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 validate_plotter

# ----------------------------------------------------------


[docs]def rehash_plotter(modeladmin, request, queryset): """Recalculates the hash of a plotter""" for q in queryset: q.save()
rehash_plotter.short_description = "Rehash selected plotters" # ----------------------------------------------------------
[docs]class PlotterModelForm(forms.ModelForm): name = NameField( widget=forms.TextInput(attrs=dict(size=80)), help_text=Messages["algo_name"], ) declaration_file = CodeMirrorJSONFileField( label="Declaration", help_text=Messages["json"], ) source_code_file = CodeMirrorPythonFileField( label="Source code", help_text=Messages["code"], ) description_file = CodeMirrorRSTFileField( label="Description", required=False, allow_empty_file=True, help_text=Messages["description"], ) parameters = CodeMirrorJSONCharField(readonly=True, required=False,) sample_data = CodeMirrorJSONCharField(readonly=True, required=False,)
[docs] class Meta: model = Plotter exclude = [] widgets = { "short_description": forms.TextInput(attrs=dict(size=100),), }
[docs] 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 = "" 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") content_file = ContentFile(old_declaration) content_file.name = self.instance.declaration_file.name return content_file # we validate the algorithm to present errors close to the field # on the form. try: validate_plotter(json.loads(new_declaration)) except Exception as e: 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"]
[docs] 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 ): mutable_data = self.data.copy() mutable_data["declaration_file"] = ContentFile( self.data["declaration_file"], name="unsaved" ) self.data = mutable_data
# ----------------------------------------------------------
[docs]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_links = ("id", "name") list_filter = ("author",) readonly_fields = ( "hash", "short_description", "dataformat", "referenced_libraries", ) actions = [ rehash_plotter, ] form = PlotterModelForm 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"),), ), )
admin.site.register(Plotter, PlotterAdmin)
[docs]class PlotterParameterModelForm(forms.ModelForm): name = NameField( widget=forms.TextInput(attrs=dict(size=80)), help_text=Messages["name"], ) data = CodeMirrorJSONCharField(help_text=Messages["json"],) description = CodeMirrorRSTFileField( required=False, help_text=Messages["description"], )
[docs] class Meta: model = PlotterParameter exclude = [] widgets = { "short_description": forms.TextInput(attrs=dict(size=100),), }
[docs]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",) form = PlotterParameterModelForm 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",),),), )
admin.site.register(PlotterParameter, PlotterParameterAdmin)
[docs]class DefaultPlotterAdmin(admin.ModelAdmin): list_display = ("id", "dataformat", "plotter", "parameter") search_fields = [ "dataformat__name", "authors__username", "name", ] list_display_links = ("id",)
admin.site.register(DefaultPlotter, DefaultPlotterAdmin)