-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85e1107
commit 7d3b9c7
Showing
11 changed files
with
276 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,35 @@ | ||
import aiohttp_jinja2 | ||
from aiohttp import web | ||
from jinja2 import FileSystemLoader | ||
|
||
from . import views | ||
from . import templates, views | ||
from .config import Config | ||
|
||
|
||
def get_aiohttp_app(config: Config) -> web.Application: | ||
app = web.Application() | ||
|
||
jinja2_env = aiohttp_jinja2.setup( | ||
app, | ||
loader=FileSystemLoader(templates.TEMPLATES_DIR), | ||
context_processors=[ | ||
aiohttp_jinja2.request_processor, | ||
templates.config_context_processor, | ||
], | ||
) | ||
|
||
jinja2_env.filters["webcal_url"] = templates.webcal_url | ||
|
||
app["config"] = config | ||
|
||
app.add_routes( | ||
[ | ||
web.get("/.health/", views.healthcheck), | ||
web.get("/{slug}.ics", views.calendar), | ||
web.get("/.health/", views.healthcheck, name="healthcheck"), | ||
web.get("/{slug}.ics", views.calendar, name="calendar"), | ||
] | ||
) | ||
|
||
if config.listing.enabled: | ||
app.add_routes([web.get("/all/", views.calendar_listing, name="listing")]) | ||
|
||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from pathlib import Path | ||
|
||
import jinja2 | ||
from aiohttp import web | ||
from yarl import URL | ||
|
||
from .config import CalendarConfig | ||
|
||
TEMPLATES_DIR = Path(__file__).resolve().parent / "templates" | ||
|
||
|
||
async def config_context_processor(request: web.Request) -> dict: | ||
return {"config": request.app["config"]} | ||
|
||
|
||
@jinja2.pass_context | ||
def webcal_url(context: dict, calendar_config: CalendarConfig) -> URL: | ||
request: web.Request = context["request"] | ||
config = context["config"] | ||
calendar_url = context["url"](context, "calendar", slug=calendar_config.slug) | ||
|
||
url = request.url.with_scheme("webcal").with_path(calendar_url.path) | ||
|
||
if config.listing.include_credentials and calendar_config.auth is not None: | ||
url = url.with_user(calendar_config.auth.username).with_password( | ||
calendar_config.auth.password | ||
) | ||
|
||
return url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Calmerge</title> | ||
<meta name="robots" content="noindex" /> | ||
<style> | ||
table { | ||
min-width: 50vw; | ||
max-width: 100vw; | ||
} | ||
|
||
td { | ||
text-align: center; | ||
padding: 0.75rem; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Calmerge</h1> | ||
<table> | ||
<tr> | ||
<th>URL</th> | ||
<th>Name</th> | ||
<th>Description</th> | ||
<th>Offsets</th> | ||
<th>TTL</th> | ||
<th>Auth</th> | ||
<th>+</th> | ||
</tr> | ||
{% for calendar in config.calendars|sort(attribute="slug") %} | ||
<tr> | ||
{% with calendar_url = url('calendar', slug=calendar.slug) %} | ||
<td><a href="{{ calendar_url }}">{{ calendar_url }}</a></td> | ||
{% endwith %} | ||
<td>{{ calendar.name|default("-", true) }}</td> | ||
<td>{{ calendar.description|default("-", true) }}</td> | ||
<td>{{ calendar.offset_days|sort|join(", ") }}</td> | ||
<td>{{ calendar.ttl_hours }} hours</td> | ||
{% if config.listing.include_credentials and calendar.auth %} | ||
<td><code>{{ calendar.auth.username }}:{{ calendar.auth.password }}</code></td> | ||
{% else %} | ||
<td>{{ "Yes" if calendar.auth else "No" }}</td> | ||
{% endif %} | ||
<td><a href="{{ calendar|webcal_url }}">Add to calendar</a></td> | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from aiohttp import BasicAuth | ||
from aiohttp.test_utils import TestClient | ||
|
||
from calmerge.config import Config | ||
|
||
|
||
async def test_listing_view(client: TestClient, config: Config) -> None: | ||
response = await client.get("/all/", auth=BasicAuth("user", "password")) | ||
assert response.status == 200 | ||
|
||
|
||
async def test_requires_auth(client: TestClient, config: Config) -> None: | ||
response = await client.get("/all/") | ||
assert response.status == 401 | ||
|
||
|
||
async def test_webcal_url(client: TestClient) -> None: | ||
response = await client.get("/all/", auth=BasicAuth("user", "password")) | ||
assert response.status == 200 | ||
|
||
text = await response.text() | ||
|
||
assert f"webcal://127.0.0.1:{client.port}/python.ics" in text | ||
assert f"webcal://user:[email protected]:{client.port}/python-authed.ics" in text |