Пример #1
0
def FirstCoffeeOfTheDay(consumption):
    key = 'first_coffee_of_the_day'
    # define some motivational descriptions
    # first, get the date of the last coffee
    last_day = Consumption.objects.order_by('-date').first().date.date()
    if consumption.date.date() > last_day:
        new = Achievement(**get_kwargs_for_key(key))
        consumption.user.achievements.append(new)
        consumption.user.save()
Пример #2
0
def SymmetricCoffee(consumption):
    key = 'symmetric_coffee'
    # first get all of todays coffees for current user
    coffees = Consumption.objects(user=consumption.user,
                                  date__gte=pendulum.today)
    p1, p2 = [p[0] for p in COFFEE_PRICES]
    todays_coffees = ([c.price_per_unit
                       for c in coffees] + [consumption.price_per_unit])
    if todays_coffees == [p1, p2, p1, p1, p2, p1]:
        new = Achievement(**get_kwargs_for_key(key))
        consumption.user.achievements.append(new)
        consumption.user.save()
Пример #3
0
def stalker(consumption, target_username, key, min_consumptions=5):
    target_user = User.objects.get(username=target_username)
    todays_target_consumptions = (Consumption.objects(
        user=target_user, date__gte=pendulum.today()).order_by('-date'))
    todays_user_consumptions = list(
        Consumption.objects(
            user=consumption.user,
            date__gte=pendulum.today()).order_by('-date')) + [consumption]
    if (len(todays_target_consumptions) ==
            min_consumptions  # at least X consumptions
            and ([c.price_per_unit for c in todays_target_consumptions]
                 == [c.price_per_unit
                     for c in todays_user_consumptions])  # same products
            and all([
                t.date < u.date.replace(tzinfo=t.date.tzinfo) for t, u in zip(
                    todays_target_consumptions, todays_user_consumptions)
            ])  # alternating
        ):
        new = Achievement(**get_kwargs_for_key(key))
        consumption.user.achievements.append(new)
        consumption.user.save()
Пример #4
0
def Minimalist(consumption):
    key = 'minimalist'
    n_consumptions_total = len(Consumption.objects(user=consumption.user))

    if n_consumptions_total < 5:
        return

    today = consumption.date
    p1, p2 = [p[0] for p in COFFEE_PRICES]

    last_consumption_date = Consumption.objects(
        user=consumption.user).order_by('-date').first().date

    if not last_consumption_date.weekday() == 4:
        return

    if last_consumption_date.date() == today.date():
        return

    shift = 1 if n_consumptions_total == 5 else 0
    if (Consumption.objects(user=consumption.user).order_by('-date')[4-shift].date.date() == \
        Consumption.objects(user=consumption.user).order_by('-date')[5-shift].date.date()):
        return

    complete_week_deltas = [datetime.timedelta(i) for i in range(0, 5, 1)]

    consumptions_to_check = Consumption.objects(
        user=consumption.user).order_by('-date')[0:5]
    for consumption_to_check, delta in zip(consumptions_to_check,
                                           complete_week_deltas):
        if consumption_to_check.date.date() != (last_consumption_date -
                                                delta).date():
            return

        if consumption_to_check.price_per_unit != p1:
            return

    new = Achievement(**get_kwargs_for_key(key))
    consumption.user.achievements.append(new)
    consumption.user.save()