fixed short link creation, minor files fixes

This commit is contained in:
Alexander Karpov 2023-03-16 14:28:54 +03:00
parent 4bdd85228f
commit 3aa4939a1f
8 changed files with 108 additions and 7 deletions

View File

@ -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

View File

@ -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",
),
),
]

View File

@ -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})

20
akarpov/files/signlas.py Normal file
View File

@ -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

View File

@ -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()

View File

@ -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"]

View File

@ -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",
),
),
]

View File

@ -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,