From 1f18f4e50bc228f165afacba163e73b339755c7d Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 27 May 2020 10:52:27 +0300 Subject: [PATCH] CollectionMember: add stokens when we create/change the member. --- .../0011_collectionmember_stoken.py | 19 +++++++++++++++ .../migrations/0012_auto_20200527_0743.py | 23 +++++++++++++++++++ django_etesync/models.py | 1 + django_etesync/serializers.py | 9 ++++++-- 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 django_etesync/migrations/0011_collectionmember_stoken.py create mode 100644 django_etesync/migrations/0012_auto_20200527_0743.py diff --git a/django_etesync/migrations/0011_collectionmember_stoken.py b/django_etesync/migrations/0011_collectionmember_stoken.py new file mode 100644 index 0000000..6b79bf6 --- /dev/null +++ b/django_etesync/migrations/0011_collectionmember_stoken.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.3 on 2020-05-27 07:43 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_etesync', '0010_auto_20200526_1539'), + ] + + operations = [ + migrations.AddField( + model_name='collectionmember', + name='stoken', + field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.PROTECT, to='django_etesync.Stoken'), + ), + ] diff --git a/django_etesync/migrations/0012_auto_20200527_0743.py b/django_etesync/migrations/0012_auto_20200527_0743.py new file mode 100644 index 0000000..28b8745 --- /dev/null +++ b/django_etesync/migrations/0012_auto_20200527_0743.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.3 on 2020-05-27 07:43 + +from django.db import migrations + + +def create_stokens(apps, schema_editor): + Stoken = apps.get_model('django_etesync', 'Stoken') + CollectionMember = apps.get_model('django_etesync', 'CollectionMember') + + for member in CollectionMember.objects.all(): + member.stoken = Stoken.objects.create() + member.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_etesync', '0011_collectionmember_stoken'), + ] + + operations = [ + migrations.RunPython(create_stokens), + ] diff --git a/django_etesync/models.py b/django_etesync/models.py index c9c95a1..ca68209 100644 --- a/django_etesync/models.py +++ b/django_etesync/models.py @@ -139,6 +139,7 @@ class AccessLevels(models.TextChoices): class CollectionMember(models.Model): + stoken = models.OneToOneField(Stoken, on_delete=models.PROTECT, null=True) collection = models.ForeignKey(Collection, related_name='members', on_delete=models.CASCADE) user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) encryptionKey = models.BinaryField(editable=True, blank=False, null=False) diff --git a/django_etesync/serializers.py b/django_etesync/serializers.py index f78803a..7079742 100644 --- a/django_etesync/serializers.py +++ b/django_etesync/serializers.py @@ -212,6 +212,7 @@ class CollectionSerializer(serializers.ModelSerializer): process_revisions_for_item(main_item, revision_data) models.CollectionMember(collection=instance, + stoken=models.Stoken.objects.create(), user=validated_data.get('owner'), accessLevel=models.AccessLevels.ADMIN, encryptionKey=encryption_key, @@ -258,8 +259,11 @@ class CollectionMemberSerializer(serializers.ModelSerializer): def update(self, instance, validated_data): with transaction.atomic(): # We only allow updating accessLevel - instance.accessLevel = validated_data.pop('accessLevel') - instance.save() + access_level = validated_data.pop('accessLevel') + if instance.accessLevel != access_level: + instance.stoken = models.Stoken.objects.create() + instance.accessLevel = access_level + instance.save() return instance @@ -314,6 +318,7 @@ class InvitationAcceptSerializer(serializers.Serializer): member = models.CollectionMember.objects.create( collection=invitation.collection, + stoken=models.Stoken.objects.create(), user=invitation.user, accessLevel=invitation.accessLevel, encryptionKey=encryption_key,