2020-02-19 18:53:43 +00:00
|
|
|
# Copyright © 2017 Tom Hacohen
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, version 3.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-02-20 10:02:59 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2020-02-19 12:55:56 +00:00
|
|
|
from django.db import models
|
2020-02-19 18:53:43 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.validators import RegexValidator
|
|
|
|
from django.utils.functional import cached_property
|
|
|
|
|
|
|
|
|
2020-03-10 14:40:42 +00:00
|
|
|
Base64Url256BitValidator = RegexValidator(regex=r'^[a-zA-Z0-9\-_]{43}=?$', message='Expected a 256bit base64url.')
|
|
|
|
UidValidator = RegexValidator(regex=r'[a-zA-Z0-9]', message='Not a valid UID')
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Collection(models.Model):
|
|
|
|
uid = models.CharField(db_index=True, blank=False, null=False,
|
|
|
|
max_length=44, validators=[UidValidator])
|
|
|
|
version = models.PositiveSmallIntegerField()
|
|
|
|
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
2020-03-12 19:02:27 +00:00
|
|
|
mainItem = models.OneToOneField('CollectionItem', related_name='of_collection', on_delete=models.PROTECT)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('uid', 'owner')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.uid
|
|
|
|
|
2020-03-12 19:02:27 +00:00
|
|
|
@cached_property
|
|
|
|
def content(self):
|
|
|
|
return self.mainItem.content
|
|
|
|
|
2020-02-20 11:56:16 +00:00
|
|
|
|
2020-02-19 18:53:43 +00:00
|
|
|
class CollectionItem(models.Model):
|
2020-03-12 19:02:27 +00:00
|
|
|
uid = models.CharField(db_index=True, blank=False, null=True,
|
2020-02-19 18:53:43 +00:00
|
|
|
max_length=44, validators=[UidValidator])
|
2020-02-20 10:02:59 +00:00
|
|
|
collection = models.ForeignKey(Collection, related_name='items', on_delete=models.CASCADE)
|
2020-03-10 15:49:23 +00:00
|
|
|
version = models.PositiveSmallIntegerField()
|
2020-03-12 19:02:27 +00:00
|
|
|
encryptionKey = models.BinaryField(editable=True, blank=False, null=True)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('uid', 'collection')
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-03-12 19:02:27 +00:00
|
|
|
return '{} {}'.format(self.uid, self.collection.uid)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
2020-02-20 11:56:16 +00:00
|
|
|
@cached_property
|
|
|
|
def content(self):
|
2020-02-20 20:41:39 +00:00
|
|
|
return self.revisions.get(current=True)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
|
2020-02-20 12:42:35 +00:00
|
|
|
def chunk_directory_path(instance, filename):
|
|
|
|
item = instance.item
|
|
|
|
col = item.collection
|
|
|
|
user_id = col.owner.id
|
|
|
|
return Path('user_{}'.format(user_id), col.uid, item.uid, instance.uid)
|
|
|
|
|
|
|
|
|
2020-02-19 18:53:43 +00:00
|
|
|
class CollectionItemChunk(models.Model):
|
|
|
|
uid = models.CharField(db_index=True, blank=False, null=False,
|
2020-03-10 14:40:42 +00:00
|
|
|
max_length=44, validators=[Base64Url256BitValidator])
|
2020-02-20 11:56:16 +00:00
|
|
|
item = models.ForeignKey(CollectionItem, related_name='chunks', on_delete=models.CASCADE)
|
2020-02-19 18:53:43 +00:00
|
|
|
order = models.CharField(max_length=100, blank=False, null=False)
|
2020-02-20 12:48:19 +00:00
|
|
|
chunkFile = models.FileField(upload_to=chunk_directory_path, max_length=150, unique=True)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
class Meta:
|
2020-02-20 11:56:16 +00:00
|
|
|
unique_together = ('item', 'order')
|
2020-03-10 15:56:24 +00:00
|
|
|
ordering = ('item', 'order')
|
2020-02-19 12:55:56 +00:00
|
|
|
|
2020-02-19 18:53:43 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.uid
|
2020-02-20 11:56:16 +00:00
|
|
|
|
|
|
|
|
2020-02-20 20:41:39 +00:00
|
|
|
class CollectionItemRevision(models.Model):
|
2020-03-12 13:52:36 +00:00
|
|
|
uid = models.CharField(db_index=True, unique=True, blank=False, null=False,
|
|
|
|
max_length=44, validators=[UidValidator])
|
2020-02-20 20:41:39 +00:00
|
|
|
item = models.ForeignKey(CollectionItem, related_name='revisions', on_delete=models.CASCADE)
|
2020-03-12 14:02:00 +00:00
|
|
|
meta = models.BinaryField(editable=True, blank=True, null=True)
|
2020-02-20 11:56:16 +00:00
|
|
|
chunks = models.ManyToManyField(CollectionItemChunk, related_name='items')
|
|
|
|
hmac = models.CharField(max_length=50, blank=False, null=False)
|
2020-02-26 18:04:26 +00:00
|
|
|
current = models.BooleanField(db_index=True, default=True, null=True)
|
2020-02-26 14:55:47 +00:00
|
|
|
deleted = models.BooleanField(default=False)
|
2020-02-20 11:56:16 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('item', 'current')
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-03-12 13:52:36 +00:00
|
|
|
return '{} {} current={}'.format(self.uid, self.item.uid, self.current)
|
2020-02-26 18:38:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|