From 605a0d5f90f6f20f18206ded3066a9a023b24da8 Mon Sep 17 00:00:00 2001
From: Philip ABBET <philip.abbet@idiap.ch>
Date: Wed, 23 Nov 2016 11:25:39 +0100
Subject: [PATCH] [website] Allows the creation of a new version of binary
 algorithms

---
 .../templates/algorithms/edition.html         |  4 +++
 beat/web/algorithms/tests/tests_api.py        | 34 +++++++++++++++++++
 beat/web/algorithms/views.py                  | 18 ++++++----
 3 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/beat/web/algorithms/templates/algorithms/edition.html b/beat/web/algorithms/templates/algorithms/edition.html
index 92818d047..f97dfb546 100644
--- a/beat/web/algorithms/templates/algorithms/edition.html
+++ b/beat/web/algorithms/templates/algorithms/edition.html
@@ -590,7 +590,11 @@ function setupEditor(algorithm, dataformats, libraries)
 <div class="row">
   <div class="col-sm-offset-1 col-sm-10 contribution_editor" style="display: none;">
     <div class="alert alert-info">
+      {% if new_version %}
+      <i class="fa fa-info"></i> <span>The algorithm is implemented in <strong>{{ algorithm_language_name }}</strong>, compiled as a <strong>shared library</strong>. The new algorithm version that will be created will copy the original shared library.</span>
+      {% else %}
       <i class="fa fa-info"></i> <span>The original algorithm is implemented in <strong>{{ algorithm_language_name }}</strong>, compiled as a <strong>shared library</strong>. The forked algorithm that will be created will copy the original shared library.</span>
+      {% endif %}
     </div>
   </div>
 </div>
diff --git a/beat/web/algorithms/tests/tests_api.py b/beat/web/algorithms/tests/tests_api.py
index dd1a6e98b..1a25f26fa 100755
--- a/beat/web/algorithms/tests/tests_api.py
+++ b/beat/web/algorithms/tests/tests_api.py
@@ -988,6 +988,40 @@ class AlgorithmCreation(AlgorithmsAPIBase):
         self.assertEqual(algorithm.source_code, algorithm.fork_of.source_code)
 
 
+    def test_cxx_algorithm_version(self):
+        self.client.login(username='jackdoe', password='1234')
+
+        response = self.client.post(self.url,
+            json.dumps({
+                'name': 'binary_personal',
+                'description': 'blah',
+                'declaration': AlgorithmsAPIBase.CXX_DECLARATION,
+                'previous_version': 'jackdoe/binary_personal/1',
+            }), content_type='application/json')
+
+        url = reverse('api_algorithms:object', args=['jackdoe', 'binary_personal', 2])
+        content = self.checkResponse(response, 201,
+                                     content_type='application/json',
+                                     location=url)
+
+        self.assertTrue(isinstance(content, dict))
+        self.assertEqual(content['name'], 'binary_personal')
+        self.assertTrue(content['url'].endswith(url))
+
+        try:
+            algorithm = Algorithm.objects.get(author__username='jackdoe', name='binary_personal', version=2)
+        except:
+            self.assertTrue(False)
+
+        self.assertEqual(algorithm.short_description, '')
+        self.assertEqual(algorithm.description, 'blah')
+        self.assertEqual(algorithm.language, Code.CXX)
+        self.assertTrue(algorithm.valid())
+        self.checkAlgorithm(algorithm, declaration=AlgorithmsAPIBase.CXX_DECLARATION, code=None)
+
+        self.assertEqual(algorithm.source_code, algorithm.previous_version.source_code)
+
+
 class AlgorithmUpdate(AlgorithmsAPIBase):
     def setUp(self):
         super(AlgorithmUpdate, self).setUp()
diff --git a/beat/web/algorithms/views.py b/beat/web/algorithms/views.py
index d7a0c9a58..a42fd4699 100755
--- a/beat/web/algorithms/views.py
+++ b/beat/web/algorithms/views.py
@@ -75,12 +75,18 @@ def create(request, name=None):
 
         description = previous_version.description
 
-        parameters['algorithm_version'] = previous_version.version + 1
-        parameters['source_code']       = previous_version.source_code_file.read()
-        parameters['declaration']       = previous_version.declaration_file.read().replace('\n', '')
-        parameters['short_description'] = previous_version.short_description
-        parameters['description']       = description.replace('\n', '\\n')
-        parameters['html_description']  = restructuredtext(description).replace('\n', '')
+        parameters['algorithm_version']       = previous_version.version + 1
+        parameters['declaration']             = previous_version.declaration_file.read().replace('\n', '')
+        parameters['short_description']       = previous_version.short_description
+        parameters['description']             = description.replace('\n', '\\n')
+        parameters['html_description']        = restructuredtext(description).replace('\n', '')
+        parameters['algorithm_language']      = previous_version.json_language
+        parameters['algorithm_language_name'] = previous_version.language_fullname()
+        parameters['binary']                  = previous_version.is_binary()
+        parameters['new_version']             = True
+
+        if not previous_version.is_binary():
+            parameters['source_code'] = previous_version.source_code_file.read()
     else:
         parameters['source_code'] = prototypes.binary_load('algorithm.py')  \
                                            .replace('\n\n        # TODO: Implement this algorithm\n\n',
-- 
GitLab