Ejemplo n.º 1
0
    def create(self, request):
        decoded_token = decode_token(request.META)

        serializer = EventSerializer(data=request.data)
        if serializer.is_valid():
            event = Event()

            data = serializer.data
            data['calendar_id'] = -1
            data['created'] = now()
            data['updated'] = now()
            data['user_id'] = decoded_token['user_id']

            inserted = event.insert_event(data)

            result = {}
            if inserted.get('inserted', 0) == 1:
                e = EventSerializer(event.get_event_with_pk(
                    inserted['generated_keys'][0]),
                                    many=True)
                if len(e.data) > 0:
                    result = e.data[0]

            event.close()

            return Response(result, status=status.HTTP_201_CREATED)

        return Response(serializer.errors)
Ejemplo n.º 2
0
    def create(self, request, calendar_id):
        # self.kwargs['calendar_id']
        decoded_token = decode_token(request.META)

        calendar = Calendar()
        obj = calendar.get(decoded_token['user_id'], calendar_id)
        calendar.close()

        if len(obj) < 1:
            calendar_id = -1
            # return Response({"message": "calendar_id does not exists."},
            #                status=status.HTTP_404_NOT_FOUND)

        serializer = EventSerializer(data=request.data)
        if serializer.is_valid():
            event = Event()

            data = serializer.data
            data['calendar_id'] = calendar_id
            data['created'] = now()
            data['updated'] = now()
            data['user_id'] = decoded_token['user_id']

            inserted = event.insert_event(data)
            event.close()

            return Response({
                "message": "Event created.",
                "data": inserted
            },
                            status=status.HTTP_201_CREATED)

        print("OK")
        print(serializer.errors)

        return Response(serializer.errors)