我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.conf.settings.SITE_ID。
def login(request): provider_list = [] for provider in get_providers(): try: # social_app??? provider?? ?? ?????. ??? ??? ?? ??? ??? ???????. # ??? Provider? ????? social\_app ???? ????????. provider.social_app = SocialApp.objects.get(provider=provider.id, sites=settings.SITE_ID) except SocialApp.DoesNotExist: # Provider ??? ?? ?? .social_app? None?? ?? provider.social_app = None provider_list.append(provider) return auth_login(request, form_class=LoginForm, extra_context={ 'provider_list': provider_list, })
def render(self, context): if 'request' in context: site_pk = get_current_site(context['request']).pk else: site_pk = settings.SITE_ID flatpages = FlatPage.objects.filter(sites__id=site_pk) # If a prefix was specified, add a filter if self.starts_with: flatpages = flatpages.filter( url__startswith=self.starts_with.resolve(context)) # If the provided user is not authenticated, or no user # was provided, filter the list to only public flatpages. if self.user: user = self.user.resolve(context) if not user.is_authenticated(): flatpages = flatpages.filter(registration_required=False) else: flatpages = flatpages.filter(registration_required=False) context[self.context_name] = flatpages return ''
def get_current(self, request=None): """ Returns the current Site based on the SITE_ID in the project's settings. If SITE_ID isn't defined, it returns the site with domain matching request.get_host(). The ``Site`` object is cached the first time it's retrieved from the database. """ from django.conf import settings if getattr(settings, 'SITE_ID', ''): site_id = settings.SITE_ID return self._get_site_by_id(site_id) elif request: return self._get_site_by_request(request) raise ImproperlyConfigured( "You're using the Django \"sites framework\" without having " "set the SITE_ID setting. Create a site in your database and " "set the SITE_ID setting or pass a request to " "Site.objects.get_current() to fix this error." )
def render(self, context): if 'request' in context: site_pk = get_current_site(context['request']).pk else: site_pk = settings.SITE_ID flatpages = FlatPage.objects.filter(sites__id=site_pk) # If a prefix was specified, add a filter if self.starts_with: flatpages = flatpages.filter( url__startswith=self.starts_with.resolve(context)) # If the provided user is not authenticated, or no user # was provided, filter the list to only public flatpages. if self.user: user = self.user.resolve(context) if not user.is_authenticated: flatpages = flatpages.filter(registration_required=False) else: flatpages = flatpages.filter(registration_required=False) context[self.context_name] = flatpages return ''
def _safe_get_siteid(site): global is_site_initializing, is_first_warn if not site: try: site = Site.objects.get_current() siteid = site.id except Exception as e: if is_site_initializing and isinstance(e, DatabaseError) and str(e).find('django_site') > -1: if is_first_warn: log.warn(str(e).strip()) is_first_warn = False log.warn('Can not get siteid; probably before syncdb; ROLLBACK') connection._rollback() else: is_site_initializing = False siteid = settings.SITE_ID else: is_site_initializing = False else: siteid = site.id return siteid
def get_queryset(self): """ Return the first preferences object for the current site. If preferences do not exist create it. """ queryset = super(SingletonManager, self).get_queryset() # Get current site current_site = None if getattr(settings, 'SITE_ID', None) is not None: current_site = Site.objects.get_current() # If site found limit queryset to site. if current_site is not None: queryset = queryset.filter(sites=settings.SITE_ID) try: queryset.get() except self.model.DoesNotExist: # Create object (for current site) if it doesn't exist. obj = self.model.objects.create() if current_site is not None: obj.sites.add(current_site) return queryset
def do_default_site(self, using=DEFAULT_DB): """ If no site was selected, selects the site used to create the article as the default site. Returns True if an additional save is required, False otherwise. """ if not len(self.sites.all()): sites = Site.objects.all() if hasattr(sites, 'using'): sites = sites.using(using) self.sites.add(sites.get(pk=settings.SITE_ID)) return True return False
def create_organization_object(org_name, creator, attrs={}): '''Creates an OrganizationProfile object without saving to the database''' name = attrs.get('name', org_name) first_name, last_name = _get_first_last_names(name) email = attrs.get('email', u'') new_user = User(username=org_name, first_name=first_name, last_name=last_name, email=email, is_active=True) new_user.save() registration_profile = RegistrationProfile.objects.create_profile(new_user) if email: site = Site.objects.get(pk=settings.SITE_ID) registration_profile.send_activation_email(site) profile = OrganizationProfile( user=new_user, name=name, creator=creator, created_by=creator, city=attrs.get('city', u''), country=attrs.get('country', u''), organization=attrs.get('organization', u''), home_page=attrs.get('home_page', u''), twitter=attrs.get('twitter', u'')) return profile
def update_site(sender, **kwargs): """ Update `Site` object matching `SITE_ID` setting with `SITE_DOMAIN` and `SITE_PORT` settings. """ Site = apps.get_model('sites', 'Site') domain = settings.SITE_DOMAIN if settings.SITE_PORT: domain += ':%s' % settings.SITE_PORT Site.objects.update_or_create( pk=settings.SITE_ID, defaults=dict( domain=domain, name=settings.SITE_NAME)) # We set an explicit pk instead of relying on auto-incrementation, # so we need to reset the database sequence. sequence_sql = connection.ops.sequence_reset_sql(no_style(), [Site]) if sequence_sql: cursor = connection.cursor() for command in sequence_sql: cursor.execute(command)
def test_extenders_on_root(self): self._update_page(1, navigation_extenders="TestMenu") menu_pool.clear(settings.SITE_ID) context = self.get_context() tpl = Template("{% load menu_tags %}{% show_menu 0 100 100 100 %}") tpl.render(context) nodes = context['children'] self.assertEqual(len(nodes), 2) self.assertEqual(len(nodes[0].children), 4) self.assertEqual(len(nodes[0].children[3].children), 1) self._update_page(1, in_navigation=False) menu_pool.clear(settings.SITE_ID) tpl = Template("{% load menu_tags %}{% show_menu %}") tpl.render(context) nodes = context['children'] self.assertEqual(len(nodes), 5)
def check_i18n(output): with output.section("Internationalization") as section: if isinstance(getattr(settings, 'CMS_LANGUAGES', {}), dict): section.success("New style CMS_LANGUAGES") else: section.warn("Old style (tuple based) CMS_LANGUAGES, please switch to the new (dictionary based) style") if getattr(settings, 'LANGUAGE_CODE', '').find('_') > -1: section.warn("LANGUAGE_CODE must contain a valid language code, not a locale (e.g.: 'en-us' instead of 'en_US'): '%s' provided" % getattr(settings, 'LANGUAGE_CODE', '')) for lang in getattr(settings, 'LANGUAGES', ()): if lang[0].find('_') > -1: section.warn("LANGUAGES must contain valid language codes, not locales (e.g.: 'en-us' instead of 'en_US'): '%s' provided" % lang[0]) if settings.SITE_ID == hash(settings.SITE_ID): for site, items in get_cms_setting('LANGUAGES').items(): if type(site) == int: for lang in items: if lang['code'].find('_') > -1: section.warn("CMS_LANGUAGES entries must contain valid language codes, not locales (e.g.: 'en-us' instead of 'en_US'): '%s' provided" % lang['code']) else: section.error("SITE_ID must be an integer, not %r" % settings.SITE_ID) for deprecated in ['CMS_HIDE_UNTRANSLATED', 'CMS_LANGUAGE_FALLBACK', 'CMS_LANGUAGE_CONF', 'CMS_SITE_LANGUAGES', 'CMS_FRONTEND_LANGUAGES']: if hasattr(settings, deprecated): section.warn("Deprecated setting %s found. This setting is now handled in the new style CMS_LANGUAGES and can be removed" % deprecated)
def get_languages(): if settings.SITE_ID != hash(settings.SITE_ID): raise ImproperlyConfigured( "SITE_ID must be an integer" ) if not settings.USE_I18N: return _ensure_languages_settings( {settings.SITE_ID: [{'code': settings.LANGUAGE_CODE, 'name': settings.LANGUAGE_CODE}]}) if settings.LANGUAGE_CODE not in dict(settings.LANGUAGES): raise ImproperlyConfigured( 'LANGUAGE_CODE "%s" must have a matching entry in LANGUAGES' % settings.LANGUAGE_CODE ) languages = getattr(settings, 'CMS_LANGUAGES', { settings.SITE_ID: [{'code': code, 'name': _(name)} for code, name in settings.LANGUAGES] }) if VERIFIED in languages: return languages return _ensure_languages_settings(languages)
def handle(self, *args, **options): try: from_site = int(options.get('from_site', None)) except Exception: from_site = settings.SITE_ID try: to_site = int(options.get('to_site', None)) except Exception: to_site = settings.SITE_ID try: assert from_site != to_site except AssertionError: raise CommandError('Sites must be different') from_site = self.get_site(from_site) to_site = self.get_site(to_site) pages = Page.objects.drafts().filter(site=from_site, depth=1) with transaction.atomic(): for page in pages: page.copy_page(None, to_site) self.stdout.write('Copied CMS Tree from SITE_ID {0} successfully to SITE_ID {1}.\n'.format(from_site.pk, to_site.pk))
def test_flatpage_admin_form_edit(self): """ Existing flatpages can be edited in the admin form without triggering the url-uniqueness validation. """ existing = FlatPage.objects.create( url="/myflatpage1/", title="Some page", content="The content") existing.sites.add(settings.SITE_ID) data = dict(url='/myflatpage1/', **self.form_data) f = FlatpageForm(data=data, instance=existing) self.assertTrue(f.is_valid(), f.errors) updated = f.save() self.assertEqual(updated.title, "A test page")