from pyramid.response import Response
from pyramid.view import view_config, forbidden_view_config
from pyramid.security import authenticated_userid
from velruse import login_url
import re

import pyramid.httpexceptions as exc
import logging
import sqlalchemy.exc

from pi_director.models.models import (
    DBSession,
    MyModel,
    )

from pi_director.models.UserModel import UserModel

from pi_director.security import (
    groupfinder,
    )

from pi_director.controllers.controllers import (
    get_pis,
    )
from pi_director.controllers.user_controls import get_users

@forbidden_view_config(renderer="pi_director:templates/forbidden.mak")
def forbidden(request):
    logging.info("Going into forbidden")
    m=re.match("^/?(ajax|api).*$",request.path)
    if m != None:
        '''we're trying to hit an api or ajax query without authentication'''
        return Response("{'status':'Forbidden'}")

    userid=authenticated_userid(request)
    if userid:
       logging.info("User exists but some other problem occured, FORBIDDEN.")
       group=groupfinder(userid,request)
       logging.info("User {user} access level {access}".format(user=userid,access=group))
       return ("")

    if groupfinder(None,request) is None:
        request.session['goingto']=request.path
        logging.info("Should be shunting to login page")
        loc = request.route_url('velruse.google-login', _query=(('next', request.path),))
        return exc.HTTPFound(location=loc)



@view_config(route_name='home', renderer="pi_director:templates/home.mak",permission="admin")
def view_home(request):
    logged_in=authenticated_userid(request)
    loginurl = login_url(request, 'google')
    PiList=get_pis()
    return {"loginurl": loginurl,"logged_in":logged_in,"logouturl": request.route_url('logout'),'pis':PiList}

@view_config(route_name='users', renderer="pi_director:templates/user.mak",permission="admin")
def view_users(request):
    logged_in=authenticated_userid(request)
    loginurl = login_url(request, 'google')
    UserList = get_users()
    return {"loginurl": loginurl,"logged_in":logged_in,"logouturl": request.route_url('logout'),'users':UserList}

@view_config(route_name='redirectme')
def redirect_me(request):
    uid=request.matchdict['uid']
    url="http://www.stackexchange.com"
    try:
        row=DBSession.query(MyModel).filter(MyModel.uuid==uid).first()
        if row:
            url=row.url
            logging.info("UID {uid}: {page}".format(uid=row.uuid,page=url))
        else:
            row=MyModel()
            row.uuid=uid
            row.url="http://www.stackexchange.com"
            row.landscape=True
            DBSession.add(row)
            DBSession.flush()
            logging.warn("UID {uid} NOT FOUND. ADDED TO TABLE WITH DEFAULT URL".format(uid=uid))
            url=row.url
    except Exception:
            logging.error("Something went south with DB when searching for {uid}".format(uid=uid))

    raise exc.HTTPFound(url)

@@ -853,16 +853,140 @@
 tion'''%0A
+        logging.warn(%22Someone tried to hit an ajax/api without authentication.  Route: %7Broute%7D%22.format(route=request.path))%0A
         

Add a logging warning to show someone tried to hit a protected route