Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove singleton behaviour, hard dependency on django.contrib.sites #21

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions breadcrumbs/breadcrumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,11 @@ def __unicode__(self):
def __repr__(self):
return u"Breadcrumb <%s,%s>" % (self.name, self.url)


class Singleton(object):

__instance__ = None

def __new__(cls, *a, **kw):
if Singleton.__instance__ is None:
Singleton.__instance__ = object.__new__(cls, *a, **kw)
cls._Singleton__instance = Singleton.__instance__
return Singleton.__instance__

def _drop_it(self):
Singleton.__instance__ = None


class Breadcrumbs(Singleton):
class Breadcrumbs(object):
"""
Breadcrumbs maintain a list of breadcrumbs that you can get interating with
class or with get_breadcrumbs().
"""
__bds = []
__autohome = getattr(settings, 'BREADCRUMBS_AUTO_HOME', False)
__urls = []
__started = False

def __call__(self, *args, **kwargs):
if not len(args) and not len(kwargs):
Expand Down Expand Up @@ -128,9 +109,7 @@ def __init__(self, *a, **kw):
Call validate and if ok, call fill bd
"""
super(Breadcrumbs, self).__init__(*a, **kw)
if not self.__started:
self._clean()
self.__started = True
self._clean()
if a or kw:
self._add(*a, **kw)

Expand Down
15 changes: 10 additions & 5 deletions breadcrumbs/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@
from django.conf import settings
from django.http import Http404
from .breadcrumbs import Breadcrumbs
from .views import flatpage

try:
from django.utils.deprecation import MiddlewareMixin
except ImportError: # Django < 1.10
# Works perfectly for everyone using MIDDLEWARE_CLASSES
MiddlewareMixin = object

class BreadcrumbsMiddleware(object):

class BreadcrumbsMiddleware(MiddlewareMixin):
def process_request(self, request):
request.breadcrumbs = Breadcrumbs()
request.breadcrumbs._clean()
if not hasattr(request, 'breadcrumbs'):
request.breadcrumbs = Breadcrumbs()


class FlatpageFallbackMiddleware(object):
class FlatpageFallbackMiddleware(MiddlewareMixin):
def process_response(self, request, response):
# do nothing if flatpages middleware isn't enabled, also if response
# code isn't 404.
if response.status_code != 404:
return response
try:
from .views import flatpage
return flatpage(request, request.path_info)
# Return the original response if any errors happened. Because this
# is a middleware, we can't assume the errors will be caught elsewhere.
Expand Down