Example #1
0
 def test_piano_keys_resource_generation_valid_configurations(self):
     resource = self.test_data.create_resource(
         "piano-keys",
         "Piano Keys",
         "resources/piano-keys.html",
         "PianoKeysResourceGenerator",
     )
     kwargs = {
         "resource_slug": resource.slug,
     }
     base_url = reverse("resources:generate", kwargs=kwargs)
     empty_generator = get_resource_generator(resource.generator_module)
     combinations = resource_valid_test_configurations(
         empty_generator.valid_options)
     print()
     for combination in combinations:
         print("   - Testing combination: {} ... ".format(combination),
               end="")
         url = base_url + query_string(combination)
         response = self.client.get(url)
         self.assertEqual(HTTPStatus.OK, response.status_code)
         subtitle = "{} highlight - {}".format(
             bool_to_yes_no(combination["highlight"]),
             combination["paper_size"],
         )
         self.assertEqual(
             response.get("Content-Disposition"),
             'attachment; filename="Resource Piano Keys ({subtitle}).pdf"'.
             format(subtitle=subtitle))
         print("ok")
Example #2
0
 def test_grid_resource_generation_valid_configurations(self):
     resource = self.test_data.create_resource(
         "grid",
         "Grid",
         "resources/grid.html",
         "GridResourceGenerator",
     )
     kwargs = {
         "resource_slug": resource.slug,
     }
     base_url = reverse("resources:generate", kwargs=kwargs)
     empty_generator = get_resource_generator(resource.generator_module)
     combinations = resource_valid_test_configurations(
         empty_generator.valid_options)
     print()
     for combination in combinations:
         print("   - Testing combination: {} ... ".format(combination),
               end="")
         url = base_url + query_string(combination)
         response = self.client.get(url)
         self.assertEqual(HTTPStatus.OK, response.status_code)
         subtitle = combination["paper_size"]
         self.assertEqual(
             response.get("Content-Disposition"),
             'attachment; filename="Resource Grid ({subtitle}).pdf"'.format(
                 subtitle=subtitle))
         print("ok")
Example #3
0
    def handle(self, *args, **options):
        """Automatically called when the makeresources command is given."""
        BASE_PATH = "staticfiles/resources/"
        if not os.path.exists(BASE_PATH):
            os.makedirs(BASE_PATH)

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

        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)
            combinations = resource_valid_test_configurations(
                empty_generator.valid_options, header_text=False)
            progress_bar = tqdm(combinations, ascii=True)
            # Create PDF for all possible combinations
            for combination in progress_bar:
                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) = generate_resource_pdf(resource.name, generator)

                filename = "{}.pdf".format(filename)
                pdf_file_output = open(os.path.join(BASE_PATH, filename), "wb")
                pdf_file_output.write(pdf_file)
                pdf_file_output.close()
 def test_sorting_network_cards_resource_generation_valid_configurations(
         self):
     resource = self.test_data.create_resource(
         "sorting-network-cards",
         "Sorting Network Cards",
         "resources/sorting-network-cards.html",
         "SortingNetworkCardsResourceGenerator",
     )
     kwargs = {
         "resource_slug": resource.slug,
     }
     base_url = reverse("resources:generate", kwargs=kwargs)
     empty_generator = get_resource_generator(resource.generator_module)
     combinations = resource_valid_test_configurations(
         empty_generator.valid_options)
     print()
     for combination in combinations:
         print("   - Testing combination: {} ... ".format(combination),
               end="")
         url = base_url + query_string(combination)
         response = self.client.get(url)
         self.assertEqual(HTTPStatus.OK, response.status_code)
         subtitle = "{} - {}".format(
             combination["type"].replace("_", " "),
             combination["paper_size"],
         )
         self.assertEqual(
             response.get("Content-Disposition"),
             'attachment; filename="Resource Sorting Network Cards ({subtitle}).pdf"'
             .format(subtitle=subtitle))
         print("ok")
    def handle(self, *args, **options):
        """Automatically called when makeresourcethumbnails command is given."""
        resources = Resource.objects.order_by("name")

        for resource in resources:
            base_path = BASE_PATH_TEMPLATE.format(resource=resource.slug)
            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)
            combinations = resource_valid_test_configurations(
                empty_generator.valid_options, header_text=False)

            # 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)

                thumbnail = generate_resource_thumbnail(
                    resource.name, generator)

                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 = open(os.path.join(base_path, filename), "wb")
                thumbnail_file.write(thumbnail)
                thumbnail_file.close()
    def test_treasure_hunt_resource_generation_valid_configurations(self):
        resource = self.test_data.create_resource(
            "treasure-hunt",
            "Treasure Hunt",
            "resources/treasure-hunt.html",
            "TreasureHuntResourceGenerator",
        )
        kwargs = {
            "resource_slug": resource.slug,
        }
        base_url = reverse("resources:generate", kwargs=kwargs)
        empty_generator = get_resource_generator(resource.generator_module)
        combinations = resource_valid_test_configurations(
            empty_generator.valid_options
        )
        print()
        for combination in combinations:
            print("   - Testing combination: {} ... ".format(combination), end="")
            url = base_url + query_string(combination)
            response = self.client.get(url)
            self.assertEqual(HTTPStatus.OK, response.status_code)

            if combination["prefilled_values"] == "blank":
                range_text = "blank"
            else:
                range_min = 0
                if combination["prefilled_values"] == "easy":
                    range_max = 99
                elif combination["prefilled_values"] == "medium":
                    range_max = 999
                elif combination["prefilled_values"] == "hard":
                    range_max = 9999
                SUBTITLE_TEMPLATE = "{} - {} to {}"
                number_order_text = combination["number_order"].title()
                range_text = SUBTITLE_TEMPLATE.format(number_order_text, range_min, range_max)

            if combination["art"] == "colour":
                art_style_text = "full colour"
            else:
                art_style_text = "black and white"

            if combination["instructions"]:
                instructions_text = "with instructions"
            else:
                instructions_text = "without instructions"

            subtitle = "{} - {} - {} - {}".format(
                range_text,
                art_style_text,
                instructions_text,
                combination["paper_size"]
            )

            self.assertEqual(
                response.get("Content-Disposition"),
                'attachment; filename="Resource Treasure Hunt ({subtitle}).pdf"'.format(subtitle=subtitle)
            )
            print("ok")
 def test_binary_cards_small_resource_generation_valid_configurations(self):
     resource = self.test_data.create_resource(
         "binary-cards",
         "Binary Cards (small)",
         "resources/binary-cards-small.html",
         "BinaryCardsSmallResourceGenerator",
     )
     kwargs = {
         "resource_slug": resource.slug,
     }
     base_url = reverse("resources:generate", kwargs=kwargs)
     empty_generator = get_resource_generator(resource.generator_module)
     combinations = resource_valid_test_configurations(
         empty_generator.valid_options)
     print()
     for combination in combinations:
         print("   - Testing combination: {} ... ".format(combination),
               end="")
         url_combination = {}
         for parameter in combination:
             if combination[parameter] is True:
                 url_combination[parameter] = "yes"
             elif combination[parameter] is False:
                 url_combination[parameter] = "no"
             else:
                 url_combination[parameter] = combination[parameter]
         url = base_url + query_string(url_combination)
         response = self.client.get(url)
         self.assertEqual(HTTPStatus.OK, response.status_code)
         if combination["dot_counts"]:
             display_numbers_text = "with dot counts"
         else:
             display_numbers_text = "without dot counts"
         if combination["black_back"]:
             black_back_text = "with black back"
         else:
             black_back_text = "without black back"
         subtitle = "{} bits - {} - {} - {}".format(
             combination["number_bits"],
             display_numbers_text,
             black_back_text,
             combination["paper_size"],
         )
         self.assertEqual(
             response.get("Content-Disposition"),
             'attachment; filename="Resource Binary Cards (small) ({subtitle}).pdf"'
             .format(subtitle=subtitle))
         print("ok")
