Files
shopdb-flask/shopdb/core/api/operatingsystems.py
cproudlock cd353b6432
Some checks failed
CI / backend (push) Has been cancelled
CI / naming (push) Has been cancelled
CI / frontend (push) Has been cancelled
Review safe-polish: docs accuracy, dead imports, no-emoji, geenforce robustness
From the full multi-agent review (0 high, 7 medium, 17 low findings). Applies
the mechanical, low-risk items; design/policy findings left for a decision.

Docs accuracy: CLAUDE.md contract 0.10.0 -> 0.11.0 and both stale Alembic head
citations -> 7d24_customfield_searchable / 31 migrations; Dockerfile bundled-
plugin comment fixed (drop nonexistent "equipment", add machines +
measuringtools, count eleven).

Style/naming (LOCKED rules): remove a CSS-escaped pushpin emoji before location
search results (no-emoji policy); rename ManifestEditor shareRoot -> shareroot
(variable mirrors the API field verbatim).

Dead code: remove confirmed-unused imports across ~20 modules (require_role/
require_permission scaffold residue, stray db/Vendor/Model/current_user/Optional/
error_response); drop unused build_scope import + a stale GEENFORCE_API_KEY
docstring clause in geenforce. Migration files left untouched.

Correctness: geenforce ingest robustness - record_enforcement_report now 400s
on a non-dict counts / non-list results instead of 500; _apply_app_link ignores
a non-numeric appid per its docstring instead of 500. Regression tests added.

Backend query.get sweep finished: auth.py refresh -> db.session.get (last one).

910 backend tests pass; pyflakes clean; naming green; frontend build green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-13 08:02:43 -04:00

147 lines
4.4 KiB
Python

"""Operating Systems API endpoints - Full CRUD."""
from flask import Blueprint, request
from flask_jwt_extended import jwt_required
from shopdb.extensions import db
from shopdb.core.models import OperatingSystem
from shopdb.utils.responses import (
success_response,
error_response,
paginated_response,
ErrorCodes
)
from shopdb.utils.pagination import get_pagination_params, paginate_query
from shopdb.utils.authz import require_role
from shopdb.utils.import_mode import apply_import_timestamps
operatingsystems_bp = Blueprint('operatingsystems', __name__)
@operatingsystems_bp.route('', methods=['GET'])
@jwt_required(optional=True)
def list_operatingsystems():
"""List all operating systems."""
page, per_page = get_pagination_params(request)
query = OperatingSystem.query
if request.args.get('active', 'true').lower() != 'false':
query = query.filter(OperatingSystem.isactive == True)
# Exact-match natural-key lookup for idempotent import. Legacy OS rows have
# only a name; pair with ?osversion= when versions are tracked separately.
if exactosname := request.args.get('osname'):
query = query.filter(OperatingSystem.osname == exactosname)
if exactosversion := request.args.get('osversion'):
query = query.filter(OperatingSystem.osversion == exactosversion)
if search := request.args.get('search'):
query = query.filter(OperatingSystem.osname.ilike(f'%{search}%'))
query = query.order_by(OperatingSystem.osname)
items, total = paginate_query(query, page, per_page)
data = [os.to_dict() for os in items]
return paginated_response(data, page, per_page, total)
@operatingsystems_bp.route('/<int:os_id>', methods=['GET'])
@jwt_required(optional=True)
def get_operatingsystem(os_id: int):
"""Get a single operating system."""
os = db.session.get(OperatingSystem, os_id)
if not os:
return error_response(
ErrorCodes.NOT_FOUND,
f'Operating system with ID {os_id} not found',
http_code=404
)
return success_response(os.to_dict())
@operatingsystems_bp.route('', methods=['POST'])
@jwt_required()
@require_role('admin')
def create_operatingsystem():
"""Create a new operating system."""
data = request.get_json()
if not data or not data.get('osname'):
return error_response(ErrorCodes.VALIDATION_ERROR, 'osname is required')
existing = OperatingSystem.query.filter_by(
osname=data['osname'],
osversion=data.get('osversion')
).first()
if existing:
return error_response(
ErrorCodes.CONFLICT,
f"Operating system '{data['osname']} {data.get('osversion', '')}' already exists",
http_code=409
)
os = OperatingSystem(
osname=data['osname'],
osversion=data.get('osversion'),
architecture=data.get('architecture'),
endoflife=data.get('endoflife')
)
db.session.add(os)
apply_import_timestamps(os, data)
db.session.commit()
return success_response(os.to_dict(), message='Operating system created', http_code=201)
@operatingsystems_bp.route('/<int:os_id>', methods=['PUT'])
@jwt_required()
@require_role('admin')
def update_operatingsystem(os_id: int):
"""Update an operating system."""
os = db.session.get(OperatingSystem, os_id)
if not os:
return error_response(
ErrorCodes.NOT_FOUND,
f'Operating system with ID {os_id} not found',
http_code=404
)
data = request.get_json()
if not data:
return error_response(ErrorCodes.VALIDATION_ERROR, 'No data provided')
for key in ['osname', 'osversion', 'architecture', 'endoflife', 'isactive']:
if key in data:
setattr(os, key, data[key])
apply_import_timestamps(os, data)
db.session.commit()
return success_response(os.to_dict(), message='Operating system updated')
@operatingsystems_bp.route('/<int:os_id>', methods=['DELETE'])
@jwt_required()
@require_role('admin')
def delete_operatingsystem(os_id: int):
"""Delete (deactivate) an operating system."""
os = db.session.get(OperatingSystem, os_id)
if not os:
return error_response(
ErrorCodes.NOT_FOUND,
f'Operating system with ID {os_id} not found',
http_code=404
)
os.isactive = False
db.session.commit()
return success_response(message='Operating system deleted')