Source code for beat.web.toolchains.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/.           #
#                                                                             #
###############################################################################

import simplejson

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

import beat.core.hash
import beat.core.toolchain

from ..common.models import Contribution
from ..common.models import StoredContribution
from ..common.models import StoredContributionManager
from ..common.models import get_contribution_declaration_filename
from ..common.models import get_contribution_description_filename
from ..common.storage import OverwriteStorage


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


[docs]def validate_toolchain(declaration): """Validates the declaration of a toolchain JSON string""" toolchain = beat.core.toolchain.Toolchain(settings.PREFIX, declaration) if not toolchain.valid: errors = 'The toolchain declaration is **invalid**. Errors:\n * ' + \ '\n * '.join(toolchain.errors) raise SyntaxError(errors) return toolchain
#----------------------------------------------------------
[docs]class ToolchainStorage(OverwriteStorage): def __init__(self, *args, **kwargs): super(ToolchainStorage, self).__init__(*args, location=settings.TOOLCHAINS_ROOT, **kwargs)
#----------------------------------------------------------
[docs]class ToolchainManager(StoredContributionManager):
[docs] def create_toolchain(self, author, name, short_description='', description='', declaration=None, version=1, previous_version=None, fork_of=None): # Create the database representation of the toolchain toolchain = self.model( author = author, name = self.model.sanitize_name(name), version = version, sharing = self.model.PRIVATE, previous_version = previous_version, fork_of = fork_of, ) # Check the provided declaration if declaration is None: if previous_version is not None: declaration = previous_version.declaration elif fork_of is not None: declaration = fork_of.declaration else: tc = beat.core.toolchain.Toolchain(settings.PREFIX, data=None) declaration = tc.data elif not(isinstance(declaration, dict)): declaration = simplejson.loads(declaration) if len(short_description) > 0: declaration['description'] = short_description toolchain.declaration = declaration # Check the provided description if description is None: if previous_version is not None: toolchain.description = previous_version.description elif fork_of is not None: toolchain.description = fork_of.description else: toolchain.description = description # Save the toolchain (will run the validation) toolchain.save() if toolchain.errors: toolchain.delete() # undo saving to respect current API return (None, toolchain.errors) return (toolchain, None)
#----------------------------------------------------------
[docs]class Toolchain(StoredContribution): #_____ Constants _______ DEFAULT_TOOLCHAIN_TEXT = """\ { "blocks": [], "datasets": [], "connections": [], "analyzers": [] } """ #_____ Fields __________ declaration_file = models.FileField(storage=ToolchainStorage(), upload_to=get_contribution_declaration_filename, blank=True, null=True, max_length=200, db_column='declaration' ) description_file = models.FileField(storage=ToolchainStorage(), upload_to=get_contribution_description_filename, blank=True, null=True, max_length=200, db_column='description' ) # read-only parameters that are updated at every save(), if required errors = models.TextField(blank=True, null=True, help_text="Errors detected while validating the toolchain. Automatically set by the platform.") objects = ToolchainManager() #_____ Utilities __________
[docs] def get_absolute_url(self): return reverse( 'toolchains: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_toolchains:object', 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_toolchains:share', args=(self.author.username, self.name, self.version,), )
[docs] def get_new_experiment_url(self): '''Returns the view to create a new experiment from self''' return reverse( 'experiments:new-from-toolchain', args=(self.author.username, self.name, self.version,), )
#_____ Overrides __________
[docs] def save(self, *args, **kwargs): # Retrieve the toolchain declaration declaration = self.declaration # Compute the hash of the content content_hash = beat.core.hash.hashJSON(declaration, 'description') content_modified = (content_hash != self.hash) if content_modified: # toolchains can be saved even if they are not valid... wrapper = None errors = '' try: wrapper = validate_toolchain(declaration) except Exception as e: errors = str(e) self.hash = content_hash self.short_description = wrapper.description if (wrapper is not None) and (wrapper.description is not None) else '' # Store the errors (if applicable) if errors is not None and not errors.strip(): self.errors = None else: self.errors = errors else: self.short_description = declaration.get('description', '') # Ensures that the sharing informations are consistent if self.sharing == Contribution.USABLE: self.sharing = Contribution.PUBLIC # Invoke the base implementation super(Toolchain, self).save(*args, **kwargs)
#_____ Methods __________
[docs] def is_valid(self): return (self.errors is None)
[docs] def modifiable(self): return (self.experiments.count() == 0) and super(Toolchain, self).modifiable()
[docs] def deletable(self): return (self.experiments.count() == 0) and super(Toolchain, self).deletable()
[docs] def core(self): return beat.core.toolchain.Toolchain(settings.PREFIX, self.declaration)