Example #1
0
async def admin_control_handler(request: web_request.Request) -> CoroutineType:
    auth_key = request.match_info["authKey"]

    if key_valid(auth_key, admin_keys):
        this_week = compo.get_week(False)
        next_week = compo.get_week(True)

        data = await request.post()

        def data_param(week, param, field):
            nonlocal data

            if field in data:
                week[param] = data[field]

        data_param(this_week, "theme", "currentWeekTheme")
        data_param(this_week, "date", "currentWeekDate")
        data_param(next_week, "theme", "nextWeekTheme")
        data_param(next_week, "date", "nextWeekDate")

        if "submissionsOpen" in data:
            if data["submissionsOpen"] == "Yes":
                compo.get_week(True)["submissionsOpen"] = True
            if data["submissionsOpen"] == "No":
                compo.get_week(True)["submissionsOpen"] = False

        if "rolloutWeek" in data:
            if data["rolloutWeek"] == "on":
                compo.move_to_next_week()

        if "newEntryEntrant" in data:
            new_entry_week = True
            if "newEntryWeek" in data:
                new_entry_week = False

            new_entry_discord_id = None
            if "newEntryDiscordID" in data:
                if data["newEntryDiscordID"] != "":
                    try:
                        new_entry_discord_id = int(data["newEntryDiscordID"])
                    except ValueError:
                        new_entry_discord_id = None

            compo.create_blank_entry(data["newEntryEntrant"],
                                     new_entry_discord_id, new_entry_week)
        compo.save_weeks()
        return web.Response(status=204, text="Nice")
    else:
        return web.Response(status=404, text="File not found")
Example #2
0
async def submit(context: commands.Context) -> None:
    """
    Creates a submission entry for the user.
    Replies with a link to the management panel with options to create or edit.
    """
    week = compo.get_week(True)

    if not week["submissionsOpen"]:
        closed_info = "Sorry! Submissions are currently closed."
        await context.send(closed_info)
        return

    for entry in week["entries"]:
        if entry["discordID"] == context.author.id:
            key = http_server.create_edit_key(entry["uuid"])
            url = "%s/edit/%s" % (url_prefix(), key)
            edit_info = ("Link to edit your existing "
                         "submission: " + url + expiry_message())
            await context.send(edit_info)
            return

    new_entry = compo.create_blank_entry(context.author.name,
                                         context.author.id)
    key = http_server.create_edit_key(new_entry)
    url = "%s/edit/%s" % (url_prefix(), key)

    await context.send("Submission form: " + url + expiry_message())
Example #3
0
    def test_create_blank_entry_returns_valid_uuid(self):
        result = compo.create_blank_entry("wiglaf", discord_id="is still a wiener")

        try:
            uuid.UUID(result["uuid"])
        except ValueError:
            pytest.fail("create_blank_entry did not assign a valid uuid.")
Example #4
0
    def test_can_find_next_week(self):
        entry = compo.create_blank_entry("Findable Guy", 1)

        compo.next_week["entries"].append(entry)

        found_entry = compo.find_entry_by_uuid(entry["uuid"])
        assert found_entry is entry
Example #5
0
    def test_cant_find_nonexistant(self):
        entry = compo.create_blank_entry("Hidden Guy", 0)

        compo.current_week["entries"].append(entry)

        found_entry = compo.find_entry_by_uuid("???")
        assert found_entry is None
Example #6
0
async def submit(context: commands.Context) -> None:
    """Provides a link to submit your entry."""
    global config

    week = compo.get_week(True)

    if not week["submissionsOpen"]:
        closed_info = "Sorry! Submissions are currently closed."
        await context.send(closed_info)
        return

    for entry in week["entries"]:
        if entry["discordID"] == context.author.id:
            key = keys.create_edit_key(entry["uuid"])
            url = "%s/edit/%s" % (config["url_prefix"], key)
            edit_info = ("Link to edit your existing "
                         "submission: " + url + expiry_message())
            await context.send(edit_info)
            return

    new_entry = compo.create_blank_entry(context.author.name,
                                         context.author.id)
    week["entries"].append(new_entry)
    key = keys.create_edit_key(new_entry["uuid"])
    url = "%s/edit/%s" % (config["url_prefix"], key)

    await context.send("Submission form: " + url + expiry_message())
Example #7
0
    def test_only_invalid_entries_is_zero_valid(self):
        week = compo.blank_week()
        entry = compo.create_blank_entry("Invalid", 0)

        week["entries"].append(entry)

        assert not compo.entry_valid(entry)
        assert compo.count_valid_entries(week) == 0
