-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #560 from uccser/issue/545
- Loading branch information
Showing
8 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""Module for generating Barcode Checksum Poster resource.""" | ||
|
||
from PIL import Image | ||
from utils.retrieve_query_parameter import retrieve_query_parameter | ||
|
||
|
||
def resource_image(request, resource): | ||
"""Create a image for Barcode Checksum Poster resource. | ||
Args: | ||
request: HTTP request object (QueryDict). | ||
resource: Object of resource data (Resource). | ||
Returns: | ||
A list of Pillow image objects. | ||
""" | ||
# Retrieve parameters | ||
parameter_options = valid_options() | ||
barcode_length = retrieve_query_parameter(request, "barcode_length", parameter_options["barcode_length"]) | ||
|
||
image_path = "static/img/resources/barcode-checksum-poster/{}-digits.png" | ||
image = Image.open(image_path.format(barcode_length)) | ||
return image | ||
|
||
|
||
def subtitle(request, resource): | ||
"""Return the subtitle string of the resource. | ||
Used after the resource name in the filename, and | ||
also on the resource image. | ||
Args: | ||
request: HTTP request object (QueryDict). | ||
resource: Object of resource data (Resource). | ||
Returns: | ||
Text for subtitle (str). | ||
""" | ||
barcode_length = retrieve_query_parameter(request, "barcode_length") | ||
paper_size = retrieve_query_parameter(request, "paper_size") | ||
return "{} digits - {}".format(barcode_length, paper_size) | ||
|
||
|
||
def valid_options(): | ||
"""Provide dictionary of all valid parameters. | ||
This excludes the header text parameter. | ||
Returns: | ||
All valid options (dict). | ||
""" | ||
return { | ||
"barcode_length": ["12", "13"], | ||
"paper_size": ["a4", "letter"], | ||
} |
Binary file added
BIN
+263 KB
csunplugged/static/img/resources/barcode-checksum-poster/12-digits.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+243 KB
csunplugged/static/img/resources/barcode-checksum-poster/13-digits.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions
19
csunplugged/templates/resources/barcode-checksum-poster.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{% extends "resources/resource.html" %} | ||
|
||
<!-- Description of resource --> | ||
{% block description %} | ||
<p> | ||
This resource is a poster showing how to calculate the checksum for a 12 or 13 digit barcode. | ||
</p> | ||
{% endblock description %} | ||
|
||
{% block generation_form %} | ||
<fieldset> | ||
<legend>Barcode length</legend> | ||
<input type="radio" name="barcode_length" id="barcode_length_12" value="12" checked="checked"> | ||
<label for="barcode_length_12">12 digits</label> | ||
<br> | ||
<input type="radio" name="barcode_length" id="barcode_length_13" value="13"> | ||
<label for="barcode_length_13">13 digits</label> | ||
</fieldset> | ||
{% endblock generation_form %} |
19 changes: 19 additions & 0 deletions
19
csunplugged/tests/resources/urls/test_barcode_checksum_poster.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from tests.BaseTestWithDB import BaseTestWithDB | ||
from django.urls import reverse | ||
|
||
|
||
class BarcodeChecksumPosterResourceURLTest(BaseTestWithDB): | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.language = "en" | ||
self.RESOURCE_SLUG = "barcode-checksum-poster" | ||
self.RESOURCE_URL_KWARGS = {"resource_slug": self.RESOURCE_SLUG} | ||
|
||
def test_valid_barcode_checksum_poster_resource_url(self): | ||
url = reverse("resources:resource", kwargs=self.RESOURCE_URL_KWARGS) | ||
self.assertEqual(url, "/en/resources/barcode-checksum-poster/") | ||
|
||
def test_valid_barcode_checksum_poster_resource_generate_url(self): | ||
url = reverse("resources:generate", kwargs=self.RESOURCE_URL_KWARGS) | ||
self.assertEqual(url, "/en/resources/barcode-checksum-poster/generate") |
120 changes: 120 additions & 0 deletions
120
csunplugged/tests/resources/views/test_barcode_checksum_poster.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
from django.urls import reverse | ||
from tests.BaseTestWithDB import BaseTestWithDB | ||
from tests.resources.ResourcesTestDataGenerator import ResourcesTestDataGenerator | ||
from utils.import_resource_module import import_resource_module | ||
from utils.create_query_string import query_string | ||
from utils.resource_valid_test_configurations import resource_valid_test_configurations | ||
|
||
|
||
class BarcodeChecksumPosterResourceViewTest(BaseTestWithDB): | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.test_data = ResourcesTestDataGenerator() | ||
self.language = "en" | ||
|
||
def test_barcode_checksum_poster_resource_form_view(self): | ||
resource = self.test_data.create_resource( | ||
"barcode-checksum-poster", | ||
"Barcode Checksum Poster", | ||
"resources/barcode-checksum-poster.html", | ||
"barcode_checksum_poster.py", | ||
) | ||
kwargs = { | ||
"resource_slug": resource.slug, | ||
} | ||
url = reverse("resources:resource", kwargs=kwargs) | ||
response = self.client.get(url) | ||
self.assertEqual(200, response.status_code) | ||
|
||
def test_barcode_checksum_poster_resource_generation_valid_configurations(self): | ||
resource = self.test_data.create_resource( | ||
"barcode-checksum-poster", | ||
"Barcode Checksum Poster", | ||
"resources/barcode-checksum-poster.html", | ||
"barcode_checksum_poster.py", | ||
) | ||
kwargs = { | ||
"resource_slug": resource.slug, | ||
} | ||
base_url = reverse("resources:generate", kwargs=kwargs) | ||
resource_module = import_resource_module(resource) | ||
valid_options = resource_module.valid_options() | ||
combinations = resource_valid_test_configurations(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(200, response.status_code) | ||
subtitle = "{} digits - {}".format( | ||
combination["barcode_length"], | ||
combination["paper_size"], | ||
) | ||
self.assertEqual( | ||
response.get("Content-Disposition"), | ||
'attachment; filename="Resource Barcode Checksum Poster ({subtitle}).pdf"'.format(subtitle=subtitle) | ||
) | ||
print("ok") | ||
|
||
def test_barcode_checksum_poster_resource_generation_missing_barcode_length_parameter(self): | ||
resource = self.test_data.create_resource( | ||
"barcode-checksum-poster", | ||
"Barcode Checksum Poster", | ||
"resources/barcode-checksum-poster.html", | ||
"barcode_checksum_poster.py", | ||
) | ||
kwargs = { | ||
"resource_slug": resource.slug, | ||
} | ||
url = reverse("resources:generate", kwargs=kwargs) | ||
get_parameters = { | ||
"paper_size": "a4", | ||
"header_text": "", | ||
} | ||
url += query_string(get_parameters) | ||
response = self.client.get(url) | ||
self.assertEqual(404, response.status_code) | ||
|
||
def test_binary_cards_small_resource_generation_missing_paper_size_parameter(self): | ||
resource = self.test_data.create_resource( | ||
"barcode-checksum-poster", | ||
"Barcode Checksum Poster", | ||
"resources/barcode-checksum-poster.html", | ||
"barcode_checksum_poster.py", | ||
) | ||
kwargs = { | ||
"resource_slug": resource.slug, | ||
} | ||
url = reverse("resources:generate", kwargs=kwargs) | ||
get_parameters = { | ||
"barcode_length": "12", | ||
"header_text": "", | ||
} | ||
url += query_string(get_parameters) | ||
response = self.client.get(url) | ||
self.assertEqual(404, response.status_code) | ||
|
||
def test_binary_cards_small_resource_generation_missing_header_text_parameter(self): | ||
resource = self.test_data.create_resource( | ||
"barcode-checksum-poster", | ||
"Barcode Checksum Poster", | ||
"resources/barcode-checksum-poster.html", | ||
"barcode_checksum_poster.py", | ||
) | ||
kwargs = { | ||
"resource_slug": resource.slug, | ||
} | ||
url = reverse("resources:generate", kwargs=kwargs) | ||
get_parameters = { | ||
"barcode_length": "12", | ||
"paper_size": "a4", | ||
} | ||
url += query_string(get_parameters) | ||
response = self.client.get(url) | ||
self.assertEqual(200, response.status_code) | ||
filename = "Resource Barcode Checksum Poster (12 digits - a4).pdf" | ||
self.assertEqual( | ||
response.get("Content-Disposition"), | ||
'attachment; filename="{}"'.format(filename) | ||
) |