98 lines
3.2 KiB
Python
98 lines
3.2 KiB
Python
"""
|
|
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/<cpu_id>')
|
|
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/<cpu_id>', 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/<cpu_id>', 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/<cpu_id>/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']})
|