Ejemplo n.º 1
0
 def post(self, request, format=None):
     print("Create quote called")
     serializer = QuoteSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
 def put(self, request, pk, format=None):
     quote = self.get_object(pk)
     serializer = QuoteSerializer(quote, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 3
0
class QuoteDetail(APIView):
    """
    Retrieve, update or delete a quote instance.
    """
    @swagger_auto_schema(
        auto_schema=None, )
    def get_object(self, pk):
        try:
            return Quote.objects.get(pk=pk)
        except Quote.DoesNotExist:
            raise Http404

    @swagger_auto_schema(
        responses={
            200: QuoteSerializer(many=True),
            401: 'Unauthorized',
            404: 'No quote found for the given id'
        },
        tags=['Get Quotes'],
        operation_description="Method to fetch a quote",
    )
    def get(self, request, pk, format=None):
        quote = self.get_object(pk)
        serializer = QuoteSerializer(quote)
        return Response(serializer.data)

    @swagger_auto_schema(
        description="Method to update a quote",
        request_body=QuoteSerializer,
        responses={
            200: QuoteSerializer(many=True),
            401: 'Unauthorized',
            201: 'Quote updated'
        },
        tags=['Create, Update and Delete Quote'],
        operation_description="Method to update a quote",
    )
    def put(self, request, pk, format=None):
        quote = self.get_object(pk)
        serializer = QuoteSerializer(quote, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    @swagger_auto_schema(
        description="Method to delete a quote",
        request_body=QuoteSerializer,
        responses={
            200: QuoteSerializer(many=True),
            401: 'Unauthorized',
            201: 'Quote deleted'
        },
        tags=['Create, Update and Delete Quote'],
        operation_description="Method to update a quote",
    )
    def delete(self, request, pk, format=None):
        quote = self.get_object(pk)
        quote.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Ejemplo n.º 4
0
 def post(self, request, *args, **kwargs):
     quotes_all_obj = QuoteSerializer(data=request.data)
     if quotes_all_obj.is_valid():
         new_obj = quotes_all_obj.save()
         new_ser_obj = QuoteSerializer(new_obj)
         return Response(new_ser_obj.data)
     else:
         return Response(new_ser_obj.errors)
Ejemplo n.º 5
0
 def put(self, request, id, *args, **kwargs):
     quote_obj = Quotes.objects.get(id=id)
     quote_ser_obj = QuoteSerializer(instance=quote_obj,
                                     data=request.data,
                                     partial=True)
     if quote_ser_obj.is_valid():
         updated_obj = quote_ser_obj.save()
         updated_ser_obj = QuoteSerializer(updated_obj)
         return Response(updated_ser_obj.data)
     else:
         return Response(updated_ser_obj.errors)
Ejemplo n.º 6
0
def get_quote(request):
    quote_id = False
    payload_list = str(request.body).split('&')
    for item in payload_list:
        key, val = item.split('=')
        if key == 'text':
            quote_id = val
            break
    if not quote_id:
        data = {
            "response_type": "in_channel",
            "text": f"You need to provide a quote number!",
        }
        return JsonResponse(data)

    quote = Quote.objects.all().filter(id=quote_id)
    serializer = QuoteSerializer(quote, many=True)
    try:
        data = {
            "response_type": "in_channel",
            "text": f"Quote #{serializer.data[0]['id']}",
            "attachments": [{
                "text": serializer.data[0]['text']
            }]
        }
    except IndexError:
        data = {
            "response_type": "in_channel",
            "text": f"Quote #{quote_id} not found in the database",
        }
    return JsonResponse(data)
Ejemplo n.º 7
0
def search_quote(request):
    quote_id = False
    payload_list = str(request.body).split('&')
    for item in payload_list:
        key, val = item.split('=')
        if key == 'text':
            search_string = val.replace('+', ' ')
            break
    if not search_string:
        data = {
            "response_type": "in_channel",
            "text": f"You need to provide a string to search for!",
        }
        return JsonResponse(data)

    quotes = Quote.objects.all().filter(~Q(channel='#crazypoker')).filter(
        text__icontains=search_string)
    serializer = QuoteSerializer(quotes, many=True)
    try:
        i = choice(range(len(serializer.data)))
        data = {
            "response_type": "in_channel",
            "text": f"Quote #{serializer.data[i]['id']}",
            "attachments": [{
                "text": serializer.data[i]['text']
            }]
        }
    except IndexError:
        data = {
            "response_type": "in_channel",
            "text":
            f"No quote found containing '{search_string}' in the database",
        }
    return JsonResponse(data)
Ejemplo n.º 8
0
 def get(self, request, *args, **kwargs):
     r = []
     all_obj = Quotes.objects.all()
     for i in all_obj:
         r.append(i.id)
     n = random.choice(r)
     the_obj = Quotes.objects.get(id=n)
     the_ser_obj = QuoteSerializer(the_obj)
     return Response(the_ser_obj.data)
Ejemplo n.º 9
0
 def get(self, request, lang, *args, **kwargs):
     lang_obj = Language.objects.filter(language_name=lang)
     if lang_obj:
         quote_all_obj = Quotes.objects.filter(lang=lang_obj[0])
         if quote_all_obj:
             quote_ser_all_obj = QuoteSerializer(quote_all_obj, many=True)
             return Response(quote_ser_all_obj.data)
         else:
             raise Exception(" no quotes found with that language")
     else:
         raise Exception("not a valid language")
Ejemplo n.º 10
0
class QuoteList(APIView):
    '''
    List all quotes or create a new quote
    '''
    @swagger_auto_schema(
        responses={
            200: QuoteSerializer(many=True),
            401: 'Unauthorized',
            404: 'No quotes found'
        },
        tags=['Get Quotes'],
        operation_description="Method to fetch all the quotes",
    )
    def get(self, request, format=None):
        print("Get quotes list called")
        quotes = Quote.objects.all()
        serializer = QuoteSerializer(quotes, many=True)
        return Response(serializer.data)

    @swagger_auto_schema(
        description="Method to post a new building",
        request_body=QuoteSerializer,
        responses={
            200: QuoteSerializer(many=False),
            401: 'Unauthorized',
            201: 'Quote Added'
        },
        tags=['Create, Update and Delete Quote'],
        operation_description="Method to post a new Quote",
    )
    def post(self, request, format=None):
        print("Create quote called")
        serializer = QuoteSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 11
0
def random_quote(request):

    pks = Quote.objects.filter(Q(channel='#crazy88')
                               | Q(channel='#slack')).values_list(
                                   'pk', flat=True).order_by('id')
    random_pk = choice(pks)
    quote = Quote.objects.all().filter(
        Q(channel='#crazy88') | Q(channel='#slack')).filter(id=random_pk)
    serializer = QuoteSerializer(quote, many=True)
    data = {
        "response_type": "in_channel",
        "text": f"Quote #{serializer.data[0]['id']}",
        "attachments": [{
            "text": serializer.data[0]['text']
        }]
    }
    return JsonResponse(data)
Ejemplo n.º 12
0
 def get(self, request, lang, *args, **kwargs):
     lang_obj = Language.objects.filter(language_name=lang)
     print(lang_obj)
     if lang_obj:
         r = []
         all_obj = Quotes.objects.filter(lang=lang_obj[0])
         if all_obj:
             for i in all_obj:
                 r.append(i.id)
             n = random.choice(r)
             the_obj = Quotes.objects.get(id=n)
             the_ser_obj = QuoteSerializer(the_obj)
             return Response(the_ser_obj.data)
         else:
             raise Exception("No quotes with that language")
     else:
         raise Exception("not a valid language")
Ejemplo n.º 13
0
 def get(request):
     quotes = Quotes.objects.all()
     if type(quotes) == Response:
         return quotes
     return Response(QuoteSerializer(quotes, many=True).data)
Ejemplo n.º 14
0
 def get(self, request, pk, format=None):
     quote = self.get_object(pk)
     serializer = QuoteSerializer(quote)
     return Response(serializer.data)
Ejemplo n.º 15
0
 def get(self, request, format=None):
     print("Get quotes list called")
     quotes = Quote.objects.all()
     serializer = QuoteSerializer(quotes, many=True)
     return Response(serializer.data)
Ejemplo n.º 16
0
 def get(self, request, *args, **kwargs):
     quote_all_obj = Quotes.objects.all()
     quote_ser_all_obj = QuoteSerializer(quote_all_obj, many=True)
     return Response(quote_ser_all_obj.data)
Ejemplo n.º 17
0
 def get(self, request, format=None):
     quote = Quote.objects.all()
     serializer = QuoteSerializer(quote, many=True)
     return Response(serializer.data)