diff --git a/akarpov/files/apps.py b/akarpov/files/apps.py index dcdfbd7..1e716e1 100644 --- a/akarpov/files/apps.py +++ b/akarpov/files/apps.py @@ -5,3 +5,9 @@ class FilesConfig(AppConfig): name = "akarpov.files" verbose_name = _("Files") + + def ready(self): + try: + import akarpov.files.signals # noqa F401 + except ImportError: + pass diff --git a/akarpov/files/migrations/0005_alter_basefile_description_alter_basefile_folder_and_more.py b/akarpov/files/migrations/0005_alter_basefile_description_alter_basefile_folder_and_more.py new file mode 100644 index 0000000..6be5a96 --- /dev/null +++ b/akarpov/files/migrations/0005_alter_basefile_description_alter_basefile_folder_and_more.py @@ -0,0 +1,40 @@ +# Generated by Django 4.1.7 on 2023-03-16 11:15 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ("files", "0004_alter_basefile_short_link_alter_folder_short_link"), + ] + + operations = [ + migrations.AlterField( + model_name="basefile", + name="description", + field=models.TextField(blank=True), + ), + migrations.AlterField( + model_name="basefile", + name="folder", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="files", + to="files.folder", + ), + ), + migrations.AlterField( + model_name="folder", + name="parent", + field=models.ForeignKey( + blank=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="children", + to="files.folder", + ), + ), + ] diff --git a/akarpov/files/models.py b/akarpov/files/models.py index 6d6eeb9..d8a66fd 100644 --- a/akarpov/files/models.py +++ b/akarpov/files/models.py @@ -18,14 +18,14 @@ class BaseFile(TimeStampedModel, ShortLink): """model to store user's files""" name = CharField(max_length=100) - description = TextField() + description = TextField(blank=True) slug = SlugField(max_length=20, blank=True) private = BooleanField(default=True) user = ForeignKey("users.User", related_name="files", on_delete=CASCADE) folder = ForeignKey( - "files.Folder", related_name="files", null=True, on_delete=CASCADE + "files.Folder", related_name="files", blank=True, null=True, on_delete=CASCADE ) file = FileField(blank=False, upload_to=user_file_upload_mixin) @@ -42,7 +42,7 @@ class Folder(TimeStampedModel, ShortLink): slug = SlugField(max_length=20, blank=True) user = ForeignKey("users.User", related_name="files_folders", on_delete=CASCADE) - parent = ForeignKey("self", related_name="children", on_delete=CASCADE) + parent = ForeignKey("self", blank=True, related_name="children", on_delete=CASCADE) def get_absolute_url(self): return reverse("files:folder", kwargs={"slug": self.slug}) diff --git a/akarpov/files/signlas.py b/akarpov/files/signlas.py new file mode 100644 index 0000000..cb88c7c --- /dev/null +++ b/akarpov/files/signlas.py @@ -0,0 +1,20 @@ +from django.db.models.signals import pre_save +from django.dispatch import receiver + +from akarpov.files.models import BaseFile +from akarpov.utils.generators import generate_charset + + +@receiver(pre_save, sender=BaseFile) +def file_on_save(sender, instance: BaseFile, **kwargs): + if instance.id is None: + if instance.private: + slug = generate_charset(20) + while BaseFile.objects.filter(slug=slug).exists(): + slug = generate_charset(20) + else: + slug = generate_charset(5) + while BaseFile.objects.filter(slug=slug).exists(): + slug = generate_charset(5) + + instance.slug = slug diff --git a/akarpov/tools/shortener/models.py b/akarpov/tools/shortener/models.py index 8dc9336..013e68d 100644 --- a/akarpov/tools/shortener/models.py +++ b/akarpov/tools/shortener/models.py @@ -38,9 +38,17 @@ def __str__(self): def create_model_link(sender, instance, created, **kwargs): # had to move to models due to circular import if created: - link = Link.objects.create( - source=instance.get_absolute_url(), creator=instance.creator - ) + link = Link(source=instance.get_absolute_url()) + if hasattr(instance, "private"): + if instance.private: + return + if hasattr(instance, "public"): + if not instance.public: + return + if hasattr(instance, "creator"): + link.creator = instance.creator + + link.save() instance.short_link = link instance.save() diff --git a/akarpov/users/admin.py b/akarpov/users/admin.py index 40310a9..a8cb5d0 100644 --- a/akarpov/users/admin.py +++ b/akarpov/users/admin.py @@ -29,6 +29,7 @@ class UserAdmin(auth_admin.UserAdmin): }, ), (_("Important dates"), {"fields": ("last_login", "date_joined")}), + (_("Other"), {"fields": ("short_link",)}), ) list_display = ["username", "is_superuser"] search_fields = ["username", "email"] diff --git a/akarpov/users/migrations/0005_user_short_link.py b/akarpov/users/migrations/0005_user_short_link.py new file mode 100644 index 0000000..5019c7d --- /dev/null +++ b/akarpov/users/migrations/0005_user_short_link.py @@ -0,0 +1,25 @@ +# Generated by Django 4.1.7 on 2023-03-16 11:13 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ("shortener", "0001_initial"), + ("users", "0004_remove_user_short_link"), + ] + + operations = [ + migrations.AddField( + model_name="user", + name="short_link", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to="shortener.link", + ), + ), + ] diff --git a/akarpov/users/models.py b/akarpov/users/models.py index c36aeb5..4b8eb3b 100644 --- a/akarpov/users/models.py +++ b/akarpov/users/models.py @@ -4,9 +4,10 @@ from django.utils.translation import gettext_lazy as _ from akarpov.common.models import BaseImageModel +from akarpov.tools.shortener.models import ShortLink -class User(AbstractUser, BaseImageModel): +class User(AbstractUser, BaseImageModel, ShortLink): """ Default custom user model for akarpov. If adding fields that need to be filled at user signup,