Skip to content

Commit 11545b3

Browse files
committed
Update tests.
1 parent 63bc3fb commit 11545b3

31 files changed

Lines changed: 696 additions & 127 deletions

coltrane/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def _get_template_tag_module_name(base_dir: Path, file: Path) -> str:
8181
return module_name
8282

8383

84-
def _get_default_template_settings(base_dir: Path):
84+
def _get_default_template_settings(base_dir: Path) -> List:
8585
"""
8686
Gets default template settings, including templates and built-in template tags.
8787
"""

coltrane/management/commands/build.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from django.conf import settings
1212
from django.core import management
13-
from django.core.management.base import BaseCommand, CommandError
13+
from django.core.management.base import BaseCommand
1414

1515
from halo import Halo
1616
from log_symbols.symbols import LogSymbols
@@ -168,6 +168,7 @@ def handle(self, *args, **options):
168168
self.output_result_counts.create_count = 0
169169
self.output_result_counts.update_count = 0
170170
self.output_result_counts.skip_count = 0
171+
self.errors = []
171172

172173
start_time = time.time()
173174

@@ -216,7 +217,6 @@ def handle(self, *args, **options):
216217
logger.exception(ex)
217218

218219
spinner.start("Create HTML files")
219-
errors = []
220220

221221
with ThreadPoolExecutor(max_workers=self.threads_count) as executor:
222222
logger.debug(f"Multithread with {self.threads_count} threads")
@@ -243,7 +243,7 @@ def handle(self, *args, **options):
243243
error_detail = f"'{error_detail}"
244244

245245
error_message = f"Rendering {path} failed. {error_detail}"
246-
errors.append(error_message)
246+
self.errors.append(error_message)
247247

248248
result_msg = f"Create {self.output_result_counts.create_count} HTML files, {self.output_result_counts.skip_count} unmodified, {self.output_result_counts.update_count} updated"
249249

@@ -256,12 +256,12 @@ def handle(self, *args, **options):
256256

257257
elapsed_time = time.time() - start_time
258258

259-
for error_message in errors:
259+
for error_message in self.errors:
260260
spinner.fail(error_message)
261261

262262
self.stdout.write()
263263

