Skip to content

Commit

Permalink
Use SearchSourcesAction in main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nabla-c0d3 committed Mar 5, 2023
1 parent 5d2c405 commit 276806a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 63 deletions.
3 changes: 1 addition & 2 deletions securedrop/journalist.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from actions.sources import SearchSourcesAction
from actions.sources_actions import SearchSourcesAction
from db import db
from encryption import EncryptionManager, GpgKeyNotFoundError
from execution import asynchronous
from journalist_app import create_app
from models import Source
from sdconfig import SecureDropConfig

config = SecureDropConfig.get_current()
Expand Down
72 changes: 23 additions & 49 deletions securedrop/journalist_app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import store
import werkzeug

from actions.sources_actions import SearchSourcesAction
from db import db
from encryption import EncryptionManager
from flask import (
Expand Down Expand Up @@ -65,57 +67,29 @@ def logout() -> werkzeug.Response:

@view.route("/")
def index() -> str:
# Gather the count of unread submissions for each source
# ID. This query will be joined in the queries for starred and
# unstarred sources below, and the unread counts added to
# their result sets as an extra column.
unread_stmt = (
db.session.query(Submission.source_id, func.count("*").label("num_unread"))
.filter_by(seen_files=None, seen_messages=None)
.group_by(Submission.source_id)
.subquery()
)

# Query for starred sources, along with their unread
# submission counts.
starred = (
db.session.query(Source, unread_stmt.c.num_unread)
.filter_by(pending=False, deleted_at=None)
.filter(Source.last_updated.isnot(None))
.filter(SourceStar.starred.is_(True))
.outerjoin(SourceStar)
.options(joinedload(Source.submissions))
.options(joinedload(Source.star))
.outerjoin(unread_stmt, Source.id == unread_stmt.c.source_id)
.order_by(Source.last_updated.desc())
.all()
# Fetch all sources
all_sources = SearchSourcesAction(db_session=db.session).perform()

# Add "num_unread" attributes to the source entities
# First gather the count of unread submissions for each source ID
# TODO(AD): Remove this and add support for pagination; switch to a (hybrid?) property
unread_submission_counts_results = (
db.session.query(Submission.source_id, func.count("*"))
.filter_by(downloaded=False)
.group_by(Submission.source_id).all()
)

# Now, add "num_unread" attributes to the source entities.
for source, num_unread in starred:
source.num_unread = num_unread or 0
starred = [source for source, num_unread in starred]

# Query for sources without stars, along with their unread
# submission counts.
unstarred = (
db.session.query(Source, unread_stmt.c.num_unread)
.filter_by(pending=False, deleted_at=None)
.filter(Source.last_updated.isnot(None))
.filter(~Source.star.has(SourceStar.starred.is_(True)))
.options(joinedload(Source.submissions))
.options(joinedload(Source.star))
.outerjoin(unread_stmt, Source.id == unread_stmt.c.source_id)
.order_by(Source.last_updated.desc())
.all()
source_ids_to_unread_submission_counts = {
source_id: subs_count for source_id, subs_count in unread_submission_counts_results
}
# TODO(AD): Remove this dynamically-added attribute; switch to a (hybrid?) property
for source in all_sources:
source.num_unread = source_ids_to_unread_submission_counts.get(source.id, 0)

response = render_template(
"index.html",
unstarred=[src for src in all_sources if not src.is_starred],
starred=[src for src in all_sources if src.is_starred],
)

# Again, add "num_unread" attributes to the source entities.
for source, num_unread in unstarred:
source.num_unread = num_unread or 0
unstarred = [source for source, num_unread in unstarred]

response = render_template("index.html", unstarred=unstarred, starred=starred)
return response

@view.route("/reply", methods=("POST",))
Expand Down
13 changes: 1 addition & 12 deletions securedrop/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def to_json(self) -> "Dict[str, Any]":
"size": self.size,
"is_file": self.is_file,
"is_message": self.is_message,
"is_read": self.seen,
"is_read": self.downloaded,
"uuid": self.uuid,
"download_url": url_for(
"api.download_submission",
Expand All @@ -245,17 +245,6 @@ def to_json(self) -> "Dict[str, Any]":
}
return json_submission

@property
def seen(self) -> bool:
"""
If the submission has been downloaded or seen by any journalist, then the submission is
considered seen.
"""
if self.downloaded or self.seen_files.count() or self.seen_messages.count():
return True

return False


class Reply(db.Model):
__tablename__ = "replies"
Expand Down

0 comments on commit 276806a

Please sign in to comment.