|
1 | | -from django.contrib import admin |
2 | | -from .models import Process, Task, TaskDependence, Job, JobTask |
| 1 | +from .exceptions import ProcessException |
| 2 | +from .forms import JobForm, JobTaskForm, TaskForm |
| 3 | +from .models import Job, JobTask, Process, Task, TaskDependence |
| 4 | +from django.contrib import admin, messages |
| 5 | +from django.utils.translation import ngettext |
| 6 | +from django.utils.translation import gettext_lazy as _ |
3 | 7 |
|
4 | 8 |
|
5 | 9 | # Register your models here. |
6 | 10 | @admin.register(Process) |
7 | 11 | class ProcessAdmin(admin.ModelAdmin): |
8 | | - list_display = ('__str__', 'is_active', 'minute', 'hour', 'day_of_month', 'month', 'day_of_week') |
| 12 | + actions = ['run_on_demand'] |
| 13 | + list_display = ('__str__', 'description', 'is_active', 'recurrence') |
| 14 | + list_filter = ('is_active',) |
| 15 | + search_fields = ('name', 'description') |
| 16 | + |
| 17 | + # noinspection PyMethodMayBeStatic |
| 18 | + def recurrence(self, process): |
| 19 | + return '|'.join([process.minute, process.hour, process.day_of_month, process.month, process.day_of_week]) |
| 20 | + recurrence.short_description = 'recurrence' |
| 21 | + |
| 22 | + def run_on_demand(self, request, queryset): |
| 23 | + for process in queryset: |
| 24 | + __, __ = Job.create(process) |
| 25 | + self.message_user(request, ngettext( |
| 26 | + '%d process was successfully started.', |
| 27 | + '%d processes were successfully started.', |
| 28 | + len(queryset), |
| 29 | + ) % len(queryset), messages.SUCCESS) |
| 30 | + run_on_demand.short_description = _('run on demand') |
9 | 31 |
|
10 | 32 |
|
11 | 33 | @admin.register(Task) |
12 | 34 | class TaskAdmin(admin.ModelAdmin): |
| 35 | + form = TaskForm |
13 | 36 | list_display = ('__str__', 'is_active', 'process') |
14 | 37 |
|
15 | | - |
16 | | -@admin.register(TaskDependence) |
17 | | -class TaskDependenceAdmin(admin.ModelAdmin): |
18 | | - list_display = ('__str__', 'task', 'parent') |
| 38 | + # noinspection PyMethodMayBeStatic,PyUnusedLocal |
| 39 | + def get_readonly_fields(self, request, obj=None): |
| 40 | + if obj: |
| 41 | + return self.readonly_fields + ('process',) |
| 42 | + return self.readonly_fields |
19 | 43 |
|
20 | 44 |
|
21 | 45 | @admin.register(Job) |
22 | 46 | class JobAdmin(admin.ModelAdmin): |
| 47 | + form = JobForm |
| 48 | + actions = ['cancel'] |
23 | 49 | list_display = ('__str__', 'dt_start', 'dt_end', 'observations') |
| 50 | + list_filter = ('status',) |
| 51 | + change_form_template = 'process/admin_change_job.html' |
| 52 | + |
| 53 | + # noinspection PyMethodMayBeStatic,PyUnusedLocal |
| 54 | + def get_readonly_fields(self, request, obj=None): |
| 55 | + if obj and obj.status not in Job.cancelable: |
| 56 | + return 'process', 'status', 'dt_start', 'dt_end', 'observations' |
| 57 | + return 'process', 'dt_start', 'dt_end', 'observations' |
| 58 | + |
| 59 | + def get_queryst(self, request): |
| 60 | + qs = Job.objects.all().prefetch_related('process') |
| 61 | + # TODO: this should be handled by some parameter to the ChangeList. |
| 62 | + ordering = self.get_ordering(request) |
| 63 | + if ordering: |
| 64 | + qs = qs.order_by(*ordering) |
| 65 | + return qs |
| 66 | + |
| 67 | + # noinspection PyMethodMayBeStatic, PyUnusedLocal |
| 68 | + def has_add_permission(self, request): |
| 69 | + return False |
| 70 | + |
| 71 | + def cancel(self, request, queryset): |
| 72 | + # check cancelable |
| 73 | + not_cancelable = [j for j in queryset if j.status not in Job.cancelable] |
| 74 | + if not_cancelable: |
| 75 | + self.message_user( |
| 76 | + request, |
| 77 | + ngettext( |
| 78 | + 'following job is not in cancelable status: %(job)s', |
| 79 | + 'following jobs are not in cancelable status: %(job)s', |
| 80 | + len(not_cancelable) |
| 81 | + ) % {'job': ', '.join([str(job) for job in not_cancelable])}, |
| 82 | + messages.ERROR |
| 83 | + ) |
| 84 | + return |
| 85 | + # attempt to cancel |
| 86 | + try: |
| 87 | + for job in queryset: |
| 88 | + job.cancel() |
| 89 | + except ProcessException as e: |
| 90 | + self.message_user(_('there was an exception when cancelling jobs %s') % e, messages.ERROR) |
| 91 | + return |
| 92 | + # success |
| 93 | + self.message_user(request, ngettext( |
| 94 | + '%d job was successfully canceled.', |
| 95 | + '%d jobs were successfully canceled.', |
| 96 | + len(queryset), |
| 97 | + ) % len(queryset), messages.SUCCESS) |
| 98 | + cancel.short_description = _('cancel') |
24 | 99 |
|
25 | 100 |
|
26 | 101 | @admin.register(JobTask) |
27 | 102 | class JobTaskAdmin(admin.ModelAdmin): |
| 103 | + form = JobTaskForm |
28 | 104 | list_display = ('__str__', 'dt_start', 'dt_end', 'observations') |
| 105 | + |
| 106 | + def get_readonly_fields(self, request, obj=None): |
| 107 | + if obj: |
| 108 | + return self.readonly_fields + ('job', 'task', 'dt_start', 'dt_end', 'observations') |
| 109 | + return self.readonly_fields |
| 110 | + |
| 111 | + # noinspection PyMethodMayBeStatic, PyUnusedLocal |
| 112 | + def has_add_permission(self, request): |
| 113 | + return False |
0 commit comments