__author__ = 'tanshu'

import pkg_resources
from pyramid.httpexceptions import HTTPForbidden, HTTPFound
from pyramid.response import FileResponse, Response
from pyramid.view import view_config


@view_config(route_name='home')
def my_view(request):
    package, resource = 'picard:static/base.html'.split(':', 1)
    icon = pkg_resources.resource_filename(package, resource)
    return FileResponse(icon, request=request)


@view_config(context=HTTPForbidden)
def forbidden(request):
    if request.path.startswith('/api/') \
        or 'X-Requested-With' in request.headers and request.headers['X-Requested-With'] == 'XMLHttpRequest':
        response = Response("Forbidden")
        response.status_int = 401
        return response
    else:
        return HTTPFound(location=request.route_url('login', _query={'came_from': request.path}))


