X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=html_generator.py;h=f18703211897d73f90fdec216645e24f22a53e33;hb=HEAD;hp=7d18e0a5738a111e9bd4acda97cccbb88d7d8338;hpb=5722eb825e11c817209e0ce4b29e8acfcb8b1e3d;p=turbot-web diff --git a/html_generator.py b/html_generator.py index 7d18e0a..f187032 100644 --- a/html_generator.py +++ b/html_generator.py @@ -5,7 +5,7 @@ Created on Thu Jan 6 23:35:23 2022 @author: Avram Gottschlich """ """ -I copied several functions from your code; +I copied several functions from your code; if it's easier to refer to them rather than copying them that's absolutely fine This rewrites the html each time it is called @@ -14,20 +14,46 @@ that would be great Requires sorttable.js, which should be included """ -from turbot.channel import channel_url +import boto3 from boto3.dynamodb.conditions import Key +import os +import re +import sys -website = "https://halibut.cworth.org/" -#change this if we're using AWS or some other subdomain instead +WEBROOT = "/srv/halibut.cworth.org/www" -def find_hunt_for_hunt_id(turb, hunt_id): + +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 internal_link(hunt, name): + """Returns a path for a link on this site.""" + + # Just generate a relative link, (which is just the name itself) + return "{}".format(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)""" + return re.sub(r'[ /]', '_', name) + +def channel_url(channel_id): + """Given a channel ID, return the URL for that channel.""" + + return "https://halibutthatbass.slack.com/archives/{}".format(channel_id) + +def find_hunt_for_hunt_id(table, hunt_id): """Given a hunt ID find the database item for that hunt Returns None if hunt ID is not found, otherwise a dictionary with all fields from the hunt's row in the table, (channel_id, active, hunt_id, name, url, sheet_url, etc.). """ - response = turb.table.get_item( + response = table.get_item( Key={ 'hunt_id': hunt_id, 'SK': 'hunt-{}'.format(hunt_id) @@ -38,10 +64,10 @@ def find_hunt_for_hunt_id(turb, hunt_id): else: return None -def hunt_puzzles_for_hunt_id(turb, hunt_id): +def hunt_puzzles_for_hunt_id(table, hunt_id): """Return all puzzles that belong to the given hunt_id""" - response = turb.table.query( + response = table.query( KeyConditionExpression=( Key('hunt_id').eq(hunt_id) & Key('SK').begins_with('puzzle-') @@ -59,16 +85,18 @@ def link(lin, text): #internal links, doesn't open new tab return '{}'.format(lin, text) -def hunt_info(turb, hunt): +def hunt_info(table, hunt_id): """ - Retrieves list of rounds, puzzles for the given hunt + Retrieves list of rounds, puzzles for the given hunt """ + + hunt = find_hunt_for_hunt_id(table, hunt_id) + name = hunt["name"] - hunt_id = hunt["hunt_id"] channel_id = hunt["channel_id"] - - puzzles = hunt_puzzles_for_hunt_id(turb, hunt_id) - + + puzzles = hunt_puzzles_for_hunt_id(table, hunt_id) + rounds = set() for puzzle in puzzles: if "rounds" not in puzzle: @@ -77,8 +105,8 @@ def hunt_info(turb, hunt): rounds.add(rnd) 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 @@ -90,7 +118,7 @@ def round_stat(rnd, puzzles): meta_solved = 0 for puzzle in puzzles: if "rounds" not in puzzle: - continue + continue if rnd in puzzle["rounds"]: if puzzle['type'] == 'meta': metas.append(puzzle) @@ -107,18 +135,19 @@ def round_stat(rnd, puzzles): unsolved_puzzles = sorted(unsolved_puzzles, key = lambda i: i['name']) rnd_puzzles = metas + unsolved_puzzles + solved_puzzles return puzzle_count, solved_count, rnd_puzzles, meta_solved, len(metas) - - - -def overview(puzzles, rounds): + + + +def overview(hunt, puzzles, rounds): #big board, main page. saves as index.html start = ['\n', '\n', '
\n', ' \n', ' \n', + ' \n', '\n', - ' \n', + ' \n', ' \n', '\n', 'Puzzles: {}/{}
\n'.format(solved_count, puzzle_count), 'Metas: {}/{}
\n'.format(meta_solved, metas), '{}
\n'.format(link(website + "index.html", 'Hunt Overview')), + '{}
\n'.format(link(internal_link(hunt, "index") + ".html", 'Hunt Overview')), '{} | \n'.format(elink(slack_url, puzzle['name']+meta)), - '{} | \n'.format(elink(puzzle['url'], 'Puzzle')), + '{} | \n'.format(elink(puzzle.get('url',''), 'Puzzle')), '{} | \n'.format(elink(puzzle['sheet_url'], 'Sheet')), - '{} | \n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')), - '{} | \n'.format(puzzle['solution']), + '{} | \n'.format(link(internal_link(hunt, filename_from_name(puzzle['name'])) + ".html", 'Overview')), + '{} | \n'.format(", ".join(puzzle['solution']).upper()), # '\n', - ' | {} | \n'.format("".join(puzzle['tags'])), + '{} | \n'.format(", ".join(puzzle.get('tags',[]))), '
{} | \n'.format(elink(slack_url, puzzle['name']+meta)), - '{} | \n'.format(elink(puzzle['url'], 'Puzzle')), + '{} | \n'.format(elink(puzzle.get('url',''), 'Puzzle')), '{} | \n'.format(elink(puzzle['sheet_url'], 'Sheet')), - '{} | \n'.format(link(website + "_".join(puzzle['name'].split()) + '.html', 'Overview')), + '{} | \n'.format(link(internal_link(hunt, filename_from_name(puzzle['name'])) + ".html", 'Overview')), '\n', # ' | \n', - ' | {} | \n'.format(" ".join(puzzle['tags'])), + '{} | \n'.format(", ".join(puzzle.get('tags',[]))), '
{}
'.format(link(website + 'index.html', 'Hunt Overview')), + '{}
'.format(link(internal_link(hunt, 'index') + ".html", 'Hunt Overview')), '\n', '\n', '\n'.format(status), @@ -300,7 +337,7 @@ def puzzle_overview(puzzle): '{} | \n'.format(elink(slack_url, 'Channel')), #slack channel - '{} | \n'.format(elink(puzzle['url'], 'Puzzle')), + '{} | \n'.format(elink(puzzle.get('url',''), 'Puzzle')), '{} | \n'.format(elink(puzzle['sheet_url'], 'Sheet')), 'Additional Resources | \n', '
Round(s): {} | \n'.format(" ".join(round_url)), #round page on our site - 'Tags: {} | \n'.format(" ".join(puzzle['tags'])), #add tags + 'Tags: {} | \n'.format(", ".join(puzzle.get('tags',[]))), #add tags '
Answer: {} | \n'.format(solution), @@ -319,14 +356,15 @@ def puzzle_overview(puzzle): '\n', '\n', '