from django.db.models import Count


def ac_url(url_name, url_params, context):
    if account := context.get("account"):
        url_name = "account_" + url_name
        url_params |= {"account": account.pk}
    elif category := context.get("category"):
        url_name = "category_" + url_name
        url_params |= {"category": category.pk}

    return url_name, url_params


def get_datalist(user, field):
    return [
        t[field]
        for t in user.transaction_set.exclude(**{field: None})
        .values(field)
        .order_by(field)
        .annotate(n=Count(field))
        .order_by("-n")
    ]
