diff --git a/akarpov/files/views.py b/akarpov/files/views.py index 5c5dc20..64a46ab 100644 --- a/akarpov/files/views.py +++ b/akarpov/files/views.py @@ -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): - File.objects.create(user=request.user, file=uploaded_file) + 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)} diff --git a/akarpov/templates/files/upload.html b/akarpov/templates/files/upload.html index 646b843..ae7daa2 100644 --- a/akarpov/templates/files/upload.html +++ b/akarpov/templates/files/upload.html @@ -71,7 +71,6 @@ {"name": "upload_id", "value": data.result.upload_id} ); } - $("#messages").append($('
').text(JSON.stringify(data.result))); var progress = parseInt(data.loaded / data.total * 100.0, 10); $("#progress").text(Array(progress).join("=") + "> " + progress + "%"); }, diff --git a/akarpov/users/admin.py b/akarpov/users/admin.py index a8cb5d0..29bdccf 100644 --- a/akarpov/users/admin.py +++ b/akarpov/users/admin.py @@ -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"] diff --git a/akarpov/users/migrations/0007_user_left_file_upload.py b/akarpov/users/migrations/0007_user_left_file_upload.py new file mode 100644 index 0000000..e0558dc --- /dev/null +++ b/akarpov/users/migrations/0007_user_left_file_upload.py @@ -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)", + ), + ), + ] diff --git a/akarpov/users/models.py b/akarpov/users/models.py index 4b8eb3b..f9783ba 100644 --- a/akarpov/users/models.py +++ b/akarpov/users/models.py @@ -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.