From 118a11937dae496569167daf737141a25c370314 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 14 Oct 2019 14:05:26 +0200
Subject: [PATCH 3/5] dj2: use set and/or clear method for M2M (#36895)

remove RemovedInDjango20Warning:
Direct assignment to the forward side of a many-to-many set is deprecated due to the implicit save() that happens.
Use items.set() instead.
---
 combo/apps/lingo/views.py   |  2 +-
 combo/manager/forms.py      | 10 +++++-----
 tests/test_dataviz.py       |  4 ++--
 tests/test_import_export.py |  4 ++--
 tests/test_lingo_manager.py |  4 ++--
 tests/test_pages.py         |  4 ++--
 tests/test_public.py        |  8 ++++----
 7 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/combo/apps/lingo/views.py b/combo/apps/lingo/views.py
index 8804dca..7ba92f6 100644
--- a/combo/apps/lingo/views.py
+++ b/combo/apps/lingo/views.py
@@ -343,7 +343,7 @@ class PayMixin(object):
 
         transaction.save()
         transaction.regie = regie
-        transaction.items = items
+        transaction.items.set(items)
         transaction.remote_items = ','.join([x.id for x in remote_items])
         transaction.status = 0
         transaction.amount = total_amount
diff --git a/combo/manager/forms.py b/combo/manager/forms.py
index 9ca5441..9a4e3dd 100644
--- a/combo/manager/forms.py
+++ b/combo/manager/forms.py
@@ -149,23 +149,23 @@ class CellVisibilityForm(forms.Form):
         if self.cleaned_data['visibility'] == 'all':
             self.instance.public = True
             self.instance.restricted_to_unlogged = False
-            self.instance.groups = []
+            self.instance.groups.clear()
         elif self.cleaned_data['visibility'] == 'logged':
             self.instance.public = False
             self.instance.restricted_to_unlogged = False
-            self.instance.groups = []
+            self.instance.groups.clear()
         elif self.cleaned_data['visibility'] == 'unlogged':
             self.instance.public = True
             self.instance.restricted_to_unlogged = True
-            self.instance.groups = []
+            self.instance.groups.clear()
         elif self.cleaned_data['visibility'] == 'groups-any':
             self.instance.public = False
             self.instance.restricted_to_unlogged = False
-            self.instance.groups = self.cleaned_data['groups']
+            self.instance.groups.set(self.cleaned_data['groups'])
         elif self.cleaned_data['visibility'] == 'groups-none':
             self.instance.public = False
             self.instance.restricted_to_unlogged = True
-            self.instance.groups = self.cleaned_data['groups']
+            self.instance.groups.set(self.cleaned_data['groups'])
         self.instance.save()
         return self.instance
 
diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py
index 8fcb349..90bfc3b 100644
--- a/tests/test_dataviz.py
+++ b/tests/test_dataviz.py
@@ -302,14 +302,14 @@ def test_chartng_cell_view(app, normal_user):
             group = Group(name='plop')
             group.save()
             cell.public = False
-            cell.groups = [group]
+            cell.groups.set([group])
             cell.save()
             resp = app.get('/api/dataviz/graph/1/?width=400', status=403)
 
             app = login(app, username='normal-user', password='normal-user')
             resp = app.get('/api/dataviz/graph/1/?width=400', status=403)
 
-            normal_user.groups = [group]
+            normal_user.groups.set([group])
             normal_user.save()
             resp = app.get('/api/dataviz/graph/1/?width=400', status=200)
 
diff --git a/tests/test_import_export.py b/tests/test_import_export.py
index 9661c36..0d2c3fe 100644
--- a/tests/test_import_export.py
+++ b/tests/test_import_export.py
@@ -142,11 +142,11 @@ def test_group_restrictions_import_export(app, some_data):
     group.save()
 
     page = Page.objects.get(slug='one')
-    page.groups = [group]
+    page.groups.set([group])
     page.save()
 
     cell = TextCell.objects.get(order=0)
-    cell.groups = [group]
+    cell.groups.set([group])
     cell.save()
 
     output = get_output_of_command('export_site')
diff --git a/tests/test_lingo_manager.py b/tests/test_lingo_manager.py
index f1a93f7..4dccd3a 100644
--- a/tests/test_lingo_manager.py
+++ b/tests/test_lingo_manager.py
@@ -163,9 +163,9 @@ def test_download_transaction(app, admin_user, payment_backend):
                                         bank_transaction_id='567', status=eopayment.PAID)
     trans2 = Transaction.objects.create(regie=regie, remote_items='remote items omg', order_id='2', user=user,
                                         bank_transaction_id='136', status=eopayment.PAID)
-    trans1.items = [b_item,]
+    trans1.items.set([b_item])
     trans1.save()
-    trans2.items = [b_item,]
+    trans2.items.set([b_item])
     trans2.save()
     app = login(app)
     resp = app.get('/manage/lingo/transactions/download-csv/', status=200)
diff --git a/tests/test_pages.py b/tests/test_pages.py
index b8f40a9..1164100 100644
--- a/tests/test_pages.py
+++ b/tests/test_pages.py
@@ -111,14 +111,14 @@ def test_page_visibility():
     group.save()
     user1 = User(username='foo')
     user1.save()
-    user1.groups = [group]
+    user1.groups.set([group])
     user2 = User(username='bar')
     user2.save()
 
     assert page.is_visible(user1)
     assert page.is_visible(user2)
 
-    page.groups = [group]
+    page.groups.set([group])
     assert not page.is_visible()
     assert page.is_visible(user1)
     assert not page.is_visible(user2)
diff --git a/tests/test_public.py b/tests/test_public.py
index 1131265..99997d2 100644
--- a/tests/test_public.py
+++ b/tests/test_public.py
@@ -99,7 +99,7 @@ def test_page_contents_group_presence(app, normal_user):
     cell = TextCell(page=page, placeholder='content', text='Foobar', order=0,
             public=False)
     cell.save()
-    cell.groups = [group]
+    cell.groups.set([group])
     resp = app.get('/', status=200)
     assert 'Foobar' not in resp.text
 
@@ -107,7 +107,7 @@ def test_page_contents_group_presence(app, normal_user):
     resp = app.get('/', status=200)
     assert 'Foobar' not in resp.text
 
-    normal_user.groups = [group]
+    normal_user.groups.set([group])
     resp = app.get('/', status=200)
     assert 'Foobar' in resp.text
 
@@ -120,7 +120,7 @@ def test_page_contents_group_absence(app, normal_user):
     cell = TextCell(page=page, placeholder='content', text='Foobar', order=0,
             public=False, restricted_to_unlogged=True)
     cell.save()
-    cell.groups = [group]
+    cell.groups.set([group])
     resp = app.get('/', status=200)
     assert 'Foobar' not in resp.text
 
@@ -128,7 +128,7 @@ def test_page_contents_group_absence(app, normal_user):
     resp = app.get('/', status=200)
     assert 'Foobar' in resp.text
 
-    normal_user.groups = [group]
+    normal_user.groups.set([group])
     resp = app.get('/', status=200)
     assert 'Foobar' not in resp.text
 
-- 
2.20.1

