我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.apps.apps.is_installed()。
def login(self, **credentials): """ Sets the Factory to appear as if it has successfully logged into a site. Returns True if login is possible; False if the provided credentials are incorrect, or the user is inactive, or if the sessions framework is not available. """ from django.contrib.auth import authenticate user = authenticate(**credentials) if (user and user.is_active and apps.is_installed('django.contrib.sessions')): self._login(user) return True else: return False
def _get_sitemap_full_url(sitemap_url): if not django_apps.is_installed('django.contrib.sites'): raise ImproperlyConfigured("ping_google requires django.contrib.sites, which isn't installed.") if sitemap_url is None: try: # First, try to get the "index" sitemap URL. sitemap_url = reverse('django.contrib.sitemaps.views.index') except NoReverseMatch: try: # Next, try for the "global" sitemap URL. sitemap_url = reverse('django.contrib.sitemaps.views.sitemap') except NoReverseMatch: pass if sitemap_url is None: raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.") Site = django_apps.get_model('sites.Site') current_site = Site.objects.get_current() return 'http://%s%s' % (current_site.domain, sitemap_url)
def warningFlag(self): ''' When viewing individual event registrations, there are a large number of potential issues that can arise that may warrant scrutiny. This property just checks all of these conditions and indicates if anything is amiss so that the template need not check each of these conditions individually repeatedly. ''' if not hasattr(self,'invoice'): return True if apps.is_installed('danceschool.financial'): ''' If the financial app is installed, then we can also check additional properties set by that app to ensure that there are no inconsistencies ''' if self.invoice.revenueNotYetReceived != 0 or self.invoice.revenueMismatch: return True return ( self.priceWithDiscount != self.invoice.total or self.invoice.unpaid or self.invoice.outstandingBalance != 0 )
def warningFlag(self): ''' When viewing individual event registrations, there are a large number of potential issues that can arise that may warrant scrutiny. This property just checks all of these conditions and indicates if anything is amiss so that the template need not check each of these conditions individually repeatedly. ''' if not hasattr(self,'invoiceitem'): return True if apps.is_installed('danceschool.financial'): ''' If the financial app is installed, then we can also check additional properties set by that app to ensure that there are no inconsistencies ''' if self.invoiceitem.revenueNotYetReceived != 0 or self.invoiceitem.revenueMismatch: return True return ( self.price != self.invoiceitem.grossTotal or self.invoiceitem.invoice.unpaid or self.invoiceitem.invoice.outstandingBalance != 0 )
def resend_activation_email(self, request, queryset): """ Re-sends activation emails for the selected users. Note that this will *only* send activation emails for users who are eligible to activate; emails will not be sent to users whose activation keys have expired or who have already activated. """ if apps.is_installed('django.contrib.sites'): site = apps.get_model('sites', 'Site').objects.get_current() else: site = RequestSite(request) for profile in queryset: user = profile.user RegistrationProfile.objects.resend_activation_mail( user.email, site, request)
def items(self): if not django_apps.is_installed('django.contrib.sites'): raise ImproperlyConfigured("FlatPageSitemap requires django.contrib.sites, which isn't installed.") Site = django_apps.get_model('sites.Site') current_site = Site.objects.get_current() return current_site.flatpage_set.filter(registration_required=False)
def get_current_site(request): """ Checks if contrib.sites is installed and returns either the current ``Site`` object or a ``RequestSite`` object based on the request. """ # Imports are inside the function because its point is to avoid importing # the Site models when django.contrib.sites isn't installed. if apps.is_installed('django.contrib.sites'): from .models import Site return Site.objects.get_current(request) else: from .requests import RequestSite return RequestSite(request)
def ping_google(sitemap_url=None, ping_url=PING_URL): """ Alerts Google that the sitemap for the current site has been updated. If sitemap_url is provided, it should be an absolute path to the sitemap for this site -- e.g., '/sitemap.xml'. If sitemap_url is not provided, this function will attempt to deduce it by using urlresolvers.reverse(). """ if sitemap_url is None: try: # First, try to get the "index" sitemap URL. sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.index') except urlresolvers.NoReverseMatch: try: # Next, try for the "global" sitemap URL. sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap') except urlresolvers.NoReverseMatch: pass if sitemap_url is None: raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.") if not django_apps.is_installed('django.contrib.sites'): raise ImproperlyConfigured("ping_google requires django.contrib.sites, which isn't installed.") Site = django_apps.get_model('sites.Site') current_site = Site.objects.get_current() url = "http://%s%s" % (current_site.domain, sitemap_url) params = urlencode({'sitemap': url}) urlopen("%s?%s" % (ping_url, params))
def get_urls(self, page=1, site=None, protocol=None): # Determine protocol if self.protocol is not None: protocol = self.protocol if protocol is None: protocol = 'http' # Determine domain if site is None: if django_apps.is_installed('django.contrib.sites'): Site = django_apps.get_model('sites.Site') try: site = Site.objects.get_current() except Site.DoesNotExist: pass if site is None: raise ImproperlyConfigured( "To use sitemaps, either enable the sites framework or pass " "a Site/RequestSite object in your view." ) domain = site.domain if getattr(self, 'i18n', False): urls = [] current_lang_code = translation.get_language() for lang_code, lang_name in settings.LANGUAGES: translation.activate(lang_code) urls += self._urls(page, protocol, domain) translation.activate(current_lang_code) else: urls = self._urls(page, protocol, domain) return urls
def __init__(self): if not apps.is_installed('django.contrib.sites'): raise ImproperlyConfigured( "You cannot use RedirectFallbackMiddleware when " "django.contrib.sites is not installed." )
def static(path): global _static if _static is None: if apps.is_installed('django.contrib.staticfiles'): from django.contrib.staticfiles.templatetags.staticfiles import static as _static else: from django.templatetags.static import static as _static return _static(path)
def _session(self): """ Obtains the current session variables. """ if apps.is_installed('django.contrib.sessions'): engine = import_module(settings.SESSION_ENGINE) cookie = self.cookies.get(settings.SESSION_COOKIE_NAME) if cookie: return engine.SessionStore(cookie.value) else: s = engine.SessionStore() s.save() self.cookies[settings.SESSION_COOKIE_NAME] = s.session_key return s return {}
def ping_google(sitemap_url=None, ping_url=PING_URL): """ Alerts Google that the sitemap for the current site has been updated. If sitemap_url is provided, it should be an absolute path to the sitemap for this site -- e.g., '/sitemap.xml'. If sitemap_url is not provided, this function will attempt to deduce it by using urls.reverse(). """ if sitemap_url is None: try: # First, try to get the "index" sitemap URL. sitemap_url = reverse('django.contrib.sitemaps.views.index') except NoReverseMatch: try: # Next, try for the "global" sitemap URL. sitemap_url = reverse('django.contrib.sitemaps.views.sitemap') except NoReverseMatch: pass if sitemap_url is None: raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.") if not django_apps.is_installed('django.contrib.sites'): raise ImproperlyConfigured("ping_google requires django.contrib.sites, which isn't installed.") Site = django_apps.get_model('sites.Site') current_site = Site.objects.get_current() url = "http://%s%s" % (current_site.domain, sitemap_url) params = urlencode({'sitemap': url}) urlopen("%s?%s" % (ping_url, params))
def __init__(self, get_response=None): if not apps.is_installed('django.contrib.sites'): raise ImproperlyConfigured( "You cannot use RedirectFallbackMiddleware when " "django.contrib.sites is not installed." ) super(RedirectFallbackMiddleware, self).__init__(get_response)
def is_installed(appname): return appname in settings.INSTALLED_APPS
def is_installed(appname): return appname in settings.INSTALLED_APPS #or apps.is_installed(appname)
def installed(self): "A list of all the installed Spectator apps." return [app for app in self.all() if self.is_installed(app)]
def is_installed(self, app_name): "Is this Spectator app installed?" return apps.is_installed('spectator.%s' % app_name)
def is_enabled(self, app_name): """Determine if a particular Spectator app is installed and enabled. app_name is like 'events' or 'reading'. Usage: if is_enabled('events'): print("Events is enabled") Doesn't offer much over apps.is_installed() yet, but would let us add other conditions in future, like being able to enable/disable installed apps. """ return apps.is_installed('spectator.%s' % app_name)
def check_dependencies(self): """ Check that all things needed to run the admin have been correctly installed. The default implementation checks that admin and contenttypes apps are installed, as well as the auth context processor. """ if not apps.is_installed('django.contrib.admin'): raise ImproperlyConfigured( "Put 'django.contrib.admin' in your INSTALLED_APPS " "setting in order to use the admin application.") if not apps.is_installed('django.contrib.contenttypes'): raise ImproperlyConfigured( "Put 'django.contrib.contenttypes' in your INSTALLED_APPS " "setting in order to use the admin application.") try: default_template_engine = Engine.get_default() except Exception: # Skip this non-critical check: # 1. if the user has a non-trivial TEMPLATES setting and Django # can't find a default template engine # 2. if anything goes wrong while loading template engines, in # order to avoid raising an exception from a confusing location # Catching ImproperlyConfigured suffices for 1. but 2. requires # catching all exceptions. pass else: if ('django.contrib.auth.context_processors.auth' not in default_template_engine.context_processors): raise ImproperlyConfigured( "Enable 'django.contrib.auth.context_processors.auth' " "in your TEMPLATES setting in order to use the admin " "application.")
def populate(self): if ( self.request.user.has_perm('filer.change_folder') or self.request.user.has_perm('core.change_emailtemplate') or (apps.is_installed('djangocms_forms') and self.request.user.has_perm('djangocms_forms.export_formsubmission')) ): menu = self.toolbar.get_or_create_menu('core-content',_('Content')) if self.request.user.has_perm('filer.change_folder'): menu.add_link_item(_('Manage Uploaded Files'), reverse('admin:filer_folder_changelist')) if self.request.user.has_perm('core.change_emailtemplate'): menu.add_link_item(_('Manage Email Templates'), reverse('admin:core_emailtemplate_changelist')) if apps.is_installed('djangocms_forms') and self.request.user.has_perm('djangocms_forms.export_formsubmission'): menu.add_link_item(_('View/Export Survey Responses'), reverse('admin:djangocms_forms_formsubmission_changelist'))
def refundFlag(self): if ( not hasattr(self,'invoice') or self.invoice.adjustments != 0 or (apps.is_installed('danceschool.financial') and self.invoice.revenueRefundsReported != 0) ): return True return False