from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.middleware import csrf
from rest_framework.response import Response
from rest_framework.exceptions import AuthenticationFailed
from wq.db.rest import app
from wq.db.rest.models import get_object_id
from wq.db.rest.views import SimpleViewSet, ModelViewSet


class AuthView(SimpleViewSet):
    def user_info(self, request):
        user_dict = app.router.serialize(request.user)
        user_dict['id'] = get_object_id(request.user)
        return Response({
            'user': user_dict,
            'config': app.router.get_config(request.user),
            'csrftoken': csrf.get_token(request),
        })

    def csrf_info(self, request):
        response = {}
        token = csrf.get_token(request)
        if token:
            response['csrftoken'] = token
        return Response(response)


class LoginView(AuthView):
    def list(self, request, *args, **kwargs):
        if request.user.is_authenticated():
            return self.user_info(request)
        else:
            return self.csrf_info(request)

    def create(self, request, *args, **kwargs):
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None and user.is_active:
            login(request, user)
            return self.user_info(request)
        else:
            raise AuthenticationFailed(detail="Invalid username or password")


class LogoutView(AuthView):
    def list(self, request, *args, **kwargs):
        if request.user.is_authenticated():
            logout(request)
        return self.csrf_info(request)


class UserViewSet(ModelViewSet):
    lookup_field = 'username'

app.router.add_page('login', {'name': 'Log in', 'url': 'login'}, LoginView)
app.router.add_page('logout', {'name': 'Log out', 'url': 'logout'}, LogoutView)

app.router.register_viewset(User, UserViewSet)

from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.middleware import csrf
from rest_framework.response import Response
from rest_framework.exceptions import AuthenticationFailed
from wq.db.rest import app
from wq.db.rest.models import get_object_id
from wq.db.rest.views import SimpleViewSet, ModelViewSet


class AuthView(SimpleViewSet):
    def user_info(self, request):
        user_dict = app.router.serialize(request.user)
        user_dict['id'] = get_object_id(request.user)
        return Response({
            'user': user_dict,
            'config': app.router.get_config(request.user),
            'csrftoken': csrf.get_token(request),
        })

    def csrf_info(self, request):
        response = {}
        token = csrf.get_token(request)
        if token:
            response['csrftoken'] = token
        return Response(response)


class LoginView(AuthView):
    def list(self, request, *args, **kwargs):
        if request.user.is_authenticated():
            return self.user_info(request)
        else:
            return self.csrf_info(request)

    def create(self, request, *args, **kwargs):
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None and user.is_active:
            login(request, user)
            return self.user_info(request)
        else:
            raise AuthenticationFailed(detail="Invalid username or password")


class LogoutView(AuthView):
    def list(self, request, *args, **kwargs):
        if request.user.is_authenticated():
            logout(request)
        return self.csrf_info(request)


class UserViewSet(ModelViewSet):
    lookup_field = 'username'

app.router.add_page('login', {'url': 'login'}, LoginView)
app.router.add_page('logout', {'url': 'logout'}, LogoutView)

app.router.register_viewset(User, UserViewSet)

use default names