]> git.cworth.org Git - turbot-web/commitdiff
Make the code robust against a puzzle without some fields
authorCarl Worth <cworth@cworth.org>
Wed, 12 Jan 2022 08:12:58 +0000 (00:12 -0800)
committerCarl Worth <cworth@cworth.org>
Wed, 12 Jan 2022 08:12:58 +0000 (00:12 -0800)
Specifically, some puzzles may be missing any of the 'rounds', 'tags',
or 'url' keys. Previousl, the code was crashing whenever this was the case.

With this commit, empty strings or empty lists are used as the values
for any missing keys as appropriate.

html_generator.py

index 3ee751c989348e6c5004ec399fa6dc403f4972be..b944391ed2cd15745f57b1c29ac563d889b2893e 100644 (file)
@@ -246,22 +246,22 @@ def round_overview(rnd, puzzles):
         if puzzle['status'] == 'solved':
             puzzle_list += [ '                    <tr>\n',
              '                        <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
-             '                        <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
+             '                        <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
              '                        <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
              '                        <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
              '                        <td>{}</td>\n'.format(puzzle['solution']),
             # '                        <td></td>\n',
-             '                        <td>{}</td>\n'.format("".join(puzzle['tags'])),
+             '                        <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
              '                    </tr>\n']
         else:
             puzzle_list += [ '                    <tr>\n',
              '                        <td><b>{}</b></td>\n'.format(elink(slack_url, puzzle['name']+meta)),
-             '                        <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
+             '                        <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
              '                        <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
              '                        <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
              '                        <td></td>\n',
             # '                        <td></td>\n',
-             '                        <td>{}</td>\n'.format(" ".join(puzzle['tags'])),
+             '                        <td>{}</td>\n'.format(" ".join(puzzle.get('tags',[]))),
              '                    </tr>\n']
     end = ['                </tbody>\n',
     '            </table>\n',
@@ -285,7 +285,10 @@ def puzzle_overview(puzzle):
     else:
         meta = ''
     slack_url = channel_url(puzzle['channel_id'])
-    round_url = [link(website + "_".join(rnd.split()) + "_round.html", rnd) for rnd in puzzle['rounds']]
+    if 'rounds' in puzzle:
+        round_url = [link(website + "_".join(rnd.split()) + "_round.html", rnd) for rnd in puzzle['rounds']]
+    else:
+        round_url = ''
     if puzzle['status'] == 'solved':
         solution = puzzle['solution']
         status = 'solved'
@@ -308,7 +311,7 @@ def puzzle_overview(puzzle):
      '        <table class="center">\n',
      '            <tr>\n',
      '                <td>{}</td>\n'.format(elink(slack_url, 'Channel')), #slack channel
-     '                <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
+     '                <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
      '                <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
      '                <td>Additional Resources</td>\n',
      '            </tr>\n',
@@ -316,7 +319,7 @@ def puzzle_overview(puzzle):
      '        <table class="center">\n',
      '            <tr>\n',
      '                <td>Round(s): {}</td>\n'.format(" ".join(round_url)), #round page on our site
-     '                <td>Tags: {}</td>\n'.format(" ".join(puzzle['tags'])), #add tags
+     '                <td>Tags: {}</td>\n'.format(" ".join(puzzle.get('tags',[]))), #add tags
      '            </tr>\n',
      '            <tr>\n',
      '                <td>Answer: {}</td>\n'.format(solution),
@@ -378,17 +381,20 @@ def puzzle_lists(puzzles, filt):
         else:
             meta = ''
         slack_url = channel_url(puzzle['channel_id'])
-        round_url = link(website + "_".join(rnd.split()) + "_round.html", puzzle['rounds'][0])
+        if 'rounds' in puzzle:
+            round_url = link(website + "_".join(rnd.split()) + "_round.html", puzzle['rounds'][0])
+        else:
+            round_url = ''
         #assuming one round per puzzle for now
 
         solved_code += ['                    <tr>\n',
          '                        <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
-         '                        <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
+         '                        <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
          '                        <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
          '                        <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
          '                        <td>{}</td>\n'.format(puzzle['solution']),
          '                        <td>{}</td>\n'.format(round_url),
-         '                        <td>{}</td>\n'.format("".join(puzzle['tags'])),
+         '                        <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
          '                    </tr>\n']
     for puzzle in unsolved_puzzles:
         if puzzle['type'] == 'meta':
@@ -396,17 +402,20 @@ def puzzle_lists(puzzles, filt):
         else:
             meta = ''
         slack_url = channel_url(puzzle['channel_id'])
-        round_url = link(website + "_".join(rnd.split()) + "_round.html", puzzle['rounds'][0])
+        if 'rounds' in puzzle:
+            round_url = link(website + "_".join(rnd.split()) + "_round.html", puzzle['rounds'][0])
+        else:
+            round_url = ''
         #assuming one round per puzzle for now
 
         unsolved_code += ['                    <tr>\n',
          '                        <td>{}</td>\n'.format(elink(slack_url, puzzle['name']+meta)),
-         '                        <td>{}</td>\n'.format(elink(puzzle['url'], 'Puzzle')),
+         '                        <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
          '                        <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
          '                        <td>{}</td>\n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')),
          '                        <td></td>\n',
          '                        <td>{}</td>\n'.format(round_url),
-         '                        <td>{}</td>\n'.format("".join(puzzle['tags'])),
+         '                        <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
          '                    </tr>\n']
     end = ['                </tbody>\n',
     '            </table>\n',