Example #1
0
def init():
    """ Re-initialize drink's database """
    from drink.objects.finder import reset
    reset()
    with db as root:
        root.clear()
        transaction.commit()

        from .objects import users as obj


        request.identity = FakeId()
        groups = root['groups'] = obj.GroupList('groups', '/')
        users = root['users'] = obj.UserList('users', '/')
        root['groups']['users'] = obj.Group('users', '/groups/')

        admin = obj.User('admin', '/users/')
        request.identity.user = admin

        anon = obj.User('anonymous', '/users/')

        groups.owner = admin
        groups.read_groups = set()
        groups.write_groups = set()

        users.owner = admin
        users.read_groups = set()
        users.write_groups = set()
        users.min_rights = 't'

        root['users']['anonymous'] = anon
        root['users']['admin'] = admin

        admin.set_field('password', 'admin')
        admin.surname = "BOFH"
        admin.name = "Mr Admin"
        admin.owner = admin

        anon.groups = set()
        anon.owner = admin

        users = root['groups']['users']
        users.owner = admin
        users.read_groups = set()
        users.write_groups = set()
        users.min_rights = 't'

        settings = Settings('settings', '/')
        settings.server_backend = config.get('server', 'backend')
        settings.server_port = int(config.get('server', 'port'))
        settings.server_address = config.get('server', 'host')
        settings.debug_framework = config.get('server', 'debug')
        settings.active_objects = set(config.items('objects'))

        root['settings'] = settings

        root['pages'] = Page('pages', '/')

        # deploy layout
        for pagename, name in config.items('layout'):
            try:
                root[pagename] = classes[ name ](pagename, '/')
            except Exception, e:
                log.error('Unable to create %r element with type %r (%r)\nexpect problems, try to enable this object type next time.'%(pagename, name, e))

        mdown = classes['Web page (markdown)']('help', '/pages/')
        help = os.path.abspath(os.path.join(DEFAULTS_DIR, "HELP.md"))
        mdown.content = open(help).read()
        mdown.owner = admin
        mdown.min_rights = 'r'
        root['pages']['help'] = mdown
Example #2
0
__all__ = ["classes", "get_object", "init"]


class _AuthDict(dict):
    def iterkeys(self):
        adm = request.identity.admin
        return (k for k in dict.iterkeys(self) if not self[k].hidden_class or adm)

    def keys(self):
        return list(self.iterkeys())


classes = _AuthDict()

objects_to_load = [k for k, v in config.items("objects")]


def get_object(current, objpath, no_raise=False):
    """ Fetch an object from database, looking at permissions to approve

    :arg current: root object to browse for childrens
    :type current: :class:`drink.Page`
    :arg objpath: path to the children
    :type objpath: str
    :arg no_raise: (optional) don't raise exceptions
    :type no_raise: `bool`
    """
    path_list = [drink.omni(p) for p in objpath.split("/") if p]
    last_idx = len(path_list) - 1
    pending_path = False