Exemple #1
0
def new_template(blog_id, tpl_type):
    with db.atomic() as txn:
        user = auth.is_logged_in(request)
        blog = Blog.load(blog_id)
        permission = auth.is_blog_designer(user, blog)

        auth.check_template_lock(blog)

        mappings_index = template_mapping_index.get(tpl_type, None)
        if mappings_index is None:
            raise Exception('Mapping type not found')

        template = Template(
            blog=blog,
            theme=blog.theme,
            template_type=tpl_type,
            publishing_mode=publishing_mode.do_not_publish,
            body='',
        )
        template.save(user)
        template.title = 'Untitled Template #{}'.format(template.id)
        template.save(user)

        if tpl_type != template_type.media:

            new_template_mapping = TemplateMapping(
                template=template,
                is_default=True,
                path_string="'" + utils.create_basename(template.title, blog) +
                "'")

            new_template_mapping.save()
            from core.cms import fileinfo
            fileinfo.build_mapping_xrefs((new_template_mapping, ))

    from settings import BASE_URL
    redirect(BASE_URL + '/template/{}/edit'.format(template.id))
Exemple #2
0
def theme_install_to_blog(installed_theme, blog):

    json_obj = json.loads(installed_theme.json)
    templates = json_obj["data"]
    kvs = json_obj["kv"]

    for t in templates:

        template = templates[t]["template"]
        table_obj = Template()

        for name in table_obj._meta.fields:
            if name not in ("id"):
                setattr(table_obj, name, template[name])

        table_obj.theme = installed_theme
        table_obj.blog = blog
        table_obj.save()

        mappings = templates[t]["mapping"]

        for mapping in mappings:
            mapping_obj = TemplateMapping()

            for name in mapping_obj._meta.fields:
                if name not in ("id"):
                    setattr(mapping_obj, name, mappings[mapping][name])

            mapping_obj.template = table_obj
            mapping_obj.save()

    kv_index = {}
    kx = System()

    for kv in kvs:
        kv_current = kvs[kv]
        new_kv = kx.add_kv(**kv_current)
        kv_index[kv_current['id']] = new_kv.id

    for kv in kv_index:
        kv_current = kv
        new_kv_value = kv_index[kv]

        kv_to_change = KeyValue.get(KeyValue.id == new_kv_value)

        parent = kv_to_change.__dict__['_data']['parent']

        if parent is None:
            continue

        kv_to_change.parent = kv_index[parent]
        kv_to_change.save()

    from core import cms

    # TODO: use purge_blog instead

    # for n in blog.pages():
    cms.build_pages_fileinfos(blog.pages())

    # for n in blog.index_templates:
    cms.build_indexes_fileinfos(blog.index_templates)