from django.contrib import admin
from django import forms
from django.template import RequestContext
from django.conf.urls import url
from django.contrib import messages
from django.shortcuts import render_to_response
from django.contrib.gis.admin import OSMGeoAdmin
from django.contrib.gis.geos import GEOSGeometry

from .models import ExportFormat, ExportProfile, Job, Region, ExportProvider, ExportProviderType, \
    ProviderTask, DatamodelPreset, License, UserLicense
import logging

logger = logging.getLogger(__name__)

admin.site.register(ExportFormat)
admin.site.register(ExportProfile)
admin.site.register(ExportProviderType)
admin.site.register(ProviderTask)
admin.site.register(DatamodelPreset)
admin.site.register(License)
admin.site.register(UserLicense)

class HOTRegionGeoAdmin(OSMGeoAdmin):
    """
    Admin model to allow Region editing in admin interface.

    Uses OSM for base layer in map in admin.
    """
    model = Region
    exclude = ['the_geom', 'the_geog']

    def save_model(self, request, obj, form, change):  # pragma no cover
        geom_merc = obj.the_geom_webmercator
        obj.the_geom = geom_merc.transform(ct=4326, clone=True)
        obj.the_geog = GEOSGeometry(obj.the_geom.wkt)
        obj.save()


class JobAdmin(OSMGeoAdmin):
    """
    Admin model for editing Jobs in the admin interface.
    """
    search_fields = ['uid', 'name', 'user__username', 'region__name']
    list_display = ['uid', 'name', 'user', 'region']
    exclude = ['the_geom', 'the_geog']
    actions = ['select_exports']

    update_template = 'admin/update_regions.html'
    update_complete_template = 'admin/update_complete.html'

    def select_exports(self, request):
        """
        Select exports to update.
        """
        selected = ','.join(request.POST.getlist(admin.ACTION_CHECKBOX_NAME))
        regions = Region.objects.all()

        # noinspection PyProtectedMember
        return render_to_response(self.update_template, {
            'regions': regions,
            'selected': selected,
            'opts': self.model._meta,
        }, context_instance=RequestContext(request))

    select_exports.short_description = "Assign a region to the selected exports"

    def update_exports(self, request):
        """
        Update selected exports.
        """
        selected = request.POST.get('selected', '')
        num_selected = len(selected.split(','))
        region_uid = request.POST.get('region', '')
        region = Region.objects.get(uid=region_uid)
        for selected_id in selected.split(','):
            export = Job.objects.get(id=selected_id)
            export.region = region
            export.save()

        messages.success(request, '{0} exports updated.'.format(num_selected))
        # noinspection PyProtectedMember
        return render_to_response(self.update_complete_template, {
            'num_selected': len(selected.split(',')),
            'region': region.name,
            'opts': self.model._meta,
        }, context_instance=RequestContext(request))

    def get_urls(self):
        urls = super(JobAdmin, self).get_urls()
        update_urls = [
            url(r'^select/$', self.admin_site.admin_view(self.select_exports)),
            url(r'^update/$', self.admin_site.admin_view(self.update_exports),
                name="update_regions"),
                         ]
        return update_urls + urls


class ExportConfigAdmin(admin.ModelAdmin):
    """
    Admin model for editing export configurations in the admin interface.
    """
    search_fields = ['uid', 'name', 'user__username']
    list_display = ['uid', 'name', 'user', 'config_type', 'published', 'created_at']


class ExportProviderForm(forms.ModelForm):
    """
    Admin form for editing export providers in the admin interface.
    """
    class Meta:
        model = ExportProvider
        fields = ['name',
                  'slug',
                  'url',
                  'preview_url',
                  'service_copyright',
                  'service_description',
                  'layer',
                  'export_provider_type',
                  'level_from',
                  'level_to',
                  'config',
                  'user',
                  'license',
                  'zip',
                  'display',
                  ]

    def clean_config(self):
        config = self.cleaned_data.get('config')
        if config == "":
            return self.cleaned_data

        service_type = self.cleaned_data.get('export_provider_type').type_name

        logger.info("Checking config for service type {}".format(service_type))
        logger.info("type(service_type): {}".format(str(type(service_type))))
        logger.info("service_type == 'osm': {}".format(str(service_type == 'osm')))
        logger.info("service_type in ['osm', 'osm-generic']: {}".format(str(service_type in ['osm', 'osm-generic'])))

        if service_type in ['wms', 'wmts']:
            from ..utils.external_service import ExternalRasterServiceToGeopackage, \
                                                 ConfigurationError, SeedConfigurationError
            service = ExternalRasterServiceToGeopackage(config=config)
            try:
                conf_dict, seed_configuration, mapproxy_configuration = service.get_check_config()
                logger.info("Config is valid for MapProxy configuration")
            except (ConfigurationError, SeedConfigurationError) as e:
                logger.info("Invalid MapProxy config")
                raise forms.ValidationError(e.message)

        elif service_type in ['osm', 'osm-generic']:
            from ..feature_selection.feature_selection import FeatureSelection
            try:
                f = FeatureSelection.example(config)
                logger.info("Created FeatureSelection for osm config")
            except AssertionError:
                logger.info("Failed to create FeatureSelection for osm config")
                raise forms.ValidationError("Invalid configuration")

        return config


class ExportProviderAdmin(admin.ModelAdmin):
    """
    Admin model for editing export providers in the admin interface.
    """
    form = ExportProviderForm
    list_display = ['name', 'slug', 'export_provider_type', 'user', 'license', 'display']


# register the new admin models
admin.site.register(Region, HOTRegionGeoAdmin)
admin.site.register(Job, JobAdmin)
admin.site.register(ExportProvider, ExportProviderAdmin)

@@ -4542,369 +4542,8 @@
 me%0A%0A
-        logger.info(%22Checking config for service type %7B%7D%22.format(service_type))%0A        logger.info(%22type(service_type): %7B%7D%22.format(str(type(service_type))))%0A        logger.info(%22service_type == 'osm': %7B%7D%22.format(str(service_type == 'osm')))%0A        logger.info(%22service_type in %5B'osm', 'osm-generic'%5D: %7B%7D%22.format(str(service_type in %5B'osm', 'osm-generic'%5D)))%0A%0A
     
@@ -4578,24 +4578,24 @@
 ', 'wmts'%5D:%0A
+
             
@@ -4951,82 +4951,8 @@
 g()%0A
-                logger.info(%22Config is valid for MapProxy configuration%22)%0A
     
@@ -5021,63 +5021,8 @@
  e:%0A
-                logger.info(%22Invalid MapProxy config%22)%0A
     
@@ -5291,181 +5291,30 @@
     
-    logger.info(%22Created FeatureSelection for osm config%22)%0A            except AssertionError:%0A                logger.info(%22Failed to create FeatureSelection for osm config%22)
+except AssertionError:
 %0A   

Remove log trace code