code quality

This commit is contained in:
bkfox
2023-03-13 17:47:00 +01:00
parent 934817da8a
commit 112770eddf
162 changed files with 4798 additions and 4069 deletions

View File

@ -1,5 +1,4 @@
"""
Manage diffusions using schedules, to update, clean up or check diffusions.
"""Manage diffusions using schedules, to update, clean up or check diffusions.
A generated diffusion can be unconfirmed, that means that the user must confirm
it by changing its type to "normal". The behaviour is controlled using
@ -13,9 +12,9 @@ from django.core.management.base import BaseCommand
from django.db import transaction
from django.utils import timezone as tz
from aircox.models import Schedule, Diffusion
from aircox.models import Diffusion, Schedule
logger = logging.getLogger('aircox.commands')
logger = logging.getLogger("aircox.commands")
class Actions:
@ -26,20 +25,28 @@ class Actions:
def update(self):
episodes, diffusions = [], []
for schedule in Schedule.objects.filter(program__active=True,
initial__isnull=True):
for schedule in Schedule.objects.filter(
program__active=True, initial__isnull=True
):
eps, diffs = schedule.diffusions_of_month(self.date)
if eps:
episodes += eps
if diffs:
diffusions += diffs
logger.info('[update] %s: %d episodes, %d diffusions and reruns',
str(schedule), len(eps), len(diffs))
logger.info(
"[update] %s: %d episodes, %d diffusions and reruns",
str(schedule),
len(eps),
len(diffs),
)
with transaction.atomic():
logger.info('[update] save %d episodes and %d diffusions',
len(episodes), len(diffusions))
logger.info(
"[update] save %d episodes and %d diffusions",
len(episodes),
len(diffusions),
)
for episode in episodes:
episode.save()
for diffusion in diffusions:
@ -48,9 +55,10 @@ class Actions:
diffusion.save()
def clean(self):
qs = Diffusion.objects.filter(type=Diffusion.TYPE_UNCONFIRMED,
start__lt=self.date)
logger.info('[clean] %d diffusions will be removed', qs.count())
qs = Diffusion.objects.filter(
type=Diffusion.TYPE_UNCONFIRMED, start__lt=self.date
)
logger.info("[clean] %d diffusions will be removed", qs.count())
qs.delete()
@ -61,45 +69,57 @@ class Command(BaseCommand):
parser.formatter_class = RawTextHelpFormatter
today = datetime.date.today()
group = parser.add_argument_group('action')
group = parser.add_argument_group("action")
group.add_argument(
'-u', '--update', action='store_true',
help='generate (unconfirmed) diffusions for the given month. '
'These diffusions must be confirmed manually by changing '
'their type to "normal"'
"-u",
"--update",
action="store_true",
help="generate (unconfirmed) diffusions for the given month. "
"These diffusions must be confirmed manually by changing "
'their type to "normal"',
)
group.add_argument(
'-l', '--clean', action='store_true',
help='remove unconfirmed diffusions older than the given month'
"-l",
"--clean",
action="store_true",
help="remove unconfirmed diffusions older than the given month",
)
group = parser.add_argument_group('date')
group = parser.add_argument_group("date")
group.add_argument(
'--year', type=int, default=today.year,
help='used by update, default is today\'s year')
"--year",
type=int,
default=today.year,
help="used by update, default is today's year",
)
group.add_argument(
'--month', type=int, default=today.month,
help='used by update, default is today\'s month')
"--month",
type=int,
default=today.month,
help="used by update, default is today's month",
)
group.add_argument(
'--next-month', action='store_true',
help='set the date to the next month of given date'
' (if next month from today'
"--next-month",
action="store_true",
help="set the date to the next month of given date"
" (if next month from today",
)
def handle(self, *args, **options):
date = datetime.date(year=options['year'], month=options['month'],
day=1)
if options.get('next_month'):
month = options.get('month')
date = datetime.date(
year=options["year"], month=options["month"], day=1
)
if options.get("next_month"):
month = options.get("month")
date += tz.timedelta(days=28)
if date.month == month:
date += tz.timedelta(days=28)
date = date.replace(day=1)
actions = Actions(date)
if options.get('update'):
if options.get("update"):
actions.update()
if options.get('clean'):
if options.get("clean"):
actions.clean()
if options.get('check'):
if options.get("check"):
actions.check()