def user_form_create(request): data = JSONParser().parse(request) serializer = UserSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.errors, status=400)
def user_delete(request, pk): try: user = User.objects.get(pk=pk) except User.DoesNotExist: return Response({'error': 'User does not exist.'}, status=404) user.is_active = False serializer = UserSerializer(user, data=user.__dict__) if serializer.is_valid(): serializer.save() return Response(status=204) return Response(serializer.errors, status=400)
def user_form_get(request, pk): try: user = User.objects.get(pk=pk) except User.DoesNotExist: return Response({'error': 'User does not exist.'}, status=404) serializer = UserSerializer(user) return Response(serializer.data)
def get_user_from_email(request, email): try: user = User.objects.get(email=email) except User.DoesNotExist: return Response({'error': 'User does not exist.'}, status=404) serializer = UserSerializer(user) return Response(serializer.data)
def user_form_update(request, pk): try: user = User.objects.get(pk=pk) except User.DoesNotExist: return Response({'error': 'User does not exist.'}, status=404) data = JSONParser().parse(request) if data.get('old_password') is not None: if user.check_password(data.get('old_password')): user.set_password(data.get('password')) user.save() return Response(status=200) else: return Response({'old_password': ['Wrong password']}, status=409) serializer = UserSerializer(user, data=data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=400)
def user_list(request): users = User.objects.all() serializer = UserSerializer(users, many=True) return Response(serializer.data)
# views.py Copyright ©️ 2020 by the HabitRabbit developers (ardianq, lachchri16, sweiland, YellowIcicle). from django.contrib.auth.decorators import permission_required from drf_yasg.utils import swagger_auto_schema from rest_framework.decorators import api_view, permission_classes from rest_framework.parsers import JSONParser from rest_framework.permissions import AllowAny from rest_framework.response import Response from HabitRabbit.models import Habit, Type, Message, ProfilePicture, User, FAQ from HabitRabbit.serializers import HabitSerializer, TypeSerializer, MessageSerializer, \ ProfilePictureSerializer, UserSerializer, EmailSerializer, UserNumberSerializer, FaqSerializer, UniqueUserSerializer # GETs for all @swagger_auto_schema(method='GET', responses={200: UserSerializer(many=True)}) @api_view(['GET']) @permission_required('HabitRabbit.view_user', raise_exception=True) def user_list(request): users = User.objects.all() serializer = UserSerializer(users, many=True) return Response(serializer.data) @swagger_auto_schema(method='GET', responses={200: HabitSerializer(many=True)}) @api_view(['GET']) @permission_required('HabitRabbit.view_habit', raise_exception=True) def habit_list(request): habits = Habit.objects.all() serializer = HabitSerializer(habits, many=True) return Response(serializer.data)