(request)
View for handeling protected short links
View for handeling protected short links
View for handeling protected short links
def link_auth(request):
    """View for handeling protected short links"""
    if request.method == 'GET':
        code = request.GET.get('next')
        if not code:
            return redirect('400.html')
        return render(request, 'auth/link_auth.html')

    if request.method == 'POST':
        pygmy_client = pygmy_client_object(settings, request)
        data = json.loads(request.body.decode('utf-8'))
        code = data['code']
        secret_key = data['secret_key']
        if not code or not secret_key:
            return render(request, '400.html', status=400)
        try:
            if code.startswith('+') or code.endswith('+'):
                clickmeta = pygmy_client.link_stats(
                    code, secret_key=secret_key)
                response = dict(clickmeta=clickmeta)
            else:
                url_obj = pygmy_client.unshorten(code, secret=secret_key)
                response = dict(long_url=url_obj['long_url'])
        except UnAuthorized:
            response = dict(error='Wrong secret key.')
        except ObjectNotFound as e:
            return render(
                request, '404.html', context=API_ERROR(e.args[0]), status=404)
        # return redirect(long_url, permanent=True)
        return JsonResponse(response)
def link_auth ( request ) : if request . method == 'GET' : code = request . GET . get ( 'next' ) if not code : return redirect ( '400.html' ) return render ( request , 'auth/link_auth.html' ) if request . method == 'POST' : pygmy_client = pygmy_client_object ( settings , request ) data = json . loads ( request . body . decode ( 'utf-8' ) ) code = data [ 'code' ] secret_key = data [ 'secret_key' ] if not code or not secret_key : return render ( request , '400.html' , status = 400 ) try : if code . startswith ( '+' ) or code . endswith ( '+' ) : clickmeta = pygmy_client . link_stats ( code , secret_key = secret_key ) response = dict ( clickmeta = clickmeta ) else : url_obj = pygmy_client . unshorten ( code , secret = secret_key ) response = dict ( long_url = url_obj [ 'long_url' ] ) except UnAuthorized : response = dict ( error = 'Wrong secret key.' ) except ObjectNotFound as e : return render ( request , '404.html' , context = API_ERROR ( e . args [ 0 ] ) , status = 404 ) # return redirect(long_url, permanent=True) return JsonResponse ( response )