ProfileDone
Some checks failed
K8S Fission Deployment / Deployment fission functions (push) Failing after 21s
Some checks failed
K8S Fission Deployment / Deployment fission functions (push) Failing after 21s
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import crud
|
import crud
|
||||||
from flask import jsonify, request
|
from flask import jsonify, request
|
||||||
|
from helpers import CORS_HEADERS
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -10,7 +10,7 @@ def main():
|
|||||||
"name": "profile-admin-get-insert-delete-put",
|
"name": "profile-admin-get-insert-delete-put",
|
||||||
"http_triggers": {
|
"http_triggers": {
|
||||||
"profile-admin-get-insert-delete-put-http": {
|
"profile-admin-get-insert-delete-put-http": {
|
||||||
"url": "/ailbl/admin/users/{user_id}/profiles",
|
"url": "/ailbl/admin/users/{User_Id}/profiles",
|
||||||
"methods": ["POST", "PUT", "DELETE", "GET"]
|
"methods": ["POST", "PUT", "DELETE", "GET"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,14 +34,15 @@ def main():
|
|||||||
|
|
||||||
def make_insert_request():
|
def make_insert_request():
|
||||||
try:
|
try:
|
||||||
user_id = request.headers.get("X-Fission-Params-UserId")
|
user_id = request.headers.get(
|
||||||
|
"X-Fission-Params-UserId") # X-Fission lay tren path
|
||||||
if not user_id:
|
if not user_id:
|
||||||
return jsonify({"error": "user_id is required"}), 400
|
return jsonify({"error": "user_id is required"}), 400
|
||||||
# Lấy dữ liệu từ body của request (thông tin hồ sơ người dùng)
|
# Lấy dữ liệu từ body của request (thông tin hồ sơ người dùng)
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
|
||||||
response, status = crud.insert_profile(user_id, data)
|
response, status, header = crud.insert_profile(user_id, data)
|
||||||
return jsonify(response), status
|
return jsonify(response), status, header
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
@@ -54,8 +55,8 @@ def make_update_request():
|
|||||||
# Lấy dữ liệu từ body của request (thông tin hồ sơ người dùng)
|
# Lấy dữ liệu từ body của request (thông tin hồ sơ người dùng)
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
|
||||||
response, status = crud.update_profile(user_id, data)
|
response, status, header = crud.update_profile(user_id, data)
|
||||||
return jsonify(response), status
|
return jsonify(response), status, header
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
@@ -66,8 +67,8 @@ def make_delete_request():
|
|||||||
if not user_id:
|
if not user_id:
|
||||||
return jsonify({"error": "user_id is required"}), 400
|
return jsonify({"error": "user_id is required"}), 400
|
||||||
# Call CRUD function to delete profile
|
# Call CRUD function to delete profile
|
||||||
response, status = crud.delete_profile(user_id)
|
response, status, header = crud.delete_profile(user_id)
|
||||||
return jsonify(response), status
|
return jsonify(response), status, header
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
@@ -75,9 +76,10 @@ def make_delete_request():
|
|||||||
def make_get_request():
|
def make_get_request():
|
||||||
try:
|
try:
|
||||||
user_id = request.headers.get("X-Fission-Params-UserId")
|
user_id = request.headers.get("X-Fission-Params-UserId")
|
||||||
|
print(f"Received user_id: {user_id}") # Log user_id để kiểm tra
|
||||||
if not user_id:
|
if not user_id:
|
||||||
return jsonify({"error": "user_id is required"}), 400
|
return jsonify({"error": "user_id is required"}), 400
|
||||||
response, status = crud.get_profile(user_id)
|
response, status, header = crud.get_profile(user_id)
|
||||||
return jsonify(response), status
|
return jsonify(response), status, header
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import crud
|
import crud
|
||||||
from flask import jsonify, request
|
from flask import jsonify, request
|
||||||
from helpers import init_db_connection, CORS_HEADERS
|
from helpers import CORS_HEADERS
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -33,15 +32,16 @@ def main():
|
|||||||
|
|
||||||
def make_insert_request():
|
def make_insert_request():
|
||||||
try:
|
try:
|
||||||
user_id = request.headers.get("X-UserId")
|
user_id = request.headers.get("X-User")
|
||||||
if not user_id:
|
if not user_id:
|
||||||
return jsonify({"errorCode": "USER_ID_REQUIRED"}), 400, CORS_HEADERS
|
return jsonify({"errorCode": "USER_ID_REQUIRED"}), 400, CORS_HEADERS
|
||||||
|
|
||||||
# Lấy dữ liệu từ body của request (thông tin hồ sơ người dùng)
|
# Lấy dữ liệu từ body của request (thông tin hồ sơ người dùng)
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
|
||||||
response, status = crud.insert_profile(data)
|
response, status, header = crud.insert_profile(user_id, data)
|
||||||
|
|
||||||
return jsonify(response), status
|
return jsonify(response), status, header
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
@@ -50,13 +50,13 @@ def make_update_request():
|
|||||||
try:
|
try:
|
||||||
data = request.get_json() # Lay du lieu json tu request body
|
data = request.get_json() # Lay du lieu json tu request body
|
||||||
|
|
||||||
user_id = data.get("user_id")
|
user_id = request.headers.get("X-User")
|
||||||
if not user_id:
|
if not user_id:
|
||||||
return jsonify({"error": "user_id is required"}), 400
|
return jsonify({"error": "user_id is required"}), 400
|
||||||
|
|
||||||
# Call CRUD function to update profile
|
# Call CRUD function to update profile
|
||||||
response, status = crud.update_profile
|
response, status, header = crud.update_profile(user_id, data)
|
||||||
return jsonify(response), status
|
return jsonify(response), status, header
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
@@ -68,8 +68,8 @@ def make_get_request():
|
|||||||
if not user_id:
|
if not user_id:
|
||||||
return jsonify({"error": "user_id is required"}), 400
|
return jsonify({"error": "user_id is required"}), 400
|
||||||
|
|
||||||
response, status = crud.get_profile(user_id)
|
response, status, header = crud.get_profile(user_id)
|
||||||
return jsonify(response), status
|
return jsonify(response), status, header
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|||||||
45
apps/crud.py
45
apps/crud.py
@@ -1,30 +1,27 @@
|
|||||||
import io
|
import io
|
||||||
from flask import Response
|
from flask import Response
|
||||||
from helpers import init_db_connection, CORS_HEADERS
|
from helpers import init_db_connection, CORS_HEADERS
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
|
|
||||||
def get_profile(user_id):
|
def get_profile(id):
|
||||||
try:
|
try:
|
||||||
conn = init_db_connection()
|
conn = init_db_connection()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# Truy van thong tin nguoi dung tu bang ailbl_user_profiles
|
# Truy van thong tin nguoi dung tu bang ailbl_user_profiles
|
||||||
query = "SELECT * FROM ailbl_user_profiles WHERE user_id = %s"
|
query = "SELECT * FROM ailbl_user_profile WHERE id = %s"
|
||||||
cursor.execute(query, (user_id,))
|
cursor.execute(query, (id,))
|
||||||
profile = cursor.fetchone() # fetchone la gi ?
|
profile = cursor.fetchone() # fetchone la gi ?
|
||||||
|
|
||||||
if profile:
|
if profile:
|
||||||
return {
|
return {
|
||||||
"user_id": profile[0],
|
"id": profile[0],
|
||||||
"first_name": profile[1],
|
"first_name": profile[1],
|
||||||
"last_name": profile[2],
|
"last_name": profile[2],
|
||||||
"dob": profile[3],
|
"dob": profile[3],
|
||||||
"gender": profile[4],
|
"gender": profile[4],
|
||||||
"address": profile[5],
|
"created": profile[5],
|
||||||
"phone": profile[6],
|
"modified": profile[6]
|
||||||
"created": profile[7],
|
|
||||||
"modified": profile[8]
|
|
||||||
}, 200, CORS_HEADERS
|
}, 200, CORS_HEADERS
|
||||||
else:
|
else:
|
||||||
return {"error": "Profile not found"}, 404, CORS_HEADERS
|
return {"error": "Profile not found"}, 404, CORS_HEADERS
|
||||||
@@ -35,24 +32,22 @@ def get_profile(user_id):
|
|||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
def update_profile(user_id, data):
|
def update_profile(id, data):
|
||||||
try:
|
try:
|
||||||
conn = init_db_connection()
|
conn = init_db_connection()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
# Cap nhat thong tin nguoi dung trong bang ailbl_user_profiles
|
# Cap nhat thong tin nguoi dung trong bang ailbl_user_profiles
|
||||||
query = """
|
query = """
|
||||||
UPDATE ailbl_user_profiles
|
UPDATE ailbl_user_profile
|
||||||
SET first_name = %s, last_name = %s, dob = %s, gender = %s, address = %s, phone = %s, modified = NOW()
|
SET first_name = %s, last_name = %s, dob = %s, gender = %s, modified = NOW()
|
||||||
WHERE user_id = %s
|
WHERE id = %s
|
||||||
"""
|
"""
|
||||||
cursor.execute(query, (
|
cursor.execute(query, (
|
||||||
data.get("first_name"),
|
data.get("first_name"),
|
||||||
data.get("last_name"),
|
data.get("last_name"),
|
||||||
data.get("dob"),
|
data.get("dob"),
|
||||||
data.get("gender"),
|
data.get("gender"),
|
||||||
data.get("address"),
|
id
|
||||||
data.get("phone"),
|
|
||||||
user_id
|
|
||||||
))
|
))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
return {"message": "Profile updated successfully"}, 200, CORS_HEADERS
|
return {"message": "Profile updated successfully"}, 200, CORS_HEADERS
|
||||||
@@ -64,23 +59,21 @@ def update_profile(user_id, data):
|
|||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
def insert_profile(user_id, data):
|
def insert_profile(id, data):
|
||||||
try:
|
try:
|
||||||
conn = init_db_connection()
|
conn = init_db_connection()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
# Tao moi thong tin nguoi dung trong bang ailbl_user_profiles
|
# Tao moi thong tin nguoi dung trong bang ailbl_user_profile
|
||||||
query = """
|
query = """
|
||||||
INSERT INTO ailbl_user_profiles (user_id, first_name, last_name, dob, gender, address, phone, created, modified)
|
INSERT INTO ailbl_user_profile (id, first_name, last_name, dob, gender, created, modified)
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, NOW(), NOW())
|
VALUES (%s, %s, %s, %s, %s, NOW(), NOW())
|
||||||
"""
|
"""
|
||||||
cursor.execute(query, (
|
cursor.execute(query, (
|
||||||
user_id,
|
id,
|
||||||
data.get("first_name"),
|
data.get("first_name"),
|
||||||
data.get("last_name"),
|
data.get("last_name"),
|
||||||
data.get("dob"),
|
data.get("dob"),
|
||||||
data.get("gender"),
|
data.get("gender"),
|
||||||
data.get("address"),
|
|
||||||
data.get("phone")
|
|
||||||
))
|
))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
return {"message": "Profile created successfully"}, 201, CORS_HEADERS
|
return {"message": "Profile created successfully"}, 201, CORS_HEADERS
|
||||||
@@ -91,14 +84,14 @@ def insert_profile(user_id, data):
|
|||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
def delete_profile(user_id):
|
def delete_profile(id):
|
||||||
try:
|
try:
|
||||||
conn = init_db_connection()
|
conn = init_db_connection()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
|
||||||
# Xoa thong tin nguoi dung trong bang ailbl_user_profiles
|
# Xoa thong tin nguoi dung trong bang ailbl_user_profiles
|
||||||
query = "DELETE FROM ailbl_user_profiles WHERE user_id = %s"
|
query = "DELETE FROM ailbl_user_profile WHERE id = %s"
|
||||||
cursor.execute(query, (user_id,))
|
cursor.execute(query, (id,))
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
return {"message": "Profile deleted successfully"}, 200, CORS_HEADERS
|
return {"message": "Profile deleted successfully"}, 200, CORS_HEADERS
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ import typing
|
|||||||
import psycopg2
|
import psycopg2
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from psycopg2.extras import LoggingConnection
|
from psycopg2.extras import LoggingConnection
|
||||||
from ory_kratos_client.api import identity_api
|
|
||||||
from ory_kratos_client.configuration import Configuration
|
|
||||||
|
|
||||||
CORS_HEADERS = {
|
CORS_HEADERS = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -100,5 +99,3 @@ def check_port_open(ip: str, port: int, timeout: int = 30):
|
|||||||
except Exception as err:
|
except Exception as err:
|
||||||
current_app.logger.err(f"Check port open error: {err}")
|
current_app.logger.err(f"Check port open error: {err}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Flask==3.1.0
|
Flask==3.1.0
|
||||||
# psycopg2-binary==2.9.10
|
psycopg2-binary==2.9.10
|
||||||
# pydantic==2.11.3
|
pydantic==2.11.3
|
||||||
# minio==7.2.5
|
minio==7.2.5
|
||||||
# Pillow==10.4.0
|
Pillow==10.4.0
|
||||||
# boto3==1.35.70
|
boto3==1.35.70
|
||||||
Reference in New Issue
Block a user