UserAddresDone
Some checks failed
K8S Fission Deployment / Deployment fission functions (push) Failing after 20s

This commit is contained in:
QuangMinh_123
2025-12-11 13:29:03 +00:00
parent 1620f13124
commit 5359f80e3c
10 changed files with 591 additions and 173 deletions

View File

@@ -1,8 +1,7 @@
import crud
import logging
from flask import jsonify, request
# from storage.minio_client import get_minio_client, check_existing_avatar_on_minio, upload_to_minio
ALLOWED_IMAGE_TYPES = {"image/jpeg", "image/png", "image/gif", "image/webp"}
from filters import AddressPage
def main():
@@ -32,18 +31,27 @@ def main():
def make_insert_request():
try:
user_id = request.headers.get("X-User") # Lay user_id tu header X-User
# Lay file tu form-data voi key la 'avatar'
file = request.files.get("avatar")
if not user_id or not file:
return jsonify({"error": "user_id or file is required"}), 400
# Check mimetype(kieu du lieu cua file anh)
if file.mimetype not in ALLOWED_IMAGE_TYPES:
return jsonify(
{"error": "Invalid file type. Only JPG, PNG, GIF, WEBP are allowed."}
), 400
response, status = crud.update_or_create_avatar(user_id, file)
user_id = request.headers.get("X-User") # Ly user_id t header X-User
if not user_id:
return jsonify({"error": "user_id is required"}), 400
data = request.get_json() # Lấy dữ liệu từ body request
if not data:
return jsonify({"error": "Address data is required"}), 400
address = data.get("address")
if not address:
return jsonify({"error": "Address is required"}), 400
# Kiểm tra nếu địa chỉ đã tồn tại cho người dùng này
if crud.exists_address_for_post(user_id, address):
return jsonify({"error": "Address already exists for this user"}), 409
# Tạo mới địa chỉ người dùng
response, status = crud.create_address(user_id, data)
return jsonify(response), status
except Exception as e:
return jsonify({"error": str(e)}), 500
@@ -53,7 +61,28 @@ def make_get_request():
user_id = request.headers.get("X-User")
if not user_id:
return jsonify({"error": "user_id is required"}), 400
return crud.get_avatar_url(user_id)
# return jsonify(response), status
# Lấy tham số filter và phân trang từ request
# Sử dụng AddressPage để lấy filter và phân trang
paging = AddressPage.from_request_queries()
print(paging)
# Gọi hàm CRUD để lấy danh sách địa chỉ của user_id với phân trang và lọc
response = crud.filter_address(user_id, paging)
return jsonify(
response,
), 200
# Kiểm tra xem response có dữ liệu không
# if not response[0]: # Nếu không có dữ liệu
# return jsonify({"message": "No addresses found"}), 404
# return jsonify({
# "data": response[0], # Dữ liệu (danh sách các địa chỉ)
# "status": response[1], # Mã trạng thái HTTP (200)
# "headers": response[2] # Headers CORS
# }), response[1], response[2]
except Exception as e:
return jsonify({"error": str(e)}), 500