From 4bf661bdf2040fc805b3c762724522d32b1381cd Mon Sep 17 00:00:00 2001 From: Nikita Romanov Date: Tue, 7 Nov 2023 13:14:52 +0300 Subject: [PATCH] add image --- backend/api/v1/serializers.py | 14 +++++++-- backend/documents/models.py | 6 ++++ backend/request/users.http | 6 ++-- "qwe\321\216.json" | 58 ----------------------------------- 4 files changed, 21 insertions(+), 63 deletions(-) delete mode 100644 "qwe\321\216.json" diff --git a/backend/api/v1/serializers.py b/backend/api/v1/serializers.py index b8b34f6..a9c3da3 100644 --- a/backend/api/v1/serializers.py +++ b/backend/api/v1/serializers.py @@ -1,5 +1,5 @@ """Сериализаторы для API.""" - +from django.core.files.base import ContentFile from django.contrib.auth import get_user_model from django.db import transaction from djoser.serializers import UserSerializer @@ -19,6 +19,15 @@ User = get_user_model() +class Base64ImageField(serializers.ImageField): + def to_internal_value(self, data): + if isinstance(data, str) and data.startswith("data:image"): + format, imgstr = data.split(";base64,") + ext = format.split("/")[-1] + + data = ContentFile(base64.b64decode(imgstr), name="temp." + ext) + + return super().to_internal_value(data) class TemplateFieldSerializer(serializers.ModelSerializer): """Сериализатор поля шаблона.""" @@ -94,7 +103,7 @@ class TemplateSerializerMinified(serializers.ModelSerializer): """Сериализатор шаблонов сокращенный.""" is_favorited = serializers.SerializerMethodField() - + image = Base64ImageField(required=True, allow_null=True) class Meta: model = Template exclude = ("template",) @@ -102,6 +111,7 @@ class Meta: "name", "category", "owner", + "image", "modified", "deleted", "description", diff --git a/backend/documents/models.py b/backend/documents/models.py index 5ebf463..32c0194 100644 --- a/backend/documents/models.py +++ b/backend/documents/models.py @@ -151,6 +151,12 @@ class TemplateField(models.Model): length = models.PositiveIntegerField( blank=True, null=True, verbose_name="Размер поля ввода" ) + image = models.ImageField( + "Картинка", + upload_to="posts/", + null=True, + blank=True, + ) class Meta: verbose_name = "Поле шаблона" diff --git a/backend/request/users.http b/backend/request/users.http index e41207b..d2f6eed 100644 --- a/backend/request/users.http +++ b/backend/request/users.http @@ -1,13 +1,13 @@ @TOKEN = Token b5b767cb36da725f900892f627a28dc1c0e40488 -# @URL = https://documents-template.site/ -@URL = http://127.0.0.1:8000 +@URL = https://documents-template.site +# @URL = http://127.0.0.1:8000 ### POST {{URL}}/api/users/ Content-Type: application/json { - "email": "nikox112@mail.ru", + "email": "nikox11882@mail.ru", "username": "nikox1122@mail.ru", "password": "456852Zx", "first_name": "kewk", diff --git "a/qwe\321\216.json" "b/qwe\321\216.json" deleted file mode 100644 index acaf910..0000000 --- "a/qwe\321\216.json" +++ /dev/null @@ -1,58 +0,0 @@ -{ - "id": 1, - "is_favorited": false, - "groups_fields": [ - { - "group_id": 1, - "group_name": "Ребенок", - "fields": [ - { - "id": 1, - "name": "Чебурашка", - "hint": "Олень", - "type": "fio", - "lenth": 100, - "mask": "" - }, - { - "id": 52, - "name": "Олег", - "hint": "жменька", - "type": "fio", - "lenth": 100, - "mask": "" - } - - ] - }, - { - "group_id": "", - "group_name": "No_group", - "fields": [ - { - "id": 13, - "name": "Чейцйцбурашка", - "hint": "Олейцйцнь", - "type": "fйцйцio", - "lenth": 100, - "mask": "" - }, - { - "id": 5112, - "name": "Олчясясег", - "hint": "жмячсясенька", - "type": "fiячсясo", - "lenth": 100, - "mask": "" - } - - ] - } - ], - "name": "Заявление в детский сад", - "modified": "2023-10-18", - "deleted": false, - "description": "Данный шаблон необходим для заполнения заявления в детский сад. Заявление может быть составлено в простой письменной форме. Можно доработать шаблон под себя и прописать наиболее важные поля после его скачивания.", - "owner": null, - "category": null -} \ No newline at end of file