From effd132af202426c46290a1a151136ae26ca643a Mon Sep 17 00:00:00 2001 From: huangzhuang_3rd Date: Mon, 1 Jun 2026 18:31:20 +0800 Subject: [PATCH] =?UTF-8?q?v1.8.0:=20=E6=A8=A1=E5=9D=97=E5=8C=96=E9=87=8D?= =?UTF-8?q?=E6=9E=84=20+=20=E5=90=8E=E5=8F=B0=E7=99=BB=E5=BD=95=E8=AE=A4?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/routes/api_cpus.py | 97 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 modules/routes/api_cpus.py diff --git a/modules/routes/api_cpus.py b/modules/routes/api_cpus.py new file mode 100644 index 0000000..2e91fcb --- /dev/null +++ b/modules/routes/api_cpus.py @@ -0,0 +1,97 @@ +""" +CPU CRUD API +""" +import uuid +from datetime import datetime +from flask import Blueprint, request, jsonify +from config import CPUS_FILE +from utils import load_data, save_data, parse_date_to_timestamp + +cpus_bp = Blueprint('api_cpus', __name__) + + +def _safe_sort_key(x, key): + val = x.get(key) + if val is None: + return 0 if key in ['cores', 'threads', 'views'] else '' + return val + + +@cpus_bp.route('/api/cpus') +def api_cpus(): + cpus = load_data(CPUS_FILE) + hide_hidden = request.args.get('all', '0') == '0' + if hide_hidden: + cpus = [c for c in cpus if c.get('visible', True)] + keyword = request.args.get('q', '').strip().lower() + if keyword: + cpus = [c for c in cpus if keyword in c.get('name', '').lower() or + keyword in c.get('manufacturer', '').lower()] + sort_by = request.args.get('sort', 'default') + reverse = request.args.get('order', 'desc') == 'desc' + if sort_by == 'default': + cpus = sorted(cpus, key=lambda x: ( + not x.get('is_pinned', False), + -(parse_date_to_timestamp(x.get('publish_date', '')) or + parse_date_to_timestamp(x.get('created_at', '')) or 0) + )) + elif sort_by in ['name', 'cores', 'threads', 'price_usd', 'created_at', + 'publish_date', 'views', 'updated_at']: + cpus = sorted(cpus, key=lambda x: _safe_sort_key(x, sort_by), reverse=reverse) + return jsonify(cpus) + + +@cpus_bp.route('/api/cpus/') +def api_cpu_detail(cpu_id): + cpus = load_data(CPUS_FILE) + cpu = next((c for c in cpus if c['id'] == cpu_id), None) + if not cpu: + return jsonify({'error': 'CPU not found'}), 404 + return jsonify(cpu) + + +@cpus_bp.route('/api/cpus', methods=['POST']) +def api_create_cpu(): + data = request.get_json() + cpus = load_data(CPUS_FILE) + data['id'] = uuid.uuid4().hex[:12] + data['created_at'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + data['visible'] = data.get('visible', True) + data['publish_date'] = data.get('publish_date', '') + data['views'] = data.get('views', 0) + data['is_pinned'] = data.get('is_pinned', False) + cpus.append(data) + save_data(CPUS_FILE, cpus) + return jsonify(data) + + +@cpus_bp.route('/api/cpus/', methods=['PUT']) +def api_update_cpu(cpu_id): + data = request.get_json() + cpus = load_data(CPUS_FILE) + cpu = next((c for c in cpus if c['id'] == cpu_id), None) + if not cpu: + return jsonify({'error': 'CPU not found'}), 404 + cpu.update(data) + cpu['updated_at'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + save_data(CPUS_FILE, cpus) + return jsonify(cpu) + + +@cpus_bp.route('/api/cpus/', methods=['DELETE']) +def api_delete_cpu(cpu_id): + cpus = load_data(CPUS_FILE) + cpus = [c for c in cpus if c['id'] != cpu_id] + save_data(CPUS_FILE, cpus) + return jsonify({'success': True}) + + +@cpus_bp.route('/api/cpus//visible', methods=['POST']) +def api_toggle_cpu_visible(cpu_id): + cpus = load_data(CPUS_FILE) + cpu = next((c for c in cpus if c['id'] == cpu_id), None) + if not cpu: + return jsonify({'error': 'CPU not found'}), 404 + cpu['visible'] = not cpu.get('visible', True) + save_data(CPUS_FILE, cpus) + return jsonify({'success': True, 'visible': cpu['visible']})