# Path: ddserver/utils/deps.py
# def require(**requirements):
#   ''' Decorator to inject requirements into a function.
# 
#       The decorated function is called with a named argument for each
#       specified requirement containing the exported instances.
#   '''
# 
#   exports = {name : Export.load(requirement)
#              for name, requirement
#              in requirements.iteritems()}
# 
#   def wrapper(func):
#     def wrapped(*args, **kwargs):
#       # Update the keyword arguments with the required instances
#       kwargs.update({name : export.instance
#                      for name, export
#                      in exports.iteritems()
#                      if name not in kwargs})
# 
#       return func(*args,
#                   **kwargs)
# 
#     wrapped.__name__ = func.__name__
# 
#     return wrapped
#   return wrapper
# 
# Path: ddserver/interface/user.py
# class UserManager(object):
#   def __getitem__(self,
#                   key,
#                   db):
#   def login(self,
#             session,
#             messages,
#             username,
#             password):
#   def logout(self,
#              session,
#              messages):
#   def generate_authcode(self, username, db):
#   def authorized(self, session):
# def authorized(admin = False):
#   def wrapper(func):
#     def wrapped(users,
#                 messages,
#                 *args,
#                 **kwargs):
# def authorized_by_code():
#   def wrapper(func):
#     def wrapped(users,
#                 messages,
#                 *args,
#                 **kwargs):
# def template_auth(templates):
#   def __(users):


import bottle
from ddserver.web import route
from ddserver.utils.deps import require
from ddserver.interface.user import authorized_admin
from ddserver.interface import validation
from ddserver.interface.validation import validate
  ''' Adds a new user. '''
  return templates['adduser.html']()



@route('/admin/users/add', method = 'POST')
@authorized_admin()
@validate('/admin/users/add',
          username = validation.UniqueUsername(max = 255),
          email = validation.Email())
@require(db = 'ddserver.db:Database',
         users = 'ddserver.interface.user:UserManager',
         emails = 'ddserver.mail:EmailManager',
         messages = 'ddserver.interface.message:MessageManager')
def post_user_add(user,
                  data,
                  db,
                  users,
                  emails,
                  messages):
  ''' Manually add a new user. '''

  with db.cursor() as cur:
    cur.execute('''
      INSERT
      INTO users
      SET `username` = %(username)s,
          `email` = %(email)s,
          `admin` = 0,
          `active` = 0,

# Path: ddserver/utils/deps.py
# def require(**requirements):
#   ''' Decorator to inject requirements into a function.
# 
#       The decorated function is called with a named argument for each
#       specified requirement containing the exported instances.
#   '''
# 
#   exports = {name : Export.load(requirement)
#              for name, requirement
#              in requirements.iteritems()}
# 
#   def wrapper(func):
#     def wrapped(*args, **kwargs):
#       # Update the keyword arguments with the required instances
#       kwargs.update({name : export.instance
#                      for name, export
#                      in exports.iteritems()
#                      if name not in kwargs})
# 
#       return func(*args,
#                   **kwargs)
# 
#     wrapped.__name__ = func.__name__
# 
#     return wrapped
#   return wrapper
# 
# Path: ddserver/interface/user.py
# class UserManager(object):
#   def __getitem__(self,
#                   key,
#                   db):
#   def login(self,
#             session,
#             messages,
#             username,
#             password):
#   def logout(self,
#              session,
#              messages):
#   def generate_authcode(self, username, db):
#   def authorized(self, session):
# def authorized(admin = False):
#   def wrapper(func):
#     def wrapped(users,
#                 messages,
#                 *args,
#                 **kwargs):
# def authorized_by_code():
#   def wrapper(func):
#     def wrapped(users,
#                 messages,
#                 *args,
#                 **kwargs):
# def template_auth(templates):
#   def __(users):


# Path: ddserver/interface/pages/admin/users.py
import bottle
from ddserver.web import route
from ddserver.utils.deps import require
from ddserver.interface.user import authorized_admin
from ddserver.interface import validation
from ddserver.interface.validation import validate

  ''' Adds a new user. '''
  return templates['adduser.html']()



@route('/admin/users/add', method = 'POST')
@authorized_admin()
@validate('/admin/users/add',
          username = validation.UniqueUsername(max = 255),
          email = validation.Email())
@require(db = 'ddserver.db:Database',
         users = 'ddserver.interface.user:UserManager',
         emails = 'ddserver.mail:EmailManager',
         messages = 'ddserver.interface.message:MessageManager')
def post_user_add(user,
                  data,
                  db,
                  users,
                  emails,
                  messages):
  ''' Manually add a new user. '''

  with db.cursor() as cur:
    cur.execute('''
      INSERT
      INTO users
      SET `username` = %(username)s,
          `email` = %(email)s,
          `admin` = 0,
          `active` = 0,

          `created` = CURRENT_TIMESTAMP