From e03e916d8e202a5403998203823135c84938d190 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 20 Apr 2021 15:39:12 +0200
Subject: [PATCH] misc: remove jsonfield requirement (#53289)

---
 chrono/agendas/migrations/0003_booking.py         |  4 ++--
 .../agendas/migrations/0056_auto_20200811_1611.py |  4 ++--
 .../agendas/migrations/0075_auto_20210216_1553.py |  4 ++--
 chrono/agendas/migrations/0079_text_to_jsonb.py   | 15 +++++++++++++++
 chrono/agendas/models.py                          |  7 +++----
 requirements.txt                                  |  1 -
 setup.py                                          |  1 -
 7 files changed, 24 insertions(+), 12 deletions(-)
 create mode 100644 chrono/agendas/migrations/0079_text_to_jsonb.py

diff --git a/chrono/agendas/migrations/0003_booking.py b/chrono/agendas/migrations/0003_booking.py
index 5ea140f..4593e03 100644
--- a/chrono/agendas/migrations/0003_booking.py
+++ b/chrono/agendas/migrations/0003_booking.py
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals
 
-import jsonfield.fields
+from django.contrib.postgres.fields import JSONField
 from django.db import migrations, models
 
 
@@ -19,7 +19,7 @@ class Migration(migrations.Migration):
                     'id',
                     models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True),
                 ),
-                ('extra_data', jsonfield.fields.JSONField(null=True)),
+                ('extra_data', JSONField(null=True)),
                 ('event', models.ForeignKey(to='agendas.Event', on_delete=models.CASCADE)),
             ],
             options={},
diff --git a/chrono/agendas/migrations/0056_auto_20200811_1611.py b/chrono/agendas/migrations/0056_auto_20200811_1611.py
index aa9a35d..9616fed 100644
--- a/chrono/agendas/migrations/0056_auto_20200811_1611.py
+++ b/chrono/agendas/migrations/0056_auto_20200811_1611.py
@@ -3,7 +3,7 @@
 from __future__ import unicode_literals
 
 import django.db.models.deletion
-import jsonfield.fields
+from django.contrib.postgres.fields import JSONField
 from django.db import migrations, models
 
 
@@ -23,7 +23,7 @@ class Migration(migrations.Migration):
                 ),
                 ('timestamp', models.DateTimeField(auto_now_add=True)),
                 ('seen', models.BooleanField(default=False)),
-                ('booking_errors', jsonfield.fields.JSONField(default=dict)),
+                ('booking_errors', JSONField(default=dict)),
                 ('bookings', models.ManyToManyField(to='agendas.Booking')),
             ],
             options={
diff --git a/chrono/agendas/migrations/0075_auto_20210216_1553.py b/chrono/agendas/migrations/0075_auto_20210216_1553.py
index 05ced74..04bcd22 100644
--- a/chrono/agendas/migrations/0075_auto_20210216_1553.py
+++ b/chrono/agendas/migrations/0075_auto_20210216_1553.py
@@ -3,7 +3,7 @@
 from __future__ import unicode_literals
 
 import django.db.models.deletion
-import jsonfield.fields
+from django.contrib.postgres.fields import JSONField
 from django.db import migrations, models
 
 
@@ -27,7 +27,7 @@ class Migration(migrations.Migration):
         migrations.AddField(
             model_name='event',
             name='recurrence_rule',
-            field=jsonfield.fields.JSONField(null=True, verbose_name='Recurrence rule'),
+            field=JSONField(null=True, verbose_name='Recurrence rule', blank=True),
         ),
         migrations.AddField(
             model_name='event',
diff --git a/chrono/agendas/migrations/0079_text_to_jsonb.py b/chrono/agendas/migrations/0079_text_to_jsonb.py
new file mode 100644
index 0000000..9578ce5
--- /dev/null
+++ b/chrono/agendas/migrations/0079_text_to_jsonb.py
@@ -0,0 +1,15 @@
+from combo.utils.db import EnsureJsonbType
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('agendas', '0078_absence_reasons'),
+    ]
+
+    operations = [
+        EnsureJsonbType(model_name='booking', field_name='extra_data'),
+        EnsureJsonbType(model_name='event', field_name='recurrence_rule'),
+        EnsureJsonbType(model_name='eventcancellationreport', field_name='booking_errors'),
+    ]
diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 03b05f7..5f3ba05 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -30,7 +30,7 @@ import vobject
 from dateutil.rrule import DAILY, WEEKLY, rrule, rruleset
 from django.conf import settings
 from django.contrib.auth.models import Group
-from django.contrib.postgres.fields import ArrayField
+from django.contrib.postgres.fields import ArrayField, JSONField
 from django.core.exceptions import FieldDoesNotExist, ValidationError
 from django.core.validators import MaxValueValidator, MinValueValidator
 from django.db import connection, models, transaction
@@ -47,7 +47,6 @@ from django.utils.timezone import is_aware, localtime, make_aware, make_naive, n
 from django.utils.translation import ugettext
 from django.utils.translation import ugettext_lazy as _
 from django.utils.translation import ungettext
-from jsonfield import JSONField
 
 from chrono.interval import Interval, IntervalSet
 from chrono.utils.requests_wrapper import requests as requests_wrapper
@@ -1089,7 +1088,7 @@ class Event(models.Model):
     agenda = models.ForeignKey(Agenda, on_delete=models.CASCADE)
     start_datetime = models.DateTimeField(_('Date/time'))
     repeat = models.CharField(_('Repeat'), max_length=16, blank=True, choices=REPEAT_CHOICES)
-    recurrence_rule = JSONField(_('Recurrence rule'), null=True)
+    recurrence_rule = JSONField(_('Recurrence rule'), null=True, blank=True)
     recurrence_end_date = models.DateField(_('Recurrence end date'), null=True, blank=True)
     primary_event = models.ForeignKey('self', null=True, on_delete=models.CASCADE, related_name='recurrences')
     duration = models.PositiveIntegerField(_('Duration (in minutes)'), default=None, null=True, blank=True)
@@ -2251,7 +2250,7 @@ class EventCancellationReport(models.Model):
     timestamp = models.DateTimeField(auto_now_add=True)
     seen = models.BooleanField(default=False)
     bookings = models.ManyToManyField(Booking)
-    booking_errors = JSONField()
+    booking_errors = JSONField(default=dict)
 
     def __str__(self):
         return '%s - %s' % (self.timestamp.strftime('%Y-%m-%d %H:%M:%S'), self.event)
diff --git a/requirements.txt b/requirements.txt
index 75fc6d7..303faf3 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,6 +1,5 @@
 django>=1.8, <1.9
 gadjo
 djangorestframework>=3.1, <3.7
-django-jsonfield >= 0.9.3
 requests
 vobject
diff --git a/setup.py b/setup.py
index e10fd47..b2750cc 100644
--- a/setup.py
+++ b/setup.py
@@ -165,7 +165,6 @@ setup(
         'gadjo',
         'djangorestframework>=3.4',
         'django-filter',
-        'django-jsonfield >= 0.9.3',
         'vobject',
         'python-dateutil',
         'requests',
-- 
2.30.2

