old_contents: from __future__ import unicode_literals
  
  from django.contrib import admin
  from django.shortcuts import redirect
  from django.urls import reverse
  from django.utils.translation import ugettext_lazy as _
  
  from waldur_core.core import admin as core_admin
  
  from . import models, tasks
  
  
  class InvoiceItemInline(core_admin.UpdateOnlyModelAdmin, admin.TabularInline):
      model = models.InvoiceItem
      readonly_fields = ('name', 'price', 'unit_price', 'unit', 'start', 'end',
                         'project_name', 'project_uuid', 'product_code', 'article_code')
      exclude = ('project',)
  
  
  class OpenStackItemInline(InvoiceItemInline):
      model = models.OpenStackItem
      readonly_fields = InvoiceItemInline.readonly_fields + ('package', 'package_details')
  
  
  class OfferingItemInline(InvoiceItemInline):
      model = models.OfferingItem
      readonly_fields = InvoiceItemInline.readonly_fields + ('offering', 'offering_details')
  
  
  class InvoiceAdmin(core_admin.ExtraActionsMixin,
                     core_admin.UpdateOnlyModelAdmin,
                     admin.ModelAdmin):
      inlines = [OpenStackItemInline, OfferingItemInline]
      readonly_fields = ('customer', 'state', 'total', 'year', 'month')
      list_display = ('customer', 'total', 'year', 'month', 'state')
      list_filter = ('state', 'customer')
      search_fields = ('customer', 'uuid')
  
      def get_extra_actions(self):
          return [
              self.send_invoice_report,
              self.update_current_cost,
          ]
  
      def send_invoice_report(self, request):
          tasks.send_invoice_report.delay()
          message = _('Invoice report task has been scheduled')
          self.message_user(request, message)
          return redirect(reverse('admin:invoices_invoice_changelist'))
  
      send_invoice_report.short_description = _('Send invoice report as CSV to email')
  
      def update_current_cost(self, request):
          tasks.update_invoices_current_cost.delay()
          message = _('Task has been scheduled.')
          self.message_user(request, message)
          return redirect(reverse('admin:invoices_invoice_changelist'))
  
      send_invoice_report.short_description = _('Update current cost for invoices')
  
  
  class ServiceDowntimeAdmin(admin.ModelAdmin):
      def get_readonly_fields(self, request, obj=None):
          # Downtime record is protected from modifications
          if obj is not None:
              return self.readonly_fields + ('start', 'end', 'settings')
          return self.readonly_fields
  
  
  admin.site.register(models.Invoice, InvoiceAdmin)
  admin.site.register(models.ServiceDowntime, ServiceDowntimeAdmin)
  
new_contents: from __future__ import unicode_literals
  
  from django.contrib import admin
  from django.shortcuts import redirect
  from django.urls import reverse
  from django.utils.translation import ugettext_lazy as _
  
  from waldur_core.core import admin as core_admin
  
  from . import models, tasks
  
  
  class InvoiceItemInline(core_admin.UpdateOnlyModelAdmin, admin.TabularInline):
      model = models.InvoiceItem
      readonly_fields = ('name', 'price', 'unit_price', 'unit', 'start', 'end',
                         'project_name', 'project_uuid', 'product_code', 'article_code')
      exclude = ('project',)
  
  
  class OpenStackItemInline(InvoiceItemInline):
      model = models.OpenStackItem
      readonly_fields = InvoiceItemInline.readonly_fields + ('package', 'package_details')
  
  
  class OfferingItemInline(InvoiceItemInline):
      model = models.OfferingItem
      readonly_fields = InvoiceItemInline.readonly_fields + ('offering', 'offering_details')
  
  
  class InvoiceAdmin(core_admin.ExtraActionsMixin,
                     core_admin.UpdateOnlyModelAdmin,
                     admin.ModelAdmin):
      inlines = [OpenStackItemInline, OfferingItemInline]
      readonly_fields = ('customer', 'state', 'total', 'year', 'month')
      list_display = ('customer', 'total', 'year', 'month', 'state')
      list_filter = ('state', 'customer')
      search_fields = ('customer', 'uuid')
  
      def get_extra_actions(self):
          return [
              self.send_invoice_report,
              self.update_current_cost,
          ]
  
      def send_invoice_report(self, request):
          tasks.send_invoice_report.delay()
          message = _('Invoice report task has been scheduled')
          self.message_user(request, message)
          return redirect(reverse('admin:invoices_invoice_changelist'))
  
      send_invoice_report.short_description = _('Send invoice report as CSV to email')
  
      def update_current_cost(self, request):
          tasks.update_invoices_current_cost.delay()
          message = _('Task has been scheduled.')
          self.message_user(request, message)
          return redirect(reverse('admin:invoices_invoice_changelist'))
  
      send_invoice_report.short_description = _('Update current cost for invoices')
  
  
  class ServiceDowntimeAdmin(admin.ModelAdmin):
      list_display = ('settings', 'start', 'end')
      list_display_links = ('settings',)
      list_filter = ('settings',)
      date_hierarchy = 'start'
  
      def get_readonly_fields(self, request, obj=None):
          # Downtime record is protected from modifications
          if obj is not None:
              return self.readonly_fields + ('start', 'end', 'settings')
          return self.readonly_fields
  
  
  admin.site.register(models.Invoice, InvoiceAdmin)
  admin.site.register(models.ServiceDowntime, ServiceDowntimeAdmin)
  
subject: Improve django model admin for service downtime [WAL-1666]