# 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
def post_suffix_add(user,
                    data,
                    db,
                    messages):
  ''' Add a new suffix. '''

  with db.cursor() as cur:
    cur.execute('''
      SELECT *
      FROM `suffixes`
      WHERE `name` = %(name)s
    ''', {'name': data.suffix_name})

    if cur.rowcount > 0:
      messages.error('Suffix with same name already exists')
      bottle.redirect('/admin/suffixes/add')

    cur.execute('''
      INSERT
      INTO `suffixes`
      SET `name` = %(name)s
    ''', {'name': data.suffix_name})

  messages.success('Ok, done.')

  bottle.redirect('/admin/suffixes/list')



@route('/admin/suffixes/delete', method = 'POST')

# 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/suffixes.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

def post_suffix_add(user,
                    data,
                    db,
                    messages):
  ''' Add a new suffix. '''

  with db.cursor() as cur:
    cur.execute('''
      SELECT *
      FROM `suffixes`
      WHERE `name` = %(name)s
    ''', {'name': data.suffix_name})

    if cur.rowcount > 0:
      messages.error('Suffix with same name already exists')
      bottle.redirect('/admin/suffixes/add')

    cur.execute('''
      INSERT
      INTO `suffixes`
      SET `name` = %(name)s
    ''', {'name': data.suffix_name})

  messages.success('Ok, done.')

  bottle.redirect('/admin/suffixes/list')



@route('/admin/suffixes/delete', method = 'POST')

@authorized_admin()