Package: django-stronghold
StrongholdGet inside your stronghold and make all your Django views default login_requiredStronghold is a very small and easy to use django app that makes all your Django project default to require login for all of your views.WARNING: still in development, so some of the DEFAULTS and such will be changing without notice.InstallationInstall via pip.pipinstalldjango-strongholdAdd stronghold to your INSTALLED_APPS in your Django settings fileINSTALLED_APPS=(#...'stronghold',)Then add the stronghold middleware to your MIDDLEWARE_CLASSES in your Django settings fileMIDDLEWARE_CLASSES=(#...'stronghold.middleware.LoginRequiredMiddleware',)UsageIf you followed the installation instructions now all your views are defaulting to require a login.
To make a view public again you can use the public decorator provided instronghold.decoratorslike so:For function based viewsfromstronghold.decoratorsimportpublic@publicdefsomeview(request):# do some work#...For class based views (decorator)fromdjango.utils.decoratorsimportmethod_decoratorfromstronghold.decoratorsimportpublicclassSomeView(View):defget(self,request,*args,**kwargs):# some view logic#...@method_decorator(public)defdispatch(self,*args,**kwargs):returnsuper(SomeView,self).dispatch(*args,**kwargs)For class based views (mixin)fromstronghold.viewsimportStrongholdPublicMixinclassSomeView(StrongholdPublicMixin,View):passConfiguration (optional)STRONGHOLD_DEFAULTSUse Strongholds defaults in addition to your own settings.Default:STRONGHOLD_DEFAULTS=TrueYou can add a tuple of url regexes in your settings file with theSTRONGHOLD_PUBLIC_URLSsetting. Any url that matches against these patterns
will be made public without using the@publicdecorator.STRONGHOLD_PUBLIC_URLSDefault:STRONGHOLD_PUBLIC_URLS=()If STRONGHOLD_DEFAULTS is True STRONGHOLD_PUBLIC_URLS contains:(r'^%s.+$'%settings.STATIC_URL,r'^%s.+$'%settings.MEDIA_URL,)When settings.DEBUG = True. This is additive to your settings to support serving
Static files and media files from the development server. It does not replace any
settings you may have inSTRONGHOLD_PUBLIC_URLS.Note: Public URL regexes are matched againstHttpRequest.path_info.STRONGHOLD_PUBLIC_NAMED_URLSYou can add a tuple of url names in your settings file with theSTRONGHOLD_PUBLIC_NAMED_URLSsetting. Names in this setting will be reversed usingdjango.core.urlresolvers.reverseand any url matching the output of the reverse
call will be made public without using the@publicdecorator:Default:STRONGHOLD_PUBLIC_NAMED_URLS=()If STRONGHOLD_DEFAULTS is True additionally we search fordjango.contrib.authif it exists, we add the login and logout view names toSTRONGHOLD_PUBLIC_NAMED_URLSSTRONGHOLD_USER_TEST_FUNCOptionally, set STRONGHOLD_USER_TEST_FUNC to a callable to limit access to users
that pass a custom test. The callback receives aUserobject and should
returnTrueif the user is authorized. This is equivalent to decorating a
view withuser_passes_test.Example:STRONGHOLD_USER_TEST_FUNC=lambdauser:user.is_staffDefault:STRONGHOLD_USER_TEST_FUNC=lambdauser:user.is_authenticatedCompatiblityTested with:Django 1.8.xDjango 1.9.xDjango 1.10.xDjango 1.11.xDjango 2.0.xDjango 2.1.xDjango 2.2.xContributeSee CONTRIBUTING.md