# Buggy code
def to_json(self):
    # type: () -> Dict[str, Union[str, int, bool]]
    json_submission = {
        "source_url": url_for("api.single_source", source_uuid=self.source.uuid),
        "reply_url": url_for(
            "api.single_reply", source_uuid=self.source.uuid, reply_uuid=self.uuid
        ),
        "filename": self.filename,
        "size": self.size,
        "journalist_username": self.journalist.username,
        "journalist_first_name": self.journalist.first_name,
        "journalist_last_name": self.journalist.last_name,
        "journalist_uuid": self.journalist.uuid,
        "uuid": self.uuid,
        "is_deleted_by_source": self.deleted_by_source,
    }
    return json_submission


# Traceback (AttributeError)
172.17.0.1 - - [31/Mar/2020 20:57:10] "GET /api/v1/replies HTTP/1.1" 500 -
Traceback (most recent call last):
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/venvs/securedrop-app-code/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/redshiftzero/Documents/Github/securedrop-1.2.0-rc2/securedrop/securedrop/journalist_app/api.py", line 48, in decorated_function
return f(*args, **kwargs)
File "/Users/redshiftzero/Documents/Github/securedrop-1.2.0-rc2/securedrop/securedrop/journalist_app/api.py", line 310, in get_all_replies
{'replies': [reply.to_json() for reply in replies]}), 200
File "/Users/redshiftzero/Documents/Github/securedrop-1.2.0-rc2/securedrop/securedrop/journalist_app/api.py", line 310, in <listcomp>
{'replies': [reply.to_json() for reply in replies]}), 200
File "/Users/redshiftzero/Documents/Github/securedrop-1.2.0-rc2/securedrop/securedrop/models.py", line 289, in to_json
'journalist_username': self.journalist.username,
AttributeError: 'NoneType' object has no attribute 'username'

# Fixed code
def to_json(self):
    # type: () -> Dict[str, Union[str, int, bool]]
    username = "deleted"
    first_name = ""
    last_name = ""
    uuid = "deleted"
    if self.journalist:
        username = self.journalist.username
        first_name = self.journalist.first_name
        last_name = self.journalist.last_name
        uuid = self.journalist.uuid
    json_submission = {
        "source_url": url_for("api.single_source", source_uuid=self.source.uuid),
        "reply_url": url_for(
            "api.single_reply", source_uuid=self.source.uuid, reply_uuid=self.uuid
        ),
        "filename": self.filename,
        "size": self.size,
        "journalist_username": username,
        "journalist_first_name": first_name,
        "journalist_last_name": last_name,
        "journalist_uuid": uuid,
        "uuid": self.uuid,
        "is_deleted_by_source": self.deleted_by_source,
    }
    return json_submission