264-
if errors:
264+
if self.errors:
265265
self.stderr.write(
266266
self.style.ERROR(
267267
f"Static site output completed with errors in {elapsed_time:.4f}s\n"

coltrane/manifest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ def render_html(self):
109109
"""
110110

111111
# Mock an HttpRequest when generating the HTML for static sites
112-
request = StaticRequest(path=self.directory)
112+
request = StaticRequest(path=self.directory, META={})
113113

114114
(template, context) = render_markdown(self.slug, request)
115115
rendered_html = render_to_string(template, context)
116+
print("rendered_htmlrendered_htmlrendered_html", rendered_html)
116117

117118
return rendered_html
118119

coltrane/renderer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class StaticRequest:
2929
"""
3030

3131
path: str
32+
META: Dict
3233

3334

3435
def render_markdown_path(path) -> Dict[str, Optional[Dict]]:

coltrane/retriever.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def get_data() -> Dict:
4141
data_directory = get_data_directory()
4242

4343
for path in data_directory.rglob("*.json"):
44-
if path.is_file:
44+
if path.is_file():
4545
directory_without_base_and_file_name = (
4646
(str(path)).replace(str(data_directory), "").replace(path.name, "")
4747
)
@@ -81,5 +81,5 @@ def get_content_paths(slug: str = None) -> Iterable[Path]:
8181
paths = directory.rglob("*.md")
8282

8383
for path in paths:
84-
if path.is_file:
84+
if path.is_file():
8585
yield path

coltrane/sitemaps.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import Path
2+
13
from django.contrib.sitemaps import Sitemap
24

35
from coltrane.retriever import get_content_paths
@@ -8,10 +10,11 @@ class ContentSitemap(Sitemap):
810
priority = 0.5
911

1012
def items(self):
13+
# TODO: This could get cached so it isn't re-generated every time
1114
return list(get_content_paths())
1215

13-
def location(self, obj):
14-
relative_url = str(obj)[7:-3]
16+
def location(self, path: Path):
17+
relative_url = str(path)[7:-3]
1518

1619
if relative_url.endswith("/index"):
1720
relative_url = relative_url[:-6]

coltrane/templates/coltrane/base.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<!DOCTYPE html>
2-
<html lang="{{ lang|default:"en" }}">
2+
<html lang="{% if lang %}{{ lang }}{% else %}en{% endif %}">
33

44
<head>
55
<meta charset="utf-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<title>{{ title|default:"" }}</title>
7+
<title>{% if title %}{{ title }}{% endif %}</title>
88
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css">
99
</head>
1010

coltrane/templatetags/coltrane_tags.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from typing import Dict
1+
from typing import Dict, Union
22

33
from django import template
4+
from django.core.handlers.wsgi import WSGIRequest
45
from django.template.base import Node
56
from django.template.exceptions import TemplateSyntaxError
67
from django.template.loader_tags import construct_relative_path
@@ -17,18 +18,24 @@
1718
register = template.Library()
1819

1920

21+
class NoParentError(Exception):
22+
pass
23+
24+
2025
@register.simple_tag(takes_context=True)
21-
def directory_contents(context, directory: str = None) -> Dict[str, str]:
26+
def directory_contents(
27+
context, directory: str = None, exclude: str = None
28+
) -> Dict[str, str]:
2229
if not directory:
2330
request = context["request"]
2431
directory = request.path
25-
26-
if directory.startswith("/"):
27-
directory = directory[1:]
2832
elif isinstance(directory, SafeString):
2933
# Force SafeString to be a normal string so it can be used with `Path` later
3034
directory = directory + ""
3135

36+
if directory.startswith("/"):
37+
directory = directory[1:]
38+
3239
content_paths = get_content_paths(directory)
3340
contents = []
3441

@@ -42,10 +49,12 @@ def directory_contents(context, directory: str = None) -> Dict[str, str]:
4249
content_slug = f"{directory}/{path_slug}"
4350
content_directory = content_directory / directory
4451

45-
directory_without_name = str(path).replace(path.name, "")[:-1]
52+
if exclude:
53+
if exclude.startswith("/"):
54+
exclude = exclude[1:]
4655

47-
if str(content_directory) != directory_without_name:
48-
continue
56+
if exclude == content_slug:
57+
continue
4958

5059
(_, metadata) = get_html_and_markdown(content_slug)
5160

@@ -54,6 +63,26 @@ def directory_contents(context, directory: str = None) -> Dict[str, str]:
5463
return contents
5564

5665

66+
@register.filter()
67+
def parent(path: Union[str, WSGIRequest] = "") -> str:
68+
if hasattr(path, "path"):
69+
# Handle if a `request` is passed in
70+
path = path.path
71+
72+
path = path.strip()
73+
74+
if path.endswith("/"):
75+
path = path[:-1]
76+
77+
if path == "":
78+
raise NoParentError()
79+
80+
last_slash_index = path.rindex("/")
81+
path = path[:last_slash_index]
82+
83+
return path
84+
85+
5786
class IncludeMarkdownNode(Node):
5887
"""
5988
Based on: `django.template.loader_tags.IncludeNode`.

coltrane/urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
app_name = "coltrane"
1111

12+
sitemaps = {"content": ContentSitemap}
13+
1214
urlpatterns = []
1315

1416
if settings.DEBUG:
1517
urlpatterns += [
1618
path("__reload__/", include("django_browser_reload.urls")),
1719
]
1820

19-
sitemaps = {"content": ContentSitemap}
20-
2121
urlpatterns += [
2222
path(
2323
"sitemap.xml",

coltrane/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def dict_merge(
4444
return source
4545

4646

47-
def threadpool(f, executor=None):
47+
def threadpool(func):
4848
"""
4949
A decorator to convert a regular function so that it gets run in another thread.
5050
@@ -59,8 +59,8 @@ def threadpool(f, executor=None):
5959
More details: https://stackoverflow.com/a/14331755
6060
"""
6161

62-
@wraps(f)
62+
@wraps(func)
6363
def wrap(*args, **kwargs):
64-
return (executor or ThreadPoolExecutor()).submit(f, *args, **kwargs)
64+
return ThreadPoolExecutor().submit(func, *args, **kwargs)
6565

6666
return wrap

0 commit comments

Comments
 (0)