CollectionMember: add stokens when we create/change the member.

This commit is contained in:
Tom Hacohen 2020-05-27 10:52:27 +03:00
parent 91aadb6565
commit 1f18f4e50b
4 changed files with 50 additions and 2 deletions

View File

@ -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'),
),
]

View File

@ -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),
]

View File

@ -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)

View File

@ -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,