from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.utils.lru_cache import lru_cache

from nodeconductor.cost_tracking import models
from nodeconductor.structure import models as structure_models


@lru_cache(maxsize=1)
def _get_service_content_type_queryset():
    services = structure_models.Service.get_all_models()
    service_content_type_ids = [ContentType.objects.get_for_model(s).id for s in services]
    return ContentType.objects.filter(id__in=service_content_type_ids)


class PriceListItemAdmin(admin.ModelAdmin):
    list_display = ('uuid', 'key', 'item_type', 'value', 'units', 'service')

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "content_type":
            kwargs["queryset"] = _get_service_content_type_queryset()
        return super(PriceListItemAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)


class DefaultPriceListItemAdmin(admin.ModelAdmin):
    list_display = ('uuid', 'key', 'item_type', 'value', 'units', 'service_content_type')

    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ['service_content_type']
        else:
            return []

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "service_content_type":
            kwargs["queryset"] = _get_service_content_type_queryset()
        return super(DefaultPriceListItemAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)


admin.site.register(models.PriceListItem, PriceListItemAdmin)
admin.site.register(models.DefaultPriceListItem, DefaultPriceListItemAdmin)

@@ -240,16 +240,42 @@
 e_models
+, admin as structure_admin
 %0A%0A%0A@lru_
@@ -333,16 +333,63 @@
 yset():%0A
+    %22%22%22 Get list of services content types %22%22%22%0A
     serv
@@ -1033,32 +1033,69 @@
 ceListItemAdmin(
+structure_admin.ChangeReadonlyMixin, 
 admin.ModelAdmin
@@ -1089,32 +1089,32 @@
 in.ModelAdmin):%0A
-
     list_display
@@ -1191,20 +1191,18 @@
 e')%0A
-%0A
     
-def 
+chan
 ge
-t
 _rea
@@ -1217,130 +1217,36 @@
 elds
-(self, request, obj=None):%0A        if obj:%0A            return %5B'service_content_type'%5D%0A        else:%0A            return %5B%5D
+ = ('service_content_type',)
 %0A%0A  

Fix MR comments (nc-707)