production-taskbar / backend / helpdesk / admin.py
admin.py
Raw
from typing import Any

from django.conf import settings
from django.contrib import admin, messages
from django.contrib.admin.options import ModelAdmin
from django.core.exceptions import PermissionDenied
from django.db import models
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.urls import path
from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _
from django.views.decorators.http import require_GET
from django_json_widget.widgets import JSONEditorWidget
from simple_history.admin import SimpleHistoryAdmin

from helpdesk.models import (CategoriesSet, Category, Department, Issue,
                             Problem, Reciever, Status)
from helpdesk.reciever import synchronize_reciever


@admin.register(Department)
class DepartmentAdmin(SimpleHistoryAdmin):
    list_display = ('name', 'location', 'categories_set', 'reciever')
    list_filter = ('location', 'reciever')
    search_fields = ['name']
    formfield_overrides = {
        models.JSONField: {
            'widget': JSONEditorWidget
        },
    }
    list_select_related = (
        'location',
        'categories_set',
        'reciever',
    )
    list_per_page = getattr(settings, 'LIST_PER_PAGE')


@admin.register(Reciever)
class RecieverAdmin(SimpleHistoryAdmin):
    list_display = ('name', )
    formfield_overrides = {
        models.JSONField: {
            'widget': JSONEditorWidget
        },
    }

    def get_form(self, request, obj=None, **kwargs):    # type: ignore
        if request.user.is_superuser:
            return super(RecieverAdmin, self).get_form(request, obj, **kwargs)
        else:
            raise PermissionDenied()


@admin.register(CategoriesSet)
class CategoriesSetAdmin(SimpleHistoryAdmin):
    list_display = ('name', )
    filter_horizontal = ('categories', )
    search_fields = ['name']


@admin.register(Category)
class CategoryAdmin(SimpleHistoryAdmin):
    list_display = ('name', 'location')
    filter_horizontal = ('problems', )
    list_filter = ('location', )
    formfield_overrides = {
        models.JSONField: {
            'widget': JSONEditorWidget
        },
    }
    search_fields = ['name', 'label', 'location__name']
    list_select_related = ('location', )


@admin.register(Problem)
class ProblemAdmin(SimpleHistoryAdmin):
    list_display = ('name', )
    list_filter = ('require_scan', )
    search_fields = ['name', 'label']


@admin.register(Status)
class StatusAdmin(SimpleHistoryAdmin):
    list_display = ('name', 'color')
    readonly_fields = ('id', )


@admin.register(Issue)
class IssueAdmin(ModelAdmin):    # type: ignore
    change_list_template = "helpdesk/issue_changelist.html"
    list_display = ('id', 'issue_id', 'title', 'department', 'user_id',
                    'responsibility', 'status', 'create_datetime',
                    'close_datetime')
    list_filter = ('department__location', 'department', 'hostname', 'status',
                   'responsibility', 'create_datetime', 'close_datetime')
    search_fields = [
        'title', 'description', 'user_id', 'phone', 'comments',
        'responsibility', 'issue_id'
    ]
    readonly_fields = [
        'department',
        'id',
        'issue_id',
        'user_id',
        'description',
        'phone',
        'hostname',
        'create_datetime',
        'update_datetime',
        'close_datetime',
    ]
    list_select_related = (
        'department',
        'status',
    )
    list_per_page = getattr(settings, 'LIST_PER_PAGE')

    def get_urls(self) -> Any:
        urls = super().get_urls()
        my_urls = [
            path('sync_opened_issues/', self.sync_opened_issues),
        ]
        return my_urls + urls

    @method_decorator(require_GET)
    def sync_opened_issues(self, request: HttpRequest) -> HttpResponse:
        opened_issues = Issue.objects.filter(close_datetime__isnull=True)
        processed_count = 0
        closed_count = 0

        for issue in opened_issues:
            synchronize_reciever(issue)
            processed_count += 1
            if issue.close_datetime:
                closed_count += 1

        messages.success(
            request,
            _('Finished processing %(processed_count)s issues, closed %(closed_count)s'
              ) % {
                  'processed_count': processed_count,
                  'closed_count': closed_count,
              })
        return HttpResponseRedirect(request.META.get('HTTP_REFERER', ''))