예제 #1
0
def record_source_interaction(source: Source) -> None:
    """
    Updates the source's interaction count, pending status, and timestamp.
    """
    source.interaction_count += 1
    source.pending = False
    source.last_updated = datetime.datetime.utcnow()
    db.session.flush()
예제 #2
0
def create_source_and_submissions(
        source_index,
        source_count,
        num_submissions=2,
        num_replies=2,
        journalist_who_replied=None  # noqa: W605, E501
):
    # Store source in database
    codename = current_app.crypto_util.genrandomid()
    filesystem_id = current_app.crypto_util.hash_codename(codename)
    journalist_designation = current_app.crypto_util.display_id()
    source = Source(filesystem_id, journalist_designation)
    source.pending = False
    db.session.add(source)
    db.session.commit()

    # Generate submissions directory and generate source key
    os.mkdir(current_app.storage.path(source.filesystem_id))
    current_app.crypto_util.genkeypair(source.filesystem_id, codename)

    # Generate some test submissions
    for _ in range(num_submissions):
        source.interaction_count += 1
        submission_text = next(submissions)
        fpath = current_app.storage.save_message_submission(
            source.filesystem_id, source.interaction_count,
            source.journalist_filename, submission_text)
        source.last_updated = datetime.datetime.utcnow()
        submission = Submission(source, fpath)
        db.session.add(submission)

    # Generate some test replies
    for _ in range(num_replies):
        source.interaction_count += 1
        fname = "{}-{}-reply.gpg".format(source.interaction_count,
                                         source.journalist_filename)
        current_app.crypto_util.encrypt(
            next(replies), [
                current_app.crypto_util.get_fingerprint(source.filesystem_id),
                config.JOURNALIST_KEY
            ], current_app.storage.path(source.filesystem_id, fname))

        if not journalist_who_replied:
            journalist = Journalist.query.first()
        else:
            journalist = journalist_who_replied
        reply = Reply(journalist, source, fname)
        db.session.add(reply)
        db.session.flush()
        seen_reply = SeenReply(reply_id=reply.id, journalist_id=journalist.id)
        db.session.add(seen_reply)

    db.session.commit()

    print("Test source {}/{} (codename: '{}', journalist designation '{}') "
          "added with {} submissions and {} replies".format(
              source_index, source_count, codename, journalist_designation,
              num_submissions, num_replies))
예제 #3
0
    def new_source(self):
        fid_len = random.randint(4, 32)
        designation_len = random.randint(4, 32)
        source = Source(
            random_chars(fid_len, nullable=False,
                         chars=string.ascii_lowercase),
            random_chars(designation_len, nullable=False))
        source.flagged = bool_or_none()
        source.last_updated = random_datetime(nullable=False)
        source.pending = False

        db.session.add(source)
        db.session.flush()
        self.sources.append(source.id)
예제 #4
0
def create_source_and_submissions(num_submissions=2, num_replies=2):
    # Store source in database
    codename = current_app.crypto_util.genrandomid()
    filesystem_id = current_app.crypto_util.hash_codename(codename)
    journalist_designation = current_app.crypto_util.display_id()
    source = Source(filesystem_id, journalist_designation)
    source.pending = False
    db.session.add(source)
    db.session.commit()

    # Generate submissions directory and generate source key
    os.mkdir(current_app.storage.path(source.filesystem_id))
    current_app.crypto_util.genkeypair(source.filesystem_id, codename)

    # Generate some test submissions
    for _ in range(num_submissions):
        source.interaction_count += 1
        fpath = current_app.storage.save_message_submission(
            source.filesystem_id, source.interaction_count,
            source.journalist_filename, 'test submission!')
        source.last_updated = datetime.datetime.utcnow()
        submission = Submission(source, fpath)
        db.session.add(submission)

    # Generate some test replies
    for _ in range(num_replies):
        source.interaction_count += 1
        fname = "{}-{}-reply.gpg".format(source.interaction_count,
                                         source.journalist_filename)
        current_app.crypto_util.encrypt(
            'this is a test reply!', [
                current_app.crypto_util.getkey(source.filesystem_id),
                config.JOURNALIST_KEY
            ], current_app.storage.path(source.filesystem_id, fname))

        journalist = Journalist.query.first()
        reply = Reply(journalist, source, fname)
        db.session.add(reply)

    db.session.commit()

    print("Test source (codename: '{}', journalist designation '{}') "
          "added with {} submissions and {} replies".format(
              codename, journalist_designation, num_submissions, num_replies))
