<commit_before>from django.conf import settings


class DjangoModelAdminLoader(object):
    def __init__(self, root_endpoint, admin_site):
        self.root_endpoint = root_endpoint
        self.admin_site = admin_site
    
    def get_logger(self):
        return self.root_endpoint.get_logger()
    
    def register_resources(self):
        for model, admin_model in self.admin_site._registry.iteritems():
            if model in self.root_endpoint.registry:
                continue
            resource_class = self.generate_resource(admin_model)
            resource = self.root_endpoint.register(model, resource_class)
            self.register_inlines(admin_model, resource)
    
    def generate_resource(self, admin_model):
        from django.contrib.admin import ModelAdmin
        if not isinstance(admin_model, ModelAdmin):
            return
        
        from hyperadmin.resources.models import ModelResource
        from django import forms
        if admin_model.fieldsets:
            mfields = list()
            for section, params in admin_model.fieldsets:
                mfields.extend(params['fields'])
        else:
            mfields = admin_model.fields
        class GeneratedModelResource(ModelResource):
            #raw_id_fields = ()
            fields = mfields
            exclude = admin_model.exclude
            #fieldsets = None
            #filter_vertical = ()
            #filter_horizontal = ()
            #radio_fields = {}
            #prepopulated_fields = {}<commit_msg>more error handling when loading models from admin site<commit_after>from django.conf import settings
from django.db import models


class DjangoModelAdminLoader(object):
    def __init__(self, root_endpoint, admin_site):
        self.root_endpoint = root_endpoint
        self.admin_site = admin_site
    
    def get_logger(self):
        return self.root_endpoint.get_logger()
    
    def register_resources(self):
        for model, admin_model in self.admin_site._registry.iteritems():
            if not isinstance(model, models.Model):
                continue
            if model in self.root_endpoint.registry:
                continue
            resource_class = self.generate_resource(admin_model)
            try:
                resource = self.root_endpoint.register(model, resource_class)
                self.register_inlines(admin_model, resource)
            except Exception as error:
                self.get_logger().exception('Could not autoload: %s' % admin_model)
    
    def generate_resource(self, admin_model):
        from django.contrib.admin import ModelAdmin
        if not isinstance(admin_model, ModelAdmin):
            return
        
        from hyperadmin.resources.models import ModelResource
        from django import forms
        if admin_model.fieldsets:
            mfields = list()
            for section, params in admin_model.fieldsets:
                mfields.extend(params['fields'])
        else:
            mfields = admin_model.fields
        class GeneratedModelResource(ModelResource):
            #raw_id_fields = ()
            fields = mfields
            exclude = admin_model.exclude
            #fieldsets = None
            #filter_vertical = ()
            #filter_horizontal = ()
            #radio_fields = {}
            #prepopulated_fields = {}
{'fraction_non_alphanumeric': 0.07035928143712575, 'fraction_numerical': 0.0, 'mean_word_length': 2.1370892018779344, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6705453', 'n_tokens_mistral': 834, 'n_tokens_neox': 817, 'n_words': 193}