chore: initial snapshot of tubco people portal
This commit is contained in:
104
backend/workflows/services.py
Normal file
104
backend/workflows/services.py
Normal file
@@ -0,0 +1,104 @@
|
||||
from pathlib import Path
|
||||
import logging
|
||||
import time
|
||||
|
||||
import requests
|
||||
from django.conf import settings
|
||||
|
||||
from .models import WorkflowConfig
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def is_nextcloud_enabled() -> bool:
|
||||
config = WorkflowConfig.objects.order_by('id').first()
|
||||
if config and config.nextcloud_enabled_override is not None:
|
||||
return bool(config.nextcloud_enabled_override)
|
||||
return bool(settings.NEXTCLOUD_ENABLED)
|
||||
|
||||
|
||||
def is_email_test_mode() -> bool:
|
||||
config = WorkflowConfig.objects.order_by('id').first()
|
||||
if config and config.email_test_mode_override is not None:
|
||||
return bool(config.email_test_mode_override)
|
||||
return bool(settings.EMAIL_TEST_MODE)
|
||||
|
||||
|
||||
def get_email_test_redirect() -> str:
|
||||
return settings.EMAIL_TEST_REDIRECT
|
||||
|
||||
|
||||
def get_nextcloud_settings() -> dict[str, str]:
|
||||
config = WorkflowConfig.objects.order_by('id').first()
|
||||
base_url = (
|
||||
config.nextcloud_base_url_override.strip()
|
||||
if config and config.nextcloud_base_url_override.strip()
|
||||
else settings.NEXTCLOUD_BASE_URL
|
||||
)
|
||||
username = (
|
||||
config.nextcloud_username_override.strip()
|
||||
if config and config.nextcloud_username_override.strip()
|
||||
else settings.NEXTCLOUD_USERNAME
|
||||
)
|
||||
password = (
|
||||
config.nextcloud_password_override
|
||||
if config and config.nextcloud_password_override
|
||||
else settings.NEXTCLOUD_PASSWORD
|
||||
)
|
||||
directory = (
|
||||
config.nextcloud_directory_override.strip()
|
||||
if config and config.nextcloud_directory_override.strip()
|
||||
else settings.NEXTCLOUD_DIRECTORY
|
||||
)
|
||||
return {
|
||||
'base_url': (base_url or '').rstrip('/'),
|
||||
'username': username or '',
|
||||
'password': password or '',
|
||||
'directory': (directory or '').strip('/'),
|
||||
}
|
||||
|
||||
|
||||
def upload_to_nextcloud(local_file: Path, remote_filename: str) -> bool:
|
||||
if not is_nextcloud_enabled():
|
||||
return False
|
||||
|
||||
nc = get_nextcloud_settings()
|
||||
base_url = nc['base_url']
|
||||
directory = nc['directory']
|
||||
if not base_url or not directory:
|
||||
return False
|
||||
|
||||
safe_remote_name = Path(remote_filename).name
|
||||
remote_url = f"{base_url}/{directory}/{safe_remote_name}"
|
||||
retries = max(0, int(getattr(settings, 'NEXTCLOUD_UPLOAD_RETRIES', 2)))
|
||||
timeout = max(5, int(getattr(settings, 'NEXTCLOUD_UPLOAD_TIMEOUT_SECONDS', 30)))
|
||||
|
||||
for attempt in range(retries + 1):
|
||||
try:
|
||||
with local_file.open('rb') as handle:
|
||||
response = requests.put(
|
||||
remote_url,
|
||||
data=handle,
|
||||
auth=(nc['username'], nc['password']),
|
||||
timeout=timeout,
|
||||
)
|
||||
if response.status_code in (200, 201, 204):
|
||||
return True
|
||||
logger.warning(
|
||||
'Nextcloud upload failed with status %s (attempt %s/%s) for %s',
|
||||
response.status_code,
|
||||
attempt + 1,
|
||||
retries + 1,
|
||||
safe_remote_name,
|
||||
)
|
||||
except requests.RequestException as exc:
|
||||
logger.warning(
|
||||
'Nextcloud upload error (attempt %s/%s) for %s: %s',
|
||||
attempt + 1,
|
||||
retries + 1,
|
||||
safe_remote_name,
|
||||
exc,
|
||||
)
|
||||
if attempt < retries:
|
||||
time.sleep(0.6 * (attempt + 1))
|
||||
return False
|
||||
Reference in New Issue
Block a user