Example #8
0
    def test_searching_cards_resource_generation_valid_configurations(self):
        resource = self.test_data.create_resource(
            "searching-cards",
            "Searching Cards",
            "resources/searching-cards.html",
            "SearchingCardsResourceGenerator",
        )
        kwargs = {
            "resource_slug": resource.slug,
        }
        base_url = reverse("resources:generate", kwargs=kwargs)
        empty_generator = get_resource_generator(resource.generator_module)
        combinations = resource_valid_test_configurations(
            empty_generator.valid_options)
        print()
        for combination in combinations:
            print("   - Testing combination: {} ... ".format(combination),
                  end="")
            url = base_url + query_string(combination)
            response = self.client.get(url)
            self.assertEqual(HTTPStatus.OK, response.status_code)
            if combination["max_number"] == "blank":
                range_text = "blank"
            elif combination["max_number"] == "cards":
                range_text = "0 to {}".format(combination["number_cards"])
            else:
                range_text = "0 to {}".format(combination["max_number"])

            if combination["help_sheet"]:
                help_text = "with helper sheet"
            else:
                help_text = "without helper sheet"
            subtitle = "{} cards - {} - {} - {}".format(
                combination["number_cards"],
                range_text,
                help_text,
                combination["paper_size"],
            )
            self.assertEqual(
                response.get("Content-Disposition"),
                'attachment; filename="Resource Searching Cards ({subtitle}).pdf"'
                .format(subtitle=subtitle))
            print("ok")
Example #9
0
 def test_binary_windows_resource_generation_valid_configurations(self):
     resource = self.test_data.create_resource(
         "binary-windows",
         "Binary Windows",
         "resources/binary-windows.html",
         "BinaryWindowsResourceGenerator",
     )
     kwargs = {
         "resource_slug": resource.slug,
     }
     base_url = reverse("resources:generate", kwargs=kwargs)
     empty_generator = get_resource_generator(resource.generator_module)
     combinations = resource_valid_test_configurations(
         empty_generator.valid_options)
     print()
     for combination in combinations:
         print("   - Testing combination: {} ... ".format(combination),
               end="")
         url = base_url + query_string(combination)
         response = self.client.get(url)
         self.assertEqual(HTTPStatus.OK, response.status_code)
         if combination["dot_counts"]:
             count_text = "with dot counts"
         else:
             count_text = "without dot counts"
         TEMPLATE = "{} bits - {} - {} - {}"
         subtitle = TEMPLATE.format(
             combination["number_bits"],
             combination["value_type"],
             count_text,
             combination["paper_size"],
         )
         self.assertEqual(
             response.get("Content-Disposition"),
             'attachment; filename="Resource Binary Windows ({subtitle}).pdf"'
             .format(subtitle=subtitle))
         print("ok")