Skip to content

Commit

Permalink
refactor(Transcript): render via Jinja template
Browse files Browse the repository at this point in the history
- TODO(pairing): how best to make Item types available to Jinja?
- TODO(pairing): consider location of Jinja Environment object and template path
- TODO(pairing): reconsider test scope
- TODO(research): localization
  • Loading branch information
cfm committed Nov 2, 2022
1 parent 3f4648d commit d1e8266
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 61 deletions.
10 changes: 2 additions & 8 deletions securedrop_client/conversation/transcript/items/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@


class File(Item):
type = "file"

def __init__(self, record: database.File):
super().__init__()

self.filename = record.filename
self.sender = record.source.journalist_designation

@property
def context(self) -> Optional[str]:
return _("{username} sent:\n").format(username=self.sender)

@property
def transcript(self) -> str:
return _("File: {filename}\n").format(filename=self.filename)
10 changes: 1 addition & 9 deletions securedrop_client/conversation/transcript/items/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,4 @@


class Item:
@property
def transcript(self) -> str:
"""A transcription of the conversation item."""
raise NotImplementedError # pragma: nocover

@property
def context(self) -> Optional[str]:
"""Some context about the conversation item."""
raise NotImplementedError # pragma: nocover
type = None
10 changes: 2 additions & 8 deletions securedrop_client/conversation/transcript/items/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@


class Message(Item):
type = "message"

def __init__(self, record: Union[database.Message, database.Reply]):
super().__init__()

Expand All @@ -16,11 +18,3 @@ def __init__(self, record: Union[database.Message, database.Reply]):
self.sender = record.source.journalist_designation
else:
self.sender = record.journalist.username

@property
def context(self) -> Optional[str]:
return _("{username} wrote:\n").format(username=self.sender)

@property
def transcript(self) -> str:
return self.content + "\n"
39 changes: 12 additions & 27 deletions securedrop_client/conversation/transcript/transcript.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from jinja2 import Environment, PackageLoader, select_autoescape
from typing import List, Optional

from securedrop_client import db as database
Expand All @@ -6,39 +7,23 @@
from .items import transcribe as transcribe_item


def transcribe(record: database.Base) -> Optional[Item]:
return transcribe_item(record)
env = Environment(
loader=PackageLoader("securedrop_client"),
autoescape=select_autoescape(),
# Since our plain-text templates have literal whitespace:
lstrip_blocks=True,
trim_blocks=True,
)


_ENTRY_SEPARATOR = "------\n"
def transcribe(record: database.Base) -> Optional[Item]:
return transcribe_item(record)


class Transcript:
def __init__(self, conversation: database.Source) -> None:

self._items = [transcribe(record) for record in conversation.collection]
self._template = env.get_template("transcript.txt")

def __str__(self) -> str:
if len(self._items) <= 0:
return "No messages."

entries: List[str] = []

context: Optional[str] = None

for item in self._items:
if item is None:
continue

if context is not None and context == item.context:
entry = item.transcript
elif item.context is None:
entry = item.transcript # pragma: nocover
else:
entry = f"{item.context}{item.transcript}"

entries.append(entry)

context = item.context

return _ENTRY_SEPARATOR.join(entries)
return self._template.render(items=self._items)
9 changes: 0 additions & 9 deletions securedrop_client/locale/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ msgstr ""
msgid "Failed to delete source at server"
msgstr ""

msgid "{username} sent:\n"
msgstr ""

msgid "File: {filename}\n"
msgstr ""

msgid "{username} wrote:\n"
msgstr ""

msgid "Download All Files"
msgstr ""

Expand Down
16 changes: 16 additions & 0 deletions securedrop_client/templates/transcript.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% if items|length <= 0 %}No messages.{% else %}
{% for item in items %}
{% if item.type == "message" %}
{% if loop.changed(item.sender) %}
{{ item.sender }} wrote:
{% endif %}
{{ item.content }}
{% elif item.type == "file" %}
{{ item.sender }} sent:
File: {{ item.filename }}
{% endif %}
{% if item.type is defined and not loop.last %}
------
{% endif %}
{% endfor %}
{% endif %}

0 comments on commit d1e8266

Please sign in to comment.