Skip to content

Commit

Permalink
chore(products, config): add pagination to product list, add test con…
Browse files Browse the repository at this point in the history
…fig for deployment
  • Loading branch information
tyronejosee committed Sep 25, 2024
1 parent df649cd commit e2ef1e4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
12 changes: 10 additions & 2 deletions backend/apps/products/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django_filters import rest_framework as filters

from .models import Product
from .models import Product, Brand, Category
from .choices import SortChoices


Expand All @@ -14,10 +14,18 @@ class ProductFilter(filters.FilterSet):
method="filter_by_order",
label="Search query sort direction, ex `/?=sort_by=latest`",
)
brand = filters.ModelChoiceFilter(
queryset=Brand.objects.get_available(),
field_name="brand_id",
)
category = filters.ModelChoiceFilter(
queryset=Category.objects.get_available(),
field_name="category_id",
)

class Meta:
model = Product
fields = []
fields = ["brand", "category"]

def filter_by_order(self, queryset, name, value):
if value == SortChoices.LATEST:
Expand Down
13 changes: 13 additions & 0 deletions backend/apps/products/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Pagination for Products App."""

from rest_framework.pagination import PageNumberPagination


class ProductPagination(PageNumberPagination):
"""Pagination class for Product model."""

max_page_size = 100
page_query_param = "page"
page_size = 10
page_size_query_param = "page_size"
page_size_query_description = "Number of results to return per page."
2 changes: 2 additions & 0 deletions backend/apps/products/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ProductMinimalSerializer,
)
from .filters import ProductFilter
from .pagination import ProductPagination
from .schemas import (
brand_list_schema,
category_list_schema,
Expand Down Expand Up @@ -66,6 +67,7 @@ class ProductListView(ListAPIView):
serializer_class = ProductMinimalSerializer
search_fields = ["name"]
filterset_class = ProductFilter
pagination_class = ProductPagination

def get_queryset(self):
return Product.objects.get_list()
Expand Down
18 changes: 9 additions & 9 deletions backend/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,21 @@
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.AllowAny",
],
"DEFAULT_THROTTLE_CLASSES": [
"rest_framework.throttling.AnonRateThrottle",
"rest_framework.throttling.UserRateThrottle",
],
# "DEFAULT_THROTTLE_CLASSES": [
# "rest_framework.throttling.AnonRateThrottle",
# "rest_framework.throttling.UserRateThrottle",
# ],
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
"DEFAULT_CONTENT_LANGUAGE": "en",
"DEFAULT_FILTER_BACKENDS": [
"rest_framework.filters.SearchFilter",
"django_filters.rest_framework.DjangoFilterBackend",
],
"DEFAULT_THROTTLE_RATES": {
"anon": "5/second",
"user": "60/minute",
"daily": "1000/day",
},
# "DEFAULT_THROTTLE_RATES": {
# "anon": "5/second",
# "user": "60/minute",
# "daily": "1000/day",
# },
"NUM_PROXIES": None,
"SEARCH_PARAM": "search",
}
Expand Down
16 changes: 16 additions & 0 deletions backend/config/settings/production.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
"""Settings for config project (Production)."""

import os
import environ

from .base import *


env = environ.Env()
environ.Env.read_env("backend/.env")

STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": env("DB_NAME"),
"USER": env("DB_USER"),
"PASSWORD": env("DB_PASSWORD"),
"HOST": env("DB_HOST"),
"PORT": env("DB_PORT"),
}
}

# SECURE_SSL_REDIRECT = True

# SECURE_HSTS_SECONDS = 31536000 # 1 year
Expand Down

0 comments on commit e2ef1e4

Please sign in to comment.