def set_user_role(pk, db_session: Session, data): validate_data(set_role_serializer, data) # validate_permission("edit_roles", db_session, User) user = get_object_or_404(User, db_session, User.id == pk) user.set_roles(data['roles']) db_session.commit() result = user_serializer.dump(db_session.query(User).get(user.id)) return HTTPResponse(status=status.HTTP_201_CREATED, body=result)
def delete_user(pk, db_session): identity = get_user_identity(db_session) if identity.check_permission("delete_user", User): user = get_object_or_404(User, db_session, User.id == pk) db_session.delete(user) db_session.commit() raise HTTPResponse(status=204, body="Successfully deleted !") raise HTTPError(status=403, body="Access denied")
def activate_user(pk, db_session: Session, data): # validate_permission("activate_user", db_session, User) activation = data["activation"] user = get_object_or_404(User, db_session, User.id == pk) user.is_active = str_to_bool(activation) db_session.commit() if user.is_active: return HTTPResponse(*ACTIVATED_MSG) return HTTPResponse(*DEACTIVATED_MSG)
def edit_user(pk, db_session: Session, data): validate_data(user_edit_serializer, data) user = get_object_or_404(User, db_session, User.id == pk) person = data['person'] user.person.name = person['name'] user.person.last_name = person['last_name'] user.person.email = person['email'] user.phone = data['phone'] user.username = data['phone'] db_session.commit() result = user_serializer.dump(db_session.query(User).get(user.id)) return HTTPResponse(status=status.HTTP_201_CREATED, body=result)
def add_person_image(db_session: Session, data): validate_data(person_image_serializer, data) person_image = PersonImage() person = get_object_or_404(Person, db_session, Person.id == data['person_id']) set_objects_limit(person.person_images, limit=1, session=db_session) person_image.name = upload(data=data) person_image.person_id = data['person_id'] db_session.add(person_image) db_session.commit() result = db_session.query(PersonImage).get(person_image.id) result = person_image_serializer.dump(result) return HTTPResponse(status=200, body=result)
def get_user(pk, db_session): user = get_object_or_404(User, db_session, User.id == pk) result = user_serializer.dump(user) raise HTTPResponse(status=200, body=result)
def get_room(pk, db_session: Session): room = get_object_or_404(Room, db_session, Room.id == pk) result = room_serializer.dump(room) return HTTPResponse(status=status.HTTP_200_OK, body=result)
def get_user(pk, db_session): user = get_object_or_404(User, db_session, User.id == pk) result = user_serializer.dump(user) return HTTPResponse(status=status.HTTP_200_OK, body=result)
def remove_person_image(pk, db_session: Session): person_image = get_object_or_404(Person, db_session, Person.id == pk) db_session.delete(person_image) db_session.commit() return HTTPResponse(*DELETE_IMG)