Skip to content

Instantly share code, notes, and snippets.

View mansha99's full-sized avatar

Manish Sharma mansha99

View GitHub Profile
@mansha99
mansha99 / user.py
Created July 4, 2023 17:36
notice_app/notices/views/user/py
from django.http import JsonResponse
from ..serializers import RegisterSerializer
from rest_framework.decorators import api_view
from django.views.decorators.csrf import csrf_exempt
from rest_framework import status
@csrf_exempt
@api_view(['POST'])
def createAccount(request):
@mansha99
mansha99 / notices.py
Created July 4, 2023 17:38
notice_app/notices/views/notice.py
from django.http import JsonResponse
from ..models import Notice
from rest_framework import permissions
from rest_framework.decorators import api_view
from rest_framework.decorators import permission_classes
from rest_framework.permissions import IsAuthenticated
@api_view(['GET'])
@permission_classes([permissions.IsAuthenticated])
def listAllNotices(request):
output = {}
@mansha99
mansha99 / urls.py
Created July 4, 2023 17:40
notice_app/notices/urls.py
from django.urls import path
from .views import user
from .views import notices
from rest_framework_simplejwt import views as jwt_views
urlpatterns = [
path('register', user.createAccount, name='createAccount'),
path('login/', jwt_views.TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/refresh/', jwt_views.TokenRefreshView.as_view(), name='token_refresh'),
path('listAllNotices', notices.listAllNotices, name='listAllNotices'),
@mansha99
mansha99 / urls.py
Created July 4, 2023 17:41
notice_app/urls.py
from django.urls import include, re_path
from django.contrib import admin
urlpatterns = [
re_path('notices/', include('notices.urls')),
]
@mansha99
mansha99 / views.py
Created July 7, 2023 07:15
notices/views.py
from .models import Notice
from .serializers import NoticeSerializer
from rest_framework import viewsets
from .pagination import CustomPagination
from rest_framework.exceptions import ValidationError
class NoticeModelViewSet(viewsets.ModelViewSet):
queryset = Notice.objects.all()
serializer_class = NoticeSerializer
pagination_class = CustomPagination
@mansha99
mansha99 / pagination.py
Created July 7, 2023 07:22
notices/pagination.py
from rest_framework import pagination
class CustomPagination(pagination.PageNumberPagination):
page_size = 2
page_size_query_param = 'page_size'
max_page_size = 50
page_query_param = 'p'
We can make this file beautiful and searchable if this error is corrected: It looks like row 5 should actually have 3 columns, instead of 2. in line 4.
VERB,URL, DESCRIPTION
GET,http://127.0.0.1:8000//api/v1/notices/,List all Notices
GET,http://127.0.0.1:8000/api/v1/notices/?kw=Hello,List all Notices containing Hello as Title
GET,http://127.0.0.1:8000/api/v1/notices/?p=2&order=-id,List second page order by id descending
GET,http://127.0.0.1:8000/api/v1/notices/
?p=2&kw=Hello&order=-id,List second page containing title Hello order by id descending
GET,http://127.0.0.1:8000//api/v1/notices/1, Get Single notice whose id is 1
POST,http://127.0.0.1:8000//api/v1/notices/, Create new notice using title and description as params
PUT,http://127.0.0.1:8000//api/v1/notices/5/, Update ALL PARAMS existig notice with id =5
PATCH,http://127.0.0.1:8000//api/v1/notices/5/, Partial update (Selected fields) existig notice with id =5
@mansha99
mansha99 / api.py
Created July 7, 2023 09:16
project/api.py
from rest_framework import routers
from notices import views as noticeAppViews
router = routers.DefaultRouter()
router.register(r'notices', noticeAppViews.NoticeModelViewSet)
@mansha99
mansha99 / urls.py
Created July 7, 2023 09:16
urls.py
from django.urls import include, path
from .api import router
urlpatterns = [
path('api/v1/', include(router.urls)),
]
@mansha99
mansha99 / requirements.txt
Created July 8, 2023 11:47
Generated requirements.txt
asgiref==3.7.2
Django==4.2.3
django-environ==0.10.0
djangorestframework==3.14.0
pytz==2023.3
sqlparse==0.4.4