from pyramid.response import Response
from pi_director.models.models import (
    DBSession,
    MyModel,
    )
from pi_director.models.UserModel import UserModel


def authorize_user(email):
    user=DBSession.query(UserModel).filter(UserModel.email==email).one()
    user.AccessLevel=2
    DBSession.flush()

def delete_user(email):
    DBSession.query(UserModel).filter(UserModel.email==email).delete()

def get_users():
    UserList=DBSession.query(UserModel).all()
    return UserList


def make_an_admin(request):
    email=request.matchdict['email']

    '''First, make sure there aren't already admins in the system'''
    res=DBSession.query(UserModel).filter(UserModel.AccessLevel==2).first()
    if res != None:
        msg="User already an admin: {user}".format(user=res.email)
        return False
    user=DBSession.query(UserModel).filter(UserModel.email==email).first()
    user.AccessLevel=2
    DBSession.flush()    
    return True



from pyramid.response import Response
from pi_director.models.models import (
    DBSession,
    MyModel,
    )
from pi_director.models.UserModel import UserModel


def authorize_user(email):
    user=DBSession.query(UserModel).filter(UserModel.email==email).one()
    user.AccessLevel=2
    DBSession.flush()

def delete_user(email):
    DBSession.query(UserModel).filter(UserModel.email==email).delete()

def get_users():
    UserList=DBSession.query(UserModel).all()
    return UserList


def make_an_admin(request):
    email=request.matchdict['email']

    '''First, make sure there aren't already admins in the system'''
    res=DBSession.query(UserModel).filter(UserModel.AccessLevel==2).first()
    if res != None:
        msg="User already an admin: {user}".format(user=res.email)
        return False
    user=DBSession.query(UserModel).filter(UserModel.email==email).first()
    if user == None:
        user=UserModel()
        user.email=email
        DBSession.add(user)
    user.AccessLevel=2
    DBSession.flush()    
    return True



Create the user if it isn't already in the database first, then make it an admin.
Create the user if it isn't already in the database first, then make it an admin.

  from pyramid.response import Response
  from pi_director.models.models import (
      DBSession,
      MyModel,
      )
  from pi_director.models.UserModel import UserModel
  
  
  def authorize_user(email):
      user=DBSession.query(UserModel).filter(UserModel.email==email).one()
      user.AccessLevel=2
      DBSession.flush()
  
  def delete_user(email):
      DBSession.query(UserModel).filter(UserModel.email==email).delete()
  
  def get_users():
      UserList=DBSession.query(UserModel).all()
      return UserList
  
  
  def make_an_admin(request):
      email=request.matchdict['email']
  
      '''First, make sure there aren't already admins in the system'''
      res=DBSession.query(UserModel).filter(UserModel.AccessLevel==2).first()
      if res != None:
          msg="User already an admin: {user}".format(user=res.email)
          return False
      user=DBSession.query(UserModel).filter(UserModel.email==email).first()
+     if user == None:
+         user=UserModel()
+         user.email=email
+         DBSession.add(user)
      user.AccessLevel=2
      DBSession.flush()    
      return True
  
  
  
Create the user if it isn't already in the database first, then make it an admin.
## Code Before:
from pyramid.response import Response
from pi_director.models.models import (
    DBSession,
    MyModel,
    )
from pi_director.models.UserModel import UserModel


def authorize_user(email):
    user=DBSession.query(UserModel).filter(UserModel.email==email).one()
    user.AccessLevel=2
    DBSession.flush()

def delete_user(email):
    DBSession.query(UserModel).filter(UserModel.email==email).delete()

def get_users():
    UserList=DBSession.query(UserModel).all()
    return UserList


def make_an_admin(request):
    email=request.matchdict['email']

    '''First, make sure there aren't already admins in the system'''
    res=DBSession.query(UserModel).filter(UserModel.AccessLevel==2).first()
    if res != None:
        msg="User already an admin: {user}".format(user=res.email)
        return False
    user=DBSession.query(UserModel).filter(UserModel.email==email).first()
    user.AccessLevel=2
    DBSession.flush()    
    return True



## Instruction:
Create the user if it isn't already in the database first, then make it an admin.
## Code After:
from pyramid.response import Response
from pi_director.models.models import (
    DBSession,
    MyModel,
    )
from pi_director.models.UserModel import UserModel


def authorize_user(email):
    user=DBSession.query(UserModel).filter(UserModel.email==email).one()
    user.AccessLevel=2
    DBSession.flush()

def delete_user(email):
    DBSession.query(UserModel).filter(UserModel.email==email).delete()

def get_users():
    UserList=DBSession.query(UserModel).all()
    return UserList


def make_an_admin(request):
    email=request.matchdict['email']

    '''First, make sure there aren't already admins in the system'''
    res=DBSession.query(UserModel).filter(UserModel.AccessLevel==2).first()
    if res != None:
        msg="User already an admin: {user}".format(user=res.email)
        return False
    user=DBSession.query(UserModel).filter(UserModel.email==email).first()
    if user == None:
        user=UserModel()
        user.email=email
        DBSession.add(user)
    user.AccessLevel=2
    DBSession.flush()    
    return True


