Collection: implement collection membership.

This commit is contained in:
Tom Hacohen 2020-02-26 20:38:07 +02:00
parent e0d593a9b6
commit be11e3e0e6
3 changed files with 57 additions and 10 deletions

View File

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

View File

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

View File

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