예제 #5
0
def create_source_data(
    source_index: int,
    source_count: int,
    journalist_who_replied: Journalist,
    journalist_who_saw: Journalist,
    num_files: int = 2,
    num_messages: int = 2,
    num_replies: int = 2,
) -> None:
    # Store source in database
    codename = current_app.crypto_util.genrandomid()
    filesystem_id = current_app.crypto_util.hash_codename(codename)
    journalist_designation = current_app.crypto_util.display_id()
    source = Source(filesystem_id, journalist_designation)
    source.pending = False
    db.session.add(source)
    db.session.commit()

    # Generate submissions directory and generate source key
    os.mkdir(current_app.storage.path(source.filesystem_id))
    current_app.crypto_util.genkeypair(source.filesystem_id, codename)

    # Mark a third of sources as seen, a third as partially-seen, and a third as unseen
    seen_files = 0 if source_index % 3 == 0 else math.floor(num_files /
                                                            (source_index % 3))
    seen_messages = 0 if source_index % 3 == 0 else math.floor(
        num_messages / (source_index % 3))
    seen_replies = 0 if source_index % 3 == 0 else math.floor(
        num_replies / (source_index % 3))

    # Generate some test messages
    seen_messages_count = 0
    for _ in range(num_messages):
        source.interaction_count += 1
        submission_text = next(messages)
        fpath = current_app.storage.save_message_submission(
            source.filesystem_id, source.interaction_count,
            source.journalist_filename, submission_text)
        source.last_updated = datetime.datetime.utcnow()
        submission = Submission(source, fpath)
        db.session.add(submission)
        if seen_messages_count < seen_messages:
            seen_messages_count = seen_messages_count + 1
            db.session.flush()
            seen_message = SeenMessage(message_id=submission.id,
                                       journalist_id=journalist_who_saw.id)
            db.session.add(seen_message)

    # Generate some test files
    seen_files_count = 0
    for _ in range(num_files):
        source.interaction_count += 1
        fpath = current_app.storage.save_file_submission(
            source.filesystem_id, source.interaction_count,
            source.journalist_filename, "memo.txt",
            io.BytesIO(b"This is an example of a plain text file upload."))
        source.last_updated = datetime.datetime.utcnow()
        submission = Submission(source, fpath)
        db.session.add(submission)
        if seen_files_count < seen_files:
            seen_files_count = seen_files_count + 1
            db.session.flush()
            seen_file = SeenFile(file_id=submission.id,
                                 journalist_id=journalist_who_saw.id)
            db.session.add(seen_file)

    # Generate some test replies
    seen_replies_count = 0
    for _ in range(num_replies):
        source.interaction_count += 1
        fname = "{}-{}-reply.gpg".format(source.interaction_count,
                                         source.journalist_filename)
        current_app.crypto_util.encrypt(
            next(replies), [
                current_app.crypto_util.get_fingerprint(source.filesystem_id),
                config.JOURNALIST_KEY
            ], current_app.storage.path(source.filesystem_id, fname))

        reply = Reply(journalist_who_replied, source, fname)
        db.session.add(reply)
        db.session.flush()
        # Journalist who replied has seen the reply
        seen_reply = SeenReply(reply_id=reply.id,
                               journalist_id=journalist_who_replied.id)
        db.session.add(seen_reply)
        if seen_replies_count < seen_replies:
            seen_replies_count = seen_replies_count + 1
            seen_reply = SeenReply(reply_id=reply.id,
                                   journalist_id=journalist_who_saw.id)
            db.session.add(seen_reply)

    db.session.commit()

    print("Test source {}/{} (codename: '{}', journalist designation '{}') "
          "added with {} files, {} messages, and {} replies".format(
              source_index + 1, source_count, codename, journalist_designation,
              num_files, num_messages, num_replies))