Skip to content

Commit 93d49d2

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

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

sillysimple.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
HDR_FROM = "from"
3030
HDR_FROM_CONTACT = "from_contact"
31+
HDR_FROM_CONTACT_HIDE = "from_contact_hide"
3132
HDR_CONTENT_SPLITTER = "---"
3233

3334

@@ -122,6 +123,7 @@ 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+
comment.contact_hide = form.get("comment_contact_hide").strip()
125127
comment.paragraphs = form.get("comment").strip().replace("\r\n", "\n").split("\n\n")
126128
comment_fname = str(ulid.new())
127129

@@ -197,6 +199,7 @@ def __init__(self):
197199
self.created_on_dt = 0
198200
self.created_by = None
199201
self.created_by_contact = None
202+
self.contact_hide = True
200203
self.paragraphs = list()
201204

202205
def __repr__(self):
@@ -231,17 +234,22 @@ def from_path(cls, fpath: Path) -> Optional[Comment]:
231234
app_log.warn(f"Skipping file {fpath} (len {len(fpath.stem)})")
232235
return None
233236

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]
237+
meta_lines = [l.strip() for l in comment_meta.split("\n")]
238+
239+
for line in meta_lines:
240+
key, val = line.split(":", 1)
241+
if key.startswith(HDR_FROM):
242+
c.created_by = val
243+
elif key.startswith(HDR_FROM_CONTACT):
244+
c.created_by_contact = val
245+
elif key.startswith(HDR_FROM_CONTACT_HIDE):
246+
c.contact_hide = True if val == "True" else False
239247

240248
c.paragraphs = comment_paragraphs[1:]
241249

242250
return c
243251

244-
def dump_into_file(self, fpath: list[str], fname: str) -> Optional[Path]:
252+
def dump_into_file(self, fpath: list[str], fname: str) -> Optional[Path]:
245253
if self.created_by is None:
246254
return None
247255

@@ -260,6 +268,7 @@ def dump_into_file(self, fpath: list[str], fname: str) -> Optional[Path]:
260268
with p.open(mode="x") as new_comment_file:
261269
new_comment_file.write(f"{HDR_FROM}:{self.created_by}\n")
262270
new_comment_file.write(f"{HDR_FROM_CONTACT}:{self.created_by_contact}\n")
271+
new_comment_file.write(f"{HDR_FROM_CONTACT_HIDE}:{self.contact_hide}\n")
263272
new_comment_file.write(f"\n{HDR_CONTENT_SPLITTER}\n")
264273
for par in self.paragraphs:
265274
new_comment_file.write("\n")
@@ -337,6 +346,8 @@ def dump_into_file(self, fpath: list[str], fname: str) -> Optional[Path]:
337346
<form hx-post="{{ remote }}/{{ endpoint }}" hx-vals='{"for": "{{ which }}" }' hx-target="#comments" hx-swap="outerHTML" enctype="multipart/form-data">
338347
<input type="text" id="comment_author" name="comment_author" placeholder="Name" required><br>
339348
<input type="text" id="comment_contact" name="comment_contact" placeholder="e-mail or other contact info"><br>
349+
<input type="checkbox" id="comment_contact_hide" name="hide" checked />
350+
<label for="comment_contact_hide">Hide contact info</label>
340351
<textarea id="comment" name="comment" placeholder="Comment..." required></textarea><br>
341352
<button id="submit" class="custom-file-upload" type="submit">Submit</button>
342353
</form>
@@ -354,10 +365,12 @@ def dump_into_file(self, fpath: list[str], fname: str) -> Optional[Path]:
354365
<div class="comment-meta">
355366
<div class="comment-author">
356367
<span>{{ cobject.created_by | e }}</span>
368+
{%- if not cobject.contact_hide -%}
357369
{%- if cobject.created_by_contact | length -%}
358370
<span>,</span>
359371
<span>{{ cobject.created_by_contact | e }}</span>
360372
{%- endif -%}
373+
{%- endif -%}
361374
</div>
362375
<div class="comment-date">
363376
<span>{{ cobject.created_on_dt.date() }}</span>

0 commit comments

Comments
 (0)