From 06400c851dda6d0ef0ca275a47b3e3ae785528ad Mon Sep 17 00:00:00 2001
From: Philip ABBET <philip.abbet@idiap.ch>
Date: Wed, 23 Nov 2016 11:43:39 +0100
Subject: [PATCH] [website] Don't allow to use an invalid binary algorithm in
 an experiment

---
 beat/web/algorithms/static/algorithms/js/models.js    | 10 +++++++++-
 beat/web/code/serializers.py                          |  3 ++-
 beat/web/experiments/templates/experiments/setup.html |  4 ++--
 3 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/beat/web/algorithms/static/algorithms/js/models.js b/beat/web/algorithms/static/algorithms/js/models.js
index b23172046..1b12b6416 100644
--- a/beat/web/algorithms/static/algorithms/js/models.js
+++ b/beat/web/algorithms/static/algorithms/js/models.js
@@ -52,6 +52,8 @@ beat.algorithms.models.Algorithm = function(data, duplicate_of)
     this.outputs           = data.outputs;
     this.parameters        = data.parameters;
     this.splittable        = data.splittable;
+    this.valid             = data.valid;
+    this.language          = data.language;
     this.duplicate_of      = (duplicate_of !== undefined ? duplicate_of : null);
 }
 
@@ -152,8 +154,11 @@ beat.algorithms.models.Algorithm.prototype.outputsOfChannel = function(channel)
 //
 // declaration: JSON declaration of the list, as returned by the Web API
 //----------------------------------------------------------------------------------------
-beat.algorithms.models.AlgorithmsList = function(declaration)
+beat.algorithms.models.AlgorithmsList = function(declaration, valid_only)
 {
+    if (valid_only === undefined)
+        valid_only = false;
+
     // Attributes
     this.data = new Array();
 
@@ -168,6 +173,9 @@ beat.algorithms.models.AlgorithmsList = function(declaration)
     {
         var algorithm = declaration[i];
 
+        if (valid_only && !algorithm.valid)
+            continue;
+
         if ((algorithm.name.indexOf('/private/') == -1) || (shared_hashes.indexOf(algorithm.hash) == -1))
         {
             this.data.push(new beat.algorithms.models.Algorithm(algorithm));
diff --git a/beat/web/code/serializers.py b/beat/web/code/serializers.py
index efcd03240..9662168fe 100755
--- a/beat/web/code/serializers.py
+++ b/beat/web/code/serializers.py
@@ -67,10 +67,11 @@ class CodeSerializer(ContributionSerializer):
     opensource = serializers.SerializerMethodField()
     language = serializers.SerializerMethodField()
     modifiable = serializers.BooleanField()
+    valid = serializers.BooleanField()
 
     class Meta(ContributionSerializer.Meta):
         model = Code
-        default_fields = ContributionSerializer.Meta.default_fields + ['opensource', 'language']
+        default_fields = ContributionSerializer.Meta.default_fields + ['opensource', 'language', 'valid']
         extra_fields = ContributionSerializer.Meta.extra_fields + ['code']
         exclude = ContributionSerializer.Meta.exclude + ['source_code_file']
 
diff --git a/beat/web/experiments/templates/experiments/setup.html b/beat/web/experiments/templates/experiments/setup.html
index 61083eb10..c3eee5666 100644
--- a/beat/web/experiments/templates/experiments/setup.html
+++ b/beat/web/experiments/templates/experiments/setup.html
@@ -402,9 +402,9 @@ jQuery(document).ready(function() {
   {# get all algorithms #}
   function getAlgorithms() {
     var d = $.Deferred();
-    var url = '{% url "api_algorithms:all" %}?fields=name,inputs,outputs,private,hash,parameters,short_description,splittable';
+    var url = '{% url "api_algorithms:all" %}?fields=name,inputs,outputs,private,hash,parameters,short_description,splittable,valid,language';
     $.get(url).done(function(data) {
-      var r = new beat.algorithms.models.AlgorithmsList(data);
+      var r = new beat.algorithms.models.AlgorithmsList(data, true);
       advance_progressbar();
       d.resolve(r);
     }).fail(d.reject);
-- 
GitLab