Revision: add a proper uid for revisions (which we also use for sync tag).

master
Tom Hacohen 5 years ago
parent 23edc29bb8
commit f8a94eeb04

@ -0,0 +1,19 @@
# Generated by Django 3.0.3 on 2020-03-12 13:41
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('django_etesync', '0023_auto_20200310_1556'),
]
operations = [
migrations.AddField(
model_name='collectionitemrevision',
name='uid',
field=models.CharField(db_index=True, max_length=44, validators=[django.core.validators.RegexValidator(message='Not a valid UID', regex='[a-zA-Z0-9]')]),
),
]

@ -0,0 +1,19 @@
# Generated by Django 3.0.3 on 2020-03-12 13:50
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('django_etesync', '0024_collectionitemrevision_uid'),
]
operations = [
migrations.AlterField(
model_name='collectionitemrevision',
name='uid',
field=models.CharField(db_index=True, max_length=44, unique=True, validators=[django.core.validators.RegexValidator(message='Not a valid UID', regex='[a-zA-Z0-9]')]),
),
]

@ -78,6 +78,8 @@ class CollectionItemChunk(models.Model):
class CollectionItemRevision(models.Model): class CollectionItemRevision(models.Model):
uid = models.CharField(db_index=True, unique=True, blank=False, null=False,
max_length=44, validators=[UidValidator])
item = models.ForeignKey(CollectionItem, related_name='revisions', on_delete=models.CASCADE) item = models.ForeignKey(CollectionItem, related_name='revisions', on_delete=models.CASCADE)
chunks = models.ManyToManyField(CollectionItemChunk, related_name='items') chunks = models.ManyToManyField(CollectionItemChunk, related_name='items')
hmac = models.CharField(max_length=50, blank=False, null=False) hmac = models.CharField(max_length=50, blank=False, null=False)
@ -88,7 +90,7 @@ class CollectionItemRevision(models.Model):
unique_together = ('item', 'current') unique_together = ('item', 'current')
def __str__(self): def __str__(self):
return '{} {} current={}'.format(self.item.uid, self.id, self.current) return '{} {} current={}'.format(self.uid, self.item.uid, self.current)
class CollectionMember(models.Model): class CollectionMember(models.Model):

@ -16,12 +16,17 @@ import base64
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.db import transaction from django.db import transaction
from django.utils.crypto import get_random_string
from rest_framework import serializers from rest_framework import serializers
from . import models from . import models
User = get_user_model() User = get_user_model()
def generate_rev_uid(length=32):
return get_random_string(length)
class BinaryBase64Field(serializers.Field): class BinaryBase64Field(serializers.Field):
def to_representation(self, value): def to_representation(self, value):
return base64.b64encode(value).decode('ascii') return base64.b64encode(value).decode('ascii')
@ -54,14 +59,12 @@ class CollectionSerializer(serializers.ModelSerializer):
return None return None
def get_ctag(self, obj): def get_ctag(self, obj):
# FIXME: we need to have something that's more privacy friendly. Can probably just generate a uid per revision
# on revision creation (on the server) and just use that.
last_revision = models.CollectionItemRevision.objects.filter(item__collection=obj).last() last_revision = models.CollectionItemRevision.objects.filter(item__collection=obj).last()
if last_revision is None: if last_revision is None:
# FIXME: what is the etag for None? Though if we use the revision for collection it should be shared anyway. # FIXME: what is the etag for None? Though if we use the revision for collection it should be shared anyway.
return None return None
return str(last_revision.id) return last_revision.uid
def create(self, validated_data): def create(self, validated_data):
"""Function that's called when this serializer creates an item""" """Function that's called when this serializer creates an item"""
@ -148,7 +151,8 @@ class CollectionItemSerializer(serializers.ModelSerializer):
instance.save() instance.save()
chunks = revision_data.pop('chunks') chunks = revision_data.pop('chunks')
revision = models.CollectionItemRevision.objects.create(**revision_data, item=instance) revision = models.CollectionItemRevision.objects.create(**revision_data, uid=generate_rev_uid(),
item=instance)
revision.chunks.set(chunks) revision.chunks.set(chunks)
return instance return instance
@ -165,7 +169,8 @@ class CollectionItemSerializer(serializers.ModelSerializer):
current_revision.save() current_revision.save()
chunks = revision_data.pop('chunks') chunks = revision_data.pop('chunks')
revision = models.CollectionItemRevision.objects.create(**revision_data, item=instance) revision = models.CollectionItemRevision.objects.create(**revision_data, uid=generate_rev_uid(),
item=instance)
revision.chunks.set(chunks) revision.chunks.set(chunks)
return instance return instance

Loading…
Cancel
Save