datatracker/ietf/group/tasks.py
Jennifer Richards 8315c665bd
feat: copy 1wg-charters files into place (#7431)
* feat: copy 1wg-charters* into other places

* test: update test

* test: test no-copy operation

* style: Black

* fix: Skip copy if dest is a non-dir

* chore: de-lint
2024-05-20 08:21:52 -05:00

62 lines
2.1 KiB
Python

# Copyright The IETF Trust 2024, All Rights Reserved
#
# Celery task definitions
#
import shutil
from celery import shared_task
from pathlib import Path
from django.conf import settings
from django.template.loader import render_to_string
from ietf.utils import log
from .models import Group
from .utils import fill_in_charter_info, fill_in_wg_drafts, fill_in_wg_roles
@shared_task
def generate_wg_charters_files_task():
areas = Group.objects.filter(type="area", state="active").order_by("name")
groups = (
Group.objects.filter(type="wg", state="active")
.exclude(parent=None)
.order_by("acronym")
)
for group in groups:
fill_in_charter_info(group)
fill_in_wg_roles(group)
fill_in_wg_drafts(group)
for area in areas:
area.groups = [g for g in groups if g.parent_id == area.pk]
charter_path = Path(settings.CHARTER_PATH)
charters_file = charter_path / "1wg-charters.txt"
charters_file.write_text(
render_to_string("group/1wg-charters.txt", {"areas": areas}),
encoding="utf8",
)
charters_by_acronym_file = charter_path / "1wg-charters-by-acronym.txt"
charters_by_acronym_file.write_text(
render_to_string("group/1wg-charters-by-acronym.txt", {"groups": groups}),
encoding="utf8",
)
charter_copy_dest = getattr(settings, "CHARTER_COPY_PATH", None)
if charter_copy_dest is not None:
if not Path(charter_copy_dest).is_dir():
log.log(
f"Error copying 1wg-charter files to {charter_copy_dest}: it does not exist or is not a directory"
)
else:
try:
shutil.copy2(charters_file, charter_copy_dest)
except IOError as err:
log.log(f"Error copying {charters_file} to {charter_copy_dest}: {err}")
try:
shutil.copy2(charters_by_acronym_file, charter_copy_dest)
except IOError as err:
log.log(
f"Error copying {charters_by_acronym_file} to {charter_copy_dest}: {err}"
)