Exemplo n.º 1
0
def password_edit_view(request):
    """ Render the change password form page.

    Seek the database for the user datas based on user_id used in the route. If
    the user did not exist then add an error flash message and redirect to the
    user list.
    If the user exist then render an empty password form. If the form is
    validated then change the user password in the database and add
    success flash message. If the form is not valid, then display again the
    form with validation errors.

    :param request: a ``pyramid.request`` object
    """
    _ = request.translate
    user_id = request.matchdict['user_id']
    user = AuthUser.get_by_id(user_id)
    if not user:
        request.session.flash(_(u"This user did not exist!"), 'error')
        return HTTPFound(location=request.route_path('tools.user_list'))
    form = Form(request, schema=UserPasswordForm, obj=user)
    if 'form_submitted' in request.params and form.validate():
        form.bind(user)
        DBSession.add(user)
        request.session.flash(_(u"Password updated."), 'success')
        return HTTPFound(location=request.route_path('tools.user_list'))
    return dict(renderer=FormRenderer(form))
Exemplo n.º 2
0
def password_edit_view(request):
    """ Render the change password form page.

    Seek the database for the user datas based on user_id used in the route. If
    the user did not exist then add an error flash message and redirect to the
    user list.
    If the user exist then render an empty password form. If the form is
    validated then change the user password in the database and add
    success flash message. If the form is not valid, then display again the
    form with validation errors.

    :param request: a ``pyramid.request`` object
    """
    _ = request.translate
    user_id = request.matchdict['user_id']
    user = AuthUser.get_by_id(user_id)
    if not user:
        request.session.flash(_(u"This user did not exist!"), 'error')
        return HTTPFound(location=request.route_path('tools.user_list'))
    form = Form(request, schema=UserPasswordForm, obj=user)
    if 'form_submitted' in request.params and form.validate():
        form.bind(user)
        DBSession.add(user)
        request.session.flash(_(u"Password updated."), 'success')
        return HTTPFound(location=request.route_path('tools.user_list'))
    return dict(renderer=FormRenderer(form))
Exemplo n.º 3
0
def zone_add_view(request):

    _ = request.translate
    form = ZoneForm(request.POST)
    if 'form_submitted' in request.params and form.validate():
        zone = Zone()
        form.populate_obj(zone)
        DBSession.add(zone)
        request.session.flash(_(u"Zone added."), 'success')
        return HTTPFound(location=request.route_path('geo.zone_list'))
    return dict(form=form)
Exemplo n.º 4
0
def rock_add_view(request):

    _ = request.translate
    form = RockForm(request.POST)
    if 'form_submitted' in request.params and form.validate():
        rock = Rock()
        form.populate_obj(rock)
        DBSession.add(rock)
        request.session.flash(_(u"Rock added."), 'success')
        return HTTPFound(location=request.route_path('geo.rock_list'))
    return dict(form=form)
Exemplo n.º 5
0
    def initialize_db(self):
        """ Initialize the database schema and insert default values.

        :return: 0 (OK) or 1 (abnormal termination error)
        """
        config_uri = self.args.config_uri
        settings = get_appsettings(config_uri)
        engine = engine_from_config(settings, 'sqlalchemy.')
        DBSession.configure(bind=engine)

        # check if there is already a versioned database
        revision = get_alembic_revision(config_uri)
        if revision:
            print("This database is versioned. "
                  "Use the upgrade script instead!")
            return 1

        # create the tables (except alembic_version)
        Base.metadata.create_all(engine)

        # add default user & group values
        with transaction.manager:
            admins_group = AuthGroup()
            admins_group.groupname = u'admins'
            admin_user = AuthUser()
            admin_user.username = u'admin'
            admin_user.password = u'admin'
            admin_user.group = admins_group
            try:
                DBSession.add(admins_group)
                DBSession.add(admin_user)
                DBSession.flush()
            except IntegrityError:
                DBSession.rollback()
                print("There is already a database. "
                      "Use the upgrade script instead!")
                return 1

        # stamp the database with the most recent revision
        # (and create alembic_version table)
        try:
            alembic_cfg = get_alembic_settings(config_uri)
            stamp(alembic_cfg, 'head')
        except (AttributeError, ImportError):  # pragma: no cover
            # alembic is missing or not configured
            pass

        print("Database initialization done.")
        return 0
Exemplo n.º 6
0
    def initialize_db(self):
        """ Initialize the database schema and insert default values.

        :return: 0 (OK) or 1 (abnormal termination error)
        """
        config_uri = self.args.config_uri
        settings = get_appsettings(config_uri)
        engine = engine_from_config(settings, 'sqlalchemy.')
        DBSession.configure(bind=engine)

        # check if there is already a versioned database
        revision = get_alembic_revision(config_uri)
        if revision:
            print("This database is versioned. "
                  "Use the upgrade script instead!")
            return 1

        # create the tables (except alembic_version)
        Base.metadata.create_all(engine)

        # add default user & group values
        with transaction.manager:
            admins_group = AuthGroup()
            admins_group.groupname = u'admins'
            admin_user = AuthUser()
            admin_user.username = u'admin'
            admin_user.password = u'admin'
            admin_user.group = admins_group
            try:
                DBSession.add(admins_group)
                DBSession.add(admin_user)
                DBSession.flush()
            except IntegrityError:
                DBSession.rollback()
                print("There is already a database. "
                      "Use the upgrade script instead!")
                return 1

        # stamp the database with the most recent revision
        # (and create alembic_version table)
        try:
            alembic_cfg = get_alembic_settings(config_uri)
            stamp(alembic_cfg, 'head')
        except (AttributeError, ImportError):  # pragma: no cover
            # alembic is missing or not configured
            pass

        print("Database initialization done.")
        return 0
Exemplo n.º 7
0
def user_add_view(request):
    """ Render the add user form page.

    Display an empty user form or validate the user submited form. If the form
    is validated then add the user datas to the database and a success flash
    message. If the form is not valid, then display again the form with
    validation errors. Return also a list of groups to use in the group select
    form.

    :param request: a ``pyramid.request`` object
    """
    _ = request.translate
    grouplist = get_grouplist()
    form = Form(request, schema=UserForm)
    if 'form_submitted' in request.params and form.validate():
        user = form.bind(AuthUser())
        DBSession.add(user)
        request.session.flash(_(u"User added."), 'success')
        return HTTPFound(location=request.route_path('tools.user_list'))
    return dict(renderer=FormRenderer(form), grouplist=grouplist)
Exemplo n.º 8
0
def user_add_view(request):
    """ Render the add user form page.

    Display an empty user form or validate the user submited form. If the form
    is validated then add the user datas to the database and a success flash
    message. If the form is not valid, then display again the form with
    validation errors. Return also a list of groups to use in the group select
    form.

    :param request: a ``pyramid.request`` object
    """
    _ = request.translate
    grouplist = get_grouplist()
    form = Form(request, schema=UserForm)
    if 'form_submitted' in request.params and form.validate():
        user = form.bind(AuthUser())
        DBSession.add(user)
        request.session.flash(_(u"User added."), 'success')
        return HTTPFound(location=request.route_path('tools.user_list'))
    return dict(renderer=FormRenderer(form),
                grouplist=grouplist)