-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create files app to manage user uploaded files Signed-off-by: Tmpecho <[email protected]> * created serializers for file and gallery * Move file upload endpoint from content/ to files/, create file and gallery views, add file and gallery model methods Signed-off-by: Tmpecho <[email protected]> * updating progress, needed to rename 'Gallery' to 'UserGallery' as a Gallery model already exists * finished mvp for file project * updating progress * mvp for file uploading/deletion * forgot changelog, oopsie * fixed error in permissions from allowing non-admins to delete files, fixed 'security threat' * Minor fixes to file serializer Signed-off-by: Tmpecho <[email protected]> * Fix whitespace Signed-off-by: Tmpecho <[email protected]> * Add custom exeption with mixins for files class Signed-off-by: Tmpecho <[email protected]> * Update custom file exceptions, set max gallery size as global constant, throw custom exceptions Signed-off-by: Tmpecho <[email protected]> * create test for creating file when not having a gallery, format * removed duplicate method * small refactoring and removed 'url' from file model, changed it to 'OptionalFile' * fix lint --------- Signed-off-by: Tmpecho <[email protected]> Co-authored-by: Tmpecho <[email protected]> Co-authored-by: Mads Nylund <[email protected]> Co-authored-by: Johannes Aamot-Skeidsvoll <[email protected]>
- Loading branch information
1 parent
a2185c6
commit 246ad2a
Showing
45 changed files
with
909 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.contrib import admin | ||
|
||
from app.files import models | ||
|
||
admin.site.register(models.UserGallery) | ||
admin.site.register(models.File) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class FilesConfig(AppConfig): | ||
name = "app.files" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from rest_framework import status | ||
from rest_framework.exceptions import APIException, ValidationError | ||
|
||
from app.constants import MAX_GALLERY_SIZE | ||
|
||
|
||
class APINoGalleryFoundForUser(APIException): | ||
status_code = status.HTTP_400_BAD_REQUEST | ||
default_detail = "Ingen galleri ble funnet for brukeren." | ||
|
||
|
||
class NoGalleryFoundForUser(ValidationError): | ||
default_detail = "Ingen galleri ble funnet for brukeren." | ||
|
||
|
||
class APIGalleryIsFull(APIException): | ||
status_code = status.HTTP_400_BAD_REQUEST | ||
default_detail = f"Galleriet er fullt med {MAX_GALLERY_SIZE} filer." | ||
|
||
|
||
class GalleryIsFull(ValidationError): | ||
default_detail = f"Galleriet er fullt med {MAX_GALLERY_SIZE} filer." |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Generated by Django 4.2.16 on 2024-10-07 16:45 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="UserGallery", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("created_at", models.DateTimeField(auto_now_add=True)), | ||
("updated_at", models.DateTimeField(auto_now=True)), | ||
( | ||
"author", | ||
models.OneToOneField( | ||
on_delete=django.db.models.deletion.PROTECT, | ||
related_name="user_galleries", | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name="File", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("created_at", models.DateTimeField(auto_now_add=True)), | ||
("updated_at", models.DateTimeField(auto_now=True)), | ||
("title", models.CharField(max_length=80)), | ||
("url", models.URLField()), | ||
("description", models.TextField(blank=True)), | ||
( | ||
"gallery", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.PROTECT, | ||
related_name="files", | ||
to="files.usergallery", | ||
), | ||
), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Generated by Django 5.1.1 on 2024-10-31 15:22 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("files", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="file", | ||
name="url", | ||
), | ||
migrations.AddField( | ||
model_name="file", | ||
name="file", | ||
field=models.URLField(blank=True, max_length=600, null=True), | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from app.files.exceptions import ( | ||
APIGalleryIsFull, | ||
APINoGalleryFoundForUser, | ||
GalleryIsFull, | ||
NoGalleryFoundForUser, | ||
) | ||
from app.util.mixins import APIErrorsMixin | ||
|
||
|
||
class FileErrorMixin(APIErrorsMixin): | ||
@property | ||
def expected_exceptions(self): | ||
return { | ||
**super().expected_exceptions, | ||
NoGalleryFoundForUser: APINoGalleryFoundForUser, | ||
GalleryIsFull: APIGalleryIsFull, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from app.files.models.user_gallery import UserGallery | ||
from app.files.models.file import File |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from django.db import models | ||
from django.db.models import PROTECT | ||
|
||
from app.common.enums import AdminGroup, Groups | ||
from app.common.permissions import BasePermissionModel, check_has_access | ||
from app.files.models.user_gallery import UserGallery | ||
from app.util.models import BaseModel, OptionalFile | ||
|
||
|
||
class File(BaseModel, BasePermissionModel, OptionalFile): | ||
read_access = AdminGroup.admin() | ||
write_access = AdminGroup.admin() | ||
|
||
title = models.CharField(max_length=80) | ||
|
||
description = models.TextField(blank=True) | ||
gallery = models.ForeignKey( | ||
UserGallery, on_delete=PROTECT, related_name="files", blank=False | ||
) | ||
|
||
class Meta: | ||
pass | ||
|
||
def __str__(self): | ||
return self.title | ||
|
||
@classmethod | ||
def has_read_permission(cls, request): | ||
return super().has_read_permission(request) | ||
|
||
@classmethod | ||
def has_write_permission(cls, request): | ||
return check_has_access(Groups.TIHLDE, request) | ||
|
||
@classmethod | ||
def has_retrieve_permission(cls, request): | ||
return cls.has_read_permission(request) | ||
|
||
@classmethod | ||
def has_create_permission(cls, request): | ||
return check_has_access(cls.write_access, request) | ||
|
||
@classmethod | ||
def has_update_permission(cls, request): | ||
return check_has_access(cls.write_access, request) | ||
|
||
@classmethod | ||
def has_destroy_permission(cls, request): | ||
return check_has_access(Groups.TIHLDE, request) | ||
|
||
@classmethod | ||
def has_list_permission(cls, request): | ||
return cls.has_read_permission(request) | ||
|
||
def has_object_read_permission(self, request): | ||
return self.has_read_permission(request) | ||
|
||
def has_object_write_permission(self, request): | ||
return self.has_write_permission(request) | ||
|
||
def has_object_retrieve_permission(self, request): | ||
return self.has_object_read_permission(request) | ||
|
||
def has_object_update_permission(self, request): | ||
return self.gallery.author == request.user | ||
|
||
def has_object_destroy_permission(self, request): | ||
return self.gallery.author == request.user |
Oops, something went wrong.