from pyramid.security import Everyone
from pyramid.security import Authenticated
from pyramid.security import Allow
from brewman.models.auth import Role


class RootFactory(object):
    @property
    def __acl__(self):
        acl = [
            (Allow, Everyone, 'view'),
            (Allow, Authenticated, 'Authenticated')]
        for permission in Role.list():
            acl.append((Allow, permission.name, permission.name))
        return acl

    def __init__(self, request):
        pass


def pluralize(word, num=None):
    if num is None or num != 1:
        if word.endswith('y'):
            return word[:-1] + 'ies'
        elif word[-1] in 'sx' or word[-2:] in ['sh', 'ch']:
            return word + 'es'
        elif word.endswith('an'):
            return word[:-2] + 'en'
        else:
            return word + 's'
    return word


def add_route(config, name, url, has_list=True, variable='id'):
    config.add_route(name + '_' + variable, url + '/{' + variable + '}')
    config.add_route(name, url)
    if has_list:
        config.add_route(name + '_list', pluralize(url))

    config.add_route('api_' + name + '_' + variable, '/api' + url + '/{' + variable + '}')
    config.add_route('api_' + name, '/api' + url)
