Source code for beat.web.plotters.models

#!/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/.           #
#                                                                             #
###############################################################################

from django.db import models
from django.conf import settings
from django.core.urlresolvers import reverse

from django.contrib.auth.models import User

from beat.core.utils import NumpyJSONEncoder

from ..team.models import Team
from ..common.models import Contribution
from ..common.texts import Messages
from ..dataformats.models import DataFormat
from ..libraries.models import Library

import beat.core.library
import beat.core.dataformat
import beat.core.plotter

from ..common.storage import OverwriteStorage
from ..common.models import get_contribution_declaration_filename
from ..common.models import get_contribution_description_filename

from ..code.models import Code
from ..code.models import CodeManager
from ..code.models import get_contribution_source_code_filename
from ..common.models import Shareable

import simplejson
import collections


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


[docs]def validate_plotter(declaration): """Validates the declaration of a plotter code, returns wrapper""" if not declaration: raise SyntaxError('Plotter declaration cannot be empty') if not(isinstance(declaration, dict)): try: declaration_dict = simplejson.loads(declaration, object_pairs_hook=collections.OrderedDict) except Exception as e: raise SyntaxError(str(e)) else: declaration_dict = collections.OrderedDict(declaration) plotter = beat.core.plotter.Plotter(settings.PREFIX, declaration_dict) if not plotter.valid: raise SyntaxError('\n * %s' % '\n * '.join(plotter.errors)) return plotter
#----------------------------------------------------------
[docs]class PlotterStorage(OverwriteStorage): def __init__(self, *args, **kwargs): kwargs['location'] = settings.PLOTTERS_ROOT super(PlotterStorage, self).__init__(*args, **kwargs)
#----------------------------------------------------------
[docs]class PlotterManager(CodeManager):
[docs] def create_plotter(self, author, name, short_description='', description='', declaration=None, code=None, version=1, previous_version=None, fork_of=None): default = beat.core.plotter.Plotter(settings.PREFIX, data=None) return self.create_code(author, name, default, short_description, description, declaration, code, version, previous_version, fork_of)
[docs] def for_strformat(self, s): """Returns the plotter for a given encoded format name""" parts = s.split('/') query = self.filter( dataformat__author__username=parts[0], dataformat__name=parts[1], dataformat__version=int(parts[2]), ).order_by('-creation_date') default = DefaultPlotter.objects.get( plotter__dataformat__author__username=parts[0], plotter__dataformat__name=parts[1], plotter__dataformat__version=int(parts[2]), ) return { 'options': collections.OrderedDict([(k, PlotterParameter.objects.filter(plotter=k).order_by('-creation_date')) for k in query]), 'default': (default.plotter, default.parameter), }
#----------------------------------------------------------
[docs]class Plotter(Code): '''The administrator code for the plotting''' #_____ Fields __________ dataformat = models.ForeignKey(DataFormat, null=True, help_text=u'Applicable data format to use this code with', related_name='plotters', ) declaration_file = models.FileField( storage=PlotterStorage(), upload_to=get_contribution_declaration_filename, blank=True, null=True, max_length=200, db_column='declaration' ) description_file = models.FileField( storage=PlotterStorage(), upload_to=get_contribution_description_filename, blank=True, null=True, max_length=200, db_column='description' ) source_code_file = models.FileField( storage=PlotterStorage(), upload_to=get_contribution_source_code_filename, blank=True, null=True, max_length=200, db_column='source_code' ) sample_data = models.TextField(default='{}', blank=True) # Read-only parameters that are updated at every save(), if required referenced_libraries = models.ManyToManyField(Library, blank=True, related_name='used_by_plotters', symmetrical=False) objects = PlotterManager() #_____ Overrides __________
[docs] def save(self, *args, **kwargs): wrapper = self._save_preprocessing() if not wrapper.parameters: self.parameters = None else: parameters = [] for name, details in wrapper.parameters.items(): parameters.append({ "name": name, "default_value": details.get('default'), "comment": details.get('description', ''), "type": details['type'], } ) if 'choice' in details: parameters[-1]['choices'] = '[' + \ ','.join([str(k) for k in details['choice']]) + ']' if 'range' in details: parameters[-1]['minimum'] = details['range'][0] parameters[-1]['maximum'] = details['range'][1] self.parameters = simplejson.dumps(parameters, indent=4, cls=NumpyJSONEncoder) # Set dataformat self.dataformat = DataFormat.objects.get( author__username=wrapper.dataformat.storage.username, name=wrapper.dataformat.storage.name, version=int(wrapper.dataformat.storage.version), ) # Invoke the base implementation super(Plotter, self).save(*args, **kwargs) # reset referenced libraries self.referenced_libraries.clear() if wrapper.uses is not None: for l in set(wrapper.uses.values()): s = beat.core.library.Storage(settings.PREFIX, l) library = Library.objects.get(author__username=s.username, name=s.name, version=s.version, ) self.referenced_libraries.add(library)
#_____ Methods __________
[docs] def validate(self, declaration): return validate_plotter(declaration)
[docs] def modifiable(self): """Can be modified if nobody points at me""" return super(Plotter, self).modifiable() and (self.defaults.count() == 0)
[docs] def deletable(self): """Can be deleted if nobody points at me""" return super(Plotter, self).deletable() and (self.defaults.count() == 0)
[docs] def core(self): return beat.core.plotter.Plotter(settings.PREFIX, self.fullname())
[docs] def core_format(self): return beat.core.dataformat.DataFormat(settings.PREFIX, self.dataformat.fullname())
[docs] def json_parameters(self): return self.declaration['parameters'] if self.declaration['parameters'] else {}
#_____ Utilities __________
[docs] def get_absolute_url(self): return reverse( 'plotters:view', args=(self.author.username, self.name, self.version,), )
[docs]class PlotterParameter(Contribution): '''User overwrites for plotters''' usable_by = models.ManyToManyField(User, related_name='usable_plotterparameters', blank=True) usable_by_team = models.ManyToManyField(Team, related_name='usable_plotterparameters', blank=True) data = models.TextField(default='', blank=True) description = models.TextField(default='', blank=True, null=True, help_text=Messages['description']) plotter = models.ForeignKey(Plotter, related_name='plotting_parameters', null=True) #_____ Methods __________
[docs] def modifiable(self): """Can be modified if nobody points at me""" return super(PlotterParameter, self).modifiable() and (self.defaults.count() == 0)
[docs] def deletable(self): """Can be deleted if nobody points at me""" return super(PlotterParameter, self).deletable() and (self.defaults.count() == 0) and (self.sharing == Shareable.PRIVATE) and (len(self.reports.all()) == 0)
#_____ Utilities __________
[docs] def get_absolute_url(self): return reverse( 'plotters:plotterparameter-author-view', args=(self.author.username, self.name, self.version,), )
[docs] def get_api_update_url(self): '''Returns the endpoint to update this object''' return reverse( 'api_plotters:view', args=(self.author.username, self.name, self.version,), )
[docs] def get_api_share_url(self): '''Returns the endpoint to share this object''' return reverse( 'api_plotters:share', args=(self.author.username, self.name, self.version,), )
[docs]class DefaultPlotter(models.Model): '''Describes the default dataformat -> plotter relationships to use''' dataformat = models.OneToOneField(DataFormat) plotter = models.ForeignKey(Plotter, related_name='defaults') parameter = models.ForeignKey(PlotterParameter, related_name='defaults', null=True, blank=True)