Last active
March 20, 2019 13:45
-
-
Save kalsmic/e8ca6c8d1f8fe3d4d71280dc0606ae1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#meetup views | |
""" | |
Contains functionality for the meetup endpoints | |
""" | |
from django.contrib.auth.models import User | |
from django.db.models import ProtectedError, Q | |
from django.shortcuts import get_object_or_404 | |
from drf_yasg.utils import swagger_auto_schema | |
from rest_framework import status | |
from rest_framework.permissions import IsAdminUser | |
from rest_framework.permissions import IsAuthenticated | |
from rest_framework.response import Response | |
from rest_framework.views import APIView | |
from .models import Meeting | |
from .serializers import MeetingSerializer, MeetingSerializerClass | |
# Get, update or delete a meetup | |
# meetups/1 | |
class AMeeting(APIView): | |
permission_classes = (IsAuthenticated,) | |
serializer_class = MeetingSerializerClass | |
@classmethod | |
@swagger_auto_schema( | |
operation_description="Edit a meetup", | |
operation_id="Edit a specific", | |
request_body=MeetingSerializer(many=False), | |
responses={ | |
200: MeetingSerializer(many=False), | |
401: "Unathorized Access", | |
404: "Meeting Does not Exist", | |
}, | |
) | |
def put(cls, request, meeting_id): | |
if not request.user.is_superuser: | |
return Response( | |
data={ | |
"status": status.HTTP_401_UNAUTHORIZED, | |
"error": "Action restricted to Admins!", | |
}, | |
status=status.HTTP_401_UNAUTHORIZED, | |
) | |
meetup = get_object_or_404(Meeting, pk=meeting_id) | |
serializer = MeetingSerializer(meetup, data=request.data) | |
if serializer.is_valid(): | |
serializer.save() | |
result = dict(serializer.data) | |
user = User.objects.filter(Q(id=result["created_by"])).distinct().first() | |
result["created_by_name"] = user.username | |
return Response( | |
data={ | |
"status": status.HTTP_200_OK, | |
"data": [ | |
{ | |
"meetup": result, | |
"success": "Meet updated successfully", | |
} | |
], | |
}, | |
status=status.HTTP_200_OK, | |
) | |
return Response( | |
data={ | |
"status": status.HTTP_400_BAD_REQUEST, | |
"error": serializer.errors, | |
}, | |
status=status.HTTP_400_BAD_REQUEST, | |
) | |
#link to full project source code https://github.com/kalsmic/questioner/blob/develop/meetup/views.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment