from django.contrib import admin
from .models import RasterLayer, RasterLayerMetadata, RasterTile

# Register raster layer in admin
class RasterLayerModelAdmin(admin.ModelAdmin):
    readonly_fields = ('parse_log',)
    actions = ['reparse_raster']

    def reparse_raster(self, request, queryset):
        """Admin action to re-parse a rasterlayer
        """
        if queryset.count() > 1:
            self.message_user(request,
                              'You can only parse one RasterLayer at a time.',
                              level=messages.ERROR)
        else:
            rasterlayer = queryset[0]
            rasterlayer.parse_log = ''
            rasterlayer.save()
            msg = 'Parsing Raster, check parse log for progress'
            self.message_user(request, msg)

admin.site.register(RasterLayer, RasterLayerModelAdmin)

# Register read-only raster metadata in admin
class RasterLayerMetadataModelAdmin(admin.ModelAdmin):
    readonly_fields = ('rasterlayer', 'uperleftx', 'uperlefty',
                       'width', 'height', 'scalex', 'scaley', 'skewx',
                       'skewy', 'numbands')
    def has_add_permission(self, request, obj=None):
        return False
    def has_delete_permission(self, request, obj=None):
        return False

admin.site.register(RasterLayerMetadata, RasterLayerMetadataModelAdmin)

# Register read-only raster tile in admin
class RasterTileModelAdmin(admin.ModelAdmin):
    readonly_fields = ('rast', 'rasterlayer', 'filename', 'is_base', 'tilex',
                       'tiley', 'tilez')
    def has_add_permission(self, request, obj=None):
        return False
    def has_delete_permission(self, request, obj=None):
        return False

admin.site.register(RasterTile, RasterTileModelAdmin)


from django.contrib import admin
from raster.models import LegendSemantics, LegendEntry, Legend

admin.site.register(LegendSemantics)
admin.site.register(LegendEntry)


class LegendEntriesInLine(admin.TabularInline):
    model = Legend.entries.through


class LegendAdmin(admin.ModelAdmin):
    inlines = (
        LegendEntriesInLine,
    )

admin.site.register(Legend, LegendAdmin)

@@ -239,16 +239,17 @@
 e_raster
+s
 '%5D%0A%0A    
@@ -266,16 +266,17 @@
 e_raster
+s
 (self, r
@@ -305,16 +305,25 @@
      %22%22%22
+%0A        
 Admin ac
@@ -345,234 +345,28 @@
 e a 
-rasterlayer%0A        %22%22%22%0A        if queryset.count() %3E 1:%0A            self.message_user(request,%0A                              'You can only parse one R
+set of r
 aster
-L
+l
 ayer
- at a time.',%0A                              level=messages.ERROR)
+s.
 %0A   
@@ -374,13 +374,11 @@
     
-else:
+%22%22%22
 %0A   
@@ -374,35 +374,35 @@
     %22%22%22%0A        
-   
+for
  rasterlayer = q
@@ -398,17 +398,18 @@
 erlayer 
-=
+in
  queryse
@@ -413,11 +413,9 @@
 yset
-%5B0%5D
+:
 %0A   
@@ -481,20 +481,17 @@
 .save()%0A
-    
+%0A
         
@@ -511,16 +511,17 @@
 g Raster
+s
 , check 
@@ -529,16 +529,17 @@
 arse log
+s
  for pro
@@ -545,20 +545,16 @@
 ogress'%0A
-    
         

Allow several rasters to be reparsed at the same time