def save(request, user, cms_template): 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.save() 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.'.format( mapping_id, template_mapping.path_string)) else: if _forms.getunicode(n) != template_mapping.path_string: template_mapping.path_string = _forms.getunicode(n) # need to check for mapping validation # if invalid, return some kind of warning # not an exception per se? # template_mapping.save() mappings.append(template_mapping) for n in mappings: n.save() status += " Mapping #{} ({}) rebuilt.".format(n.id, n.path_string) build_mapping_xrefs(mappings) # TODO: eventually everything after this will be removed b/c of AJAX save tags = template_tags(template_id=cms_template.id, user=user) if int(_forms.getunicode('save')) == 2: from core import cms for f in cms_template.fileinfos_published: cms.push_to_queue( job_type=f.template_mapping.template.template_type, blog=cms_template.blog, site=cms_template.blog.site, data_integer=f.id) status += " {} files regenerated from template.".format( cms_template.fileinfos_published.count()) logger.info("Template {} edited by user {}.".format( cms_template.for_log, user.for_log)) return status
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