]> git.cworth.org Git - turbot-web/blobdiff - html_generator.py
Fix two bugs referencing a bogus 'rnd' variable that doesn't exist
[turbot-web] / html_generator.py
index c16f033552cb80094befa995b04645fa1d612ba6..de7291c2518076e0e8e17b02cfd3e6cfe5652fae 100644 (file)
@@ -16,11 +16,21 @@ Requires sorttable.js, which should be included
 """
 import boto3
 from boto3.dynamodb.conditions import Key
+import os
 import re
 
+WEBROOT = "/srv/halibut.cworth.org/www"
+
 website = "https://halibut.cworth.org/"
 #change this if we're using AWS or some other subdomain instead
 
+def hunt_file(hunt, name):
+    """Return a path file 'name' within the given hunt.
+
+    This will be withing WEBROOT and in a hunt-specific path."""
+
+    return "{}/{}/{}".format(WEBROOT, hunt['channel_id'], name)
+
 def filename_from_name(name):
     """Returns a string derived from name, but with all spaces and slashes
        replaced with underscores, (for making a clean filename)"""
@@ -91,7 +101,7 @@ def hunt_info(table, hunt_id):
     rounds = list(rounds)
     rounds.sort()
 
-    return puzzles, rounds
+    return hunt, puzzles, rounds
 
 def round_stat(rnd, puzzles):
     #Counts puzzles, solved, list of puzzles for a given round
@@ -123,7 +133,7 @@ def round_stat(rnd, puzzles):
 
 
 
-def overview(puzzles, rounds):
+def overview(hunt, puzzles, rounds):
     #big board, main page. saves as index.html
     start = ['<!DOCTYPE html>\n',
      '<html>\n',
@@ -131,7 +141,7 @@ def overview(puzzles, rounds):
      '  <meta charset="utf-8">\n',
      '  <meta name="viewport" content="width=device-width, initial-scale=1">\n',
      '\n',
-     '  <link rel="stylesheet" href="overview.css">\n',
+     '  <link rel="stylesheet" href="/overview.css">\n',
      '  <script type="text/javascript">\n',
      '    // Hide all elements with class="containerTab", except for the one that matches the clickable grid column\n',
      '    function openTab(tabName) {\n',
@@ -145,7 +155,7 @@ def overview(puzzles, rounds):
      '  </script>\n',
      '\n',
      '  <title>Hunt Overview</title>\n',
-     '  <script src="sorttable.js"></script>\n'
+     '  <script src="/sorttable.js"></script>\n'
      '</head>\n',
      '    <div class="sidenav">\n'
      '      <a href="index.html">Hunt Overview</a>'
@@ -200,14 +210,14 @@ def overview(puzzles, rounds):
     columns.append('  </div>\n')
     end = ['</body>\n', '</html>\n']
     html = start + expanding + columns + end
-    file = "index.html"
+    file = hunt_file(hunt, "index.html")
     f = open(file, "w")
     for line in html:
         f.write(line)
     f.close()
     return None
 
-def round_overview(rnd, puzzles):
+def round_overview(hunt, rnd, puzzles):
     #inputs: round name, puzzles
     #round overview page
     #saves as (round name)_round.html, in case meta/round share names.
@@ -219,9 +229,9 @@ def round_overview(rnd, puzzles):
         status = 'unsolved'
     start = ['<html>\n',
      '    <head>\n',
-     '        <link rel="stylesheet" href="individual.css">\n',
+     '        <link rel="stylesheet" href="/individual.css">\n',
      '        <title>Mystery Hunt 2022</title>\n',
-     '        <script src="sorttable.js"></script>\n',
+     '        <script src="/sorttable.js"></script>\n',
      '    </head>\n',
      '    <body class="{}">\n'.format(status),
      '        <h1><b>{}</b></h1>\n'.format(rnd),
@@ -274,14 +284,14 @@ def round_overview(rnd, puzzles):
     '    </body>\n',
     '</html>\n']
     html = start + puzzle_list + end
-    file = "{}_round.html".format(filename_from_name(rnd))
+    file = hunt_file(hunt, "{}_round.html".format(filename_from_name(rnd)))
     f = open(file, "w")
     for line in html:
         f.write(line)
     f.close()
     return None
 
-def puzzle_overview(puzzle):
+def puzzle_overview(hunt, puzzle):
     #overview page for individual puzzles. saves as (name of puzzle).html,
     #with underscores rather than spaces
     name = puzzle['name']
@@ -305,7 +315,7 @@ def puzzle_overview(puzzle):
      '<head>\n',
      '    <meta charset="utf-8">\n',
      '    <meta name="viewport" content="width=device-width, initial-scale=1">\n',
-     '    <link rel="stylesheet" href="individual.css">\n',
+     '    <link rel="stylesheet" href="/individual.css">\n',
      '    <title>{}</title>\n'.format(name+meta),
      '    <p>{}</p>'.format(link(website + 'index.html', 'Hunt Overview')),
      '\n',
@@ -335,13 +345,13 @@ def puzzle_overview(puzzle):
      '\n',
      '</body>\n',
      '</html>\n']
-    file = "{}.html".format(filename_from_name(name))
+    file = hunt_file(hunt, "{}.html".format(filename_from_name(name)))
     f = open(file, "w")
     for line in html:
         f.write(line)
     return None
 
-def puzzle_lists(puzzles, filt):
+def puzzle_lists(hunt, puzzles, filt):
     #filt is one of "All", "Solved", "Unsolved" and spits out the appropriate list of puzzles
     #generates pages for all puzzles, solved puzzles, unsolved puzzles
     #saves as all/solved/unsolved.html, has a sidebar to link to each other and to the big board
@@ -349,9 +359,9 @@ def puzzle_lists(puzzles, filt):
     unsolved_puzzles = [puzzle for puzzle in puzzles if puzzle['status'] != 'solved']
     start = ['<html>\n',
      '    <head>\n',
-     '        <link rel="stylesheet" href="overview.css">\n',
+     '        <link rel="stylesheet" href="/overview.css">\n',
      '        <title>Mystery Hunt 2022</title>\n',
-     '        <script src="sorttable.js"></script>\n',
+     '        <script src="/sorttable.js"></script>\n',
      '    </head>\n',
      '    <div class="sidenav">\n'
      '      <a href="index.html">Hunt Overview</a>'
@@ -385,7 +395,7 @@ def puzzle_lists(puzzles, filt):
             meta = ''
         slack_url = channel_url(puzzle['channel_id'])
         if 'rounds' in puzzle:
-            round_url = link(website + filename_from_name(rnd) + "_round.html", puzzle['rounds'][0])
+            round_url = link(website + filename_from_name(puzzle['rounds'][0]) + "_round.html", puzzle['rounds'][0])
         else:
             round_url = ''
         #assuming one round per puzzle for now
@@ -406,7 +416,7 @@ def puzzle_lists(puzzles, filt):
             meta = ''
         slack_url = channel_url(puzzle['channel_id'])
         if 'rounds' in puzzle:
-            round_url = link(website + filename_from_name(rnd) + "_round.html", puzzle['rounds'][0])
+            round_url = link(website + filename_from_name(puzzle['rounds'][0]) + "_round.html", puzzle['rounds'][0])
         else:
             round_url = ''
         #assuming one round per puzzle for now
@@ -426,19 +436,19 @@ def puzzle_lists(puzzles, filt):
     '    </body>\n',
     '</html>\n']
     if filt == "All":
-        file1 = 'all.html'
+        file1 = hunt_file(hunt, 'all.html')
         f = open(file1, "w")
         for line in start + unsolved_code + solved_code + end:
             f.write(line)
         f.close()
     elif filt == "Solved":
-        file2 = 'solved.html'
+        file2 = hunt_file(hunt, 'solved.html')
         f = open(file2, 'w')
         for line in start + solved_code + end:
             f.write(line)
         f.close()
     elif filt == "Unsolved":
-        file3 = 'unsolved.html'
+        file3 = hunt_file(hunt, 'unsolved.html')
         f = open(file3, 'w')
         for line in start + unsolved_code + end:
             f.write(line)
@@ -448,13 +458,21 @@ def puzzle_lists(puzzles, filt):
 # Initialize AWS resources to talk to database
 db = boto3.resource('dynamodb')
 table = db.Table("turbot")
-puzzles, rounds = hunt_info(table, "mh2021")
+hunt, puzzles, rounds = hunt_info(table, "mh2021")
+
+# Create a directory for the hunt in the WEBROOT
+root = hunt_file(hunt, "")
+try:
+    os.mkdir(root)
+except FileExistsError:
+    #  We're happy as a clam if the directory already exists
+    pass
 
-overview(puzzles, rounds)
+overview(hunt, puzzles, rounds)
 for rnd in rounds:
-    round_overview(rnd, puzzles)
+    round_overview(hunt, rnd, puzzles)
 for puzzle in puzzles:
-    puzzle_overview(puzzle)
-puzzle_lists(puzzles, "All")
-puzzle_lists(puzzles, "Solved")
-puzzle_lists(puzzles, "Unsolved")
+    puzzle_overview(hunt, puzzle)
+puzzle_lists(hunt, puzzles, "All")
+puzzle_lists(hunt, puzzles, "Solved")
+puzzle_lists(hunt, puzzles, "Unsolved")