Skip to content
Snippets Groups Projects
Commit 077c3251 authored by Thomas Holene Løkkeborg's avatar Thomas Holene Løkkeborg
Browse files

Merge branch 'kodetabell-modifisering' into 'master'

#146 Modifiser autentiseringstavle og subtraktorkoder

See merge request !84
parents 012e0983 75343fb1
No related branches found
No related tags found
1 merge request!84#146 Modifiser autentiseringstavle og subtraktorkoder
Pipeline #83860 passed with warnings with stages
in 2 minutes and 30 seconds
......@@ -156,13 +156,13 @@ class CodeTableBase(ModuleBase, QTableWidget, metaclass=Meta):
self.setItem(0, 0, item_headline)
self.setSpan(0, 0, 1, self.columnCount()) # Make cell span all columns
def generate_unique_authentication_code(self):
"""Generate authentication-code that does not already exist.
def generate_unique_code(self):
"""Generate code that does not already exist.
Returns
-------
string
Generated, unique authentication-code.
Unique code.
"""
# Get existing codes
existing_codes = self.get_codes()
......@@ -236,7 +236,9 @@ class CodeTableBase(ModuleBase, QTableWidget, metaclass=Meta):
for i in range(1, self.rowCount()):
row = []
for j in range(self.columnCount()):
row.append(self.item(i, j).text())
item = self.item(i, j)
text = item.text() if item is not None else ""
row.append(text)
cells.append(row)
data = {
......
......@@ -43,7 +43,7 @@ class AuthenticationBoardModule(CodeTableBase):
The default widget-initialization has a headline, a row-count of
START_NO_OF_CODES and three columns.
Row x in the first column contains the character ROW_IDENTIFIERS[x].
Row x in the second column contains the character x.
Row x in the second column contains the character x, for x = 0-9.
Row x in the third column contains an authentication code of length
CODE_LENGTH, spaced out for readability if SPACE_INTERVAL and SPACE_AMOUNT
is larger than 0.
......@@ -109,7 +109,8 @@ class AuthenticationBoardModule(CodeTableBase):
self.setItem(i, 0, item_first)
# Insert non-editable row identifier (int) in second column
item_second = QTableWidgetItem(str(i))
text_second = str(i) if i < 10 else ""
item_second = QTableWidgetItem(text_second)
item_second.setTextAlignment(Qt.AlignCenter)
item_second.setFlags(item_second.flags() ^ Qt.ItemIsEditable)
self.setItem(i, 1, item_second)
......@@ -125,7 +126,7 @@ class AuthenticationBoardModule(CodeTableBase):
# If maximum amount of rows not reached (- 1 to skip headline)
if self.rowCount() - 1 < len(ROW_IDENTIFIERS):
# Generate unique code and insert row
code = self.generate_unique_authentication_code()
code = self.generate_unique_code()
self.insertRow(selected_row_index + 1)
# Loop through all rows starting with the new row
......@@ -137,7 +138,8 @@ class AuthenticationBoardModule(CodeTableBase):
self.setItem(i, 0, item_first)
# Insert row identifier (int) in second column
item_second = QTableWidgetItem(str(i - 1))
text_second = str(i - 1) if i <= 10 else ""
item_second = QTableWidgetItem(text_second)
item_second.setTextAlignment(Qt.AlignCenter)
item_second.setFlags(item_second.flags() ^ Qt.ItemIsEditable)
self.setItem(i, 1, item_second)
......@@ -167,7 +169,8 @@ class AuthenticationBoardModule(CodeTableBase):
# If first row is removed, identifier A,B,C becomes A,B (not B,C)
for i in range(row_index, self.rowCount()):
self.item(i, 0).setText(self.code_characters[i - 1])
self.item(i, 1).setText(str(i - 1))
text_second = str(i - 1) if i <= 10 else ""
self.item(i, 1).setText(text_second)
resize_table(self, columns=False, has_headline=True)
def generate_authentication_numbers(self):
......
......@@ -13,9 +13,8 @@ from soitool.modules.code_table_base import CodeTableBase
# Characters for first and second column
ROW_IDENTIFIERS = string.ascii_uppercase
# Maximum number of codes is: number of row identifiers / 2 columns
# = 13 if identifiers are A-Z
MAXIMUM_NO_OF_CODES = len(ROW_IDENTIFIERS) // 2
# Maximum number of codes is the number of row identifiers
MAXIMUM_NO_OF_CODES = len(ROW_IDENTIFIERS)
START_NO_OF_CODES = 7
CODE_LENGTH = 8
......@@ -36,16 +35,15 @@ class SubtractorcodesModule(CodeTableBase):
"""Modified QTablewidget representing SOI-module 'Subtraktorkoder'.
The default widget-initialization has a headline, a row-count of
START_NO_OF_CODES and three columns.
The third column contains subtractorcodes of length CODE_LENGTH, spaced out
for readability if SPACE_INTERVAL and SPACE_AMOUNT larger than 0.
If there are 2 rows and ROW_IDENTIFIERS is the alphabet, the first
column will contain A and B, and the second column will contain C and D.
START_NO_OF_CODES and two columns.
The first column contains ROW_IDENTIFIERS[row-index].
The second column contains subtractorcodes of length CODE_LENGTH, spaced
out for readability if SPACE_INTERVAL and SPACE_AMOUNT larger than 0.
If parameters are given, the widget initializes accordingly:
'size' is a dict: {"width": int, "height": int},
'data' is a dict with keys "cells", "code_length", "space_interval",
"space_amount" and "code_character_type". "cells" is a 2D list where
"space_amount" and "code_character_type". "cells" is a 2D list where
cells[0] is the headline and cells[x][y] represents the value in row x,
column y. The other keys contain an integer.
......@@ -70,8 +68,12 @@ class SubtractorcodesModule(CodeTableBase):
CodeTableBase.__init__(self, size, data)
# The second column is hidden because it is not used.
self.hideColumn(1)
resize_table(self, columns=False, has_headline=True)
def insert_row_identifiers(self, has_headline=False):
"""Insert row identifiers in first and second column.
"""Insert row identifiers in first column.
Parameters
----------
......@@ -80,29 +82,12 @@ class SubtractorcodesModule(CodeTableBase):
"""
start_row = 1 if has_headline else 0
# Get row identifiers (start-index to skip headline)
# for column one and two
last_id_index_one = self.rowCount() - start_row
start_id_index_two = self.rowCount() - start_row
last_id_index_two = self.rowCount() * 2 - 2 * start_row
# If table has 3 rows and ROW_IDENTIFIERS are A-Z,
# identifiers_one = A, B, C and identifiers_two = C, D, E
identifiers_one = ROW_IDENTIFIERS[0:last_id_index_one]
identifiers_two = ROW_IDENTIFIERS[start_id_index_two:last_id_index_two]
# Insert identifiers in column one and two
for i in range(2):
for j in range(start_row, self.rowCount()):
text = (
identifiers_one[j - start_row]
if i == 0
else identifiers_two[j - start_row]
)
item = QTableWidgetItem(text)
item.setTextAlignment(Qt.AlignCenter)
item.setFlags(item.flags() ^ Qt.ItemIsEditable)
self.setItem(j, i, item)
# Insert row identifiers
for i in range(start_row, self.rowCount()):
item = QTableWidgetItem(ROW_IDENTIFIERS[i - start_row])
item.setTextAlignment(Qt.AlignCenter)
item.setFlags(item.flags() ^ Qt.ItemIsEditable)
self.setItem(i, 0, item)
def add_row(self, selected_row_index):
"""Insert row below the selected row and add data.
......@@ -113,10 +98,10 @@ class SubtractorcodesModule(CodeTableBase):
Index of the selected row.
"""
# If maximum amount of rows not reached (- 1 to skip headline)
if self.rowCount() - 1 < len(ROW_IDENTIFIERS) / 2:
if self.rowCount() - 1 < self.maximum_no_of_codes:
# Generate unique code and insert row
code = self.generate_unique_authentication_code()
code = self.generate_unique_code()
self.insertRow(selected_row_index + 1)
# Insert row identifiers
......
......@@ -74,7 +74,8 @@ class TestDefaultAuthenticationBoardAndSubtractorcodesModule(
self.assertEqual(self.subtractor.columnCount(), 3)
# Assert cell content in first and second column is correct
for i in range(1, 3): # From 1 to skip headline-row
# From 1 to skip headline-row
for i in range(1, 3):
self.assertEqual(
self.authentication.item(i, 0).text(),
ROW_IDENTIFIERS_AUTHENTICATION[i - 1],
......@@ -87,12 +88,6 @@ class TestDefaultAuthenticationBoardAndSubtractorcodesModule(
self.subtractor.item(i, 0).text(),
ROW_IDENTIFIERS_SUBTRACTOR[i - 1],
)
self.assertEqual(
self.subtractor.item(i, 1).text(),
ROW_IDENTIFIERS_SUBTRACTOR[
i - 1 + START_NO_OF_CODES_SUBTRACTOR
],
)
# Assert cell content in third column is correct
code = self.authentication.item(1, 2).text()
......@@ -118,9 +113,7 @@ class TestDefaultAuthenticationBoardAndSubtractorcodesModule(
def test_generate_unique_authentication_code(self):
"""Test function generate_unique_authentication_module."""
code_list = self.authentication.get_codes()
generated_code = (
self.authentication.generate_unique_authentication_code()
)
generated_code = self.authentication.generate_unique_code()
# Assert code length is equal to an existing code
self.assertEqual(len(generated_code), len(code_list[0]))
......@@ -162,9 +155,7 @@ class TestDefaultAuthenticationBoardAndSubtractorcodesModule(
self.authentication.item(row_index, 0).text(),
ROW_IDENTIFIERS_AUTHENTICATION[row_index - 1],
)
self.assertEqual(
self.authentication.item(row_index, 1).text(), str(row_index - 1)
)
self.assertEqual(self.authentication.item(row_index, 1).text(), "")
new_code = self.authentication.item(row_index, 2).text()
existing_code = self.authentication.item(1, 2).text()
self.assertEqual(len(new_code), len(existing_code))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment