from django.conf.urls import url
from django.contrib.auth import authenticate, login, logout
from django.views.generic import View

from yunity.utils.session import RealtimeClientData
from yunity.utils.api import ApiBase, body_as_json
import yunity.utils.status


class Login(ApiBase, View):
    def get(self, request):
        """get current login status
        ---
        tags:
            - Authentication
        responses:
            200:
                description: Login state
                schema:
                    $ref: '#/definitions/user_information_response
            404:
                description: User is not logged in

        :type request: HttpRequest
        """
        if request.user.is_authenticated():
            return self.success({'id': request.user.id, 'name': request.user.name})
        else:
            return self.error(status=yunity.utils.status.HTTP_404_NOT_FOUND)

    @body_as_json(expected_keys=['email', 'password'])
    def post(self, request, data):
        """Logs in the user using the provided credentials
        ---
        tags:
            - Authentication
        parameters:
            - in: body
              name: body
              schema:
                  id: User
                  required:
                      - email
                      - password
                  properties:
                      email:
                          type: string
                          description: email for user
                      password:
                          type: string
                          description: password of user
        responses:
            200:
                description: User logged in
            403:
                description: User credentials wrong
        ...


        :type request: HttpRequest
        """
        user = authenticate(email=data['email'], password=data['password'])
        if user is not None:
            login(request, user)
            return self.success({'id': user.id})
        else:
            return self.forbidden(reason="wrong login credentials.")


class Logout(ApiBase, View):
    def post(self, request):
        """log out the user
        ---
        tags:
          - Authentication
        responses:
            200:
                description: User logged out
        ...

        :type request: HttpRequest
        """
        RealtimeClientData.destroy_user_session(request.session.session_key)
        logout(request)
        return self.success()


urlpatterns = [
    url(r'^/login$', Login.as_view()),
    url(r'^/logout$', Logout.as_view()),
]

@@ -497,32 +497,238 @@
         schema:%0A
+                    id: user_login_response%0A                    type: object%0A                    required:%0A                      - user%0A                    properties:%0A                        user:%0A        
                 
@@ -769,32 +769,33 @@
 rmation_response
+'
 %0A            404
@@ -846,16 +846,28 @@
 ogged in
+%0A        ...
 %0A%0A      
@@ -979,24 +979,33 @@
 elf.success(
+%7B'user': 
 %7B'id': reque
@@ -1042,16 +1042,17 @@
 er.name%7D
+%7D
 )%0A      
@@ -1093,53 +1093,29 @@
 ror(
-status=yunity.utils.status.HTTP_404_NOT_FOUND
+'User not logged in.'
 )%0A%0A 
@@ -1873,22 +1873,105 @@
 on: 
-User logged in
+Login state%0A                schema:%0A                    $ref: '#/definitions/user_login_response'
 %0A   
@@ -2273,20 +2273,65 @@
 s(%7B'
-id': user.id
+user': %7B'id': request.user.id, 'name': request.user.name%7D
 %7D)%0A 

Change login return json to prefix with user: