Skip to content
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

done: #171 - APIs #172

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/BookShelf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_PAGINATION_CLASS': 'BookShelf.utilities.pagination.Pagination',
'PAGE_SIZE': 10,
}

SIMPLE_JWT = {
Expand Down
1 change: 1 addition & 0 deletions backend/BookShelf/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
path('user/', include('user_api.urls')),
path('author/', include('author_api.urls')),
path('book/', include('book_api.urls')),
path('publisher/', include('publisher_api.urls')),
# Swagger documentation URLs
path('swagger/', schema_view.with_ui('swagger',
cache_timeout=0), name='schema-swagger-ui'),
Expand Down
4 changes: 3 additions & 1 deletion backend/BookShelf/utilities/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ def has_permission(self, request, view):
return (
request.user
and request.user.is_authenticated
and request.user.role == 1
and (
request.user.role == 1 or request.user.role == 2
)
)
9 changes: 4 additions & 5 deletions backend/publisher_api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
class PublisherAdmin(admin.ModelAdmin):
list_display = [
'id', 'name', 'address', 'website',
'email', 'phone_number',
'established_year',
'email', 'phone_number', 'established_year',
'is_deleted', 'added_by',
]
list_display_links = [
'name',
]
list_filter = []
search_fields = [
'name', 'address', 'website',
'email', 'phone_number',
'established_year',
'name', 'address', 'website', 'email',
'phone_number', 'established_year',
]
readonly_fields = [
'id', 'added_date_time', 'updated_date_time',
Expand Down
6 changes: 6 additions & 0 deletions backend/publisher_api/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from publisher_api.serializers.v1 import PublisherSerializer


__all__ = [
'PublisherSerializer',
]
8 changes: 8 additions & 0 deletions backend/publisher_api/urls/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path, include

from publisher_api.urls.v1 import urlpatterns as v1


urlpatterns = [
path('v1/', include(v1)),
]
15 changes: 15 additions & 0 deletions backend/publisher_api/urls/v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# from django.urls import path
from rest_framework.routers import DefaultRouter

from publisher_api.views.v1 import PublisherViewSet


router = DefaultRouter()


router.register('', PublisherViewSet, basename='publishers')


urlpatterns = []

urlpatterns += router.urls
3 changes: 0 additions & 3 deletions backend/publisher_api/views.py

This file was deleted.

Empty file.
60 changes: 60 additions & 0 deletions backend/publisher_api/views/v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from rest_framework import status

from BookShelf.utilities.permissions import IsAdminOrModerator

from publisher_api.models import Publisher

from publisher_api.serializers import PublisherSerializer


class PublisherViewSet(ModelViewSet):
queryset = Publisher.objects.filter(is_deleted=False)
serializer_class = PublisherSerializer
permission_classes = [
IsAdminOrModerator,
]
# authentication_classes = [TokenAuthentication]

def perform_create(self, serializer):
"""
Custom logic when creating a book instance.
"""
serializer.save(added_by=self.request.user)

def perform_update(self, serializer):
"""
Custom logic when updating a book instance.
"""
serializer.save(updated_by=self.request.user)

def perform_destroy(self, instance):
"""
Custom logic when deleting a book instance.
"""
instance.is_deleted = True
instance.save()

def destroy(self, request, *args, **kwargs):
"""
Custom response after soft deleting an instance.
"""
instance = self.get_object()
self.perform_destroy(instance)
return Response(
{"message": f"Publication '{instance.name}' has been successfully deleted."}, # noqa
status=status.HTTP_200_OK,
)

# def get_queryset(self):
# return Publisher.objects.all().order_by('name')

# def get_serializer_class(self):
# return PublisherSerializer

# def get_permissions(self):
# return [IsAuthenticatedOrReadOnly()]

# def get_authentication_classes(self):
# return [TokenAuthentication]