本文整理匯總了Python中django.conf.settings.SITE_ID屬性的典型用法代碼示例。如果您正苦於以下問題:Python settings.SITE_ID屬性的具體用法?Python settings.SITE_ID怎麽用?Python settings.SITE_ID使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類django.conf.settings
的用法示例。
在下文中一共展示了settings.SITE_ID屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_current
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
def get_current(self):
"""
Returns the current ``UserSettings`` based on the SITE_ID in the
project's settings. The ``UserSettings`` object is cached the first
time it's retrieved from the database.
"""
from django.conf import settings
try:
site_id = settings.SITE_ID
except AttributeError:
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 to fix this error.')
try:
current_usersettings = USERSETTINGS_CACHE[site_id]
except KeyError:
current_usersettings = self.get(site_id=site_id)
USERSETTINGS_CACHE[site_id] = current_usersettings
return current_usersettings
示例2: render
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
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 ''
示例3: get_current
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
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."
)
示例4: create_organization_object
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
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
示例5: get_queryset
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
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
示例6: test_get_queryset
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
def test_get_queryset(self):
# Should return preferences without sites.
# Shouldn't fail on duplicates.
self.failIf(MyPreferences.singleton.get().sites.all(), "Without \
SITE_ID should not have any preferences with sites.")
# Should return preferences for current site.
# Shouldn't fail on duplicates.
settings.SITE_ID = 1
current_site = Site.objects.get_current()
obj = MyPreferences.singleton.get()
self.failUnlessEqual(current_site, obj.sites.get(), "With SITE_ID \
should have preferences for current site.")
# Should return preferences for current site.
# Shouldn't fail on duplicates.
settings.SITE_ID = 2
second_site, created = Site.objects.get_or_create(id=2)
obj = MyPreferences.singleton.get()
self.failUnlessEqual(second_site, obj.sites.get(), "With SITE_ID \
should have preferences for current site.")
示例7: render
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
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 ''
示例8: get_form_kwargs
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
def get_form_kwargs(self):
kwargs = super(SettingsView, self).get_form_kwargs()
# We're getting the current site settings in such a way as to
# avoid using any of the convenience methods that return the
# cached current UserSettings object, since is_valid may
# subsequently update the object we set here. (is_valid
# doesn't save it to the database, but because the cached
# object is updated, it still means that the object returned
# by those conveninence method, including the
# self.request.usersettings attribute set by the middleware,
# may not be in sync with the database any more.
kwargs['instance'], _ = SiteSettings.objects.get_or_create(
site_id=settings.SITE_ID,
defaults={'user': self.request.user}
)
return kwargs
示例9: sites
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
def sites(app_configs, **kwargs):
if models.Site.objects.count() == 0:
yield checks.Warning(
"Site not configured",
hint="Missing django site configuration",
id="promgen.W006",
)
for site in models.Site.objects.filter(
pk=settings.SITE_ID, domain__in=["example.com"]
):
yield checks.Warning(
"Promgen is configured to example domain",
obj=site,
hint="Please update from admin page /admin/",
id="promgen.W007",
)
# See notes in bootstrap.py
# @custom.register(checks.Tags.models)
示例10: default_site
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
def default_site():
"""
Wrapper aroung `django.conf.settings.SITE_ID` so we do not have to create a
new migration if we change how we get the default site ID
"""
return settings.SITE_ID
示例11: default_site
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
def default_site():
"""Returns the default site instance if Django settings defines SITE_ID, else None
Tech debt note: Open edX monkeypatches django.contrib.sites to override
behavior for getting the current site.
"""
if getattr(settings, 'SITE_ID', ''):
return Site.objects.get(pk=settings.SITE_ID)
return None
示例12: get_site_for_course
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
def get_site_for_course(course_id):
"""
Given a course, return the related site or None
For standalone mode, will always return the site
For multisite mode, will return the site if there is a mapping between the
course and the site. Otherwise `None` is returned
# Implementation notes
There should be only one organization per course.
TODO: Figure out how we want to handle ``DoesNotExist``
whether to let it raise back up raw or handle with a custom exception
"""
if figures.helpers.is_multisite():
org_courses = organizations.models.OrganizationCourse.objects.filter(
course_id=str(course_id))
if org_courses:
# Keep until this assumption analyzed
msg = 'Multiple orgs found for course: {}'
assert org_courses.count() == 1, msg.format(course_id)
first_org = org_courses.first().organization
if hasattr(first_org, 'sites'):
msg = 'Must have one and only one site. Org is "{}"'
assert first_org.sites.count() == 1, msg.format(first_org.name)
site = first_org.sites.first()
else:
site = None
else:
# We don't want to make assumptions of who our consumers are
# TODO: handle no organizations found for the course
site = None
else:
# Operating in single site / standalone mode, return the default site
site = Site.objects.get(id=settings.SITE_ID)
return site
示例13: setUp
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
def setUp(self):
Site.objects.get_or_create(id=settings.SITE_ID, domain='example.com', name='example.com')
self.usersettings_model.objects.create(**self.usersettings_data)
示例14: test_request
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
def test_request(self):
""" Makes sure that the request has correct `usersettings` attribute. """
middleware = CurrentUserSettingsMiddleware()
request = HttpRequest()
middleware.process_request(request)
self.assertEqual(request.usersettings.site.id, settings.SITE_ID)
示例15: setUp
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import SITE_ID [as 別名]
def setUp(self):
Site.objects.get_or_create(id=settings.SITE_ID, domain='example.com', name='example.com')