Spaces:
Sleeping
Sleeping
Download routers/admin_rules.py from Coeinterns/bank-statement-api: direct link, hf CLI and curl.
- Browser
- Download file 16 kB
-
https://huggingface.co/spaces/Coeinterns/bank-statement-api/resolve/main/routers/admin_rules.py
- Command line
-
hf download hf://spaces/Coeinterns/bank-statement-api/routers/admin_rules.py
-
curl -L -o admin_rules.py https://huggingface.co/spaces/Coeinterns/bank-statement-api/resolve/main/routers/admin_rules.py
16 kB
| import os | |
| import json | |
| import logging | |
| from typing import Optional, List | |
| from fastapi import APIRouter, Depends, HTTPException | |
| from auth.models import User | |
| from auth.service import get_current_user | |
| from models.admin_models import RuleCreateRequest, RuleUpdateRequest | |
| from core.pipeline import reload_pipeline_rules | |
| logger = logging.getLogger("befree.admin_rules") | |
| router = APIRouter(prefix="/api/v1/admin/rules", tags=["admin_rules"]) | |
| async def list_rules( | |
| category_id: Optional[str] = None, | |
| match_on: Optional[str] = None, | |
| include_inactive: bool = False, | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| """List all classification rules, optionally filtered.""" | |
| from auth.database import SessionLocal as _SL | |
| from auth.rule_models import ClassificationRule, ClassificationCategory | |
| _db = _SL() | |
| try: | |
| from sqlalchemy.orm import joinedload | |
| q = _db.query(ClassificationRule).options( | |
| joinedload(ClassificationRule.category).joinedload( | |
| ClassificationCategory.group | |
| ) | |
| ) | |
| if current_user.role != "admin" or not include_inactive: | |
| q = q.filter(ClassificationRule.is_active == True) | |
| if category_id: | |
| q = q.filter(ClassificationRule.category_id == category_id) | |
| if match_on: | |
| q = q.filter(ClassificationRule.match_on == match_on) | |
| rules = q.order_by(ClassificationRule.priority.desc(), ClassificationRule.name).all() | |
| learned_rules_list = [] | |
| learned_path = "/home/luv/Desktop/t_1/data/logs/learned_rules.json" | |
| cats = _db.query(ClassificationCategory).filter(ClassificationCategory.is_active == True).all() | |
| cat_name_to_id = {c.name.lower().strip(): c.id for c in cats} | |
| if os.path.exists(learned_path): | |
| try: | |
| with open(learned_path, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| for kw, entry in data.items(): | |
| cat_name = entry.get("category", "") | |
| cat_id = cat_name_to_id.get(cat_name.lower().strip()) | |
| if category_id and cat_id != category_id: | |
| continue | |
| if match_on and match_on != "merchant": | |
| continue | |
| learned_rules_list.append({ | |
| "id": f"learned_rule_{kw}", | |
| "name": f"Auto-Learned: {kw}", | |
| "category_id": cat_id, | |
| "category": cat_name, | |
| "keywords": [kw], | |
| "priority": 50, | |
| "match_on": "merchant", | |
| "tx_type_filter": None, | |
| "amount_range": None, | |
| "is_active": True, | |
| "created_at": entry.get("learned_at"), | |
| "updated_at": None, | |
| "created_by": f"RuleEngine ({entry.get('source', 'unknown')})", | |
| "updated_by": None, | |
| "is_learned": True, | |
| }) | |
| except Exception as e: | |
| logger.warning("Failed to load learned rules: %s", e) | |
| try: | |
| from auth.rule_models import LearnedRuleLog | |
| db_logs = _db.query(LearnedRuleLog).options( | |
| joinedload(LearnedRuleLog.category_obj).joinedload(ClassificationCategory.group) | |
| ).filter(LearnedRuleLog.status == 'pending').all() | |
| for log in db_logs: | |
| cat_id = log.category_id or cat_name_to_id.get((log.category or '').lower().strip()) | |
| if category_id and cat_id != category_id: | |
| continue | |
| learned_rules_list.append({ | |
| "id": f"learned_log_{log.id}", | |
| "name": f"Auto-Learned: {log.keyword}", | |
| "category_id": cat_id, | |
| "category": log.category_name, | |
| "group_name": log.group_name, | |
| "keywords": [log.keyword], | |
| "priority": 50, | |
| "match_on": "merchant", | |
| "tx_type_filter": None, | |
| "amount_range": None, | |
| "is_active": False, | |
| "created_at": log.created_at.isoformat() if log.created_at else None, | |
| "updated_at": log.updated_at.isoformat() if log.updated_at else None, | |
| "created_by": f"RuleEngine ({log.source})", | |
| "updated_by": log.updated_by, | |
| "is_learned": True, | |
| "hit_count": log.hit_count, | |
| "confidence": log.confidence, | |
| "source": log.source, | |
| "example_description": log.example_description, | |
| "example_descriptions": [ | |
| ex for ex in (log.example_description or "").splitlines() if ex.strip() | |
| ], | |
| }) | |
| except Exception: | |
| pass | |
| items = [r.to_dict() for r in rules] + learned_rules_list | |
| return {"items": items, "total": len(items)} | |
| finally: | |
| _db.close() | |
| async def create_rule( | |
| payload: RuleCreateRequest, | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| """Create a new classification rule. Admin only.""" | |
| if current_user.role != "admin": | |
| raise HTTPException(status_code=403, detail="Admin access required") | |
| from auth.database import SessionLocal as _SL | |
| from auth.rule_models import ClassificationRule, ClassificationCategory | |
| _db = _SL() | |
| try: | |
| cat = _db.query(ClassificationCategory).filter_by(id=payload.category_id).first() | |
| if not cat: | |
| raise HTTPException(status_code=404, detail="Category not found") | |
| rule = ClassificationRule( | |
| name=payload.name, | |
| category_id=payload.category_id, | |
| priority=payload.priority, | |
| match_on=payload.match_on, | |
| tx_type_filter=payload.tx_type_filter, | |
| created_by=current_user.username, | |
| updated_by=current_user.username, | |
| ) | |
| rule.keywords = payload.keywords | |
| if payload.amount_range and len(payload.amount_range) == 2: | |
| rule.amount_min = payload.amount_range[0] | |
| rule.amount_max = payload.amount_range[1] | |
| _db.add(rule) | |
| _db.commit() | |
| _db.refresh(rule) | |
| reload_pipeline_rules() | |
| return rule.to_dict() | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| _db.rollback() | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| finally: | |
| _db.close() | |
| async def update_rule( | |
| rule_id: str, | |
| payload: RuleUpdateRequest, | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| """Update a rule's keywords, priority, category, etc. Admin only.""" | |
| if current_user.role != "admin": | |
| raise HTTPException(status_code=403, detail="Admin access required") | |
| if rule_id.startswith("learned_rule_"): | |
| from auth.database import SessionLocal as _SL | |
| from auth.rule_models import ClassificationRule, ClassificationCategory | |
| _db = _SL() | |
| try: | |
| cat_id = payload.category_id | |
| if not cat_id: | |
| raise HTTPException(status_code=400, detail="category_id is required to promote rule") | |
| cat = _db.query(ClassificationCategory).filter_by(id=cat_id).first() | |
| if not cat: | |
| raise HTTPException(status_code=404, detail="Category not found") | |
| keyword = rule_id.replace("learned_rule_", "") | |
| rule = ClassificationRule( | |
| name=payload.name or f"Promoted: {keyword}", | |
| category_id=cat_id, | |
| priority=payload.priority if payload.priority is not None else 50, | |
| match_on=payload.match_on or "merchant", | |
| tx_type_filter=payload.tx_type_filter if payload.tx_type_filter != "" else None, | |
| created_by=current_user.username, | |
| updated_by=current_user.username, | |
| ) | |
| rule.keywords = payload.keywords if payload.keywords is not None else [keyword] | |
| if payload.amount_range and len(payload.amount_range) == 2: | |
| rule.amount_min, rule.amount_max = payload.amount_range | |
| _db.add(rule) | |
| _db.commit() | |
| _db.refresh(rule) | |
| learned_path = "/home/luv/Desktop/t_1/data/logs/learned_rules.json" | |
| if os.path.exists(learned_path): | |
| try: | |
| with open(learned_path, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| if keyword in data: | |
| del data[keyword] | |
| with open(learned_path, "w", encoding="utf-8") as f: | |
| json.dump(data, f, indent=4) | |
| except Exception as e: | |
| logger.warning("Failed to remove promoted rule from JSON: %s", e) | |
| reload_pipeline_rules() | |
| return rule.to_dict() | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| _db.rollback() | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| finally: | |
| _db.close() | |
| if rule_id.startswith("learned_log_"): | |
| from auth.database import SessionLocal as _SL | |
| from auth.rule_models import ClassificationRule, ClassificationCategory, LearnedRuleLog | |
| _db = _SL() | |
| try: | |
| log_id = rule_id.replace("learned_log_", "") | |
| log_entry = _db.query(LearnedRuleLog).filter_by(id=log_id).first() | |
| if not log_entry: | |
| raise HTTPException(status_code=404, detail="Learned log entry not found") | |
| cat_id = payload.category_id or log_entry.category_id | |
| if not cat_id: | |
| raise HTTPException(status_code=400, detail="category_id is required to promote rule") | |
| cat = _db.query(ClassificationCategory).filter_by(id=cat_id).first() | |
| if not cat: | |
| raise HTTPException(status_code=404, detail="Category not found") | |
| keyword = log_entry.keyword | |
| rule = ClassificationRule( | |
| name=payload.name or f"Promoted: {keyword}", | |
| category_id=cat_id, | |
| priority=payload.priority if payload.priority is not None else 50, | |
| match_on=payload.match_on or "merchant", | |
| tx_type_filter=payload.tx_type_filter if payload.tx_type_filter != "" else None, | |
| created_by=current_user.username, | |
| updated_by=current_user.username, | |
| ) | |
| rule.keywords = payload.keywords if payload.keywords is not None else [keyword] | |
| if payload.amount_range and len(payload.amount_range) == 2: | |
| rule.amount_min, rule.amount_max = payload.amount_range | |
| _db.add(rule) | |
| _db.commit() | |
| _db.refresh(rule) | |
| log_entry.status = 'approved' | |
| log_entry.promoted_rule_id = rule.id | |
| log_entry.updated_by = current_user.username | |
| _db.commit() | |
| reload_pipeline_rules() | |
| return rule.to_dict() | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| _db.rollback() | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| finally: | |
| _db.close() | |
| from auth.database import SessionLocal as _SL | |
| from auth.rule_models import ClassificationRule, ClassificationCategory | |
| _db = _SL() | |
| try: | |
| rule = _db.query(ClassificationRule).filter_by(id=rule_id).first() | |
| if not rule: | |
| raise HTTPException(status_code=404, detail="Rule not found") | |
| if payload.name is not None: | |
| rule.name = payload.name | |
| if payload.category_id is not None: | |
| cat = _db.query(ClassificationCategory).filter_by(id=payload.category_id).first() | |
| if not cat: | |
| raise HTTPException(status_code=404, detail="Target category not found") | |
| rule.category_id = payload.category_id | |
| if payload.keywords is not None: | |
| rule.keywords = payload.keywords | |
| if payload.priority is not None: | |
| rule.priority = payload.priority | |
| if payload.match_on is not None: | |
| rule.match_on = payload.match_on | |
| if payload.tx_type_filter is not None: | |
| rule.tx_type_filter = payload.tx_type_filter if payload.tx_type_filter != "" else None | |
| if payload.amount_range is not None: | |
| if len(payload.amount_range) == 2: | |
| rule.amount_min, rule.amount_max = payload.amount_range | |
| else: | |
| rule.amount_min = rule.amount_max = None | |
| if payload.is_active is not None: | |
| rule.is_active = payload.is_active | |
| rule.updated_by = current_user.username | |
| _db.commit() | |
| _db.refresh(rule) | |
| reload_pipeline_rules() | |
| return rule.to_dict() | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| _db.rollback() | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| finally: | |
| _db.close() | |
| async def delete_rule( | |
| rule_id: str, | |
| current_user: User = Depends(get_current_user), | |
| ): | |
| """Hard-delete a rule. Admin only.""" | |
| if current_user.role != "admin": | |
| raise HTTPException(status_code=403, detail="Admin access required") | |
| if rule_id.startswith("learned_log_"): | |
| from auth.database import SessionLocal as _SL | |
| from auth.rule_models import LearnedRuleLog | |
| _db = _SL() | |
| try: | |
| log_id = rule_id.replace("learned_log_", "") | |
| entry = _db.query(LearnedRuleLog).filter_by(id=log_id).first() | |
| if not entry: | |
| raise HTTPException(status_code=404, detail="Learned log entry not found") | |
| entry.status = 'rejected' | |
| entry.updated_by = current_user.username | |
| _db.commit() | |
| return {"ok": True} | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| _db.rollback() | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| finally: | |
| _db.close() | |
| if rule_id.startswith("learned_rule_"): | |
| keyword = rule_id.replace("learned_rule_", "") | |
| learned_path = "/home/luv/Desktop/t_1/data/logs/learned_rules.json" | |
| if os.path.exists(learned_path): | |
| try: | |
| with open(learned_path, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| if keyword in data: | |
| del data[keyword] | |
| with open(learned_path, "w", encoding="utf-8") as f: | |
| json.dump(data, f, indent=4) | |
| reload_pipeline_rules() | |
| return {"ok": True} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Failed to delete learned rule: {e}") | |
| raise HTTPException(status_code=404, detail="Learned rule not found") | |
| from auth.database import SessionLocal as _SL | |
| from auth.rule_models import ClassificationRule | |
| _db = _SL() | |
| try: | |
| rule = _db.query(ClassificationRule).filter_by(id=rule_id).first() | |
| if not rule: | |
| raise HTTPException(status_code=404, detail="Rule not found") | |
| _db.delete(rule) | |
| _db.commit() | |
| reload_pipeline_rules() | |
| return {"ok": True} | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| _db.rollback() | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| finally: | |
| _db.close() | |