Example #8
0
    def test_counts_valid_entries(self):
        week = compo.blank_week()
        entry = compo.create_blank_entry("Invalid", 0)
        entry["pdf"] = "yes"
        entry["pdfFilename"] = "here"
        entry["mp3"] = "yes"
        entry["mp3Format"] = "here"
        entry["mp3Filename"] = "yes"

        week["entries"].append(entry)

        assert compo.entry_valid(entry)
        assert compo.count_valid_entries(week) == 1
Example #9
0
async def admin_spoof_handler(request: web_request.Request) -> web.Response:
    """Create a fake new entry

       TODO: Take in more data?
    """
    auth_key = request.match_info["authKey"]

    if not keys.key_valid(auth_key, keys.admin_keys):
        return web.Response(status=401, text="Invalid or expired admin link")

    entry_data = await request.json()

    new_entry = compo.create_blank_entry(entry_data["entrantName"],
                                         int(entry_data["discordId"]))
    week = compo.get_week(entry_data["nextWeek"])
    week["entries"].append(new_entry)

    return web.Response(status=204, text="Nice")
Example #10
0
 def test_create_blank_entry_returns_string(self):
     result = compo.create_blank_entry("wiglaf", discord_id="is a wiener")
     assert type(result["uuid"]) is str
Example #11
0
async def on_message(message: discord.message.Message) -> CoroutineType:
    """
    Processes a message that is seen by the bot.

    Parameters
    ----------
    message : discord.message.Message
        The message seen by the bot
    """
    if message.author.id != client.user.id:
        if message.content.startswith(client.command_prefix):
            command = message.content[len(client.command_prefix):].lower()

            if command in ["postentries", "postentriespreview"] \
                    and str(message.author.id) in client.admins:
                week = compo.get_week(False)

                if command == "postentriespreview":
                    if not message.channel.type == discord.ChannelType.private:
                        await message.channel.send(dm_reminder)
                        return
                    week = compo.get_week(True)

                if command == "postentries" \
                        and postentries_channel != 0 \
                        and message.channel.id != postentries_channel \
                        and message.channel.type != discord.ChannelType.private:
                    await message.channel.send("This isn't the right channel"
                                               " for this!")
                    return

                async with message.channel.typing():
                    for entry in week["entries"]:
                        if not compo.entry_valid(entry):
                            continue

                        discord_user = client.get_user(entry["discordID"])

                        if discord_user is None:
                            entrant_ping = "@" + entry["entrantName"]
                        else:
                            entrant_ping = discord_user.mention

                        upload_files = []
                        upload_message = "%s - %s" % (entrant_ping,
                                                      entry["entryName"])

                        if "entryNotes" in entry:
                            upload_message += "\n" + entry["entryNotes"]

                        if entry["mp3Format"] == "mp3":
                            upload_files.append(
                                discord.File(io.BytesIO(bytes(entry["mp3"])),
                                             filename=entry["mp3Filename"]))
                        elif entry["mp3Format"] == "external":
                            upload_message += "\n" + entry["mp3"]

                        upload_files.append(
                            discord.File(io.BytesIO(bytes(entry["pdf"])),
                                         filename=entry["pdfFilename"]))

                        await message.channel.send(upload_message,
                                                   files=upload_files)

            if command == "manage" and str(message.author.id) in client.admins:
                if message.channel.type == discord.ChannelType.private:
                    key = http_server.create_admin_key()

                    url = "%s/admin/%s" % (url_prefix(), key)
                    await message.channel.send("Admin interface: " + url
                                               + expiry_message())
                    return

                else:

                    await message.channel.send(dm_reminder)
                    return

            if command == "submit":
                if message.channel.type == discord.ChannelType.private:
                    if not compo.get_week(True)["submissionsOpen"]:
                        closed_info = "Sorry! Submissions are currently closed."
                        await message.channel.send(closed_info)
                        return

                    week = compo.get_week(True)

                    for entry in week["entries"]:
                        if entry["discordID"] == message.author.id:
                            key = http_server.create_edit_key(entry["uuid"])
                            url = "%s/edit/%s" % (url_prefix(), key)
                            edit_info = ("Link to edit your existing "
                                         "submission: " + url
                                         + expiry_message())
                            await message.channel.send(edit_info)
                            return

                    new_entry = compo.create_blank_entry(
                        message.author.name, message.author.id)
                    key = http_server.create_edit_key(new_entry)
                    url = "%s/edit/%s" % (url_prefix(), key)

                    await message.channel.send("Submission form: " + url
                                               + expiry_message())
                    return

                else:
                    await message.channel.send(dm_reminder)
                    return

            if command == "help":
                await message.channel.send(help_message())
                return
        else:
            if message.channel.type == discord.ChannelType.private:
                await message.channel.send(help_message())
                return

            if str(client.user.id) in message.content:
                await message.channel.send(help_message())
                return