from rest_framework.response import Response
from rest_framework.decorators import api_view

import datetime

from authen.models import AuthenHistory

# Create your views here.

def verifyToken(authheader):
    if  authheader == None:
        return False

    if len(authheader.replace('Bearer ', '').strip()) == 0:
        return False

    return authheader.replace('Bearer ', '').strip() == 'authentication'

@api_view(['POST'])
def authentication(request, format=None):

    if request.method == 'POST':
        try:
            authheader = verifyToken(request.headers.get('Authorization'))

            if not authheader:
                return Response({
                    'success': False,
                    'err': 'Can not verify Authentication-Header.' 
                })

            timeinfo = datetime.date.today()

            AuthenHistory.objects.create(date=timeinfo)

            return Response({
                'success': True,
                'time': timeinfo
            })
        except ValueError as ex:
            return Response({
                'success': False,
                'err': str(ex)
            })
from rest_framework.response import Response
from rest_framework.decorators import api_view

import datetime

from authen.models import AuthenHistory

# Create your views here.

def verifyToken(authheader):
    if  authheader == None:
        return False

    if len(authheader.replace('Bearer ', '').strip()) == 0:
        return False

    return authheader.replace('Bearer ', '').strip() == 'authentication'

@api_view(['POST'])
def authentication(request, format=None):

    if request.method == 'POST':
        try:
            authheader = verifyToken(request.headers.get('Authorization'))

            if not authheader:
                return Response({
                    'success': False,
                    'err': 'Can not verify Authentication-Header.' 
                })

            timeinfo = datetime.date.today()

            AuthenHistory.objects.create(date=timeinfo)

            return Response({
                'success': True,
                'time': timeinfo
            })
        except ValueError as ex:
            return Response({
                'success': False,
                'err': str(ex)
            })
# Create your views here.