Python django.contrib() Examples
The following are 11
code examples of django.contrib().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django
, or try the search function
.
Example #1
Source File: tests.py From django-seo with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_sites(self): """ Tests the django.contrib.sites support. A separate metadata definition is used, WithSites, which has turned on sites support. """ path = "/abc/" site = Site.objects.get_current() path_metadata = WithSites._meta.get_model('path').objects.create(_site=site, title="Site Path title", _path=path) self.assertEqual(seo_get_metadata(path, name="WithSites").title.value, 'Site Path title') # Metadata with site=null should work path_metadata._site_id = None path_metadata.save() self.assertEqual(seo_get_metadata(path, name="WithSites").title.value, 'Site Path title') # Metadata with an explicitly wrong site should not work path_metadata._site_id = site.id + 1 path_metadata.save() self.assertEqual(seo_get_metadata(path, name="WithSites").title.value, None)
Example #2
Source File: adminlte_menu.py From django-adminlte-ui with MIT License | 6 votes |
def get_admin_site(current_app): """ Method tries to get actual admin.site class, if any custom admin sites were used. Couldn't find any other references to actual class other than in func_closer dict in index() func returned by resolver. """ try: resolver_match = resolve(reverse('%s:index' % current_app)) # Django 1.9 exposes AdminSite instance directly on view function if hasattr(resolver_match.func, 'admin_site'): return resolver_match.func.admin_site for func_closure in resolver_match.func.__closure__: if isinstance(func_closure.cell_contents, AdminSite): return func_closure.cell_contents except: pass from django.contrib import admin return admin.site
Example #3
Source File: django_app.py From scout_apm_python with MIT License | 6 votes |
def drf_router(): """ DRF Router as a lazy object because it needs to import User model which can't be done until after django.setup() """ from django.contrib.auth.models import User from rest_framework import routers from rest_framework import serializers from rest_framework import viewsets class UserSerializer(serializers.Serializer): id = serializers.IntegerField(label="ID", read_only=True) username = serializers.CharField(max_length=200) class UserViewSet(viewsets.ModelViewSet): queryset = User.objects.all() serializer_class = UserSerializer router = routers.SimpleRouter() router.register(r"users", UserViewSet) return router
Example #4
Source File: django_app.py From scout_apm_python with MIT License | 6 votes |
def tastypie_api(): """ Tastypie API as a lazy object because it needs to import User model which can't be done until after django.setup() """ from django.contrib.auth.models import User try: from tastypie.api import Api as TastypieApi from tastypie.resources import ModelResource as TastypieModelResource except ImportError: return None class UserResource(TastypieModelResource): class Meta: queryset = User.objects.all() allowed_methods = ["get"] api = TastypieApi(api_name="v1") api.register(UserResource()) return api
Example #5
Source File: utils.py From oxidizr with GNU General Public License v2.0 | 5 votes |
def email(recipient, context, template_name, sender=None): if not settings.MANDRILL_API_KEY or settings.MANDRILL_API_KEY == 'None': return current_site = Site.objects.get_current() context['domain'] = current_site.domain User = django.contrib.auth.get_user_model() # The recipient list can be a list of User model instances or Email Address string # Which means you could pass a User model QuerySet as recipient # If a recipient is User model instance then simple convert to Email Address string recipient_email_list = [] for rec in recipient: if isinstance(rec, User): if rec.first_name and rec.last_name: recipient_email_list.append('"%s %s" <%s>' % (rec.first_name, rec.last_name, rec.email)) elif rec.first_name: recipient_email_list.append('"%s" <%s>' % (rec.first_name, rec.email)) else: recipient_email_list.append('%s' % rec.email) else: recipient_email_list.append(rec) mail.send( sender=sender or settings.DEFAULT_FROM_EMAIL, recipients=recipient_email_list, template=template_name, context=context, priority=PRIORITY.now )
Example #6
Source File: utils.py From django-seo with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _get_seo_content_types(seo_models): """ Returns a list of content types from the models defined in settings (SEO_MODELS) """ from django.contrib.contenttypes.models import ContentType try: return [ContentType.objects.get_for_model(m).id for m in seo_models] except: # previously caught DatabaseError # Return an empty list if this is called too early return []
Example #7
Source File: utils.py From django-seo with BSD 3-Clause "New" or "Revised" License | 5 votes |
def register_model_in_admin(model, admin_class=None): """ Register model in Django admin interface """ from django.contrib import admin admin.site.register(model, admin_class) _reload_urlconf()
Example #8
Source File: tests.py From django-seo with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_default_fallback(self): """ Tests the ability to use the current Site name as a default fallback. """ from django.contrib.sites.models import Site site = Site.objects.get_current() self.assertEqual(site.name, self.context.title.value)
Example #9
Source File: test_django.py From openapi-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
def django_settings(self): import django from django.conf import settings from django.contrib import admin from django.urls import path if settings.configured: return settings.configure( ALLOWED_HOSTS=[ 'testserver', ], INSTALLED_APPS=[ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.messages', 'django.contrib.sessions', ], MIDDLEWARE=[ 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', ] ) django.setup() settings.ROOT_URLCONF = ( path('admin/', admin.site.urls), )
Example #10
Source File: tests.py From django-seo with BSD 3-Clause "New" or "Revised" License | 4 votes |
def test_subdomains(self): path = '/abc/' subdomain = 'msk' path_metadata = WithSubdomains._meta.get_model('path').objects.create( _subdomain='msk', title='German Path title', _path=path ) self.assertEqual( seo_get_metadata(path, name='WithSubdomains', subdomain=subdomain).title.value, 'German Path title') path_metadata._subdomain = 'spb' path_metadata.save() self.assertEqual(seo_get_metadata(path, name='WithSubdomains', subdomain=subdomain).title.value, None) # # FUTURE feature # # def test_redirect(self): # """ Tests django.contrib.redirect support, automatically adding redirects for new paths. # """ # old_path = "/abc/" # new_path = "/new-path/" # # # Check that the redirect doesn't already exist # self.assertEqual(Redirect.objects.filter(old_path=old_path, new_path=new_path).count(), 0) # # path_metadata = WithRedirect._meta.get_model('path').objects.create(title="A Path title", _path=old_path) # self.assertEqual(seo_get_metadata(old_path, name="WithRedirect").title.value, 'A Path title') # # # Rename the path # path_metadata._path = new_path # path_metadata.save() # self.assertEqual(seo_get_metadata(old_path, name="WithRedirect").title.value, None) # self.assertEqual(seo_get_metadata(new_path, name="WithRedirect").title.value, 'A Path title') # # # Check that a redirect was created # self.assertEqual(Redirect.objects.filter(old_path=old_path, new_path=new_path).count(), 1) # # def test_redirect_with_sites(self): # """ Tests django.contrib.redirect support, automatically adding redirects for new paths. # """ # old_path = "/abc/" # new_path = "/new-path/" # site = Site.objects.get_current() # # # Check that the redirect doesn't already exist # self.assertEqual(Redirect.objects.filter(old_path=old_path, new_path=new_path, site=site).count(), 0) # # path_metadata = WithRedirectSites._meta.get_model('path').objects.create(title="A Path title", _path=old_path, _site=site) # self.assertEqual(seo_get_metadata(old_path, name="WithRedirectSites").title.value, 'A Path title') # # # Rename the path # path_metadata._path = new_path # path_metadata.save() # self.assertEqual(seo_get_metadata(old_path, name="WithRedirectSites").title.value, None) # self.assertEqual(seo_get_metadata(new_path, name="WithRedirectSites").title.value, 'A Path title') # # # Check that a redirect was created # self.assertEqual(Redirect.objects.filter(old_path=old_path, new_path=new_path, site=site).count(), 1)
Example #11
Source File: adminlte_menu.py From django-adminlte-ui with MIT License | 4 votes |
def get_menu(context, request, position='left'): """ :type request: WSGIRequest """ if not isinstance(request, HttpRequest): return None use_custom_menu = get_adminlte_option('USE_CUSTOM_MENU') if use_custom_menu.get('USE_CUSTOM_MENU', '0') == '1' and use_custom_menu.get('valid') is True: return get_custom_menu(request, position) if position != 'left': return [] # Django 1.9+ available_apps = context.get('available_apps') if not available_apps: # Django 1.8 on app index only available_apps = context.get('app_list') # Django 1.8 on rest of the pages if not available_apps: try: from django.contrib import admin template_response = get_admin_site(request.current_app).index( request) available_apps = template_response.context_data['app_list'] except Exception: pass if not available_apps: logging.warn('adminlteui was unable to retrieve apps list for menu.') for app in available_apps: if app.get('app_label') == 'django_admin_settings': if request.user.has_perm('django_admin_settings.add_options') or \ request.user.has_perm( 'django_admin_settings.change_options'): app.get('models').insert(0, { 'name': _('General Options'), 'object_name': 'Options', 'perms': { 'add': True, 'change': True, 'delete': True, 'view': True }, 'admin_url': reverse( 'admin:general_option'), 'view_only': False }) else: for model in app.get('models', []): model['icon'] = get_adminlte_settings() \ .get('icons', {}).get(app['app_label'], {}).get( model['name'].lower()) # return MenuManager(available_apps, context, request) return available_apps