Skip to content

Commit

Permalink
Merge pull request #560 from uccser/issue/545
Browse files Browse the repository at this point in the history
Add barcode checksum poster resource

Fixes #545
Fixes #546
  • Loading branch information
JackMorganNZ committed Jul 20, 2017
2 parents feb0989 + 3f41679 commit 6dfbe83
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 0 deletions.
6 changes: 6 additions & 0 deletions csunplugged/resources/content/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ grid:
generation-view: grid.py
thumbnail-static-path: img/resources/grid/thumbnail.png
copies: false
barcode-checksum-poster:
name: Barcode Checksum Poster
webpage-template: resources/barcode-checksum-poster.html
generation-view: barcode_checksum_poster.py
thumbnail-static-path: img/resources/barcode-checksum-poster/thumbnail.gif
copies: false
55 changes: 55 additions & 0 deletions csunplugged/resources/views/barcode_checksum_poster.py
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"],
}
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.
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 csunplugged/templates/resources/barcode-checksum-poster.html
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 csunplugged/tests/resources/urls/test_barcode_checksum_poster.py
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 csunplugged/tests/resources/views/test_barcode_checksum_poster.py
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)
)

0 comments on commit 6dfbe83

Please sign in to comment.