Ejemplo n.º 1
0
def update(context, request):

    params = dict()
    theme = Theme.get(request.db_session, request.matchdict['name'])

    try:
        for attr in ('owner', 'author'):
            value = request.params.get(attr)
            if value:
                params[attr] = User.get(request.db_session, value)

    except NoResultFound:
        raise ParamsError('No user with email {}'.format(value))

    try:
        attr = 'parent'
        parent = request.params.get(attr)
        if parent:
            parent = Theme.get(request.db_session, parent)
            params[attr] = parent

    except NoResultFound:
        raise ParamsError('No theme named {}'.format(value))

    for attr in ('version', 'banner_height', 'banner_width', 'logo_height',
                 'logo_width', 'main_menu_levels', 'template_levels', 'name',
                 'image_full_size'):
        value = request.params.get(attr)
        if value:
            params[attr] = value

    if not params:
        raise ParamsError('Missing update fields')

    try:
        if 'name' in params and params['name'] != theme.name:
            raise NotImplementedError('Changing theme name is not supported')
            __import__('aybu.themes.{}'.format(params['name']))

        for param in params:
            setattr(theme, param, params[param])

        request.db_session.flush()

    except ImportError as e:
        error = 'Theme {} does not exists in aybu.themes package'\
                .format(params['name'])
        raise HTTPPreconditionFailed(headers={'X-Request-Error': error})

    except IntegrityError as e:
        request.db_session.rollback()
        raise HTTPConflict(headers={'X-Request-Error': str(e)})

    else:
        request.db_session.commit()

    return theme.to_dict()
Ejemplo n.º 2
0
def deploy(session, task, domain, owner_email, environment_name,
           technical_contact_email, theme_name=None, default_language=u'it',
           database_password=None, enabled=True):

    env = Environment.get(session, environment_name)
    theme = None if not theme_name else Theme.get(session, theme_name)
    owner = User.get(session, owner_email)
    technical_contact = User.get(session, technical_contact_email)

    instance = Instance.deploy(session, domain, owner, env, technical_contact,
                               theme, default_language, database_password,
                               enabled)

    return instance.id
Ejemplo n.º 3
0
def create_no_upload(context, request):
    try:
        name = request.params['name']
        parent_name = request.params.get('parent', None)
        version = request.params['version']
        author_email = request.params['author']
        owner_email = request.params['owner']
        banner_width = request.params['banner_width']
        banner_height = request.params['banner_height']
        logo_width = request.params['logo_width']
        logo_height = request.params['logo_height']
        main_menu_levels = request.params['main_menu_levels']
        template_levels = request.params['template_levels']
        image_full_size = request.params['image_full_size']

        owner = User.get(request.db_session, owner_email)
        author = User.get(request.db_session, author_email)
        if parent_name:
            parent = Theme.get(request.db_session, parent_name)
        else:
            parent = None

        # check if a theme exists in package aybu.themes
        __import__('aybu.themes.{}'.format(name))

        t = Theme(name=name, parent=parent, author=author,
                  version=version, owner=owner, banner_width=banner_width,
                  banner_height=banner_height, logo_width=logo_width,
                  logo_height=logo_height, main_menu_levels=main_menu_levels,
                  template_levels=template_levels,
                  image_full_size=image_full_size)
        request.db_session.add(t)
        request.db_session.flush()

    except (KeyError, NoResultFound) as e:
        raise ParamsError(e)

    except ImportError as e:
        error = 'Theme {} does not exists in aybu.themes package'.format(name)
        raise HTTPPreconditionFailed(headers={'X-Request-Error': error})

    except IntegrityError as e:
        error = 'Theme with name {} already exists'.format(name)
        request.db_session.rollback()
        raise HTTPConflict(headers={'X-Request-Error': error})

    else:
        request.db_session.commit()
        raise HTTPCreated()
Ejemplo n.º 4
0
def delete(context, request):

    try:
        name = request.matchdict['name']
        theme = Theme.get(request.db_session, name)
        theme.delete()
        request.db_session.flush()

    except IntegrityError:
        Theme.log.exception('Error deleting theme {}'.format(name))
        request.db_session.rollback()
        raise HTTPPreconditionFailed(
            headers={'X-Request-Error': 'Theme {} is in use'.format(name)})
    else:
        request.db_session.commit()
        raise HTTPNoContent()
Ejemplo n.º 5
0
def list(context, request):
    return {t.name: t.to_dict() for t in Theme.all(request.db_session)}
Ejemplo n.º 6
0
def info(context, request):
    return Theme.get(request.db_session,
                     request.matchdict['name']).to_dict()
Ejemplo n.º 7
0
def deploy(context, request):
    try:
        params = dict(
            domain=request.params['domain'],
            owner_email=request.params['owner_email'],
            environment_name=request.params['environment_name'],
            technical_contact_email=request.params.get(
                                   'technical_contact_email',
                                   request.params['owner_email']),
            theme_name=request.params.get('theme_name'),
            default_language=request.params.get('default_language', u'it'),
            database_password=request.params.get('database_password'),
            enabled=True if request.params.get('enabled') else False,
            verbose=True if request.params.get('verbose') else False,
        )
        check_domain_not_used(request, params['domain'])
        params['domain'] = validate_hostname(params['domain'])
        # try to get the instance, as it MUST not exists
        Instance.get_by_domain(request.db_session, params['domain'])

    except KeyError as e:
        log.exception("Error validating params")
        raise ParamsError(e)

    except NoResultFound:
        # no instance found, validate relations.
        try:
            field = 'owner_email'
            cls = 'user'
            User.log.debug("Validating owner %s", params[field])
            owner = User.get(request.db_session, params[field])
            if not owner:
                raise NoResultFound()
            if params['technical_contact_email'] != params['owner_email']:
                field = 'technical_contact_email'
                User.log.debug("Validating contact %s", params[field])
                ctc = User.get(request.db_session, params[field])
                if not ctc:
                    raise NoResultFound()

            field = 'environment_name'
            cls = 'environment'
            Environment.log.debug("validating environment %s", params[field])
            env = Environment.get(request.db_session, params[field])
            if not env:
                raise NoResultFound()

            field = 'theme_name'
            cls = 'theme'
            if params[field]:
                Theme.log.debug("Validating theme %s", params[field])
                theme = Theme.get(request.db_session, params[field])
                if not theme:
                    raise NoResultFound()

        except NoResultFound:
            raise ParamsError('{} "{}" for {} not found'\
                              .format(cls.title(), params[field], field))

        else:
            log.info("Submitting task")
            # relations exists, submit tasks
            return request.submit_task('instance.deploy', **params)

    else:
        # found instance, conflict
        error = 'instance for domain {} already exists'\
                .format(params['domain'])
        raise HTTPConflict(headers={'X-Request-Error': error})