This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: file ownership basics feat: gaps in test compatibility
- Loading branch information
Showing
17 changed files
with
340 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
Settings overrides for test environments. | ||
""" | ||
|
||
DATABASES = { | ||
"default": { | ||
"ENGINE": "django.db.backends.postgresql_psycopg2", | ||
"NAME": "postgres", | ||
"USER": "postgres", | ||
"PASSWORD": "test", | ||
"HOST": "localhost", | ||
"PORT": "5432", | ||
} | ||
} | ||
|
||
USER_UPLOAD_ROOT = "/tmp" |
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
Binary file not shown.
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.apps import AppConfig | ||
|
||
|
||
class FilesConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "files" |
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,29 @@ | ||
# Generated by Django 4.2.7 on 2023-11-17 06:15 | ||
|
||
from django.db import migrations, models | ||
import uuid | ||
|
||
|
||
class Migration(migrations.Migration): | ||
initial = True | ||
|
||
dependencies = [] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="File", | ||
fields=[ | ||
( | ||
"id", | ||
models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
), | ||
), | ||
("path", models.CharField(max_length=4096)), | ||
("size", models.IntegerField()), | ||
], | ||
), | ||
] |
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,24 @@ | ||
# Generated by Django 4.2.7 on 2023-11-18 06:02 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
("files", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="file", | ||
name="owner", | ||
field=models.ForeignKey( | ||
null=True, | ||
on_delete=django.db.models.deletion.SET_NULL, | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
] |
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 @@ | ||
import uuid | ||
|
||
from django.db import models | ||
from django.conf import settings | ||
|
||
|
||
class File(models.Model): | ||
""" | ||
Represents a file tracked by the system. | ||
""" | ||
|
||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) | ||
path = models.CharField(max_length=4096, null=False) | ||
size = models.IntegerField(null=False) | ||
owner = models.ForeignKey( | ||
settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True | ||
) |
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,39 @@ | ||
import typing | ||
import pathlib | ||
|
||
import rest_framework.serializers as drf_serializers | ||
|
||
import files.models as files_models | ||
|
||
|
||
class FileDict(typing.TypedDict): | ||
id: str | ||
path: str | ||
size: int | ||
filename: str | ||
owner_id: int | ||
|
||
|
||
class FileSerializer(drf_serializers.ModelSerializer): | ||
def validate_path(self, value: str) -> typing.Union[typing.NoReturn, str]: | ||
if not value: | ||
raise drf_serializers.ValidationError("Path must not be empty.") | ||
return value | ||
|
||
def validate_owner(self, value: int) -> typing.Union[typing.NoReturn, int]: | ||
if not value: | ||
raise drf_serializers.ValidationError("File must have an owner.") | ||
return value | ||
|
||
def to_representation(self, instance: files_models.File) -> FileDict: | ||
return { | ||
"id": instance.id, | ||
"path": instance.path, | ||
"size": instance.size, | ||
"owner_id": instance.owner.id, | ||
"filename": pathlib.Path(instance.path).name, | ||
} | ||
|
||
class Meta: | ||
model = files_models.File | ||
fields = "__all__" |
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,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
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,15 @@ | ||
import django.urls as dj_urls | ||
import rest_framework.routers as drf_routers | ||
|
||
import files.views as file_views | ||
|
||
router = drf_routers.DefaultRouter() | ||
router.register("files", file_views.FileViewSet, basename="files") | ||
|
||
urlpatterns = router.urls + [ | ||
dj_urls.path( | ||
"files/<str:file_id>/content/", | ||
file_views.FileDataView.as_view(), | ||
name="files-detail-data", | ||
), | ||
] |
Oops, something went wrong.