Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

426 optimize fm table #429

Merged
merged 14 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.2.2 on 2023-10-19 12:50

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('core_analysis', '0018_alter_dockerprocess_resources'),
]

operations = [
migrations.RemoveField(
model_name='dockerprocess',
name='file_to_analyse',
),
migrations.RemoveField(
model_name='dockerprocess',
name='owner',
),
migrations.DeleteModel(
name='Analysis',
),
migrations.DeleteModel(
name='DockerProcess',
),
]
18 changes: 18 additions & 0 deletions backend/core/fileupload/migrations/0012_alter_license_label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.2 on 2023-10-19 12:50

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core_fileupload', '0011_analysisresult'),
]

operations = [
migrations.AlterField(
model_name='license',
name='label',
field=models.TextField(default='CC BY - SA 4.0 DEED'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.2 on 2023-10-23 13:08

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('core_fileupload', '0012_alter_license_label'),
]

operations = [
migrations.AlterField(
model_name='file',
name='family',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='core_fileupload.family'),
),
migrations.AlterField(
model_name='file',
name='version',
field=models.CharField(blank=True, max_length=16, null=True),
),
]
18 changes: 18 additions & 0 deletions backend/core/fileupload/migrations/0014_file_private.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.2 on 2023-10-26 07:29

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core_fileupload', '0013_file_private_alter_file_family_alter_file_version'),
]

operations = [
migrations.AddField(
model_name='file',
name='private',
field=models.BooleanField(default=False),
),
]
35 changes: 28 additions & 7 deletions backend/core/fileupload/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from django.db import models
from django.db.models.signals import post_migrate
from django.dispatch import receiver

from core.user.models import User
from django.core.files.base import ContentFile
from django.template.defaultfilters import slugify # new
Expand Down Expand Up @@ -67,21 +70,35 @@ def create(self, **kwargs):
lic.save()
return lic

def create_default_license(self):
"""
Creates the default license.
"""
default_license_label = "CC BY - SA 4.0 DEED"
default_license, created = self.get_or_create(label=default_license_label)
return default_license

class License(models.Model):
"""
Data Model for a license in the backend
"""

objects = LicenseManager()
_default_license = "CC BY - Mention"
_default_license = "CC BY - SA 4.0 DEED"

label = models.TextField(blank=False, default=_default_license)

def __str__(self):
# do not change that
return f"{self.id}"

@receiver(post_migrate)
def create_default_license(sender, **kwargs):
"""
Creates the default license after migrations.
"""
License.objects.create_default_license()


# -------------------------------------------------- File Model --------------------------------------------------
class FileManager(models.Manager):
Expand All @@ -101,13 +118,14 @@ def save_file(self, local_file, **kwargs):
if tags is None:
raise TypeError("Tags is not set")
family = kwargs.get("family", None)
# get file from id
if kwargs.get("version", None) is None:
raise TypeError("Version is not set")
version = kwargs.get("version", None)
private = kwargs.get("private", None)
# get license from id
if kwargs.get("license", None) is None:
if kwargs.get("license", None) is None and kwargs.get("private") is None:
raise TypeError("License not set!")

file = self.model(**kwargs)

file.save()
file.tags.set(tags)
file.save()
Expand Down Expand Up @@ -140,14 +158,14 @@ class File(models.Model):
relative_upload_dir = "files/"

owner = models.ForeignKey(User, on_delete=models.RESTRICT)
family = models.ForeignKey(Family, on_delete=models.CASCADE)
family = models.ForeignKey(Family, on_delete=models.CASCADE, null=True, blank=True)
label = models.CharField(blank=False, max_length=255)
description = models.TextField(blank=True)
local_file = models.FileField(upload_to=relative_upload_dir)
uploaded_at = models.DateTimeField(auto_now_add=True)
license = models.ForeignKey(License, on_delete=models.CASCADE)
tags = models.ManyToManyField(Tag)
version = models.CharField(blank=False, null=False, max_length=16)
version = models.CharField(blank=True, null=True, max_length=16)
transpiled_file = models.FileField(
null=True, blank=True, upload_to=relative_upload_dir
)
Expand All @@ -159,6 +177,9 @@ class File(models.Model):
) # indicates if the user confirmed the upload
slug = models.SlugField(null=True)
confirmation_token = models.CharField(default="", max_length=255)
private = models.BooleanField(
default=False
)

def __str__(self):
# do not change that
Expand Down
6 changes: 5 additions & 1 deletion backend/core/fileupload/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def validate(self, data):
"""
Check that the uploaded file contains valid xml
"""
family = data.get('family', None)
if family is None:
data['family'] = None
try:
contents = ""
for line in data['local_file']:
Expand Down Expand Up @@ -108,7 +111,8 @@ def to_internal_value(self, data):

tags = list(map(lambda tag: Tag.objects.get(id=tag), data.getlist('tags')))

if 'family' in data:
family = None
if 'family' in data and data['family']:
family = Family.objects.get(id=data['family'])

license = License.objects.get(id=data['license'])
Expand Down
6 changes: 3 additions & 3 deletions backend/core/fileupload/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,21 +522,21 @@ def test_license_list_logged_in_admin(self):
self.client.force_authenticate(self.admin)
res = self.client.get("/licenses/")
json = res.json()
self.assertEqual(len(json), 2)
self.assertEqual(len(json), 3)

def test_license_list_logged_in_user(self):
# Licenses are listable when logged in with non-admin user
self.client.force_authenticate(self.user)
res = self.client.get("/licenses/")
json = res.json()
self.assertEqual(len(json), 2)
self.assertEqual(len(json), 3)

def test_license_list_logged_out(self):
# Licenses are listable when logged out
self.client.force_authenticate(None)
res = self.client.get("/licenses/")
json = res.json()
self.assertEqual(len(json), 2)
self.assertEqual(len(json), 3)

def test_license_retrieve_logged_in_admin(self):
# License is retrievable when logged in
Expand Down
Loading
Loading