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', renderer='brewman:templates/angular_base.mako')
@view_config(request_method='GET', route_name='login', renderer='brewman:templates/angular_base.mako')
def home(request):
    return {}

@view_config(context=HTTPForbidden, renderer='brewman:templates/angular_base.mako')
def forbidden(request):
    if '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'))

@view_config(route_name='favicon')
def favicon(request):
    package, resource = 'brewman:static/favicon.ico'.split(':',1)
    icon = pkg_resources.resource_filename(package, resource)
    return FileResponse(icon, request=request)