def delete_event(request):
    if request.method == 'POST':
        __data = json.loads(request.POST['event'])
        __event = Event.get_by_id_obj(__data['uuid'])
        if(__event != None):
            __event.delete()
            return JsonResponse(True, status=200, safe=False)
        return JsonResponse({"Error":"Inspection not found"}, status=405, safe=False)
    else:
        return JsonResponse({"Error":"Agromap: HTTP method not allowed"}, status=405, safe=False)
def update_event(request):
    if request.method == 'POST':
        __data = json.loads(request.POST['event'])
        __event = Event.get_by_id_obj(__data['uuid'])
        serializer = EventSerializer(__event, data=__data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(True, status=201, safe=False)
        return JsonResponse(serializer.errors, status=400, safe=False)
    else:
        return JsonResponse({"Error":"Agromap: HTTP method not allowed"}, status=405, safe=False)
def create_events(request):
    if request.method == 'POST':
        __data = json.loads(request.POST['event'])
        for __event in __data:
            if( __event['latitude'] == 'delete'):
                try:
                    event_to_delete = Event.get_by_id_obj(__event['uuid'])
                    event_to_delete.delete()
                except:
                    pass
            else:
                __event_obj = Event.get_by_id_obj(__event['uuid'])
                if(__event_obj != None):
                    serializer = EventSerializer(__event_obj, data=__event)
                else:
                    serializer = EventSerializer(data=__event)
                if serializer.is_valid():
                    serializer.save()
                else:
                    print(serializer.errors)
                    return JsonResponse("False", status=400, safe=False)
        return JsonResponse("True", status=201, safe=False)
    else:
        return JsonResponse({"Error":"Agromap: HTTP method not allowed"}, status=405, safe=False)
def delete_event(request, uuid):
    __logged_user = UserSession.GetSessionData(request)
    __event = Event.get_by_id_obj(uuid)
    __msg_text = "Evento não encontrado!"
    __msg_type = "warning"
    if (__event == None):
        __inspections = Inspection.get_all_obj()
        return render(
            request, 'inspection/list.html', {
                'title': 'Lista de Inspeções',
                'user': __logged_user,
                'inspections': __inspections,
                'msg_text': __msg_text,
                'msg_type': __msg_type
            })
    __inspection = __event.inspection
    __msg_text = "Você precisa ser o administrador da inspeção para excluir"
    __msg_type = "warning"
    if (__logged_user.id == __event.inspection.supervisor.id):
        try:
            delete_photo(
                str(__event.inspection.id) + '/' + __event.uuid + '.png')
            __event.delete()
            __msg_text = "Excluído com sucesso!"
            __msg_type = "success"
        except Exception as e:
            print(e)
            __msg_text = "Erro ao excluir!"
            __msg_type = "danger"

    __events = Event.get_by_inspection_obj(__inspection.id)
    return render(
        request, 'event/events.html', {
            'title': 'Eventos',
            'user': __logged_user,
            'inspection': __inspection,
            'events': __events,
            'msg_text': __msg_text,
            'msg_type': __msg_type
        })