Skip to content

Commit

Permalink
lastmod arg supports datetime object
Browse files Browse the repository at this point in the history
  • Loading branch information
h-janes committed Nov 28, 2022
1 parent 9f97679 commit a27b876
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
13 changes: 10 additions & 3 deletions flask_sitemapper/sitemapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Provides the `URL` and `Sitemapper` classes"""

from datetime import datetime
from functools import wraps
from typing import Callable, Union

Expand All @@ -17,7 +18,7 @@ def __init__(
self,
endpoint,
scheme: str,
lastmod: str = None,
lastmod: Union[str, datetime] = None,
changefreq: str = None,
priority: Union[str, int, float] = None,
) -> None:
Expand All @@ -27,6 +28,9 @@ def __init__(
self.changefreq = changefreq
self.priority = priority

if isinstance(self.lastmod, datetime):
self.lastmod = self.lastmod.strftime("%Y-%m-%dT%H:%M:%S")

@property
def loc(self) -> str:
"""Finds the URL from the endpoint name. Must be called within a request context"""
Expand Down Expand Up @@ -77,7 +81,10 @@ def init_app(self, app: Flask) -> None:
self.deferred_functions.clear()

def include(
self, lastmod: str = None, changefreq: str = None, priority: Union[str, int, float] = None
self,
lastmod: Union[str, datetime] = None,
changefreq: str = None,
priority: Union[str, int, float] = None,
) -> Callable:
"""A decorator for view functions to add their URL to the sitemap"""

Expand Down Expand Up @@ -109,7 +116,7 @@ def __get_endpoint_name(self, func: Callable) -> str:
def add_endpoint(
self,
view_func: Union[Callable, str],
lastmod: str = None,
lastmod: Union[str, datetime] = None,
changefreq: str = None,
priority: Union[str, int, float] = None,
) -> None:
Expand Down
58 changes: 58 additions & 0 deletions tests/test_datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from datetime import datetime

import flask

from flask_sitemapper import Sitemapper

# ----------------- TEST APP -----------------

sitemapper = Sitemapper()
app = flask.Flask(__name__)
sitemapper.init_app(app)


@sitemapper.include(lastmod=datetime(2022, 11, 28))
@app.route("/")
def r_home():
return "<h1>Home</h1>"


@app.route("/sitemap.xml")
def r_sitemap():
return sitemapper.generate()


# ----------------- END TEST APP -----------------


def test_running():
with app.test_client() as test_client:
response = test_client.get("/")
assert response.text == "<h1>Home</h1>"


def test_status_code():
with app.test_client() as test_client:
response = test_client.get("/sitemap.xml")
assert response.status_code == 200


def test_mimetype():
with app.test_client() as test_client:
response = test_client.get("/sitemap.xml")
assert response.mimetype == "application/xml"


def test_xml():
with app.test_client() as test_client:
response = test_client.get("/sitemap.xml")
assert (
response.text
== """<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://localhost/</loc>
<lastmod>2022-11-28T00:00:00</lastmod>
</url>
</urlset>"""
)

0 comments on commit a27b876

Please sign in to comment.