Banner api lari tayyor
This commit is contained in:
@@ -2,3 +2,4 @@ from .category import * # noqa
|
||||
from .ad import * # noqa
|
||||
from .ad_items import * # noqa
|
||||
from .feedback import * # noqa
|
||||
from .banner import * # noqa
|
||||
|
||||
1
core/apps/api/admin/banner/__init__.py
Normal file
1
core/apps/api/admin/banner/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .banner import * # noqa
|
||||
12
core/apps/api/admin/banner/banner.py
Normal file
12
core/apps/api/admin/banner/banner.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from django.contrib import admin
|
||||
from unfold.admin import ModelAdmin
|
||||
|
||||
from core.apps.api.models import Banner
|
||||
|
||||
|
||||
@admin.register(Banner)
|
||||
class BannerAdmin(ModelAdmin):
|
||||
list_display = (
|
||||
"id",
|
||||
"__str__",
|
||||
)
|
||||
@@ -1,6 +1,7 @@
|
||||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django_core.models.base import AbstractBaseModel
|
||||
from model_bakery import baker
|
||||
|
||||
|
||||
class Banner(AbstractBaseModel):
|
||||
@@ -12,6 +13,10 @@ class Banner(AbstractBaseModel):
|
||||
bg_color = models.CharField(verbose_name=_("BG Color"), max_length=255)
|
||||
text_color = models.CharField(verbose_name=_("Text Color"), max_length=255)
|
||||
|
||||
@classmethod
|
||||
def _baker(cls):
|
||||
return baker.make(cls)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.pk)
|
||||
|
||||
|
||||
@@ -3,3 +3,4 @@ from .search import * # noqa
|
||||
from .ad import * # noqa
|
||||
from .user import * # noqa
|
||||
from .notification import * # noqa
|
||||
from .banner import * # noqa
|
||||
|
||||
1
core/apps/api/serializers/banner/__init__.py
Normal file
1
core/apps/api/serializers/banner/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .banner import * # noqa
|
||||
28
core/apps/api/serializers/banner/banner.py
Normal file
28
core/apps/api/serializers/banner/banner.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from rest_framework import serializers
|
||||
from core.apps.api.models import Banner
|
||||
|
||||
|
||||
class BaseBannerSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Banner
|
||||
fields = [
|
||||
"title",
|
||||
"description",
|
||||
"mobile_image",
|
||||
"desktop_image",
|
||||
"link",
|
||||
"bg_color",
|
||||
"text_color",
|
||||
]
|
||||
|
||||
|
||||
class ListBannerSerializer(BaseBannerSerializer):
|
||||
class Meta(BaseBannerSerializer.Meta): ...
|
||||
|
||||
|
||||
class RetrieveBannerSerializer(BaseBannerSerializer):
|
||||
class Meta(BaseBannerSerializer.Meta): ...
|
||||
|
||||
|
||||
class CreateBannerSerializer(BaseBannerSerializer):
|
||||
class Meta(BaseBannerSerializer.Meta): ...
|
||||
@@ -2,3 +2,4 @@ from .category import * # noqa
|
||||
from .ad import * # noqa
|
||||
from .search import * # noqa
|
||||
from .user import * # noqa
|
||||
from .banner import * # noqa
|
||||
|
||||
1
core/apps/api/tests/banner/__init__.py
Normal file
1
core/apps/api/tests/banner/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .test_banner import * # noqa
|
||||
58
core/apps/api/tests/banner/test_banner.py
Normal file
58
core/apps/api/tests/banner/test_banner.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import pytest
|
||||
from django.urls import reverse
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
from core.apps.api.models import Banner
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def instance(db):
|
||||
return Banner._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("banner-list"),
|
||||
"retrieve": reverse("banner-detail", kwargs={"pk": instance.pk}),
|
||||
"retrieve-not-found": reverse("banner-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_retrieve(data):
|
||||
urls, client, _ = data
|
||||
response = client.get(urls["retrieve"])
|
||||
data_resp = response.json()
|
||||
assert response.status_code == 200
|
||||
assert data_resp["status"] is True
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_retrieve_not_found(data):
|
||||
urls, client, _ = data
|
||||
response = client.get(urls["retrieve-not-found"])
|
||||
data_resp = response.json()
|
||||
assert response.status_code == 404
|
||||
assert data_resp["status"] is False
|
||||
@@ -2,9 +2,10 @@ from django.urls import include, path
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
from core.apps.api.views import CategoryHomeApiViewSet, CategoryViewSet, HomeAdApiView, SearchHistoryViewSet, \
|
||||
UserLikeViewSet, NotificationViewSet
|
||||
UserLikeViewSet, NotificationViewSet, BannerViewSet
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register("banner", BannerViewSet, basename="banner")
|
||||
router.register("notification", NotificationViewSet, basename="notification")
|
||||
router.register("user-like", UserLikeViewSet, basename="user-like")
|
||||
router.register("category", CategoryViewSet, basename="category")
|
||||
|
||||
@@ -3,3 +3,4 @@ from .search import * # noqa
|
||||
from .ad import * # noqa
|
||||
from .user import * # noqa
|
||||
from .notification import * # noqa
|
||||
from .banner import * # noqa
|
||||
|
||||
1
core/apps/api/views/banner/__init__.py
Normal file
1
core/apps/api/views/banner/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .banner import * # noqa
|
||||
25
core/apps/api/views/banner/banner.py
Normal file
25
core/apps/api/views/banner/banner.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from rest_framework.permissions import IsAuthenticated, AllowAny
|
||||
from rest_framework.viewsets import ReadOnlyModelViewSet
|
||||
from drf_spectacular.utils import extend_schema
|
||||
from django_core.mixins import BaseViewSetMixin
|
||||
|
||||
from core.apps.api.models import Banner
|
||||
from core.apps.api.serializers.banner import (
|
||||
ListBannerSerializer,
|
||||
RetrieveBannerSerializer,
|
||||
CreateBannerSerializer,
|
||||
)
|
||||
|
||||
|
||||
@extend_schema(tags=['Banner'])
|
||||
class BannerViewSet(BaseViewSetMixin, ReadOnlyModelViewSet):
|
||||
queryset = Banner.objects.all()
|
||||
serializer_class = ListBannerSerializer
|
||||
permission_classes = [AllowAny]
|
||||
|
||||
action_permission_classes = {}
|
||||
action_serializers = {
|
||||
'list': ListBannerSerializer,
|
||||
'retrieve': RetrieveBannerSerializer,
|
||||
'create': CreateBannerSerializer,
|
||||
}
|
||||
Reference in New Issue
Block a user