More refactoring

This commit is contained in:
GDay
2023-03-31 02:09:54 +02:00
parent a694a9f53c
commit e3517bd4c6
16 changed files with 715 additions and 750 deletions
+29
View File
@@ -1,5 +1,6 @@
from datetime import datetime, timedelta
from keyword import iskeyword
import ast
# Django
from django import get_version
@@ -222,6 +223,34 @@ class Schedule(models.Model):
help_text=_("Name of kwarg to pass intended schedule date"),
)
def parse_kwargs(self):
if not self.kwargs:
return {}
try:
# first try the dict syntax
return ast.literal_eval(self.kwargs)
except (SyntaxError, ValueError):
# else use the kwargs syntax
try:
parsed_kwargs = (
ast.parse(f"f({self.kwargs})").body[0].value.keywords
)
return {
kwarg.arg: ast.literal_eval(kwarg.value)
for kwarg in parsed_kwargs
}
except (SyntaxError, ValueError):
return {}
def parse_args(self):
if not self.args:
return tuple()
args = ast.literal_eval(self.args)
# single value won't eval to tuple, so:
if type(args) != tuple:
args = (args,)
return args
def calculate_next_run(self, next_run=None):
# next run is always in UTC
next_run = next_run or self.next_run