up step
Some checks failed
K8S Fission Deployment / Deployment fission functions (push) Failing after 12s
Some checks failed
K8S Fission Deployment / Deployment fission functions (push) Failing after 12s
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
from flask import current_app, jsonify, request
|
||||
from helpers import CORS_HEADERS, db_row_to_dict, init_db_connection
|
||||
from psycopg2 import IntegrityError
|
||||
@@ -15,7 +13,7 @@ def main():
|
||||
"fntimeout": 300,
|
||||
"http_triggers": {
|
||||
"ai-admin-update-delete-user-http": {
|
||||
"url": "/ai/admin/users/{UserID}",
|
||||
"url": "/ailbl/ai/admin/users/{UserID}",
|
||||
"methods": ["DELETE", "PUT"]
|
||||
}
|
||||
}
|
||||
@@ -35,6 +33,40 @@ def main():
|
||||
|
||||
|
||||
def make_update_request():
|
||||
r"""make_update_request() -> tuple[Response, int, dict]
|
||||
|
||||
Update an existing user by ID.
|
||||
|
||||
Retrieves the user ID from ``X-Fission-Params-UserID`` header, validates
|
||||
the request body using :class:`AiUserUpdate` schema, and performs a
|
||||
partial update on the user record.
|
||||
|
||||
Uses row-level locking (``SELECT ... FOR UPDATE``) to prevent concurrent
|
||||
modification conflicts.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing:
|
||||
- JSON response with updated user data or error details
|
||||
- HTTP status code (200 on success, 400/404/409 on error)
|
||||
- CORS headers dict
|
||||
|
||||
Raises:
|
||||
ValidationError: If request body fails Pydantic validation (returns 400).
|
||||
IntegrityError: If email conflicts with another user (returns 409).
|
||||
|
||||
Example::
|
||||
|
||||
>>> # PUT /ai/admin/users/550e8400-e29b-41d4-a716-446655440000
|
||||
>>> # Header: X-Fission-Params-UserID: 550e8400-e29b-41d4-a716-446655440000
|
||||
>>> # Body: {"name": "Jane Doe"}
|
||||
>>> # Response: 200 OK
|
||||
>>> {
|
||||
... "id": "550e8400-e29b-41d4-a716-446655440000",
|
||||
... "name": "Jane Doe",
|
||||
... "email": "john@example.com",
|
||||
... "modified": "2024-01-02T10:00:00"
|
||||
... }
|
||||
"""
|
||||
user_id = request.headers.get("X-Fission-Params-UserID")
|
||||
if not user_id:
|
||||
return jsonify({"errorCode": "MISSING_USER_ID"}), 400, CORS_HEADERS
|
||||
@@ -97,6 +129,19 @@ def make_update_request():
|
||||
|
||||
|
||||
def __delete_user(cursor, id: str):
|
||||
r"""Delete a user from the database by ID.
|
||||
|
||||
Args:
|
||||
cursor: Database cursor object for executing queries.
|
||||
id (str): UUID of the user to delete.
|
||||
|
||||
Returns:
|
||||
dict | str: User data dict if deleted successfully,
|
||||
or ``"USER_NOT_FOUND"`` string if user doesn't exist.
|
||||
|
||||
Note:
|
||||
This is a private function. Use :func:`make_delete_request` instead.
|
||||
"""
|
||||
cursor.execute("SELECT 1 FROM ai_user WHERE id = %(id)s", {"id": id})
|
||||
if not cursor.fetchone():
|
||||
return "USER_NOT_FOUND"
|
||||
@@ -106,7 +151,30 @@ def __delete_user(cursor, id: str):
|
||||
return db_row_to_dict(cursor, row)
|
||||
|
||||
def make_delete_request():
|
||||
r"""make_delete_request() -> tuple[Response, int, dict]
|
||||
|
||||
Delete a user by ID.
|
||||
|
||||
Retrieves the user ID from ``X-Fission-Params-UserID`` header and
|
||||
deletes the user from the database if found.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing:
|
||||
- JSON response with deleted user data or error details
|
||||
- HTTP status code (200 on success, 400/404/500 on error)
|
||||
- CORS headers dict (may be omitted on some error responses)
|
||||
|
||||
Example::
|
||||
|
||||
>>> # DELETE /ai/admin/users/550e8400-e29b-41d4-a716-446655440000
|
||||
>>> # Header: X-Fission-Params-UserID: 550e8400-e29b-41d4-a716-446655440000
|
||||
>>> # Response: 200 OK
|
||||
>>> {
|
||||
... "id": "550e8400-e29b-41d4-a716-446655440000",
|
||||
... "name": "John Doe",
|
||||
... "email": "john@example.com"
|
||||
... }
|
||||
"""
|
||||
user_id = request.headers.get("X-Fission-Params-UserID")
|
||||
if not user_id:
|
||||
return jsonify({"errorCode": "MISSING_USER_ID"}), 400, CORS_HEADERS
|
||||
|
||||
Reference in New Issue
Block a user