-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* DISP-218 Add models `Attachment` and `ModelWithAttachmentsMixin`
- Loading branch information
Showing
37 changed files
with
746 additions
and
733 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.contrib import admin | ||
from django.urls import reverse | ||
from django.utils.html import format_html | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from .models import Attachment | ||
|
||
|
||
@admin.register(Attachment) | ||
class AttachmentAdmin(admin.ModelAdmin): | ||
list_display = ( | ||
'uid', 'created', '_name', 'size', 'content_type', | ||
'object_content_type', 'object_id' | ||
) | ||
readonly_fields = ('_name',) | ||
|
||
def _name(self, obj): | ||
url = reverse('staff-attachments', kwargs={'attachment_uid': obj.uid}) | ||
return format_html(f'<a href="{url}">{obj.name}</a>') | ||
_name.short_description = _('Имя') | ||
_name.admin_order_field = 'name' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.db import models | ||
|
||
|
||
class AttachmentQuerySet(models.QuerySet): | ||
|
||
def delete(self, *args, **kwargs): | ||
for obj in self: | ||
obj.delete() | ||
super().delete(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Generated by Django 2.2.2 on 2019-06-14 11:24 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import uuid | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('contenttypes', '0002_remove_content_type_name'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Attachment', | ||
fields=[ | ||
('uid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), | ||
('created', models.DateTimeField(auto_now_add=True, verbose_name='Создано')), | ||
('name', models.CharField(max_length=100, verbose_name='Имя')), | ||
('path', models.CharField(max_length=512, verbose_name='Путь')), | ||
('size', models.BigIntegerField(verbose_name='Размер')), | ||
('bucket_name', models.CharField(max_length=255)), | ||
('content_type', models.CharField(max_length=255)), | ||
('object_id', models.CharField(blank=True, max_length=255, null=True)), | ||
('object_content_type', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')), | ||
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)), | ||
], | ||
options={ | ||
'verbose_name': 'Вложение', | ||
'verbose_name_plural': 'Вложения', | ||
}, | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,87 @@ | ||
import uuid | ||
|
||
from django.conf import settings | ||
from django.contrib.contenttypes.fields import ( | ||
GenericForeignKey, GenericRelation | ||
) | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.db import models | ||
from django.contrib.postgres.fields import JSONField | ||
from django.utils.translation import gettext as _ | ||
|
||
from .managers import AttachmentQuerySet | ||
|
||
|
||
class Attachment(models.Model): | ||
uid = models.UUIDField( | ||
primary_key=True, | ||
default=uuid.uuid4, | ||
editable=False | ||
) | ||
created = models.DateTimeField( | ||
verbose_name=_('Создано'), | ||
editable=False, | ||
auto_now_add=True | ||
) | ||
name = models.CharField( | ||
verbose_name=_('Имя'), | ||
max_length=settings.MINIO_STORAGE_MAX_FILE_NAME_LEN | ||
) | ||
path = models.CharField( | ||
verbose_name=_('Путь'), | ||
max_length=512 | ||
) | ||
size = models.BigIntegerField( | ||
verbose_name=_('Размер') | ||
) | ||
bucket_name = models.CharField( | ||
max_length=255 | ||
) | ||
content_type = models.CharField( | ||
max_length=255 | ||
) | ||
user = models.ForeignKey( | ||
to=settings.AUTH_USER_MODEL, | ||
null=True, | ||
blank=True, | ||
on_delete=models.SET_NULL | ||
) | ||
object_content_type = models.ForeignKey( | ||
to=ContentType, | ||
null=True, | ||
blank=True, | ||
on_delete=models.CASCADE | ||
) | ||
object_id = models.CharField( | ||
max_length=255, | ||
null=True, | ||
blank=True | ||
) | ||
content_object = GenericForeignKey( | ||
ct_field='object_content_type', | ||
fk_field='object_id' | ||
) | ||
objects = AttachmentQuerySet.as_manager() | ||
|
||
class Meta: | ||
verbose_name = _('Вложение') | ||
verbose_name_plural = _('Вложения') | ||
|
||
def __str__(self): | ||
return self.path | ||
|
||
def delete(self, *args, **kwargs): | ||
from apiqa_storage.serializers import delete_file | ||
if not self.content_object: | ||
delete_file(self) | ||
return super().delete(*args, **kwargs) | ||
|
||
|
||
class AttachFilesMixin(models.Model): | ||
attachments = JSONField(_('Вложения'), default=list, blank=True) | ||
class ModelWithAttachmentsMixin(models.Model): | ||
attachments = GenericRelation( | ||
to=Attachment, | ||
object_id_field='object_id', | ||
content_type_field='object_content_type' | ||
) | ||
|
||
class Meta: | ||
abstract = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from rest_framework import routers | ||
|
||
from .viewsets import AttachmentViewSet | ||
|
||
router = routers.SimpleRouter() | ||
router.register('file-upload', AttachmentViewSet, base_name='file_upload') |
Oops, something went wrong.