Ignore:
Timestamp:
30 Nov 2018, 07:34:44 (6 years ago)
Author:
Henrik Bettermann
Message:

Do not allow uploading data with trailing whitespaces.

Location:
main/waeup.ikoba/trunk/src/waeup/ikoba/utils
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.ikoba/trunk/src/waeup/ikoba/utils/helpers.py

    r13135 r15258  
    756756
    757757def check_csv_charset(iterable):
    758     """Check contents of `iterable` regarding valid CSV encoding.
     758    """Check contents of `iterable` regarding valid CSV encoding and
     759    trailing whitespaces in data.
    759760
    760761    `iterable` is expected to be an iterable on _rows_ (not
     
    764765
    765766    Returns line num of first illegal char or ``None``. Line nums
    766     start counting with 1 (not zero).
     767    start counting with 1 (not zero). Returns -1 if data contain
     768    trailing whitespaces.
    767769    """
    768770    linenum = 1
    769     reader = csv.DictReader(iterable)
    770771    try:
     772        reader = csv.DictReader(iterable)
    771773        for row in reader:
    772774            linenum += 1
     775            for value in row.values():
     776                if value.endswith(' '):
     777                    return -1
    773778    except UnicodeDecodeError:
    774779        return linenum
  • main/waeup.ikoba/trunk/src/waeup/ikoba/utils/tests/test_helpers.py

    r12434 r15258  
    533533            'some text that \n'
    534534            '\n'      # this empty line will break
    535             'is not a csv file \n' + chr(0x92) + '\n'
     535            'is not a csv file\n' + chr(0x92) + '\n'
    536536            ).splitlines()
    537537        self.assertEqual(helpers.check_csv_charset(csv), 2)
     538
     539    def test_invalid_data3(self):
     540        csv = (
     541            "code,title,title_prefix\n"
     542            "FAC1,Faculty 1,faculty\n"
     543            "FAC2,Faculty 2 ,institute\n"
     544            "FAC3,Fäcülty 3,school\n"
     545            ).splitlines()
     546        self.assertEqual(helpers.check_csv_charset(csv), -1)
    538547
    539548
Note: See TracChangeset for help on using the changeset viewer.