<commit_before>from django.conf.urls import url
from django.contrib.auth import authenticate, login, logout
from django.middleware.csrf import get_token
from django.views.generic import View
from yunity.api.validation import validate_user_email
from yunity.api.validation import validate_user_password

from yunity.utils.session import RealtimeClientData
from yunity.utils.api.abc import ApiBase, body_as_json
from yunity.utils.api.request import Parameter


class Login(ApiBase, View):
    def get(self, request):
        """get current login status.
        Also generates a CSRF cookie which has to be used for further requests.
        ---
        tags:
            - Authentication
        responses:
            200:
                description: Login state with an empty user if not logged in
                schema:
                    id: user_login_response
                    type: object
                    properties:
                        user:
                            $ref: '#/definitions/user_information_response'
        ...

        :type request: HttpRequest
        """
        # force a CSRF token to be generated for the frontend to use
        get_token(request)
        if request.user.is_authenticated():
            return self.success({'user': {'id': request.user.id, 'display_name': request.user.display_name}})
        else:
            return self.success({'user': {}})

    @body_as_json(parameters=[
        Parameter(name='email', validator=validate_user_email),
        Parameter(name='password', validator=validate_user_password),
    ])
    def post(self, request):
        """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: Login state
                schema:
                    $ref: '#/definitions/user_login_response'
            403:
                description: User credentials wrong
        ...

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


class Logout(ApiBase, View):<commit_msg>Prefer early-out over if/else return<commit_after>from django.conf.urls import url
from django.contrib.auth import authenticate, login, logout
from django.middleware.csrf import get_token
from django.views.generic import View
from yunity.api.validation import validate_user_email
from yunity.api.validation import validate_user_password

from yunity.utils.session import RealtimeClientData
from yunity.utils.api.abc import ApiBase, body_as_json
from yunity.utils.api.request import Parameter


class Login(ApiBase, View):
    def get(self, request):
        """get current login status.
        Also generates a CSRF cookie which has to be used for further requests.
        ---
        tags:
            - Authentication
        responses:
            200:
                description: Login state with an empty user if not logged in
                schema:
                    id: user_login_response
                    type: object
                    properties:
                        user:
                            $ref: '#/definitions/user_information_response'
        ...

        :type request: HttpRequest
        """
        # force a CSRF token to be generated for the frontend to use
        get_token(request)
        if request.user.is_authenticated():
            return self.success({'user': {'id': request.user.id, 'display_name': request.user.display_name}})
        else:
            return self.success({'user': {}})

    @body_as_json(parameters=[
        Parameter(name='email', validator=validate_user_email),
        Parameter(name='password', validator=validate_user_password),
    ])
    def post(self, request):
        """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: Login state
                schema:
                    $ref: '#/definitions/user_login_response'
            403:
                description: User credentials wrong
        ...

        :type request: HttpRequest
        """
        user = authenticate(email=request.body['email'], password=request.body['password'])
        if user is None:
            return self.forbidden(reason="wrong login credentials.")

        login(request, user)
        return self.success({
            'user': {
                'id': request.user.id,
                'display_name': user.display_name,
            },
        })


class Logout(ApiBase, View):
{'fraction_non_alphanumeric': 0.07930798218568003, 'fraction_numerical': 0.003083247687564234, 'mean_word_length': 1.558720420683611, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6242982', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1352, 'n_words': 396}