Repository: tournesol-app/tournesol

Issue:
[back] feat: do not expose unused voucher fields
If we don't want users to change the public or private state of their vouchers, we can remove if from the API.

If we don't want users to change the value of their vouchers, we can remove the `value` field from the API, and make it equals to 1 by default. 

If we want to front end to be able to display friendly message when a user vouches two times for the same person, we can change the 400 Bad Request to a 409 Conflict. Read more here: https://github.com/tournesol-app/tournesol/pull/1116


Discussion:

Resolving patch:
diff --git a/backend/vouch/migrations/0004_alter_voucher_is_public_alter_voucher_value.py b/backend/vouch/migrations/0004_alter_voucher_is_public_alter_voucher_value.py
new file mode 100644
--- /dev/null
+++ b/backend/vouch/migrations/0004_alter_voucher_is_public_alter_voucher_value.py
@@ -0,0 +1,23 @@
+# Generated by Django 4.0.7 on 2022-09-16 16:15
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('vouch', '0003_alter_voucher_unique_together'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='voucher',
+            name='is_public',
+            field=models.BooleanField(default=True, help_text='Should the voucher be public?'),
+        ),
+        migrations.AlterField(
+            model_name='voucher',
+            name='value',
+            field=models.FloatField(default=1, help_text='The vouch value given by the vouching user to receiving user.'),
+        ),
+    ]
diff --git a/backend/vouch/models.py b/backend/vouch/models.py
--- a/backend/vouch/models.py
+++ b/backend/vouch/models.py
@@ -31,10 +31,10 @@ class Voucher(models.Model):
         related_name="vouchers_received",
     )
     is_public = models.BooleanField(
-        default=False, help_text="Should the voucher be public?"
+        default=True, help_text="Should the voucher be public?"
     )
     value = models.FloatField(
-        default=0,
+        default=1,
         help_text="The vouch value given by the vouching user to receiving user.",
     )
 
diff --git a/backend/vouch/serializers.py b/backend/vouch/serializers.py
--- a/backend/vouch/serializers.py
+++ b/backend/vouch/serializers.py
@@ -9,7 +9,7 @@
 
 # Allow to keep the JSON representation of `GivenVoucherSerializer` and
 # `ReadOnlyVoucherSerializer` similar.
-VOUCHER_FIELDS = ["by", "to", "is_public", "value"]
+VOUCHER_FIELDS = ["by", "to"]
 
 
 class GivenVoucherSerializer(ModelSerializer):
@@ -71,4 +71,4 @@ class ReadOnlyVoucherSerializer(ModelSerializer):
     class Meta:
         model = Voucher
         fields = VOUCHER_FIELDS
-        read_only_fields = ["to", "by", "is_public", "value"]
+        read_only_fields = ["to", "by"]


Test patch:
diff --git a/backend/vouch/tests/test_api_voucher.py b/backend/vouch/tests/test_api_voucher.py
--- a/backend/vouch/tests/test_api_voucher.py
+++ b/backend/vouch/tests/test_api_voucher.py
@@ -21,8 +21,6 @@ def setUp(self):
 
         self.valid_create_params = {
             "to": "user2",
-            "is_public": False,
-            "value": "1",
         }
 
     def test_anon_401_create(self):
@@ -52,16 +50,14 @@ def test_auth_201_create(self):
         self.assertEqual(Voucher.objects.all().count(), initial_voucher_count + 1)
 
         new_voucher = Voucher.objects.get(by=self.user1.id, to=self.user2.id)
-        self.assertEqual(new_voucher.is_public, self.valid_create_params["is_public"])
-        self.assertEqual(new_voucher.value, float(self.valid_create_params["value"]))
+        self.assertEqual(new_voucher.is_public, True)
+        self.assertEqual(new_voucher.value, 1.0)
 
         self.assertDictEqual(
             response.data,
             {
                 "by": "user1",
                 "to": "user2",
-                "is_public": False,
-                "value": 1,
             },
             response.data,
         )
@@ -231,8 +227,6 @@ def test_auth_200_list(self):
             {
                 "by": "user1",
                 "to": "user2",
-                "is_public": False,
-                "value": 1,
             },
             results[0],
         )
@@ -242,8 +236,6 @@ def test_auth_200_list(self):
             {
                 "by": "user1",
                 "to": "user3",
-                "is_public": True,
-                "value": 2,
             },
             results[1],
         )
@@ -299,8 +291,6 @@ def test_auth_200_list(self):
             {
                 "by": "user1",
                 "to": "user3",
-                "is_public": True,
-                "value": 2,
             },
             results[0],
         )
@@ -310,8 +300,6 @@ def test_auth_200_list(self):
             {
                 "by": "user2",
                 "to": "user3",
-                "is_public": False,
-                "value": 3,
             },
             results[1],
         )