Example #1
0
class UserAPIView(BaseView):
    def __init__(self, request):
        super(UserAPIView, self).__init__(request)
        self.shared = UsersShared(request)

    @view_config(route_name='api_objects', request_method='GET')
    def collection_list(self):
        schema = UserCreateSchema(context={'request': self.request})
        page = safe_integer(self.request.GET.get('page', 1))
        filter_params = UserSearchSchema().load(self.request.GET.mixed()).data
        user_paginator = self.shared.collection_list(
            page=page, filter_params=filter_params)
        headers = gen_pagination_headers(request=self.request,
                                         paginator=user_paginator)
        self.request.response.headers.update(headers)
        return schema.dump(user_paginator.items, many=True).data

    @view_config(route_name='api_objects', request_method='POST')
    def post(self):
        schema = UserCreateSchema(context={'request': self.request})
        data = schema.load(self.request.unsafe_json_body).data
        user = User()
        self.shared.populate_instance(user, data)
        user.persist(flush=True, db_session=self.request.dbsession)
        return schema.dump(user).data

    @view_config(request_method='GET')
    def get(self):
        schema = UserCreateSchema(context={'request': self.request})
        user = self.shared.user_get(self.request.matchdict['object_id'])
        return schema.dump(user).data

    @view_config(request_method="PATCH")
    def patch(self):
        user = self.shared.user_get(self.request.matchdict['object_id'])
        schema = UserEditSchema(context={
            'request': self.request,
            'modified_obj': user
        })
        data = schema.load(self.request.unsafe_json_body, partial=True).data
        self.shared.populate_instance(user, data)
        return schema.dump(user).data

    @view_config(request_method="DELETE")
    def delete(self):
        user = self.shared.user_get(self.request.matchdict['object_id'])
        self.shared.delete(user)
        return True
Example #2
0
class UserAPIView(BaseView):
    def __init__(self, request):
        super(UserAPIView, self).__init__(request)
        self.shared = UsersShared(request)

    @view_config(route_name="api_objects", request_method="GET")
    def collection_list(self):
        schema = UserCreateSchema(context={"request": self.request})
        page = safe_integer(self.request.GET.get("page", 1))
        filter_params = UserSearchSchema().load(self.request.GET.mixed()).data
        user_paginator = self.shared.collection_list(
            page=page, filter_params=filter_params
        )
        headers = gen_pagination_headers(request=self.request, paginator=user_paginator)
        self.request.response.headers.update(headers)
        return schema.dump(user_paginator.items, many=True).data

    @view_config(route_name="api_objects", request_method="POST")
    def post(self):
        schema = UserCreateSchema(context={"request": self.request})
        data = schema.load(self.request.unsafe_json_body).data
        user = User()
        self.shared.populate_instance(user, data)
        user.persist(flush=True, db_session=self.request.dbsession)
        return schema.dump(user).data

    @view_config(request_method="GET")
    def get(self):
        schema = UserCreateSchema(context={"request": self.request})
        user = self.shared.user_get(self.request.matchdict["object_id"])
        return schema.dump(user).data

    @view_config(request_method="PATCH")
    def patch(self):
        user = self.shared.user_get(self.request.matchdict["object_id"])
        schema = UserEditSchema(context={"request": self.request, "modified_obj": user})
        data = schema.load(self.request.unsafe_json_body, partial=True).data
        self.shared.populate_instance(user, data)
        return schema.dump(user).data

    @view_config(request_method="DELETE")
    def delete(self):
        user = self.shared.user_get(self.request.matchdict["object_id"])
        self.shared.delete(user)
        return True
Example #3
0
class AdminUsersViews(BaseView):
    def __init__(self, request):
        super(AdminUsersViews, self).__init__(request)
        self.shared = UsersShared(request)

    @view_config(renderer='testscaffold:templates/admin/users/index.jinja2',
                 match_param=('object=users', 'verb=GET'))
    def collection_list(self):
        page = safe_integer(self.request.GET.get('page', 1))
        user_paginator = self.shared.collection_list(page=page)
        start_number = (USERS_PER_PAGE * (self.shared.page - 1) + 1) or 1
        user_grid = UsersGrid(user_paginator,
                              start_number=start_number,
                              request=self.request)

        return {'user_paginator': user_paginator, 'user_grid': user_grid}

    @view_config(renderer='testscaffold:templates/admin/users/edit.jinja2',
                 match_param=('object=users', 'verb=POST'))
    def post(self):
        request = self.request
        user_form = UserAdminCreateForm(request.POST,
                                        context={'request': request})
        if request.method == "POST" and user_form.validate():
            user = User()
            self.shared.populate_instance(user, user_form.data)
            user.persist(flush=True, db_session=request.dbsession)
            log.info('users_post',
                     extra={
                         'user_id': user.id,
                         'user_name': user.user_name
                     })
            request.session.flash({
                'msg': self.translate(_('User created.')),
                'level': 'success'
            })
            location = request.route_url('admin_objects',
                                         object='users',
                                         verb='GET')
            return pyramid.httpexceptions.HTTPFound(location=location)

        return {"user_form": user_form}
Example #4
0
class AdminUsersViews(BaseView):
    def __init__(self, request):
        super(AdminUsersViews, self).__init__(request)
        self.shared = UsersShared(request)

    @view_config(
        renderer="testscaffold:templates/admin/users/index.jinja2",
        match_param=("object=users", "verb=GET"),
    )
    def collection_list(self):
        page = safe_integer(self.request.GET.get("page", 1))
        user_paginator = self.shared.collection_list(page=page)
        start_number = (USERS_PER_PAGE * (self.shared.page - 1) + 1) or 1
        user_grid = UsersGrid(
            user_paginator, start_number=start_number, request=self.request
        )

        return {"user_paginator": user_paginator, "user_grid": user_grid}

    @view_config(
        renderer="testscaffold:templates/admin/users/edit.jinja2",
        match_param=("object=users", "verb=POST"),
    )
    def post(self):
        request = self.request
        user_form = UserAdminCreateForm(request.POST, context={"request": request})
        if request.method == "POST" and user_form.validate():
            user = User()
            self.shared.populate_instance(user, user_form.data)
            user.persist(flush=True, db_session=request.dbsession)
            log.info(
                "users_post", extra={"user_id": user.id, "user_name": user.user_name}
            )
            request.session.flash(
                {"msg": self.translate(_("User created.")), "level": "success"}
            )
            location = request.route_url("admin_objects", object="users", verb="GET")
            return pyramid.httpexceptions.HTTPFound(location=location)

        return {"user_form": user_form}