diff --git a/django_etesync/migrations/0019_collectionmember.py b/django_etesync/migrations/0019_collectionmember.py new file mode 100644 index 0000000..142e945 --- /dev/null +++ b/django_etesync/migrations/0019_collectionmember.py @@ -0,0 +1,29 @@ +# Generated by Django 3.0.3 on 2020-02-26 18:33 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('django_etesync', '0018_auto_20200226_1803'), + ] + + operations = [ + migrations.CreateModel( + name='CollectionMember', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('encryptionKey', models.BinaryField(editable=True)), + ('accessLevel', models.CharField(choices=[('adm', 'Admin'), ('rw', 'Read Write'), ('ro', 'Read Only')], default='ro', max_length=3)), + ('collection', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='members', to='django_etesync.Collection')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + options={ + 'unique_together': {('user', 'collection')}, + }, + ), + ] diff --git a/django_etesync/models.py b/django_etesync/models.py index b11c36c..3be7b06 100644 --- a/django_etesync/models.py +++ b/django_etesync/models.py @@ -88,3 +88,25 @@ class CollectionItemRevision(models.Model): def __str__(self): return '{} {} current={}'.format(self.item.uid, self.id, self.current) + + +class CollectionMember(models.Model): + class AccessLevels(models.TextChoices): + ADMIN = 'adm' + READ_WRITE = 'rw' + READ_ONLY = 'ro' + + 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) + accessLevel = models.CharField( + max_length=3, + choices=AccessLevels.choices, + default=AccessLevels.READ_ONLY, + ) + + class Meta: + unique_together = ('user', 'collection') + + def __str__(self): + return '{} {}'.format(self.collection.uid, self.user) diff --git a/django_etesync/serializers.py b/django_etesync/serializers.py index 8736a02..c194243 100644 --- a/django_etesync/serializers.py +++ b/django_etesync/serializers.py @@ -31,29 +31,25 @@ class BinaryBase64Field(serializers.Field): class CollectionSerializer(serializers.ModelSerializer): - owner = serializers.SlugRelatedField( - slug_field=User.USERNAME_FIELD, - read_only=True - ) encryptionKey = serializers.SerializerMethodField('get_key_from_context') - permissions = serializers.SerializerMethodField('get_permission_from_context') + accessLevel = serializers.SerializerMethodField('get_access_level_from_context') ctag = serializers.SerializerMethodField('get_ctag') class Meta: model = models.Collection - fields = ('uid', 'version', 'owner', 'encryptionKey', 'permissions', 'ctag') + fields = ('uid', 'version', 'accessLevel', 'encryptionKey', 'ctag') def get_key_from_context(self, obj): request = self.context.get('request', None) if request is not None: - return 'FIXME' + return obj.members.get(user=request.user).encryptionKey return None - def get_permission_from_context(self, obj): + def get_access_level_from_context(self, obj): request = self.context.get('request', None) if request is not None: - return 'FIXME' - return 'readOnly' + return obj.members.get(user=request.user).accessLevel + return None def get_ctag(self, obj): return 'FIXME'