from django.contrib.admin.sites import AdminSite as DjangoAdminSite
from django.contrib.admin.sites import site as django_site


class HatbandAndDjangoRegistry(object):
    def __init__(self, site, default_site=None):
        if default_site is None:
            default_site = django_site
        super(HatbandAndDjangoRegistry, self).__init__()
        self._site = site
        self._registry = {}
        self.dicts = [self._registry, default_site._registry]

    def items(self):
        for d in self.dicts:
            for item in d.items():
                yield item

    def iteritems(self):
        return iter(self.items())

    def __contains__(self, k):
        for d in self.dicts:
            if k in d:
                return True
        return False


class AdminSite(DjangoAdminSite):
    def __init__(self, default_site=None, *args, **kwargs):
        if default_site is None:
            default_site = django_site
        super(AdminSite, self).__init__(*args, **kwargs)
        self._registry = HatbandAndDjangoRegistry(self,
                default_site=default_site)

    def get_urls(self):
        from django.conf.urls.defaults import patterns, url

        return patterns('',
            # Custom hatband Views here
        ) + super(AdminSite, self).get_urls()


site = AdminSite()

from django.contrib.admin.sites import AdminSite as DjangoAdminSite
from django.contrib.admin.sites import site as django_site


class AdminSite(DjangoAdminSite):
    def get_urls(self):
        from django.conf.urls.defaults import patterns, url

        return patterns('',
            # Custom hatband Views here
        ) + super(AdminSite, self).get_urls()


site = AdminSite()
site._registry = django_site._registry

Simplify this code and make sure AdminSite doesn't act like a singleton
Simplify this code and make sure AdminSite doesn't act like a singleton

 Create a faux-singleton for `AdminSite` and make sure it has a copy of
 all of the previously registered models.  This makes
 `armstrong.hatband.site` look just like `django.contrib.admin.site`.

  from django.contrib.admin.sites import AdminSite as DjangoAdminSite
  from django.contrib.admin.sites import site as django_site
  
  
- class HatbandAndDjangoRegistry(object):
-     def __init__(self, site, default_site=None):
-         if default_site is None:
-             default_site = django_site
-         super(HatbandAndDjangoRegistry, self).__init__()
-         self._site = site
-         self._registry = {}
-         self.dicts = [self._registry, default_site._registry]
- 
-     def items(self):
-         for d in self.dicts:
-             for item in d.items():
-                 yield item
- 
-     def iteritems(self):
-         return iter(self.items())
- 
-     def __contains__(self, k):
-         for d in self.dicts:
-             if k in d:
-                 return True
-         return False
- 
- 
  class AdminSite(DjangoAdminSite):
-     def __init__(self, default_site=None, *args, **kwargs):
-         if default_site is None:
-             default_site = django_site
-         super(AdminSite, self).__init__(*args, **kwargs)
-         self._registry = HatbandAndDjangoRegistry(self,
-                 default_site=default_site)
- 
      def get_urls(self):
          from django.conf.urls.defaults import patterns, url
  
          return patterns('',
              # Custom hatband Views here
          ) + super(AdminSite, self).get_urls()
  
  
  site = AdminSite()
+ site._registry = django_site._registry
  
Simplify this code and make sure AdminSite doesn't act like a singleton
## Code Before:
from django.contrib.admin.sites import AdminSite as DjangoAdminSite
from django.contrib.admin.sites import site as django_site


class HatbandAndDjangoRegistry(object):
    def __init__(self, site, default_site=None):
        if default_site is None:
            default_site = django_site
        super(HatbandAndDjangoRegistry, self).__init__()
        self._site = site
        self._registry = {}
        self.dicts = [self._registry, default_site._registry]

    def items(self):
        for d in self.dicts:
            for item in d.items():
                yield item

    def iteritems(self):
        return iter(self.items())

    def __contains__(self, k):
        for d in self.dicts:
            if k in d:
                return True
        return False


class AdminSite(DjangoAdminSite):
    def __init__(self, default_site=None, *args, **kwargs):
        if default_site is None:
            default_site = django_site
        super(AdminSite, self).__init__(*args, **kwargs)
        self._registry = HatbandAndDjangoRegistry(self,
                default_site=default_site)

    def get_urls(self):
        from django.conf.urls.defaults import patterns, url

        return patterns('',
            # Custom hatband Views here
        ) + super(AdminSite, self).get_urls()


site = AdminSite()

## Instruction:
Simplify this code and make sure AdminSite doesn't act like a singleton
## Code After:
from django.contrib.admin.sites import AdminSite as DjangoAdminSite
from django.contrib.admin.sites import site as django_site


class AdminSite(DjangoAdminSite):
    def get_urls(self):
        from django.conf.urls.defaults import patterns, url

        return patterns('',
            # Custom hatband Views here
        ) + super(AdminSite, self).get_urls()


site = AdminSite()
site._registry = django_site._registry
