|
| 1 | +from django.contrib import messages |
| 2 | +from django.core import management |
| 3 | +from ninja import Router |
| 4 | +from ninja.security import django_auth_superuser |
| 5 | + |
| 6 | +from orochi.api.models import ErrorsOut, SuccessResponse |
| 7 | + |
| 8 | +router = Router() |
| 9 | + |
| 10 | + |
| 11 | +@router.get( |
| 12 | + "/rules/update", |
| 13 | + auth=django_auth_superuser, |
| 14 | + response={200: SuccessResponse, 400: ErrorsOut}, |
| 15 | + url_name="update_rules", |
| 16 | +) |
| 17 | +def update_rules(request): |
| 18 | + """Update rules. |
| 19 | +
|
| 20 | + This endpoint triggers the synchronization of rules using a management command. |
| 21 | + It returns a success message if the synchronization is successful, or an error message if it fails. |
| 22 | +
|
| 23 | + Args: |
| 24 | + request: The request object. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + Tuple[int, dict]: A tuple containing the status code and a dictionary with a message. |
| 28 | + Returns 200 and a success message if the synchronization is successful. |
| 29 | + Returns 404 and an error message if the synchronization fails. |
| 30 | +
|
| 31 | + Raises: |
| 32 | + Exception: If an error occurs during rule synchronization. |
| 33 | + """ |
| 34 | + try: |
| 35 | + management.call_command("rules_sync", verbosity=0) |
| 36 | + messages.add_message(request, messages.INFO, "Sync Rules done") |
| 37 | + return 200, {"message": "Sync Symbols done"} |
| 38 | + except Exception as e: |
| 39 | + messages.add_message(request, messages.ERROR, f"Sync Plugin failed: {e}") |
| 40 | + return 404, {"errors": "Forbidden"} |
| 41 | + |
| 42 | + |
| 43 | +@router.get( |
| 44 | + "/rules/generate", |
| 45 | + auth=django_auth_superuser, |
| 46 | + response={200: SuccessResponse, 400: ErrorsOut}, |
| 47 | + url_name="generate_default_rule", |
| 48 | +) |
| 49 | +def generate_default_rule(request): |
| 50 | + """Generate a default rule. |
| 51 | +
|
| 52 | + This endpoint triggers the generation of a default rule using a management command. |
| 53 | + It returns a success message if the rule creation is successful, or an error message if it fails. |
| 54 | +
|
| 55 | + Args: |
| 56 | + request: The request object. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + Tuple[int, dict]: A tuple containing the status code and a dictionary with a message. |
| 60 | + Returns 200 and a success message if the rule creation is successful. |
| 61 | + Returns 404 and an error message if the rule creation fails. |
| 62 | +
|
| 63 | + Raises: |
| 64 | + Exception: If an error occurs during rule generation. |
| 65 | + """ |
| 66 | + try: |
| 67 | + management.call_command("generate_default_rule", verbosity=0) |
| 68 | + messages.add_message(request, messages.INFO, "Default Rule created") |
| 69 | + return 200, {"message": "Sync Symbols done"} |
| 70 | + except Exception as e: |
| 71 | + messages.add_message(request, messages.ERROR, f"Sync Plugin failed: {e}") |
| 72 | + return 404, {"errors": "Forbidden"} |
| 73 | + |
| 74 | + |
| 75 | +@router.get( |
| 76 | + "/plugins/update", |
| 77 | + auth=django_auth_superuser, |
| 78 | + response={200: SuccessResponse, 400: ErrorsOut}, |
| 79 | + url_name="update_plugins", |
| 80 | +) |
| 81 | +def update_plugins(request): |
| 82 | + """Update plugins for the application. |
| 83 | +
|
| 84 | + This endpoint triggers a plugin synchronization process. It then redirects to the admin page, displaying a success or error message. |
| 85 | +
|
| 86 | + Args: |
| 87 | + request: The incoming HTTP request. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + A redirect to /admin with a success message if the synchronization is successful, or a 404 error with an error message if it fails. |
| 91 | +
|
| 92 | + Raises: |
| 93 | + Exception: If an error occurs during plugin synchronization. |
| 94 | + """ |
| 95 | + |
| 96 | + try: |
| 97 | + management.call_command("plugins_sync", verbosity=0) |
| 98 | + messages.add_message(request, messages.INFO, "Sync Plugin done") |
| 99 | + return 200, {"message": "Sync Plugin done"} |
| 100 | + except Exception as e: |
| 101 | + messages.add_message(request, messages.ERROR, f"Sync Plugin failed: {e}") |
| 102 | + return 404, {"errors": "Forbidden"} |
| 103 | + |
| 104 | + |
| 105 | +@router.get( |
| 106 | + "/symbols/update", |
| 107 | + auth=django_auth_superuser, |
| 108 | + response={200: SuccessResponse, 400: ErrorsOut}, |
| 109 | + url_name="update_symbols", |
| 110 | +) |
| 111 | +def update_symbols(request): |
| 112 | + """Update symbols for the application. |
| 113 | +
|
| 114 | + This endpoint triggers a symbol synchronization process. It then redirects to the admin page, displaying a success or error message. |
| 115 | +
|
| 116 | + Args: |
| 117 | + request: The incoming HTTP request. |
| 118 | +
|
| 119 | + Returns: |
| 120 | + A redirect to /admin with a success message if the synchronization is successful, or a 404 error with an error message if it fails. |
| 121 | +
|
| 122 | + Raises: |
| 123 | + Exception: If an error occurs during symbol synchronization. |
| 124 | + """ |
| 125 | + |
| 126 | + try: |
| 127 | + management.call_command("symbols_sync", verbosity=0) |
| 128 | + messages.add_message(request, messages.INFO, "Sync Symbols done") |
| 129 | + return 200, {"message": "Sync Symbols done"} |
| 130 | + except Exception as e: |
| 131 | + messages.add_message(request, messages.ERROR, f"Sync Symbols failed: {e}") |
| 132 | + return 404, {"errors": "Forbidden"} |
0 commit comments