|
1 | | -import os |
| 1 | +import json |
2 | 2 | import uuid |
3 | 3 | import logging |
4 | 4 | from datetime import datetime |
|
9 | 9 | from app.modules.factlabel import factlabel_bp |
10 | 10 | from app.modules.hubfile.models import HubfileViewRecord |
11 | 11 | from app.modules.hubfile.services import HubfileService |
12 | | -from app.modules.factlabel.services import FactlabelService |
13 | 12 |
|
14 | 13 | logger = logging.getLogger(__name__) |
15 | 14 |
|
16 | 15 |
|
17 | 16 | @factlabel_bp.route("/factlabel/view/<int:file_id>", methods=["GET"]) |
18 | 17 | def view_factlabel(file_id): |
19 | 18 | file = HubfileService().get_or_404(file_id) |
20 | | - filename = file.name |
21 | 19 |
|
22 | | - directory_path = os.path.join( |
23 | | - "uploads", |
24 | | - f"user_{file.feature_model.dataset.user_id}", |
25 | | - f"dataset_{file.feature_model.dataset_id}", |
26 | | - "uvl", |
27 | | - ) |
| 20 | + try: |
| 21 | + if not file.factlabel_json: |
| 22 | + return jsonify({"success": False, "error": "FactLabel not ready yet"}), 404 |
28 | 23 |
|
29 | | - file_path = os.path.join(directory_path, filename) |
| 24 | + # 🔹 Convertir de string a dict |
| 25 | + try: |
| 26 | + content = json.loads(file.factlabel_json) |
| 27 | + except Exception: |
| 28 | + return ( |
| 29 | + jsonify({"success": False, "error": "Invalid FactLabel JSON in DB"}), |
| 30 | + 500, |
| 31 | + ) |
30 | 32 |
|
31 | | - try: |
32 | | - if os.path.exists(file_path): |
33 | | - content = FactlabelService().get_characterization(file) |
34 | | - # logger.info(f'JSON Content: {content}') |
35 | | - user_cookie = request.cookies.get("view_cookie") |
36 | | - if not user_cookie: |
37 | | - user_cookie = str(uuid.uuid4()) |
| 33 | + # --- Registro de la vista --- |
| 34 | + user_cookie = request.cookies.get("view_cookie") |
| 35 | + if not user_cookie: |
| 36 | + user_cookie = str(uuid.uuid4()) |
| 37 | + |
| 38 | + existing_record = HubfileViewRecord.query.filter_by( |
| 39 | + user_id=current_user.id if current_user.is_authenticated else None, |
| 40 | + file_id=file_id, |
| 41 | + view_cookie=user_cookie, |
| 42 | + ).first() |
38 | 43 |
|
39 | | - # Check if the view record already exists for this cookie |
40 | | - existing_record = HubfileViewRecord.query.filter_by( |
| 44 | + if not existing_record: |
| 45 | + new_view_record = HubfileViewRecord( |
41 | 46 | user_id=current_user.id if current_user.is_authenticated else None, |
42 | 47 | file_id=file_id, |
| 48 | + view_date=datetime.now(), |
43 | 49 | view_cookie=user_cookie, |
44 | | - ).first() |
| 50 | + ) |
| 51 | + db.session.add(new_view_record) |
| 52 | + db.session.commit() |
45 | 53 |
|
46 | | - if not existing_record: |
47 | | - # Register file view |
48 | | - new_view_record = HubfileViewRecord( |
49 | | - user_id=current_user.id if current_user.is_authenticated else None, |
50 | | - file_id=file_id, |
51 | | - view_date=datetime.now(), |
52 | | - view_cookie=user_cookie, |
53 | | - ) |
54 | | - db.session.add(new_view_record) |
55 | | - db.session.commit() |
| 54 | + # --- Preparar respuesta --- |
| 55 | + response = jsonify({"success": True, "content": content}) |
| 56 | + if not request.cookies.get("view_cookie"): |
| 57 | + response = make_response(response) |
| 58 | + response.set_cookie( |
| 59 | + "view_cookie", user_cookie, max_age=60 * 60 * 24 * 365 * 2 |
| 60 | + ) |
| 61 | + return response |
56 | 62 |
|
57 | | - # Prepare response |
58 | | - response = jsonify({"success": True, "content": content}) |
59 | | - if not request.cookies.get("view_cookie"): |
60 | | - response = make_response(response) |
61 | | - response.set_cookie( |
62 | | - "view_cookie", user_cookie, max_age=60 * 60 * 24 * 365 * 2 |
63 | | - ) |
64 | | - return response |
65 | | - else: |
66 | | - logger.info("path doesn't exist") |
67 | | - return jsonify({"success": False, "error": "File not found"}), 404 |
68 | 63 | except Exception as e: |
69 | | - return jsonify({"success": False, "error": str(e)}), 500 |
| 64 | + return ( |
| 65 | + jsonify({"success": False, "error": f"Internal server error: {str(e)}"}), |
| 66 | + 500, |
| 67 | + ) |
0 commit comments