Exemplo n.º 1
0
    def handle(self, *args, **options):
        """Automatically called when the makeresources command is given."""
        base_path = settings.RESOURCE_GENERATION_LOCATION

        if options["resource_name"]:
            resources = [Resource.objects.get(name=options["resource_name"])]
        else:
            resources = Resource.objects.order_by("name")

        if options["resource_language"]:
            generation_languages = [options["resource_language"]]
        else:
            generation_languages = []
            for language_code, _ in settings.LANGUAGES:
                if language_code not in settings.INCONTEXT_L10N_PSEUDOLANGUAGES:
                    generation_languages.append(language_code)

        for resource in resources:
            print("Creating {}".format(resource.name))

            # TODO: Import repeated in next for loop, check alternatives
            empty_generator = get_resource_generator(resource.generator_module)
            if not all([
                    isinstance(option, EnumResourceParameter)
                    for option in empty_generator.get_options().values()
            ]):
                raise TypeError(
                    "Only EnumResourceParameters are supported for pre-generation"
                )
            valid_options = {
                option.name: list(option.valid_values.keys())
                for option in empty_generator.get_options().values()
            }
            combinations = resource_valid_configurations(valid_options)
            progress_bar = tqdm(combinations, ascii=True)
            # Create PDF for all possible combinations
            for combination in progress_bar:
                for language_code in generation_languages:
                    print("  - Creating PDF in '{}'".format(language_code))
                    with translation.override(language_code):
                        if resource.copies:
                            combination[
                                "copies"] = settings.RESOURCE_COPY_AMOUNT
                        requested_options = QueryDict(
                            urlencode(combination, doseq=True))
                        generator = get_resource_generator(
                            resource.generator_module, requested_options)
                        (pdf_file, filename) = generator.pdf(resource.name)

                        pdf_directory = os.path.join(base_path, resource.slug,
                                                     language_code)
                        if not os.path.exists(pdf_directory):
                            os.makedirs(pdf_directory)

                        filename = "{}.pdf".format(filename)
                        pdf_file_output = open(
                            os.path.join(pdf_directory, filename), "wb")
                        pdf_file_output.write(pdf_file)
                        pdf_file_output.close()
 def test_dictionary_one_key_one_value(self):
     options = {
         "key1": ["value1"]
     }
     self.assertEqual(
         resource_valid_configurations(options),
         [
             {"key1": "value1"}
         ]
     )
    def handle(self, *args, **options):
        """Automatically called when makeresourcethumbnails command is given."""
        resources = Resource.objects.order_by("name")

        if options.get("all_languages"):
            languages = settings.DEFAULT_LANGUAGES
        else:
            languages = [("en", "")]
        for language_code, _ in languages:
            with translation.override(language_code):
                print("Creating thumbnails for language '{}''".format(
                    language_code))
                for resource in resources:
                    base_path = BASE_PATH_TEMPLATE.format(
                        resource=resource.slug, language=language_code)
                    if not os.path.exists(base_path):
                        os.makedirs(base_path)

                    # TODO: Import repeated in next for loop, check alternatives
                    empty_generator = get_resource_generator(
                        resource.generator_module)

                    if not all([
                            isinstance(option, EnumResourceParameter) for
                            option in empty_generator.get_options().values()
                    ]):
                        raise TypeError(
                            "Only EnumResourceParameters are supported for pre-generation"
                        )
                    valid_options = {
                        option.name: list(option.valid_values.keys())
                        for option in empty_generator.get_options().values()
                    }
                    combinations = resource_valid_configurations(valid_options)

                    # Create thumbnail for all possible combinations
                    print("Creating thumbnails for {}".format(resource.name))
                    progress_bar = tqdm(combinations, ascii=True)

                    for combination in progress_bar:
                        requested_options = QueryDict(
                            urlencode(combination, doseq=True))
                        generator = get_resource_generator(
                            resource.generator_module, requested_options)

                        filename = resource.slug + "-"
                        for (key, value) in sorted(combination.items()):
                            filename += "{}-{}-".format(
                                key, bool_to_yes_no(value))
                        filename = "{}.png".format(filename[:-1])
                        thumbnail_file_path = os.path.join(base_path, filename)

                        generator.save_thumbnail(resource.name,
                                                 thumbnail_file_path)
 def test_dictionary_two_keys_two_values(self):
     options = {
         "key1": ["value1"],
         "key2": ["value5"]
     }
     self.assertEqual(
         resource_valid_configurations(options),
         [
             {"key1": "value1", "key2": "value5"}
         ]
     )
 def test_dictionary_alphabetical_keys(self):
     options = {
         "a": ["a"],
         "b": ["b"],
         "c": ["c"],
         "d": ["d"]
     }
     self.assertEqual(
         resource_valid_configurations(options),
         [
             {"a": "a", "b": "b", "c": "c", "d": "d"}
         ]
     )
Exemplo n.º 6
0
    def handle(self, *args, **options):
        """Automatically called when the makeresources command is given."""
        base_path = settings.RESOURCE_GENERATION_LOCATION

        if options["resource_name"]:
            resources = [Resource.objects.get(name=options["resource_name"])]
        else:
            resources = Resource.objects.order_by("name")

        if options["resource_language"]:
            generation_languages = [options["resource_language"]]
        else:
            generation_languages = []
            for language_code, _ in settings.LANGUAGES:
                if language_code not in settings.INCONTEXT_L10N_PSEUDOLANGUAGES:
                    generation_languages.append(language_code)

        for resource in resources:
            print("Creating {}".format(resource.name))

            # TODO: Import repeated in next for loop, check alternatives
            empty_generator = get_resource_generator(resource.generator_module)
            if not all([
                    isinstance(option, EnumResourceParameter)
                    for option in empty_generator.get_options().values()
            ]):
                raise TypeError(
                    "Only EnumResourceParameters are supported for pre-generation"
                )
            valid_options = {
                option.name: list(option.valid_values.keys())
                for option in empty_generator.get_options().values()
            }
            combinations = resource_valid_configurations(valid_options)

            # TODO: Create PDFs in parallel

            # Create PDF for all possible combinations
            for combination in combinations:
                for language_code in generation_languages:
                    self.create_resource_pdf(resource, combination,
                                             language_code, base_path)
 def test_dictionary_three_keys_many_values(self):
     options = {
         "key1": ["value1", "value2"],
         "key2": ["value5", "value6", "value7"],
         "key3": [True, False],
     }
     self.assertEqual(
         resource_valid_configurations(options),
         [
             {"key1": "value1", "key2": "value5", "key3": True},
             {"key1": "value1", "key2": "value5", "key3": False},
             {"key1": "value1", "key2": "value6", "key3": True},
             {"key1": "value1", "key2": "value6", "key3": False},
             {"key1": "value1", "key2": "value7", "key3": True},
             {"key1": "value1", "key2": "value7", "key3": False},
             {"key1": "value2", "key2": "value5", "key3": True},
             {"key1": "value2", "key2": "value5", "key3": False},
             {"key1": "value2", "key2": "value6", "key3": True},
             {"key1": "value2", "key2": "value6", "key3": False},
             {"key1": "value2", "key2": "value7", "key3": True},
             {"key1": "value2", "key2": "value7", "key3": False},
         ]
     )