Пример #1
0
    def handle(self, *args, **options):
        try:
            sheet = Spreadsheet.objects.get(name=options["sheet"])
        except Spreadsheet.DoesNotExist:
            self.stderr.write(
                f"Sheet {options['sheet']} does not exist in the torquedata database."
            )
            return

        json_file = options["json_file"]
        template_file = options["template_file"]
        template_type = options["template_type"]

        if not json_file.exists():
            self.stderr.write(f'Path "{json_file}" does not exist.')
        if not json_file.is_file():
            self.stderr.write(f'Path "{json_file}" is not a file.')
        if not template_file.exists():
            self.stderr.write(f'Path "{template_file}" does not exist.')
        if not template_file.is_file():
            self.stderr.write(f'Path "{template_file}" is not a file.')

        template = Template(
            sheet=sheet,
            type=template_type,
            template_file=File(open(template_file, "r")),
            name=options["name"],
        )
        template.save()
        toc = TableOfContents(sheet=sheet,
                              name=options["name"],
                              json_file=json_file,
                              template=template)
        toc.save()
Пример #2
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))
Пример #3
0
    def _combine(self, registry: Registry, language: str, first: Message,
                 second: Message) -> Message:
        log.debug("Combining two templates:")
        log.debug("\t{}".format([c.value for c in first.template.components]))
        log.debug("\t{}".format([c.value for c in second.template.components]))

        shared_prefix = self._get_combinable_prefix(first, second)
        log.debug(f"Shared prefix is {[e.value for e in shared_prefix]}")
        combined = [c for c in first.template.components]

        # TODO At the moment everything is considered either positive or negative, which is sometimes weird.
        #  Add neutral sentences.
        conjunctions = registry.get("conjunctions").get(language, None)
        if not conjunctions:
            conjunctions = (defaultdict(lambda x: "NO-CONJUNCTION-DICT"), )

        if first.polarity != first.polarity:
            combined.append(
                Literal(
                    conjunctions.get("inverse_combiner",
                                     "MISSING-INVERSE-CONJUCTION")))
        else:
            combined.append(
                Literal(
                    conjunctions.get("default_combiner",
                                     "MISSING-DEFAULT-CONJUCTION")))
        combined.extend(second.template.components[len(shared_prefix):])
        log.debug("Combined thing is {}".format([c.value for c in combined]))
        new_message = Message(
            facts=first.facts +
            [fact for fact in second.facts if fact not in first.facts],
            importance_coefficient=first.importance_coefficient,
        )
        new_message.template = Template(combined)
        new_message.prevent_aggregation = True
        return new_message
Пример #4
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)