Skip to content

Commit

Permalink
Merge pull request #83 from jcass77/enh/code_refactor
Browse files Browse the repository at this point in the history
General code refactoring / cleanup.
  • Loading branch information
jcass77 authored Jul 16, 2020
2 parents d9f342b + 56028e3 commit fd30153
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 191 deletions.
58 changes: 34 additions & 24 deletions django_apscheduler/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,69 @@

from django.contrib import admin
from django.db.models import Avg
from django.utils.safestring import mark_safe
from django.utils.timezone import now

from django_apscheduler.models import DjangoJob, DjangoJobExecution
from django_apscheduler import util


def execute_now(ma, r, qs):
for item in qs:
item.next_run_time = now()
item.save()


execute_now.short_description = "Force tasks to execute right now"


@admin.register(DjangoJob)
class DjangoJobAdmin(admin.ModelAdmin):
list_display = ["id", "name", "next_run_time_sec", "average_duration"]
actions = []
search_fields = ["name"]
list_display = ["id", "name", "next_run_time", "average_duration"]

def get_queryset(self, request):
self._durations = {
item[0]: item[1]
for item in DjangoJobExecution.objects.filter(
job: duration
for job, duration in DjangoJobExecution.objects.filter(
status=DjangoJobExecution.SUCCESS,
run_time__gte=now() - datetime.timedelta(days=2),
)
.values_list("job")
.annotate(duration=Avg("duration"))
}
return super(DjangoJobAdmin, self).get_queryset(request)
return super().get_queryset(request)

def next_run_time_sec(self, obj):
def next_run_time(self, obj):
if obj.next_run_time is None:
return "(paused)"
return util.localize(obj.next_run_time)

def average_duration(self, obj):
return self._durations.get(obj.id) or 0
return self._durations.get(obj.id, "None")

average_duration.short_description = "Average Duration (sec)"


@admin.register(DjangoJobExecution)
class DjangoJobExecutionAdmin(admin.ModelAdmin):
list_display = ["id", "job", "html_status", "run_time_sec", "duration"]

status_color_mapping = {
DjangoJobExecution.ADDED: "RoyalBlue",
DjangoJobExecution.SENT: "SkyBlue",
DjangoJobExecution.MAX_INSTANCES: "yellow",
DjangoJobExecution.MISSED: "yellow",
DjangoJobExecution.MODIFIED: "yellow",
DjangoJobExecution.REMOVED: "red",
DjangoJobExecution.ERROR: "red",
DjangoJobExecution.SUCCESS: "green",
}

list_display = ["id", "job", "html_status", "local_run_time", "duration_text"]
list_filter = ["job__name", "run_time", "status"]

def run_time_sec(self, obj):
def html_status(self, obj):
return mark_safe(
f'<p style="color: {self.status_color_mapping[obj.status]}">{obj.status}</p>'
)

def local_run_time(self, obj):
return util.localize(obj.run_time)

def duration_text(self, obj):
return obj.duration or "N/A"

def get_queryset(self, request):
return (
super(DjangoJobExecutionAdmin, self)
.get_queryset(request)
.select_related("job")
)
return super().get_queryset(request).select_related("job")

html_status.short_description = "Status"
7 changes: 6 additions & 1 deletion django_apscheduler/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ This changelog is used to track all major changes to django_apscheduler.

**Enhancements**

- Drop support for Python 2.7, convert codebase to Python 3.6+.
- CI: drop coverage for Python 2.7 and Django <= 2.1, which are no longer maintained upstream.
- CI: add coverage for Python 3.7 and 3.8, as well as Django long term support (LTS) and the latest released versions.
- CI: un-pin dependency on agronholm/apscheduler#149, which has since been merged and released upstream.
- Rename Django `test_settings.py` file to prevent collision with actual test scripts.
- Clean up unused dependencies / update dependencies to latest available versions.
- Switch to Black code formatting.
- Align package layout with official [Django recommendations](https://docs.djangoproject.com/en/dev/intro/reusable-apps/#packaging-your-app)
- Align package layout with official [Django recommendations](https://docs.djangoproject.com/en/dev/intro/reusable-apps/#packaging-your-app)
- Move UI-related DjangoJobExecution.html_status out of model definition and in to the associated model admin definition.
- Add `help_text` to model fields to document their use.
- Remove unused code fragments.
- Add Python type annotations.

**Fixes**

Expand Down
Loading

0 comments on commit fd30153

Please sign in to comment.