91 lines
2.8 KiB
Python
91 lines
2.8 KiB
Python
from django.contrib.auth.models import User
|
|
from django.shortcuts import get_object_or_404
|
|
from rest_framework.views import APIView
|
|
|
|
from api.serializers.event import EventSerializer
|
|
from common.build_response import build_response
|
|
from web.models import Event, EventComment
|
|
|
|
|
|
class EventCommentView(APIView):
|
|
def post(self, request, event_id):
|
|
try:
|
|
event = get_object_or_404(Event, pk=event_id)
|
|
except Exception:
|
|
return build_response(404, message='Unable to find related event!')
|
|
|
|
data = request.POST
|
|
|
|
try:
|
|
user = User.objects.filter(email=data.get('created_by')).first()
|
|
except Exception:
|
|
return build_response(500, message="Could not find user!")
|
|
|
|
try:
|
|
new_comment = EventComment(
|
|
event=event,
|
|
content=data.get('content'),
|
|
submitted_by=user
|
|
)
|
|
|
|
new_comment.save()
|
|
except Exception:
|
|
return build_response(500, message='Unable to create new comment!')
|
|
|
|
try:
|
|
event_serializer = EventSerializer(event)
|
|
except Exception:
|
|
return build_response(422)
|
|
|
|
return build_response(data=event_serializer.data)
|
|
|
|
def put(self, request, event_id, comment_id):
|
|
try:
|
|
event = get_object_or_404(Event, pk=event_id)
|
|
except Exception:
|
|
return build_response(404, message='Unable to find related event!')
|
|
|
|
data = request.POST
|
|
|
|
try:
|
|
comment = get_object_or_404(EventComment, pk=comment_id)
|
|
except Exception:
|
|
return build_response(404, message='Unable to find comment!')
|
|
|
|
try:
|
|
comment.content = data.get('content', comment.content)
|
|
comment.save()
|
|
except Exception:
|
|
return build_response(500, message='Unable to edit comment')
|
|
|
|
try:
|
|
event_serializer = EventSerializer(event)
|
|
except Exception:
|
|
return build_response(422)
|
|
|
|
return build_response(data=event_serializer.data)
|
|
|
|
def delete(request, event_id, comment_id):
|
|
try:
|
|
event = get_object_or_404(Event, pk=event_id)
|
|
except Exception:
|
|
return build_response(404, message='Unable to find related event!')
|
|
|
|
try:
|
|
comment = get_object_or_404(EventComment, pk=comment_id)
|
|
except Exception:
|
|
return build_response(404, message='Unable to find comment!')
|
|
|
|
try:
|
|
comment.active = False
|
|
comment.save()
|
|
except Exception:
|
|
return build_response(500, message='Unable to delete comment!')
|
|
|
|
try:
|
|
event_serializer = EventSerializer(event)
|
|
except Exception:
|
|
return build_response(422)
|
|
|
|
return build_response(data=event_serializer.data)
|