Exemplo n.º 1
0
def transferCoderToCoder(coder1, coder2, pass_number='1', n_to_assign=0):
    """ Transfer articles from coder1 to coder2 """
    if type(coder1) != list and type(coder2) != list:
        print('coder1 and coder2 must be list types')
        return -1

    if pass_number == '1':
        model = ArticleQueue
    elif pass_number == '2':
        model = SecondPassQueue
    elif pass_number == 'ec':
        model = EventCreatorQueue

    src_aq = db_session.query(model).filter(model.coded_dt == None,
                                            model.coder_id.in_(coder1)).all()
    dst_aq = [
        x.article_id for x in db_session.query(model).filter(
            model.coder_id.in_(coder2)).all()
    ]

    ## shuffle so if we're transferring from many to one we don't just get one coder
    random.shuffle(src_aq)

    n_transferred = 0
    for aq in src_aq:
        if aq.article_id in dst_aq:
            ## skip if the article is in the destination queue
            ## this has the consequence that it will return to the original coder
            continue
        else:
            for c in coder2:
                if pass_number == '1':
                    new_aq = ArticleQueue(article_id=aq.article_id, coder_id=c)
                elif pass_number == '2':
                    new_aq = SecondPassQueue(article_id=aq.article_id,
                                             coder_id=c)
                elif pass_number == 'ec':
                    new_aq = EventCreatorQueue(article_id=aq.article_id,
                                               coder_id=c)

            db_session.add(new_aq)
            db_session.delete(aq)
            dst_aq.append(aq.article_id)
            n_transferred += 1

        if n_transferred >= n_to_assign:
            break

    ## TK: This query is kind of buggy for some reason.

    ## assert that dupes have not been entered
    # dupes = db_session.query(model).filter(model.coder_id.in_(coder2)).\
    #     group_by(model.article_id, model.coder_id).having(func.count(model.id) > 1).all()

    # ## rollback if this there are dupes
    # if len(dupes) != 0:
    #     db_session.rollback()
    #     print("%d duplicates have been detected. Rolling back." % len(dupes))
    #     return -1

    db_session.commit()
    return n_transferred