Skip to content

Commit dcc5ed6

Browse files
committed
Add 'hide contact' checkbox
1 parent 8d76137 commit dcc5ed6

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

comments/example/01H7A86Y8EM4DM3FM25EWPHB8W.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from:michal
1+
from_name:michal
22
from_contact:michal@gmail.com
3+
from_hide_contact:False
34

45
---
56

comments/example/deeper/01H7A86Y8EM4DM3FM25EWPHB8W.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from:niemichal
1+
from_name:niemichal
22
from_contact:niemichal@gmail.com
33

44
---

sillysimple.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
# That's what URL_PREFIX is for.
2727
URL_PREFIX = "/comments"
2828

29-
HDR_FROM = "from"
29+
HDR_FROM = "from_name"
3030
HDR_FROM_CONTACT = "from_contact"
31+
HDR_FROM_CONTACT_HIDE = "from_hide_contact"
3132
HDR_CONTENT_SPLITTER = "---"
3233

3334

@@ -122,6 +123,12 @@ def comments_for_article():
122123
comment = Comment()
123124
comment.created_by = form.get("comment_author").strip()
124125
comment.created_by_contact = form.get("comment_contact").strip()
126+
# Existence of 'hide' key implies that it's checked. Otherwise the form doesn't
127+
# even send it.
128+
if form.get("hide"):
129+
pass
130+
else:
131+
comment.contact_hide = False
125132
comment.paragraphs = form.get("comment").strip().replace("\r\n", "\n").split("\n\n")
126133
comment_fname = str(ulid.new())
127134

@@ -197,10 +204,11 @@ def __init__(self):
197204
self.created_on_dt = 0
198205
self.created_by = None
199206
self.created_by_contact = None
207+
self.contact_hide = True
200208
self.paragraphs = list()
201209

202210
def __repr__(self):
203-
ret = f"{self.created_by},{self.created_by_contact},{self.paragraphs}"
211+
ret = f"{self.created_by},{self.created_by_contact},{self.contact_hide},{self.paragraphs}"
204212
return ret
205213

206214
@classmethod
@@ -231,17 +239,22 @@ def from_path(cls, fpath: Path) -> Optional[Comment]:
231239
app_log.warn(f"Skipping file {fpath} (len {len(fpath.stem)})")
232240
return None
233241

234-
for line in comment_meta.split("\n"):
235-
if (HDR_FROM + ":") in line:
236-
c.created_by = line.split(":", maxsplit=1)[1]
237-
elif (HDR_FROM_CONTACT + ":") in line:
238-
c.created_by_contact = line.split(":", maxsplit=1)[1]
242+
meta_lines = [l.strip() for l in comment_meta.split("\n") if len(l) > 0]
243+
244+
for line in meta_lines:
245+
key, val = line.split(":", 1)
246+
if HDR_FROM in key:
247+
c.created_by = val
248+
elif HDR_FROM_CONTACT in key:
249+
c.created_by_contact = val
250+
elif HDR_FROM_CONTACT_HIDE in key:
251+
c.contact_hide = True if val == "True" else False
239252

240253
c.paragraphs = comment_paragraphs[1:]
241254

242255
return c
243256

244-
def dump_into_file(self, fpath: list[str], fname: str) -> Optional[Path]:
257+
def dump_into_file(self, fpath: list[str], fname: str) -> Optional[Path]:
245258
if self.created_by is None:
246259
return None
247260

@@ -260,6 +273,7 @@ def dump_into_file(self, fpath: list[str], fname: str) -> Optional[Path]:
260273
with p.open(mode="x") as new_comment_file:
261274
new_comment_file.write(f"{HDR_FROM}:{self.created_by}\n")
262275
new_comment_file.write(f"{HDR_FROM_CONTACT}:{self.created_by_contact}\n")
276+
new_comment_file.write(f"{HDR_FROM_CONTACT_HIDE}:{self.contact_hide}\n")
263277
new_comment_file.write(f"\n{HDR_CONTENT_SPLITTER}\n")
264278
for par in self.paragraphs:
265279
new_comment_file.write("\n")
@@ -336,7 +350,11 @@ def dump_into_file(self, fpath: list[str], fname: str) -> Optional[Path]:
336350
<div class="comment-submit">
337351
<form hx-post="{{ remote }}/{{ endpoint }}" hx-vals='{"for": "{{ which }}" }' hx-target="#comments" hx-swap="outerHTML" enctype="multipart/form-data">
338352
<input type="text" id="comment_author" name="comment_author" placeholder="Name" required><br>
339-
<input type="text" id="comment_contact" name="comment_contact" placeholder="e-mail or other contact info"><br>
353+
<div>
354+
<input type="text" id="comment_contact" name="comment_contact" placeholder="e-mail or other contact info">
355+
<input type="checkbox" id="comment_contact_hide" name="hide" checked />
356+
<label for="comment_contact_hide">Hide contact info</label>
357+
</div>
340358
<textarea id="comment" name="comment" placeholder="Comment..." required></textarea><br>
341359
<button id="submit" class="custom-file-upload" type="submit">Submit</button>
342360
</form>
@@ -354,10 +372,13 @@ def dump_into_file(self, fpath: list[str], fname: str) -> Optional[Path]:
354372
<div class="comment-meta">
355373
<div class="comment-author">
356374
<span>{{ cobject.created_by | e }}</span>
375+
{%- if cobject.contact_hide -%}
376+
{%- else -%}
357377
{%- if cobject.created_by_contact | length -%}
358378
<span>,</span>
359379
<span>{{ cobject.created_by_contact | e }}</span>
360380
{%- endif -%}
381+
{%- endif -%}
361382
</div>
362383
<div class="comment-date">
363384
<span>{{ cobject.created_on_dt.date() }}</span>

0 commit comments

Comments
 (0)