Skip to content

Commit fea016d

Browse files
author
Two Dev
committed
chore: update app settings, README.
1 parent 505bc96 commit fea016d

File tree

4 files changed

+75
-30
lines changed

4 files changed

+75
-30
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Django Chunk File Upload is an alternative utility that helps you easily edit Django's chunked, drag and drop file uploads.
44

5-
<img src="https://i.ibb.co/9y2SgmS/f-P5-Or-Gkxk0-Ynj00ct-G.webp" alt="f-P5-Or-Gkxk0-Ynj00ct-G" border="0">
5+
<img src="https://i.ibb.co/9y2SgmS/f-P5-Or-Gkxk0-Ynj00ct-G.webp" alt="f-P5-Or-Gkxk0-Ynj00ct-G">
66

77
Features
88
----------
@@ -76,7 +76,8 @@ DJANGO_CHUNK_FILE_UPLOAD = {
7676
"max_width": 1024,
7777
"max_height": 720,
7878
"to_webp": True, # focus convert image to webp type.
79-
}
79+
},
80+
"permission_classes": ("django_chunk_file_upload.permissions.AllowAny",) # default: IsAuthenticated
8081
}
8182

8283
```
@@ -88,14 +89,14 @@ models.py
8889

8990
```python
9091
from django.db import models
91-
from django_chunk_file_upload.models import FileManager
92+
from django_chunk_file_upload.models import FileManagerMixin
9293

9394

9495
class Tag(models.Model):
9596
name = models.CharField(max_length=255)
9697

9798

98-
class YourModel(FileManager):
99+
class YourModel(FileManagerMixin):
99100
tags = models.ManyToManyField(Tag)
100101
custom_field = models.CharField(max_length=255)
101102

@@ -116,6 +117,7 @@ class YourForm(ChunkedUploadFileForm):
116117

117118
views.py
118119

120+
Accepted methods: GET, POST, DELETE (UPDATE, PUT does not work with FormData).
119121
```python
120122
from django_chunk_file_upload.views import ChunkedUploadView
121123
from django_chunk_file_upload.typed import File
@@ -126,12 +128,18 @@ from .forms import YourForm
126128
class CustomChunkedUploadView(ChunkedUploadView):
127129
form_class = YourForm
128130
permission_classes = (IsAuthenticated,)
131+
129132
# file_class = File # file class
133+
# file_status = app_settings.status # default: PENDING (Used when using background task, you can change it to COMPLETED.)
130134
# optimize = True # default: True
131135
# remove_file_on_update = True # update image on admin page.
132136
# chunk_size = 1024 * 1024 * 2 # custom chunk size upload (default: 2MB).
133137
# upload_to = "custom_folder/%Y/%m/%d" # custom upload folder.
134138
# template_name = "custom_template.html" # custom template
139+
140+
# # Run background task like celery when upload is complete
141+
# def background_task(self, instance):
142+
# pass
135143
```
136144

137145
custom_template.html

django_chunk_file_upload/app_settings.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from __future__ import annotations
22

3+
import importlib
34
from dataclasses import dataclass, field, fields
45

56
from django.conf import settings
67

8+
from . import permissions
79
from .constants import StatusChoices
8-
from .permissions import BasePermission, IsAuthenticated
910

1011

1112
@dataclass(kw_only=True)
@@ -49,7 +50,9 @@ class _LazySettings(_Settings):
4950
is_metadata_storage: bool = False
5051
remove_file_on_update: bool = True
5152
status: StatusChoices = StatusChoices.PENDING
52-
permission_classes: tuple[BasePermission] = (IsAuthenticated,)
53+
permission_classes: tuple[permissions.BasePermission] = (
54+
permissions.IsAuthenticated,
55+
)
5356
optimize: bool = True
5457
image_optimizer: _ImageSettings = field(default_factory=_ImageSettings)
5558

@@ -63,6 +66,22 @@ def from_kwargs(cls, **kwargs) -> "_LazySettings":
6366
image_optimizer = kwargs.pop("image_optimizer", {}) or {}
6467
if image_optimizer and isinstance(image_optimizer, dict):
6568
kwargs["image_optimizer"] = _ImageSettings.from_kwargs(**image_optimizer)
69+
70+
permission_classes = kwargs.pop("permission_classes")
71+
if permission_classes and isinstance(
72+
permission_classes, (tuple, list, set, str)
73+
):
74+
if isinstance(permission_classes, str):
75+
permission_classes = [permission_classes]
76+
77+
perms = []
78+
for permission_class in permission_classes:
79+
paths = permission_class.split(".")
80+
module = importlib.import_module(".".join(paths[:-1]), "")
81+
permission_class = getattr(module, paths[-1])
82+
perms.append(permission_class)
83+
84+
kwargs["permission_classes"] = tuple(perms)
6685
return cls(**kwargs)
6786

6887

django_chunk_file_upload/templates/django_chunk_file_upload/chunked_upload.html

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% load i18n %}
1+
{% load static i18n %}
22
<!DOCTYPE html>
33
<html lang="en">
44
<head>
@@ -13,30 +13,30 @@
1313
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
1414
crossorigin="anonymous">
1515
<style>
16-
:root {
17-
--input-padding-x: .75rem;
18-
--input-padding-y: .75rem;
19-
}
16+
:root {
17+
--input-padding-x: .75rem;
18+
--input-padding-y: .75rem;
19+
}
2020

21-
html,
22-
body {
23-
height: 100%;
24-
}
21+
html,
22+
body {
23+
height: 100%;
24+
}
2525

26-
body {
27-
display: -ms-flexbox;
28-
display: -webkit-box;
29-
display: flex;
30-
-ms-flex-align: center;
31-
-ms-flex-pack: center;
32-
-webkit-box-align: center;
33-
align-items: center;
34-
-webkit-box-pack: center;
35-
justify-content: center;
36-
padding-top: 40px;
37-
padding-bottom: 40px;
38-
background-color: #f5f5f5;
39-
}
26+
body {
27+
display: -ms-flexbox;
28+
display: -webkit-box;
29+
display: flex;
30+
-ms-flex-align: center;
31+
-ms-flex-pack: center;
32+
-webkit-box-align: center;
33+
align-items: center;
34+
-webkit-box-pack: center;
35+
justify-content: center;
36+
padding-top: 40px;
37+
padding-bottom: 40px;
38+
background-color: #f5f5f5;
39+
}
4040

4141
</style>
4242
{{ form.media }}

examples/examples/settings.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
ROOT_URLCONF = "examples.urls"
6262

63-
TEMPLATES = [
63+
TEMPLATES = [ # noqa
6464
{
6565
"BACKEND": "django.template.backends.django.DjangoTemplates",
6666
"DIRS": [],
@@ -136,3 +136,21 @@
136136
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
137137

138138
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
139+
140+
141+
# Django Chunk File Upload
142+
DJANGO_CHUNK_FILE_UPLOAD = {
143+
"chunk_size": 1024 * 1024 * 2, # # custom chunk size upload (default: 2MB).
144+
"upload_to": "custom_folder/%Y/%m/%d", # custom upload folder.
145+
"is_metadata_storage": True, # save file metadata,
146+
"remove_file_on_update": True,
147+
"optimize": True,
148+
"image_optimizer": {
149+
"quality": 82,
150+
"compress_level": 9,
151+
"max_width": 1024,
152+
"max_height": 720,
153+
"to_webp": True, # focus convert image to webp type.
154+
},
155+
"permission_classes": ("django_chunk_file_upload.permissions.AllowAny"),
156+
}

0 commit comments

Comments
 (0)