Merge item snapshot and item to be one model.

This commit is contained in:
Tom Hacohen
2020-02-20 12:02:59 +02:00
parent 818bb8d70f
commit 0a3bb6f4bb
6 changed files with 123 additions and 34 deletions

View File

@@ -12,6 +12,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pathlib import Path
from django.db import models
from django.conf import settings
from django.core.validators import RegexValidator
@@ -33,45 +35,41 @@ class Collection(models.Model):
def __str__(self):
return self.uid
@cached_property
def current_items(self):
return self.items.filter(current=True)
class CollectionItem(models.Model):
uid = models.CharField(db_index=True, blank=False, null=False,
max_length=44, validators=[UidValidator])
version = models.PositiveSmallIntegerField()
encryptionKey = models.BinaryField(editable=True, blank=False, null=False)
collection = models.ForeignKey(Collection, on_delete=models.CASCADE)
collection = models.ForeignKey(Collection, related_name='items', on_delete=models.CASCADE)
hmac = models.CharField(max_length=50, blank=False, null=False)
current = models.BooleanField(db_index=True, default=True)
class Meta:
unique_together = ('uid', 'collection')
@cached_property
def content(self):
return self.snapshots.get(current=True)
def __str__(self):
return self.uid
class CollectionItemSnapshot(models.Model):
item = models.ForeignKey(CollectionItem, related_name='snapshots', on_delete=models.CASCADE)
current = models.BooleanField(default=True)
chunkHmac = models.CharField(max_length=50, blank=False, null=False)
class Meta:
unique_together = ('item', 'current')
def __str__(self):
return "{}, current={}".format(self.item.uid, self.current)
def chunk_directory_path(instance, filename):
col = instance.itemSnapshot.item.collection
user_id = col.owner.id
return Path('user_{}'.format(user_id), col.uid, instance.uid)
class CollectionItemChunk(models.Model):
uid = models.CharField(db_index=True, blank=False, null=False,
max_length=44, validators=[UidValidator])
itemSnapshot = models.ForeignKey(CollectionItemSnapshot, related_name='chunks', null=True, on_delete=models.SET_NULL)
items = models.ManyToManyField(CollectionItem, related_name='chunks')
order = models.CharField(max_length=100, blank=False, null=False)
# We probably just want to implement this manually because we can have more than one pointing to a file. chunkFile = models.FileField(upload_to=chunk_directory_path)
class Meta:
# unique_together = ('itemSnapshot', 'order') # Currently off because we set the item snapshot to null on deletion
ordering = ['order']
def __str__(self):