Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General code refactoring / cleanup. #83

Merged
merged 2 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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