import crudbuilder
from six import with_metaclass
from django.contrib.contenttypes.models import ContentType

from crudbuilder.exceptions import NotModelException


class BaseBuilder(object):
    def __init__(
        self,
        app,
        model,
        crud
    ):

        self.model = model
        self.app = app
        self.crud = crud

        self.custom_modelform = self._has_crud_attr('custom_modelform')
        self.modelform_excludes = self._has_crud_attr('modelform_excludes')
        self.createupdate_forms = self._has_crud_attr('createupdate_forms')

        # django tables2
        self.custom_table2 = self._has_crud_attr('custom_table2')
        self.tables2_fields = self._has_crud_attr('tables2_fields')
        self.tables2_css_class = self._has_crud_attr('tables2_css_class')
        self.tables2_pagination = self._has_crud_attr('tables2_pagination')

        self.permission_required = self._has_crud_attr('permission_required')
        self.permissions = self._has_crud_attr('permissions')
        self.login_required = self._has_crud_attr('login_required')

        self.custom_templates = self._has_crud_attr('custom_templates')
        self.custom_queryset = self._has_crud_attr('custom_queryset')
        self.inlineformset = self.get_inlineformset

    @property
    def get_model_class(self):
        """Returns model class"""
        c = ContentType.objects.get(app_label=self.app, model=self.model)
        return c.model_class()

    def _has_crud_attr(self, attr):
        return getattr(self.crud, attr, None)

    def view_permission(self, view_type):
        if self.permissions:
            return self.permissions.get(view_type, None)
        else:
            return '{}.{}_{}'.format(self.app, self.model, view_type)

    @property
    def check_login_required(self):
        return True if self.login_required else False

    @property
    def check_permission_required(self):
        return True if self.permission_required else False

    @property
    def get_inlineformset(self):
        if self.crud.inlineformset:
            return self.crud.inlineformset().construct_formset()
        else:
            return None


class MetaCrudRegister(type):
    def __new__(cls, clsname, bases, attrs):
        newclass = super(
            MetaCrudRegister, cls
            ).__new__(cls, clsname, bases, attrs)

        if bases:
            if newclass.model:
                crudbuilder.register(newclass.model, newclass)
            else:
                msg = "No model defined in {} class".format(newclass)
                raise NotModelException(msg)
        return newclass


class BaseCrudBuilder(with_metaclass(MetaCrudRegister)):
    model = None
    inlineformset = None

@@ -1,12 +1,94 @@
+import django%0Atry:%0A    from django.apps import apps%0Aexcept ImportError:%0A    pass%0A%0A
 import crudb
@@ -1442,16 +1442,33 @@
 lass%22%22%22%0A
+        try:%0A    
         
@@ -1533,16 +1533,306 @@
 .model)%0A
+        except ContentType.DoesNotExist:%0A            # try another kind of resolution%0A            # fixes a situation where a proxy model is defined in some external app.%0A            if django.VERSION %3E= (1, 7):%0A                return apps.get_model(self.app, self.model)%0A        else:%0A    
         

Fix for proxy model defined in external app.