我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用wagtail.wagtailcore.models.Site()。
def get_sharing_url(page): """Get a sharing URL for the latest revision of a page, if available.""" url_parts = page.get_url_parts() if url_parts is None: # Page is not routable. return None site_id, root_url, page_path = url_parts site = Site.objects.get(id=site_id) try: sharing_site = site.sharing_site except SharingSite.DoesNotExist: # Site is not shared. return None return sharing_site.root_url + page_path
def ready(self): # patch Site and Page models here from wagtail.wagtailcore.models import AbstractPage, Page, Site from wagtail.wagtailcore.query import PageQuerySet from .manager import MultilingualPageManager # fix PageManager to inherit from MultilingualManager # since automatic manager patching no longer works (Django 1.10 and newer) AbstractPage.objects = MultilingualPageManager() AbstractPage.objects.contribute_to_class(AbstractPage, 'objects') Page.objects = MultilingualPageManager() Page.objects.contribute_to_class(Page, 'objects') page_patch = import_module('wagtail_translation.page_patch') site_patch = import_module('wagtail_translation.site_patch') query_patch = import_module('wagtail_translation.query_patch') for name in page_patch.__all__: setattr(Page, name, getattr(page_patch, name)) for name in site_patch.__all__: setattr(Site, name, getattr(site_patch, name)) for name in query_patch.__all__: setattr(PageQuerySet, name, getattr(query_patch, name))
def get_site_root_paths(): lang = get_language() cache_key = ROOT_PATHS_CACHE_KEY_FMT.format(lang) result = cache.get(cache_key) if result is None: result = [ (site.id, site.root_page.url_path, site.root_url) for site in Site.objects.select_related('root_page').order_by('-root_page__url_path') ] cache.set(cache_key, result, 3600) return result
def handle(self, **options): fixtures_dir = os.path.join(settings.BASE_DIR, 'base', 'fixtures') fixture_file = os.path.join(fixtures_dir, 'bakerydemo.json') # Wagtail creates default Site and Page instances during install, but we already have # them in the data load. Remove the auto-generated ones. if Site.objects.filter(hostname='localhost').exists(): Site.objects.get(hostname='localhost').delete() if Page.objects.filter(title='Welcome to your new Wagtail site!').exists(): Page.objects.get(title='Welcome to your new Wagtail site!').delete() call_command('loaddata', fixture_file, verbosity=0) print("Awesome. Your data is loaded! The bakery's doors are almost ready to open...")
def create_if_necessary(cls, *args, **kwargs): root = super().create_if_necessary(*args, **kwargs) # create default Site if it doesn't exist Site.objects.get_or_create( hostname='localhost', defaults={ 'root_page': root, 'is_default_site': True } ) return root
def site(): """ Returns the Wagtail test Site, as provided in the Django configuration settings """ from django.conf import settings from wagtail.wagtailcore.models import Site return Site.objects.get(id=settings.SITE_ID)