mirror of
https://github.com/django-q2/django-q2.git
synced 2026-10-03 05:18:11 +08:00
More refactoring
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user