Ad like uchun apilar chiqarildi
This commit is contained in:
18
core/apps/api/migrations/0013_alter_feedback_comment.py
Normal file
18
core/apps/api/migrations/0013_alter_feedback_comment.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-26 10:04
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('api', '0012_rename_command_feedback_comment'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='feedback',
|
||||
name='comment',
|
||||
field=models.CharField(max_length=255, verbose_name='Comment'),
|
||||
),
|
||||
]
|
||||
@@ -1,3 +1,4 @@
|
||||
from .category import * # noqa
|
||||
from .search import * # noqa
|
||||
from .ad import * # noqa
|
||||
from .user import * # noqa
|
||||
|
||||
1
core/apps/api/serializers/user/__init__.py
Normal file
1
core/apps/api/serializers/user/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .ad_like import * # noqa
|
||||
45
core/apps/api/serializers/user/ad_like.py
Normal file
45
core/apps/api/serializers/user/ad_like.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from rest_framework import serializers
|
||||
from core.apps.accounts.models import UserLike
|
||||
from core.apps.api.models import AdModel
|
||||
from core.apps.api.serializers.ad.home_api import ListHomeAdSerializer
|
||||
from rest_framework.exceptions import ValidationError
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
class BaseUserLikeSerializer(serializers.ModelSerializer):
|
||||
ad = ListHomeAdSerializer(many=False, read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = UserLike
|
||||
fields = [
|
||||
"id",
|
||||
"ad",
|
||||
]
|
||||
|
||||
|
||||
class ListUserLikeSerializer(BaseUserLikeSerializer):
|
||||
class Meta(BaseUserLikeSerializer.Meta): ...
|
||||
|
||||
|
||||
class RetrieveUserLikeSerializer(BaseUserLikeSerializer):
|
||||
class Meta(BaseUserLikeSerializer.Meta): ...
|
||||
|
||||
|
||||
class CreateUserLikeSerializer(BaseUserLikeSerializer):
|
||||
ad = serializers.PrimaryKeyRelatedField(queryset=AdModel.objects.all())
|
||||
|
||||
class Meta(BaseUserLikeSerializer.Meta): ...
|
||||
|
||||
def validate(self, data):
|
||||
user = self.context["request"].user
|
||||
ad = data["ad"]
|
||||
|
||||
if UserLike.objects.filter(user=user, ad=ad).exists():
|
||||
raise ValidationError({"detail": _("Siz bu e’longa allaqachon like bosgansiz.")})
|
||||
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
validated_data['user'] = self.context['request'].user
|
||||
like = UserLike.objects.create(**validated_data)
|
||||
return like
|
||||
@@ -1,3 +1,4 @@
|
||||
from .category import * # noqa
|
||||
from .ad import * # noqa
|
||||
from .search import * # noqa
|
||||
from .user import * # noqa
|
||||
|
||||
1
core/apps/api/tests/user/__init__.py
Normal file
1
core/apps/api/tests/user/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .test_user_like import * # noqa
|
||||
62
core/apps/api/tests/user/test_user_like.py
Normal file
62
core/apps/api/tests/user/test_user_like.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import pytest
|
||||
from django.urls import reverse
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
from core.apps.accounts.models import UserLike, AdModel
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def instance(db):
|
||||
return UserLike._baker()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ad(db):
|
||||
return AdModel._baker()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def api_client(instance):
|
||||
client = APIClient()
|
||||
client.force_authenticate(user=instance.user)
|
||||
return client, instance
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def data(api_client):
|
||||
client, instance = api_client
|
||||
return (
|
||||
{
|
||||
"list": reverse("user-like-list"),
|
||||
"retrieve": reverse("user-like-detail", kwargs={"pk": instance.pk}),
|
||||
"retrieve-not-found": reverse("user-like-detail", kwargs={"pk": 1000}),
|
||||
},
|
||||
client,
|
||||
instance,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_list(data):
|
||||
urls, client, _ = data
|
||||
response = client.get(urls["list"])
|
||||
data_resp = response.json()
|
||||
assert response.status_code == 200
|
||||
assert data_resp["status"] is True
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_create(data,ad):
|
||||
urls, client, instance = data
|
||||
response = client.post(urls["list"], data={"ad": ad.pk})
|
||||
data_resp = response.json()
|
||||
print(data_resp)
|
||||
assert response.status_code == 201
|
||||
assert data_resp["status"] is True
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_destroy(data):
|
||||
urls, client, _ = data
|
||||
response = client.delete(urls["retrieve"])
|
||||
assert response.status_code == 204
|
||||
@@ -1,9 +1,11 @@
|
||||
from django.urls import include, path
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
from core.apps.api.views import CategoryHomeApiViewSet, CategoryViewSet, HomeAdApiView, SearchHistoryViewSet
|
||||
from core.apps.api.views import CategoryHomeApiViewSet, CategoryViewSet, HomeAdApiView, SearchHistoryViewSet, \
|
||||
UserLikeViewSet
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register("user-like", UserLikeViewSet, basename="user-like")
|
||||
router.register("category", CategoryViewSet, basename="category")
|
||||
router.register("category-home", CategoryHomeApiViewSet, basename="category-home")
|
||||
router.register("search-history", SearchHistoryViewSet, basename="search-history")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from .category import * # noqa
|
||||
from .search import * # noqa
|
||||
from .ad import * # noqa
|
||||
|
||||
from .user import * # noqa
|
||||
|
||||
1
core/apps/api/views/user/__init__.py
Normal file
1
core/apps/api/views/user/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .ad_like import * # noqa
|
||||
28
core/apps/api/views/user/ad_like.py
Normal file
28
core/apps/api/views/user/ad_like.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from rest_framework import mixins
|
||||
from rest_framework.viewsets import GenericViewSet
|
||||
from django_core.mixins.base import BaseViewSetMixin
|
||||
from drf_spectacular.utils import extend_schema
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from core.apps.accounts.models import UserLike
|
||||
from core.apps.api.serializers.user.ad_like import (
|
||||
ListUserLikeSerializer,
|
||||
CreateUserLikeSerializer,
|
||||
|
||||
)
|
||||
|
||||
|
||||
@extend_schema(tags=['User Like'])
|
||||
class UserLikeViewSet(BaseViewSetMixin, mixins.ListModelMixin, mixins.CreateModelMixin,
|
||||
mixins.DestroyModelMixin, GenericViewSet):
|
||||
serializer_class = ListUserLikeSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
http_method_names = ['get', 'post', 'delete']
|
||||
action_permission_classes = {}
|
||||
action_serializer_class = {
|
||||
'list': ListUserLikeSerializer,
|
||||
'create': CreateUserLikeSerializer,
|
||||
}
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = UserLike.objects.filter(user=self.request.user).order_by('-id')
|
||||
return queryset
|
||||
Reference in New Issue
Block a user