mirror of
https://github.com/Alexander-D-Karpov/akarpov
synced 2024-11-22 02:46:33 +03:00
fixed short link creation, minor files fixes
This commit is contained in:
parent
4bdd85228f
commit
3aa4939a1f
|
@ -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
|
||||
|
|
|
@ -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",
|
||||
),
|
||||
),
|
||||
]
|
|
@ -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
20
akarpov/files/signlas.py
Normal 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
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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"]
|
||||
|
|
25
akarpov/users/migrations/0005_user_short_link.py
Normal file
25
akarpov/users/migrations/0005_user_short_link.py
Normal 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",
|
||||
),
|
||||
),
|
||||
]
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue
Block a user