class AdminUserViews(BaseView): def __init__(self, request): super(AdminUserViews, self).__init__(request) self.shared = UsersShared(request) @view_config(renderer='testscaffold:templates/admin/users/edit.jinja2', match_param=('object=users', 'verb=GET')) @view_config(renderer='testscaffold:templates/admin/users/edit.jinja2', match_param=('object=users', 'verb=PATCH')) def get_patch(self): request = self.request user = self.shared.user_get(self.request.matchdict['object_id']) permission_form = DirectPermissionForm(request.POST, context={'request': request}) permissions_grid = UserPermissionsGrid(user.user_permissions, request=request, user=user) user_form = UserAdminUpdateForm(request.POST, obj=user, context={ 'request': request, 'modified_obj': user }) if request.method == "POST" and user_form.validate(): self.shared.populate_instance(user, user_form.data) return { "user": user, "user_form": user_form, "permission_form": permission_form, "permissions_grid": permissions_grid } @view_config( renderer='testscaffold:templates/admin/relation_remove.jinja2', match_param=('object=users', 'verb=DELETE'), request_method='GET') @view_config( renderer='testscaffold:templates/admin/relation_remove.jinja2', match_param=('object=users', 'verb=DELETE'), request_method='POST') def delete(self): request = self.request user = self.shared.user_get(self.request.matchdict['object_id']) back_url = request.route_url('admin_objects', object='users', verb='GET') if request.method == "POST": self.shared.delete(user) return pyramid.httpexceptions.HTTPFound(location=back_url) return { "parent_obj": user, "member_obj": None, "confirm_url": request.current_route_url(), "back_url": back_url }
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
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
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}
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}
class AdminUserViews(BaseView): def __init__(self, request): super(AdminUserViews, self).__init__(request) self.shared = UsersShared(request) @view_config( renderer="testscaffold:templates/admin/users/edit.jinja2", match_param=("object=users", "verb=GET"), ) @view_config( renderer="testscaffold:templates/admin/users/edit.jinja2", match_param=("object=users", "verb=PATCH"), ) def get_patch(self): request = self.request user = self.shared.user_get(self.request.matchdict["object_id"]) permission_form = DirectPermissionForm( request.POST, context={"request": request} ) permissions_grid = UserPermissionsGrid( user.user_permissions, request=request, user=user ) user_form = UserAdminUpdateForm( request.POST, obj=user, context={"request": request, "modified_obj": user} ) if request.method == "POST" and user_form.validate(): self.shared.populate_instance(user, user_form.data) return { "user": user, "user_form": user_form, "permission_form": permission_form, "permissions_grid": permissions_grid, } @view_config( renderer="testscaffold:templates/admin/relation_remove.jinja2", match_param=("object=users", "verb=DELETE"), request_method="GET", ) @view_config( renderer="testscaffold:templates/admin/relation_remove.jinja2", match_param=("object=users", "verb=DELETE"), request_method="POST", ) def delete(self): request = self.request user = self.shared.user_get(self.request.matchdict["object_id"]) back_url = request.route_url("admin_objects", object="users", verb="GET") if request.method == "POST": self.shared.delete(user) return pyramid.httpexceptions.HTTPFound(location=back_url) return { "parent_obj": user, "member_obj": None, "confirm_url": request.current_route_url(), "back_url": back_url, }