Demo entry 6706063

to print name

   

Submitted by anonymous on Jan 30, 2018 at 20:39
Language: Python. Code size: 1.5 kB.

from django.shortcuts import render
from django.contrib.auth import authenticate,login
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK,HTTP_400_BAD_REQUEST
from rest_framework.views import APIView
from rest_framework.permissions import AllowAny
from rest_framework import generics

from scm_api.serializers import (UserLoginSerializer,
                                 ItemSerializer,WarehouseSerializer)
from scm_app.models import (Warehouse,Item)

class UserLoginAPIView(APIView):
    permissions_classes = [AllowAny]
    serializer_class = UserLoginSerializer

    def post(self,request,*args,**kwargs):
        serializer = UserLoginSerializer(data=request.data)

        if serializer.is_valid():
            new_data = serializer.data
            user = authenticate(username=new_data['username'], password=new_data['password'])

            if user is not None:
                if user.is_active:
                    login(request, user)
                else:
                    return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
                return Response(new_data, status=HTTP_200_OK)
            return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)


class WarehouseList(generics.ListAPIView):
    queryset = Warehouse.objects.all()
    serializer_class =  WarehouseSerializer


class ItemList(generics.ListAPIView):
    queryset = Item.objects.all()
    serializer_class = ItemSerializer

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).