ApartamentGetMany
This commit is contained in:
parent
4fc71731c3
commit
af28414bba
|
@ -1,5 +1,6 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from .models import Apartament, Photo
|
from .models import Apartament, Photo, User
|
||||||
|
|
||||||
admin.site.register(Apartament)
|
admin.site.register(Apartament)
|
||||||
admin.site.register(Photo)
|
admin.site.register(Photo)
|
||||||
|
admin.site.register(User)
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
# Generated by Django 4.2.1 on 2023-05-08 16:44
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('pairent_app', '0004_apartament_lastprice'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='User',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('favorites_apartaments', models.CharField(max_length=100, verbose_name='Избранные квартиры')),
|
||||||
|
('apartaments_for_comparison', models.CharField(max_length=100, verbose_name='Квартиры для сравнения')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='apartament',
|
||||||
|
name='isСhild',
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='apartament',
|
||||||
|
name='isChild',
|
||||||
|
field=models.BooleanField(verbose_name='Можно с детьми'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -78,3 +78,12 @@ class Photo(models.Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = "Фотография"
|
verbose_name = "Фотография"
|
||||||
verbose_name_plural = "Фотографии"
|
verbose_name_plural = "Фотографии"
|
||||||
|
|
||||||
|
class User(models.Model):
|
||||||
|
"""Тестовая модель данных для избранных и сравнения"""
|
||||||
|
favorites_apartaments = models.CharField(max_length=100, verbose_name="Избранные квартиры")
|
||||||
|
apartaments_for_comparison = models.CharField(max_length=100, verbose_name="Квартиры для сравнения")
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name = "Пользователь"
|
||||||
|
verbose_name_plural = "Пользователи"
|
|
@ -6,5 +6,7 @@ from . import views
|
||||||
urlpatterns = format_suffix_patterns([
|
urlpatterns = format_suffix_patterns([
|
||||||
path("apartaments/", views.ApartamentViewSet.as_view({'get': 'list'})),
|
path("apartaments/", views.ApartamentViewSet.as_view({'get': 'list'})),
|
||||||
path("apartament/<int:pk>/", views.ApartamentViewSet.as_view({'get': 'retrieve'})),
|
path("apartament/<int:pk>/", views.ApartamentViewSet.as_view({'get': 'retrieve'})),
|
||||||
path("comparison/", views.ApartamentComparisonViewSet.as_view({'get': 'retrieve'})),
|
# TODO: доработать
|
||||||
|
path("apartaments/comparison/", views.ApartamentGetManyViewSet.as_view({'get': 'retrieve'})), # пример: apartaments/comparison/?user_id=1 user_id - id пользователя
|
||||||
|
path("apartaments/favorite/", views.ApartamentGetManyViewSet.as_view({'get': 'list'})),
|
||||||
])
|
])
|
||||||
|
|
|
@ -2,7 +2,7 @@ from rest_framework import viewsets
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
from .models import Apartament
|
from .models import Apartament, User
|
||||||
from .serializer import (ApartamentListSerializer,
|
from .serializer import (ApartamentListSerializer,
|
||||||
ApartamentDetailSerializer)
|
ApartamentDetailSerializer)
|
||||||
|
|
||||||
|
@ -22,11 +22,61 @@ class ApartamentViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
return ApartamentDetailSerializer
|
return ApartamentDetailSerializer
|
||||||
|
|
||||||
|
|
||||||
class ApartamentComparisonViewSet(viewsets.ReadOnlyModelViewSet):
|
# class ApartamentGetManyViewSet(viewsets.ViewSet):
|
||||||
|
# def retrieve(self, request):
|
||||||
|
# user_id = request.query_params.get("user_id", None) # получение id пользователя
|
||||||
|
# apartaments = []
|
||||||
|
# apartaments_id = User.objects.get(pk=user_id).apartaments_for_comparison.split(',') # получение id квартир
|
||||||
|
# for i in apartaments_id:
|
||||||
|
# apartaments.append(ApartamentDetailSerializer(Apartament.objects.get(pk=i)).data)
|
||||||
|
|
||||||
|
# return Response({'results': apartaments})
|
||||||
|
# def list(self, request):
|
||||||
|
# pk = request.query_params.get("pk", None) # получение id пользователя
|
||||||
|
# apartaments = []
|
||||||
|
# apartaments_id = User.objects.get(pk=pk).apartaments_for_comparison.split(',') # получение id квартир
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: доработать
|
||||||
|
class ApartamentGetManyViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
"""Вывод отдельных квартир для сравнения"""
|
"""Вывод отдельных квартир для сравнения"""
|
||||||
def retrieve(self, request, *args, **kwargs):
|
def get_queryset(self):
|
||||||
apartaments_id = request.COOKIES["apartament_comparison_id"].split(',') # получение куки пример (123,453)
|
pk = self.request.query_params.get("pk", None) # получение id пользователя
|
||||||
apartaments = []
|
queryset = []
|
||||||
|
|
||||||
|
if self.action == "retrieve": # если для страницы сравнения
|
||||||
|
apartaments_id = User.objects.get(pk=pk).apartaments_for_comparison.split(',') # получение id квартир
|
||||||
|
elif self.action == "list": # если для страницы избранного
|
||||||
|
apartaments_id = User.objects.get(pk=pk).favorites_apartaments.split(',') # получение id квартир
|
||||||
|
|
||||||
for i in apartaments_id:
|
for i in apartaments_id:
|
||||||
apartaments.append(ApartamentDetailSerializer(Apartament.objects.get(pk=i)).data)
|
queryset.append(Apartament.objects.get(pk=i))
|
||||||
return Response({"results": apartaments})
|
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
serializer_class = ApartamentDetailSerializer
|
||||||
|
|
||||||
|
# def retrieve(self, request, *args, **kwargs):
|
||||||
|
# pk = request.query_params.get("pk", None) # получение id пользователя
|
||||||
|
# apartaments = []
|
||||||
|
# print(f"pk: {pk}")
|
||||||
|
|
||||||
|
# if request.query_params.get("page") == "comparison": # если для страницы сравнения
|
||||||
|
# apartaments_id = User.objects.get(pk=pk).apartaments_for_comparison.split(',') # получение id квартир
|
||||||
|
# elif request.query_params.get("page") == "favorites": # если для страницы избранного
|
||||||
|
# apartaments_id = User.objects.get(pk=pk).favorites_apartaments.split(',') # получение id квартир
|
||||||
|
|
||||||
|
# for i in apartaments_id:
|
||||||
|
# apartaments.append(ApartamentDetailSerializer(Apartament.objects.get(pk=i)).data)
|
||||||
|
|
||||||
|
# return Response({"results": apartaments})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# def retrieve(self, request, *args, **kwargs):
|
||||||
|
# apartaments_id = request.COOKIES["apartament_comparison_id"].split(',') # получение куки пример (123,453)
|
||||||
|
# apartaments = []
|
||||||
|
# for i in apartaments_id:
|
||||||
|
# apartaments.append(ApartamentDetailSerializer(Apartament.objects.get(pk=i)).data)
|
||||||
|
# return Response({"results": apartaments})
|
Loading…
Reference in New Issue