8000 add days until bot command and basic view (#29) · EuroPython/internal-bot@91c1bbe · GitHub
[go: up one dir, main page]

Skip to content

Commit 91c1bbe

Browse files
artczclytaemnestra
andauthored
add days until bot command and basic view (#29)
This is to demonstrate testing things that are date-specific, and add a basic implementation of a view that renders a template. Co-authored-by: Mia Bajić <38294198+clytaemnestra@users.noreply.github.com>
1 parent 391ae0e commit 91c1bbe

File tree

10 files changed

+100
-5
lines changed

10 files changed

+100
-5
lines changed

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ help:
3030
# Local development
3131
# =================
3232

33+
db:
34+
docker compose up -d
35+
3336
server:
3437
$(DEV_CMD) runserver 0.0.0.0:4672
3538

intbot/core/bot/main.py

+10
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ async def poll_database():
189189
print("Channel does not exist!")
190190

191191

192+
@bot.command()
193+
async def until(ctx):
194+
"""
195+
Returns time left until the conference
196+
"""
197+
delta = settings.CONFERENCE_START - timezone.now()
198+
199+
await ctx.send(f"{delta.days} days left until the conference")
200+
201+
192202
def run_bot():
193203
bot_token = settings.DISCORD_BOT_TOKEN
194204
bot.run(bot_token)

intbot/core/views.py

+9
Original file line numberDiff line numberDiff line change
8000
@@ -0,0 +1,9 @@
1+
from django.conf import settings
2+
from django.template.response import TemplateResponse
3+
from django.utils import timezone
4+
5+
6+
def days_until(request):
7+
delta = settings.CONFERENCE_START - timezone.now()
8+
9+
return TemplateResponse(request, "days_until.html", {"days_until": delta.days})

intbot/intbot/settings.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
import os
1414
import warnings
15-
from typing import Any
15+
from datetime import datetime, timezone
1616
from pathlib import Path
17+
from typing import Any
1718

1819
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1920
BASE_DIR = Path(__file__).resolve().parent.parent
@@ -52,7 +53,9 @@
5253
TEMPLATES = [
5354
{
5455
"BACKEND": "django.template.backends.django.DjangoTemplates",
55-
"DIRS": [],
56+
"DIRS": [
57+
BASE_DIR / "templates",
58+
],
5659
"APP_DIRS": True,
5760
"OPTIONS": {
5861
"context_processors": [
@@ -117,6 +120,9 @@
117120
TASKS: dict[str, Any]
118121

119122

123+
CONFERENCE_START = datetime(2025, 7, 14, tzinfo=timezone.utc)
124+
125+
120126
# There are bunch of settings that we can skip on dev/testing environments if
121127
# not used - that should be always present on prod/staging deployments.
122128
# Instead of repeating them per-env below, they go here.

intbot/intbot/urls.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
internal_webhook_endpoint,
55
zammad_webhook_endpoint,
66
)
7+
from core.views import days_until
78
from django.contrib import admin
89
from django.urls import path
910

1011
urlpatterns = [
1112
path("admin/", admin.site.urls),
1213
path("", index),
13-
# Internal Webhooks
14+
# Webhooks
1415
path("webhook/internal/", internal_webhook_endpoint),
1516
path("webhook/github/", github_webhook_endpoint),
1617
path("webhook/zammad/", zammad_webhook_endpoint),
18+
# Public Pages
19+
path("days-until/", days_until),
1720
]

intbot/templates/days_until.html

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<body>
3+
<style type="text/css">
4+
body { background-color: #f5e5d6; color: #151f38; font-family: sans-serif;}
5+
h1 { font-size: 3em; text-align: center; margin-top: 3em; }
6+
</style>
7+
<h1>{{ days_until }} days until the conference</h1>
8+
</body>
9+
</html>

intbot/tests/test_bot/test_main.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from unittest.mock import AsyncMock, patch
2-
import discord
32

3+
import discord
44
import pytest
55
from asgiref.sync import sync_to_async
6-
from core.bot.main import ping, poll_database, qlen, source, version, wiki, close
6+
from core.bot.main import close, ping, poll_database, qlen, source, until, version, wiki
77
from core.models import DiscordMessage
88
from django.utils import timezone
9+
from freezegun import freeze_time
910

1011

1112
@pytest.mark.asyncio
@@ -189,3 +190,13 @@ async def test_polling_messages_sends_message_if_not_sent_and_sets_sent_at():
189190
assert dm.sent_at is not None
190191
end = timezone.now()
191192
assert start < dm.sent_at < end
193+
194+
195+
@pytest.mark.asyncio
196+
@freeze_time("2025-04-05")
197+
async def test_until():
198+
ctx = AsyncMock()
199+
200+
await until(ctx)
201+
202+
ctx.send.assert_called_once_with("100 days left until the conference")

intbot/tests/test_views.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from pytest_django.asserts import assertTemplateUsed
2+
3+
4+
def test_days_until_view(client):
5+
response = client.get("/days-until/")
6+
7+
assert response.status_code == 200
8+
assertTemplateUsed(response, "days_until.html")

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies = [
2424
"pytest-socket>=0.7.0",
2525
"respx>=0.22.0",
2626
"pydantic>=2.10.6",
27+
"freezegun>=1.5.1",
2728
]
2829

2930
[tool.pytest.ini_options]

uv.lock

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0