-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/api products catalogue #65
Changes from 10 commits
6375bbb
1dd9614
54ff4f3
4f06486
6a979f4
9b557fa
f5da487
6fe3ae1
4d35211
d838339
84b0232
b4a8614
ad8496f
e90c067
1f1586f
eb78938
2010e6d
c123e72
c5bd5a4
b0ecc7b
b0efde6
6cb032f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.urls import path, include | ||
from .api_views import ProductViewSet | ||
from rest_framework.routers import DefaultRouter | ||
|
||
router = DefaultRouter() | ||
router.register('', ProductViewSet, basename='products-api') | ||
|
||
urlpatterns = [ | ||
path('', include(router.urls)), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from .models import Product | ||
from .serializers import ProductSerializer | ||
from .filters import ProductFilter | ||
from rest_framework import viewsets | ||
from django_filters.rest_framework import DjangoFilterBackend | ||
Comment on lines
+1
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PEP8 import order rules not followed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not followed in the whole project. Good pointing out, we need to add something to guard id. Right now, let's ignore. |
||
|
||
|
||
class ProductViewSet(viewsets.ModelViewSet): | ||
queryset = Product.objects.filter(is_active=True, parent_product=None) | ||
serializer_class = ProductSerializer | ||
filter_backends = [DjangoFilterBackend] | ||
filterset_class = ProductFilter |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django_filters import rest_framework as filters | ||
from .models import Product | ||
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PEP8 import order rules not followed |
||
|
||
|
||
class ProductFilter(filters.FilterSet): | ||
name = filters.CharFilter(field_name='name', lookup_expr='icontains') | ||
jacoor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class Meta: | ||
model = Product | ||
fields = ['name'] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rest_framework import serializers | ||
from .models import Product | ||
|
||
|
||
class ProductSerializer(serializers.ModelSerializer): | ||
|
||
class Meta: | ||
model = Product | ||
fields = ['id', 'category', 'name', 'price', 'short_description', 'full_description', 'parent_product'] | ||
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a suggestion: you can add read_only_fields to Meta (with e.g. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import pytest | ||
from django.urls import reverse | ||
from apps.products_catalogue.models import Category, Product | ||
# pylama:ignore=W0404, W0611 | ||
from apps.users.conftest import api_client, login_url, login_data, user_instance, user_instance_token | ||
Comment on lines
+1
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PEP8 import order rules not followed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not followed in the whole project. Good pointing out, we need to add something to guard id. Right now, let's ignore. |
||
|
||
|
||
@pytest.fixture | ||
def create_category(): | ||
return Category.objects.create(name='Test Category', is_active=True) | ||
|
||
|
||
@pytest.fixture | ||
def create_product(): | ||
category = Category.objects.create(name='Test Category', is_active=True) | ||
return Product.objects.create( | ||
name="main product", | ||
category=category, | ||
price=11, | ||
short_description="short desc", | ||
full_description="full_description" | ||
) | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_access_protected_resource(api_client, user_instance_token): | ||
jacoor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
api_client.credentials(HTTP_AUTHORIZATION=f'Token {user_instance_token.key}') | ||
jacoor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
url = reverse('products-api-list') | ||
response = api_client.get(url) | ||
assert response.status_code == 200 | ||
|
||
|
||
def test_access_protected_resource_without_authentication(api_client): | ||
url = reverse('products-api-list') | ||
response = api_client.get(url) | ||
|
||
assert response.status_code == 401 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_product_detail(api_client, user_instance_token, create_product): | ||
api_client.credentials(HTTP_AUTHORIZATION=f'Token {user_instance_token.key}') | ||
|
||
url = reverse('products-api-detail', kwargs={'pk': create_product.id}) | ||
response = api_client.get(url) | ||
|
||
assert response.status_code == 200 | ||
assert response.data['id'] == create_product.id | ||
assert response.data['name'] == "main product" | ||
assert response.data['price'] == "11.00" | ||
assert response.data['short_description'] == "short desc" | ||
assert response.data['full_description'] == "full_description" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_create_product(api_client, user_instance_token, create_category): | ||
api_client.credentials(HTTP_AUTHORIZATION=f'Token {user_instance_token.key}') | ||
|
||
url = reverse('products-api-list') | ||
data = { | ||
'category': create_category.id, # replace with an existing category ID | ||
jacoor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'name': 'Test Product', | ||
'price': '19.99', | ||
'short_description': 'Test short description', | ||
'full_description': 'Test full description', | ||
} | ||
response = api_client.post(url, data, format='json') | ||
|
||
assert response.status_code == 201 | ||
assert response.data['name'] == "Test Product" | ||
assert response.data['price'] == "19.99" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_update_product(api_client, user_instance_token, create_product): | ||
api_client.credentials(HTTP_AUTHORIZATION=f'Token {user_instance_token.key}') | ||
|
||
url = reverse('products-api-detail', kwargs={'pk': create_product.id}) | ||
data = {'name': 'Updated product name'} | ||
response = api_client.patch(url, data, format='json') | ||
|
||
assert response.status_code == 200 | ||
assert response.data['name'] == "Updated product name" | ||
assert response.data['short_description'] == "short desc" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be a good idea to remove the trailing slashes. My front-end friend recently pointed out that it was easier for them that way.
router = DefaultRouter(trailing_slash=False)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont have preference @knop-k. Your call.