Source code for beat.web.dataformats.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
import collections

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

import beat.core.hash
import beat.core.dataformat

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.exceptions import ShareError
from ..common.storage import OverwriteStorage


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


[docs]class DataFormatStorage(OverwriteStorage): def __init__(self, *args, **kwargs): super(DataFormatStorage, self).__init__(*args, location=settings.DATAFORMATS_ROOT, **kwargs)
#----------------------------------------------------------
[docs]def validate_format(declaration): """Validates the declaration of a data format file descriptor""" def _get_dataformat(name): """Utility function to return the data format object given its name""" storage = beat.core.dataformat.Storage(settings.PREFIX, name) return DataFormat.objects.get( author__username=storage.username, name=storage.name, version=int(storage.version), ) if declaration is None: raise SyntaxError('Data format 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) # Check the provided declaration (if necessary) checker = beat.core.dataformat.DataFormat(settings.PREFIX, declaration_dict) # if there are errors, return them if not checker.valid: return None, None, None, checker.errors # Separates referenced formats and the extend attribute referenced_formats = list(checker.referenced.keys()) extend = None if checker.extends: referenced_formats.remove(checker.extends) extend = checker.extends # Gets referenced dataformats for k, name in enumerate(referenced_formats): referenced_formats[k] = _get_dataformat(name) if extend: extend = _get_dataformat(extend) return checker, referenced_formats, extend, None
#----------------------------------------------------------
[docs]class DataFormatManager(StoredContributionManager):
[docs] def create_dataformat(self, author, name, short_description='', description='', declaration=None, version=1, previous_version=None, fork_of=None): """Creates a new data format from a declaration as a file or JSON dict""" # Create the database representation dataformat = 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: df = beat.core.dataformat.DataFormat(settings.PREFIX, data=None) declaration = df.data elif not(isinstance(declaration, dict)): declaration = simplejson.loads(declaration) if len(short_description) > 0: declaration['#description'] = short_description dataformat.declaration = declaration # Check the provided description if not(description): if previous_version is not None: dataformat.description = previous_version.description elif fork_of is not None: dataformat.description = fork_of.description else: dataformat.description = description try: dataformat.save() except Exception as e: return None, str(e) return (dataformat, None)
#----------------------------------------------------------
[docs]class DataFormat(StoredContribution): #_____ Fields __________ declaration_file = models.FileField( storage=DataFormatStorage(), upload_to=get_contribution_declaration_filename, blank=True, null=True, max_length=200, db_column='declaration' ) description_file = models.FileField( storage=DataFormatStorage(), 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 extend = models.ForeignKey('self', related_name='extensions', null=True, blank=True) referenced_formats = models.ManyToManyField('self', related_name='referencing', blank=True, symmetrical=False) objects = DataFormatManager() #_____ Utilities __________
[docs] def get_absolute_url(self): return reverse( 'dataformats: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_dataformats: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_dataformats:share', args=(self.author.username, self.name, self.version,), )
#_____ Meta parameters __________ class Meta(StoredContribution.Meta): verbose_name = 'dataformat' verbose_name_plural = 'dataformats' #_____ Overrides __________
[docs] def save(self, *args, **kwargs): # Make sure the declaration is valid, raises SyntaxError if a problem occurs declaration = self.declaration wrapper, referenced_formats, extend, errors = validate_format(declaration) if errors: raise SyntaxError(', '.join([str(k) for k in errors])) # Update the DB entry using the validated declaration self.hash = beat.core.hash.hashJSON(declaration, '#description') self.extend = extend self.short_description = wrapper.description if wrapper.description is not None else '' # Ensures that the sharing informations are consistent if self.sharing == Contribution.USABLE: self.sharing = Contribution.PUBLIC # Invoke the base implementation super(DataFormat, self).save(*args, **kwargs) # Set the referenced formats (must be done after the save() operation # is done, to have all the ids correctly assigned) self.referenced_formats = referenced_formats
[docs] def share(self, users=None, teams=None): # Retrieve and process the list of referenced dataformats needed_formats = self.all_needed_dataformats() own_needed_formats = filter(lambda x: x.author == self.author, needed_formats) other_needed_formats = filter(lambda x: x.author != self.author, needed_formats) # Ensure that all needed dataformats from other users/teams have the necessary sharing # preferences errors = [] for other_needed_format in other_needed_formats: errors.extend(other_needed_format.is_accessible(users=users, teams=teams)) if len(errors) > 0: raise ShareError(errors) for own_needed_dataformat in own_needed_formats: own_needed_dataformat.share(users=users, teams=teams) super(DataFormat, self).share(users=users, teams=teams)
#_____ Methods __________
[docs] def modifiable(self): """Can be modified if no object is pointing at me""" return super(DataFormat, self).modifiable() and \ ((self.referencing.count() + \ self.extensions.count() + \ self.algorithm_endpoints.count() + \ self.plotters.count() + \ self.database_outputs.count() ) == 0)
[docs] def deletable(self): """Can be deleted if no object is pointing at me""" return super(DataFormat, self).deletable() and \ ((self.referencing.count() + \ self.extensions.count() + \ self.algorithm_endpoints.count() + \ self.plotters.count() + \ self.database_outputs.count() ) == 0)
[docs] def all_needed_dataformats(self): result = [] if self.extend is not None: result.append(self.extend) result.extend(self.extend.all_needed_dataformats()) referenced_formats = list(self.referenced_formats.all()) for format in referenced_formats: result.append(format) result.extend(format.all_needed_dataformats()) return list(set(result))