Exemple #1
0
def sort_by_num_opinions(revision, lsds):
    results = (DataFlagOpinion.select(
        DataFlagOpinion.lsd,
        fn.COUNT(DataFlagOpinion.id).alias("count")).join(DataRevision).where(
            DataRevision.name == revision,
            DataFlagOpinion.lsd << [d.lsd for d in lsds]).group_by(
                DataFlagOpinion.lsd).order_by(fn.COUNT(
                    DataFlagOpinion.id))).execute()

    lsds = {day.lsd: day for day in lsds}
    sort_keys = [(r.count, r.lsd) for r in results]
    _, lsd_keys = zip(*sort_keys)

    # add the days witout opinions
    for lsd in lsds.keys():
        if lsd not in lsd_keys:
            lsd_keys.append(lsd)
            sort_keys.append((lsd, 0))

    sort_keys.sort()

    # reuse old day objects
    sorted_lsds = []
    for keys in sort_keys:
        sorted_lsds.append(lsds[keys[1]])

    return sorted_lsds
Exemple #2
0
def get_notes_for_day(day):
    """
    Returns all user notes for one day.

    Parameters
    ----------
    day : :class:`Day`
        Day

    Returns
    -------
    Dict[string, Tuple[string, string]]
        Decisions ("good", "bad" or "unsure") and notes with user names as keys
    """
    if day is None:
        return {}
    try:
        entries = DataFlagOpinion.select(
            DataFlagOpinion.notes, DataFlagOpinion.decision,
            DataFlagOpinion.user_id).where(DataFlagOpinion.lsd == day.lsd)
    except DataFlagOpinion.DoesNotExist:
        entries = []

    notes = {}
    for e in entries:
        n = e.notes
        if n is not None and n != "":
            user_name = (MediaWikiUser.select(MediaWikiUser.user_name).where(
                MediaWikiUser.user_id == e.user_id).get().user_name)
            notes[user_name] = (e.decision, n)
    return notes
Exemple #3
0
def test_create_opinions(db_conn):
    # Create opinion type
    type_, _ = DataFlagOpinionType.get_or_create(name="test",
                                                 description="blubb",
                                                 metadata={"version": 0})

    # Create revision
    revision, _ = DataRevision.get_or_create(name="test", description="foo")

    with pytest.raises(db.exceptions.ValidationError):
        DataFlagOpinion.create_opinion(
            user,
            time.time(),
            True,
            type_.name,
            "test",
            "0.0.0",
            "2111",
            revision=revision.name,
        )

    with pytest.raises(db.exceptions.ValidationError):
        DataFlagOpinion.create_opinion(
            user,
            time.time(),
            "idontknow",
            type_.name,
            "test",
            "0.0.0",
            "2111",
            revision=revision.name,
        )

    return DataFlagOpinion.create_opinion(
        user,
        time.time(),
        "bad",
        type_.name,
        "test",
        "0.0.0",
        "2111",
        revision=revision.name,
        notes="I really don't know!",
    )
Exemple #4
0
def insert(user, lsd, revision, decision, notes):
    if lsd is None:
        return
    user = user.capitalize()
    try:
        existing_decision = (DataFlagOpinion.select(
            DataFlagOpinion.id,
            DataFlagOpinion.decision).join(MediaWikiUser).switch(
                DataFlagOpinion).join(DataRevision).where(
                    MediaWikiUser.user_name == user,
                    DataRevision.name == revision,
                    DataFlagOpinion.lsd == lsd.lsd,
                ).get())
    except DataFlagOpinion.DoesNotExist:
        logger.debug(
            f"Inserting opinion of user {user} for {revision}, {lsd.lsd}: {decision} (notes: '{notes}')"
        )
        opinion_type, _ = DataFlagOpinionType.get_or_create(
            **bondia_dataflagopiniontype)
        revision, _ = DataRevision.get_or_create(name=revision)
        DataFlagOpinion.create_opinion(
            user,
            time(),
            decision,
            opinion_type.name,
            __name__,
            __version__,
            lsd.lsd,
            revision.name,
            notes,
        )
    else:
        # Update the existing opinion
        logger.debug(
            f"Updating opinion of user {user} for {revision}, {lsd.lsd} (ID {existing_decision} "
            f"{existing_decision.id}): {existing_decision.decision} -> {decision} (notes: {notes})"
        )
        existing_decision.decision = decision
        existing_decision.notes = notes
        existing_decision.save()
Exemple #5
0
def get(lsd, revision, user):
    if lsd is None:
        return None
    user = user.capitalize()
    try:
        return (DataFlagOpinion.select(DataFlagOpinion.decision).join(
            MediaWikiUser).switch(DataFlagOpinion).join(DataRevision).where(
                MediaWikiUser.user_name == user,
                DataRevision.name == revision,
                DataFlagOpinion.lsd == lsd.lsd,
            ).get().decision)
    except DataFlagOpinion.DoesNotExist:
        return None
Exemple #6
0
def get_opinions_for_day(day):
    """
    Returns the number of opinions (sorted by decision) for one day.

    Parameters
    ----------
    day : :class:`Day`

    Returns
    -------
    Dict[string, int]
        Number of good, unsure, bad opinions
    """
    num_opinions = {}
    for decision in options_decision:
        num_opinions[decision] = (DataFlagOpinion.select().where(
            DataFlagOpinion.lsd == day.lsd,
            DataFlagOpinion.decision == decision).count())
    return num_opinions
Exemple #7
0
def get_user_stats(zero=True):
    """
    Get number of opinions entered per user.

    Parameters
    ----------
    zero : bool
        Include users without opinions

    Returns
    -------
    Dict[string, int]
        User name, number of opinions entered total.
    """
    num_opinions = {}
    for user_id in MediaWikiUser.select(MediaWikiUser.user_id):
        user_name = (MediaWikiUser.select(MediaWikiUser.user_name).where(
            MediaWikiUser.user_id == user_id).get().user_name)
        count = (DataFlagOpinion.select().where(
            DataFlagOpinion.user_id == user_id).count())
        if zero or count > 0:
            num_opinions[user_name] = count
    return num_opinions
Exemple #8
0
def get_days_with_opinion(revision, user):
    user = user.capitalize()
    days_with_opinion = (DataFlagOpinion.select(DataFlagOpinion.lsd).join(
        MediaWikiUser).switch(DataFlagOpinion).join(DataRevision).where(
            MediaWikiUser.user_name == user, DataRevision.name == revision))
    return [d.lsd for d in days_with_opinion]