From c14b4d2e2384304c59422f8579dea01bf412483f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 11 May 2020 16:32:49 +0200
Subject: [PATCH 1/2] agendas: add user_external_id field on Booking (#40719)

---
 .../migrations/0043_booking_user_external_id.py | 17 +++++++++++++++++
 chrono/agendas/models.py                        | 16 ++++++----------
 chrono/api/views.py                             |  2 ++
 tests/test_api.py                               | 11 ++++++++---
 4 files changed, 33 insertions(+), 13 deletions(-)
 create mode 100644 chrono/agendas/migrations/0043_booking_user_external_id.py

diff --git a/chrono/agendas/migrations/0043_booking_user_external_id.py b/chrono/agendas/migrations/0043_booking_user_external_id.py
new file mode 100644
index 0000000..3ea366f
--- /dev/null
+++ b/chrono/agendas/migrations/0043_booking_user_external_id.py
@@ -0,0 +1,17 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('agendas', '0042_auto_20200503_1231'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='booking', name='user_external_id', field=models.CharField(blank=True, max_length=250),
+        ),
+    ]
diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 3959d65..4f051ef 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -15,6 +15,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import copy
 import datetime
 import math
 import requests
@@ -660,6 +661,7 @@ class Booking(models.Model):
     user_display_label = models.CharField(
         verbose_name=_('Label displayed to user'), max_length=250, blank=True
     )
+    user_external_id = models.CharField(max_length=250, blank=True)
     user_name = models.CharField(max_length=250, blank=True)
     backoffice_url = models.URLField(blank=True)
 
@@ -717,16 +719,10 @@ class Booking(models.Model):
         return ics.serialize()
 
     def clone(self, in_waiting_list=False, primary_booking=None, save=True):
-        new_booking = Booking(
-            primary_booking=primary_booking,
-            event_id=self.event_id,
-            in_waiting_list=in_waiting_list,
-            label=self.label,
-            user_name=self.user_name,
-            backoffice_url=self.backoffice_url,
-            user_display_label=self.user_display_label,
-            extra_data=self.extra_data,
-        )
+        new_booking = copy.deepcopy(self)
+        new_booking.id = None
+        new_booking.in_waiting_list = in_waiting_list
+        new_booking.primary_booking = primary_booking
         if save:
             new_booking.save()
         return new_booking
diff --git a/chrono/api/views.py b/chrono/api/views.py
index 1c49120..f16a054 100644
--- a/chrono/api/views.py
+++ b/chrono/api/views.py
@@ -438,6 +438,7 @@ class SlotSerializer(serializers.Serializer):
     '''
 
     label = serializers.CharField(max_length=250, allow_blank=True)
+    user_external_id = serializers.CharField(max_length=250, allow_blank=True)
     user_name = serializers.CharField(max_length=250, allow_blank=True)
     user_display_label = serializers.CharField(max_length=250, allow_blank=True)
     backoffice_url = serializers.URLField(allow_blank=True)
@@ -741,6 +742,7 @@ class Fillslots(APIView):
                         event_id=event.id,
                         in_waiting_list=in_waiting_list,
                         label=payload.get('label', ''),
+                        user_external_id=payload.get('user_external_id', ''),
                         user_name=payload.get('user_name', ''),
                         backoffice_url=payload.get('backoffice_url', ''),
                         user_display_label=payload.get('user_display_label', ''),
diff --git a/tests/test_api.py b/tests/test_api.py
index cb0bb6e..f9527bc 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -675,14 +675,19 @@ def test_booking_api_fillslots(app, some_data, user):
         params={
             'slots': events_ids,
             'label': 'foo',
+            'user_external_id': 'some_external_id',
             'user_name': 'bar',
+            'user_display_label': 'foo',
             'backoffice_url': 'http://example.net/',
         },
     )
     booking_id = resp.json['booking_id']
-    assert Booking.objects.get(id=booking_id).label == 'foo'
-    assert Booking.objects.get(id=booking_id).user_name == 'bar'
-    assert Booking.objects.get(id=booking_id).backoffice_url == 'http://example.net/'
+    booking = Booking.objects.get(pk=booking_id)
+    assert booking.label == 'foo'
+    assert booking.user_external_id == 'some_external_id'
+    assert booking.user_name == 'bar'
+    assert booking.user_display_label == 'foo'
+    assert booking.backoffice_url == 'http://example.net/'
     assert Booking.objects.filter(primary_booking=booking_id, label='foo').count() == 2
     # cancel
     cancel_url = resp.json['api']['cancel_url']
-- 
2.20.1

