added user allowed file upload

This commit is contained in:
Alexander Karpov 2023-04-06 13:55:18 +03:00
parent 88fb87ebae
commit 845c989718
5 changed files with 42 additions and 10 deletions

View File

@ -46,6 +46,10 @@ def check_permissions(self, request):
class MyChunkedUploadCompleteView(ChunkedUploadCompleteView):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.message = "file is successfully uploaded"
model = ChunkedUpload
def check_permissions(self, request):
@ -55,14 +59,14 @@ def check_permissions(self, request):
)
def on_completion(self, uploaded_file, request):
if uploaded_file.size <= request.user.left_file_upload:
File.objects.create(user=request.user, file=uploaded_file)
request.user.left_file_upload -= uploaded_file.size
request.user.save()
else:
self.message = "File is too large"
if os.path.isfile(uploaded_file.file.path):
os.remove(uploaded_file.file.path)
def get_response_data(self, chunked_upload, request):
return {
"message": (
"You successfully uploaded '%s' (%s bytes)!"
% (chunked_upload.filename, chunked_upload.offset)
)
}
return {"message": (self.message)}

View File

@ -71,7 +71,6 @@
{"name": "upload_id", "value": data.result.upload_id}
);
}
$("#messages").append($('<p>').text(JSON.stringify(data.result)));
var progress = parseInt(data.loaded / data.total * 100.0, 10);
$("#progress").text(Array(progress).join("=") + "> " + progress + "%");
},

View File

@ -29,7 +29,7 @@ class UserAdmin(auth_admin.UserAdmin):
},
),
(_("Important dates"), {"fields": ("last_login", "date_joined")}),
(_("Other"), {"fields": ("short_link",)}),
(_("Other"), {"fields": ("short_link", "left_file_upload")}),
)
list_display = ["username", "is_superuser"]
search_fields = ["username", "email"]

View File

@ -0,0 +1,23 @@
# Generated by Django 4.2 on 2023-04-06 10:37
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("users", "0006_user_slug"),
]
operations = [
migrations.AddField(
model_name="user",
name="left_file_upload",
field=models.IntegerField(
default=0,
validators=[django.core.validators.MinValueValidator(0)],
verbose_name="Left file upload(in bites)",
),
),
]

View File

@ -1,5 +1,6 @@
from django.contrib.auth.models import AbstractUser
from django.db.models import CharField, TextField
from django.core.validators import MinValueValidator
from django.db.models import CharField, IntegerField, TextField
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
@ -20,6 +21,11 @@ class User(AbstractUser, BaseImageModel, ShortLink):
first_name = None # type: ignore
last_name = None # type: ignore
# files
left_file_upload = IntegerField(
"Left file upload(in bites)", default=0, validators=[MinValueValidator(0)]
)
def get_absolute_url(self):
"""Get url for user's detail view.