def queue_index_actions(blog, include_manual=False): ''' Pushes to the publishing queue all the index pages for a given blog that are marked for Immediate publishing. :param blog: The blog object whose index templates will be pushed to the queue. :param include_manual: If set to True, all templates, including those set to the Manual publishing mode, will be pushed to the queue. Default is False, since those templates are not pushed in most publishing actions. ''' templates = blog.index_templates.select().where( Template.publishing_mode != publishing_mode.do_not_publish) if include_manual is False: templates = templates.select().where( Template.publishing_mode == publishing_mode.immediate) if templates.count() == 0: raise Template.DoesNotExist("No valid index templates exist for blog {}.".format( blog.for_log)) mappings = TemplateMapping.select().where(TemplateMapping.template << templates) fileinfos = FileInfo.select().where(FileInfo.template_mapping << mappings) for f in fileinfos: Queue.push(job_type=job_type.index, priority=1, blog=blog, site=blog.site, data_integer=f.id)
def queue_index_actions(blog, include_manual=False): ''' Pushes to the publishing queue all the index pages for a given blog that are marked for Immediate publishing. :param blog: The blog object whose index templates will be pushed to the queue. :param include_manual: If set to True, all templates, including those set to the Manual publishing mode, will be pushed to the queue. Default is False, since those templates are not pushed in most publishing actions. ''' templates = blog.index_templates.select().where( Template.publishing_mode != publishing_mode.do_not_publish) if include_manual is False: templates = templates.select().where( Template.publishing_mode == publishing_mode.immediate) if templates.count() == 0: raise Template.DoesNotExist( "No valid index templates exist for blog {}.".format(blog.for_log)) mappings = TemplateMapping.select().where( TemplateMapping.template << templates) fileinfos = FileInfo.select().where(FileInfo.template_mapping << mappings) for f in fileinfos: Queue.push(job_type=job_type.index, priority=1, blog=blog, site=blog.site, data_integer=f.id)
def queue_page_archive_actions(page): ''' Pushes to the publishing queue all the page archives for a given page object. :param page: The page object whose archives will be pushed to the publishing queue. ''' #=========================================================================== # NOTE: I tried to speed this up by checking the list of fileinfos # related to mappings for the page (if any), and then pushing those # if they exist, but I haven't seen evidence it does anything tangible # for performance. # I need to double-check that old mappings are in fact invalidated # when they are changed. #=========================================================================== archive_templates = page.blog.archive_templates tags = template_tags(page=page) for n in archive_templates: try: if n.publishing_mode != publishing_mode.do_not_publish: fileinfo_mappings = FileInfo.select().where( FileInfo.page == page, FileInfo.template_mapping << n.mappings) if fileinfo_mappings.count() == 0: fileinfo_mappings = build_archives_fileinfos_by_mappings( n, (page, )) if len(fileinfo_mappings) == 0: logger.info( 'No archive fileinfos could be built for page {} with template {}' .format(page.for_log, n.for_log)) else: for fileinfo_mapping in fileinfo_mappings: Queue.push(job_type=job_type.archive, blog=page.blog, site=page.blog.site, priority=7, data_integer=fileinfo_mapping.id) except Exception as e: from core.error import QueueAddError raise QueueAddError( 'Archive template {} for page {} could not be queued: '.format( n, page.for_log, e))
def queue_page_archive_actions(page): ''' Pushes to the publishing queue all the page archives for a given page object. :param page: The page object whose archives will be pushed to the publishing queue. ''' #=========================================================================== # NOTE: I tried to speed this up by checking the list of fileinfos # related to mappings for the page (if any), and then pushing those # if they exist, but I haven't seen evidence it does anything tangible # for performance. # I need to double-check that old mappings are in fact invalidated # when they are changed. #=========================================================================== archive_templates = page.blog.archive_templates tags = template_tags(page=page) for n in archive_templates: try: if n.publishing_mode != publishing_mode.do_not_publish: fileinfo_mappings = FileInfo.select().where(FileInfo.page == page, FileInfo.template_mapping << n.mappings) if fileinfo_mappings.count() == 0: fileinfo_mappings=build_archives_fileinfos_by_mappings(n,(page,)) if len(fileinfo_mappings)==0: logger.info('No archive fileinfos could be built for page {} with template {}'.format( page.for_log, n.for_log)) else: for fileinfo_mapping in fileinfo_mappings: Queue.push(job_type=job_type.archive, blog=page.blog, site=page.blog.site, priority=7, data_integer=fileinfo_mapping.id) except Exception as e: from core.error import QueueAddError raise QueueAddError('Archive template {} for page {} could not be queued: '.format( n, page.for_log, e))
def queue_archive_template_fast(template_id, action, pass_id=0): from core.models import Template, Queue template = Template.load(template_id) blog = template.blog from core import cms from core.libs.bottle import HTTPResponse r = HTTPResponse() fileinfos = FileInfo.select().where(FileInfo.template_mapping << template.mappings).paginate(pass_id, 50) # TODO: if action is fast and no fileinfos present, redirect to full rebuild? if fileinfos.count() > 0: r.body = "Adding {}".format(pass_id * 50) for f in fileinfos: Queue.push(job_type=cms.queue.job_type.archive, blog=blog, site=blog.site, data_integer=f.id) pass_id += 1 r.add_header('Refresh', "0;{}/template/{}/{}/fast/{}".format( BASE_PATH, template_id, action, pass_id)) else: r.body = "Queue insertion finished." if action == 'publish': redir = 'publish' else: redir = 'queue' r.add_header('Refresh', "0;{}/blog/{}/{}".format( BASE_PATH, blog.id, redir)) return r
def queue_ssi_actions(blog): ''' Pushes to the publishing queue all the SSIs for a given blog. :param blog: The blog object whose SSI templates will be pushed to the queue. ''' templates = blog.ssi_templates.select() if templates.count() == 0: return None for n in templates: for f in n.fileinfos: Queue.push(job_type=job_type.include, priority=10, blog=blog, site=blog.site, data_integer=f.id)
def queue_archive_template_all(template_id, action, pass_id=0): from core.models import Template, Queue template = Template.load(template_id) blog = template.blog from core.cms import fileinfo, queue from core.libs.bottle import HTTPResponse r = HTTPResponse() pages = blog.pages.published.paginate(pass_id, 50) if pages.count() > 0: r.body = "Adding {}".format(pass_id * 50) for f in fileinfo.build_archives_fileinfos_by_mappings(template, pages): Queue.push(job_type=queue.job_type.archive, blog=blog, site=blog.site, data_integer=f.id) pass_id += 1 r.add_header('Refresh', "0;{}/template/{}/{}/all/{}".format( BASE_PATH, template_id, action, pass_id)) else: r.body = "Queue insertion finished." if action == 'publish': redir = 'publish' else: redir = 'queue' r.add_header('Refresh', "0;{}/blog/{}/{}".format( BASE_PATH, blog.id, redir)) return r
def queue_archive_template_fast(template_id, action, pass_id=0): from core.models import Template, Queue template = Template.load(template_id) blog = template.blog from core import cms from core.libs.bottle import HTTPResponse r = HTTPResponse() fileinfos = FileInfo.select().where( FileInfo.template_mapping << template.mappings).paginate(pass_id, 50) # TODO: if action is fast and no fileinfos present, redirect to full rebuild? if fileinfos.count() > 0: r.body = "Adding {}".format(pass_id * 50) for f in fileinfos: Queue.push(job_type=cms.queue.job_type.archive, blog=blog, site=blog.site, data_integer=f.id) pass_id += 1 r.add_header( 'Refresh', "0;{}/template/{}/{}/fast/{}".format(BASE_PATH, template_id, action, pass_id)) else: r.body = "Queue insertion finished." if action == 'publish': redir = 'publish' else: redir = 'queue' r.add_header('Refresh', "0;{}/blog/{}/{}".format(BASE_PATH, blog.id, redir)) return r
def queue_ssi_actions(blog): ''' Pushes to the publishing queue all the SSIs for a given blog. :param blog: The blog object whose SSI templates will be pushed to the queue. ''' templates = blog.ssi_templates.select() if templates.count() == 0: return None for n in templates: for f in n.fileinfos: Queue.push( job_type=job_type.include, priority=10, blog=blog, site=blog.site, data_integer=f.id)
def queue_archive_template_all(template_id, action, pass_id=0): from core.models import Template, Queue template = Template.load(template_id) blog = template.blog from core.cms import fileinfo, queue from core.libs.bottle import HTTPResponse r = HTTPResponse() pages = blog.pages.published.paginate(pass_id, 50) if pages.count() > 0: r.body = "Adding {}".format(pass_id * 50) for f in fileinfo.build_archives_fileinfos_by_mappings( template, pages): Queue.push(job_type=queue.job_type.archive, blog=blog, site=blog.site, data_integer=f.id) pass_id += 1 r.add_header( 'Refresh', "0;{}/template/{}/{}/all/{}".format(BASE_PATH, template_id, action, pass_id)) else: r.body = "Queue insertion finished." if action == 'publish': redir = 'publish' else: redir = 'queue' r.add_header('Refresh', "0;{}/blog/{}/{}".format(BASE_PATH, blog.id, redir)) return r
def template_save(request, user, cms_template, blog=None): ''' Core logic for saving changes to a template. ''' # TODO: move the bulk of this into the actual model # the .getunicode stuff should be moved out, # make that part of the ui # we should just submit cms_template as self, # make whatever mods to it are needed in the ui func, # and perform the validation we did elsewhere, perhaps from core.cms import fileinfo, invalidate_cache from core.utils import is_blank from core.error import TemplateSaveException, PageNotChanged import datetime status = [] _forms = request.forms cms_template.title = _forms.getunicode('template_title') cms_template.body = _forms.getunicode('template_body') if is_blank(cms_template.title): cms_template.title = "New Template (#{})".format(cms_template.id) mode = _forms.getunicode('publishing_mode') if mode in publishing_mode.modes: cms_template.publishing_mode = mode else: raise TemplateSaveException("Invalid publishing mode selected.") cms_template.modified_date = datetime.datetime.utcnow() try: cms_template.save(user) except PageNotChanged as e: status.append("(Template unchanged.)") except Exception as e: raise e new_mappings = [] for n in _forms: if n.startswith('template_mapping_'): mapping_id = int(n[len('template_mapping_'):]) try: template_mapping = TemplateMapping.get( TemplateMapping.id == mapping_id) except TemplateMapping.DoesNotExist: raise TemplateSaveException( 'Template mapping with ID #{} does not exist.'.format( mapping_id)) else: if is_blank(_forms.getunicode(n)): raise TemplateSaveException( 'Template mapping #{} ({}) cannot be blank. Use None to specify no mapping.' .format(mapping_id, template_mapping.path_string)) else: if _forms.getunicode(n) != template_mapping.path_string: template_mapping.path_string = _forms.getunicode(n) new_mappings.append(template_mapping) for n in new_mappings: n.save() status.append("Mapping #{} ({}) rebuilt.".format(n.id, n.path_string)) if new_mappings: fileinfo.build_mapping_xrefs(new_mappings) build_action = "all" else: build_action = "fast" invalidate_cache() # TODO: eventually everything after this will be removed b/c of AJAX save # tags = template_tags(template_id=cms_template.id, user=user) save_action = _forms.getunicode('save') from core.libs.bottle import response from settings import BASE_URL from core.models import Queue x_open = False if int(save_action) in (2, 3): if cms_template.template_type == template_type.page: x_open = True response.add_header( 'X-Open', '{}/template/{}/queue/{}'.format(BASE_URL, cms_template.id, build_action)) if cms_template.template_type == template_type.archive: x_open = True response.add_header( 'X-Open', '{}/template/{}/queue/{}'.format(BASE_URL, cms_template.id, build_action)) if cms_template.template_type in (template_type.include, template_type.index): # I don't think this is needed anymore, we can remove it # TODO: test it # if new_mappings: # cms.build_archives_fileinfos_by_mappings(cms_template) for f in cms_template.fileinfos_published: Queue.push(job_type=f.template_mapping.template.template_type, blog=cms_template.blog, site=cms_template.blog.site, data_integer=f.id) status.append( "{} files regenerated from template and sent to publishing queue.". format(cms_template.fileinfos_published.count())) if blog is not None: blog.theme_modified = True blog.save() from core.log import logger logger.info("Template {} edited by user {}.".format( cms_template.for_log, user.for_log)) response.body = ' '.join(status) if x_open: return response else: return response.body
def queue_page_actions(pages, no_neighbors=False, no_archive=False): ''' Pushes a Page object along with all its related items into the queue for publication. This includes any archive indices associated with the page, and the page's next and previous entries in its respective categories. Note that this will only queue items that are actually set to be published. :param page: The Page object whose actions are to be queued. :param no_neighbors: Set to True to suppress generation of next/previous posts. Useful if you've loaded all the posts for a blog into a queue and don't need to have this performed here. :param no_archive: Set to True to suppress generation of archive pages associated with this page. Also useful for mass-queued actions. ''' if pages is None: return for page in pages: if page is None: continue try: blog, site = page.blog, page.blog.site for f in page.fileinfos: if f.template_mapping.template.publishing_mode != publishing_mode.do_not_publish: Queue.push(job_type=job_type.page, blog=blog, site=site, priority=8, data_integer=f.id) if no_archive is False: queue_page_archive_actions(page) if no_neighbors is False: next_page = page.next_page previous_page = page.previous_page if next_page is not None: for f in next_page.fileinfos: if f.template_mapping.template.publishing_mode != publishing_mode.do_not_publish: Queue.push(job_type=job_type.page, blog=blog, site=site, priority=8, data_integer=f.id) queue_page_archive_actions(next_page) if previous_page is not None: for f in previous_page.fileinfos: if f.template_mapping.template.publishing_mode != publishing_mode.do_not_publish: Queue.push(job_type=job_type.page, blog=blog, site=site, priority=8, data_integer=f.id) queue_page_archive_actions(previous_page) except OperationalError as e: raise e except Exception as e: from core.error import QueueAddError raise QueueAddError('Page {} could not be queued: '.format( page.for_log, e))