Posts

Showing posts with the label django rest framework

DRF permissions - visitors can view but is_manager can POST, PUT, DELETE request

 permissions.py from rest_framework.permissions import BasePermission from rest_framework import permissions class UnAuthenticatedOrReadOnly(BasePermission):     def has_permission(self, request, view):         if request.user.is_authenticated:             if request.method in permissions.SAFE_METHODS:                 return True  # Allow unauthenticated users to perform GET and RETRIEVE             return request.user.is_manager  # Only manager users can perform POST, PUT, DELETE         return request.method in permissions.SAFE_METHODS  # Allow unauthenticated users to perform GET and RETRIEVE     def has_object_permission(self, request, view, obj):         if request.user.is_authenticated:             return request.user.is_manager  # Only manager users...

hash password during user registration - without simple JWT

 views.py from rest_framework.response import Response from rest_framework import status from core.models import User from .serializers import * from rest_framework.permissions import IsAuthenticated, AllowAny from rest_framework_simplejwt.authentication import JWTAuthentication from rest_framework.viewsets import ModelViewSet class Account(ModelViewSet):     queryset = User.objects.all()     serializer_class = AccountSeralizer     permission_classes = [IsAuthenticated]     authentication_classes = [JWTAuthentication]     def get_permissions(self):         if self.action == "create":             return [AllowAny()]         else:             return [IsAuthenticated()]     def create(self, request):         data = request.data         try:             serial...