import copy

__author__ = 'q19420'
from django.conf import settings
from django.contrib.admin import ModelAdmin
from django.contrib import admin

if "guardian" in settings.INSTALLED_APPS:
    g_is_guardian_included = True
    from guardian.admin import GuardedModelAdmin


class AdminRegister(object):
    def __init__(self, parent_admin_list=[]):
        super(AdminRegister, self).__init__()
        self.parent_admin_list = parent_admin_list
        self.base_model_admin = ModelAdmin
        self.admin_class_attributes = {}

    def get_admin_class(self, class_instance):
        if self.admin_list is None:
            pass

    def get_valid_admin_class_with_list(self, class_inst):
        #print admin_list
        copied_admin_list = copy.copy(self.parent_admin_list)
        copied_admin_list.append(self.base_model_admin)
        #print ModelAdmin
        #print final_parents
        admin_class = type(class_inst.__name__ + "Admin", tuple(copied_admin_list), self.admin_class_attributes)
        return admin_class

    def register(self, class_inst, list_display=None, search_fields=None):
        self.admin_class_attributes = {}
        if not (list_display is None):
            self.admin_class_attributes["list_display"] = list_display
        if not (search_fields is None):
            self.admin_class_attributes["list_display"] = search_fields
        admin_class = self.get_valid_admin_class_with_list(class_inst)
        admin.site.register(class_inst, admin_class)

    def register_with_all_in_list_display(self, class_inst):
        self.admin_class_attributes["list_display"] = []
        for field in class_inst.__dict__['_meta'].fields:
            self.admin_class_attributes["list_display"].append(field.name)
        admin_class = self.get_valid_admin_class_with_list(class_inst)
        admin.site.register(class_inst, admin_class)
        self.admin_class_attributes = {}
@@ -4,16 +4,31 @@
 ort copy
+%0Aimport inspect
 %0A%0A__auth
@@ -281,16 +281,428 @@
 Admin%0A%0A%0A
+def register_admin_without_duplicated_register(class_inst, admin_class, admin_site=admin.site):%0A    try:%0A        if not (class_inst in admin_site._registry):%0A            admin_site.register(class_inst, admin_class)%0A    except Exception, e:%0A        if True:  # not (' is already registered' in e.message):%0A            print class_inst, admin_class%0A            import traceback%0A            traceback.print_exc()%0A%0A%0A
 class Ad
@@ -1753,36 +1753,37 @@
 attributes%5B%22
-list_display
+search_fields
 %22%5D = search_
@@ -1864,35 +1864,58 @@
 st)%0A        
-admin.site.
+register_admin_without_duplicated_
 register(cla
@@ -2055,16 +2055,74 @@
 %22%5D = %5B%5D%0A
+        self.admin_class_attributes%5B%22search_fields%22%5D = %5B%5D%0A
         
@@ -2246,16 +2246,92 @@
 d.name)%0A
+            self.admin_class_attributes%5B%22search_fields%22%5D.append(field.name)%0A
         
@@ -2405,19 +2405,42 @@
     
-admin.site.
+register_admin_without_duplicated_
 regi
@@ -2473,44 +2473,730 @@
 ss)%0A
+%0A
     
-    self.admin_class_attributes = %7B%7D
+def class_enumerator(self, module_instance, exclude_name_list):%0A        for name, obj in inspect.getmembers(module_instance):%0A            if inspect.isclass(obj):%0A                if name in exclude_name_list:%0A                    continue%0A                yield obj%0A%0A    def register_all_model(self, module_instance, exclude_name_list=%5B%5D):%0A        %22%22%22%0A        :param module_instance: mostly the models module%0A        :param exclude_name_list: class does not need to register or is already registered%0A        :param admin_class_list:%0A        :return: N/A%0A        %22%22%22%0A        for class_instance in self.class_enumerator(module_instance, exclude_name_list):%0A            self.register_with_all_in_list_display(class_instance)%0A%0A

Fix register issues.