Exemplo n.º 1
0
def redeemed_rewards(pid, uid):
    """Get a list of a user's redeemed rewards, ordered by redeemed_at dates."""
    redeemed = Redeemed.query.join(Redeemed.reward) \
      .filter(Redeemed.user_id == uid, Reward.program_id == pid) \
      .options( \
      joinedload(Redeemed.reward), \
      joinedload(Redeemed.user), \
      joinedload(Redeemed.reward).joinedload(Reward.color), \
      joinedload(Redeemed.reward).joinedload(Reward.stamp) \
      ).order_by(Redeemed.redeemed_at).all()

    redeemed_data = dump_data_list(redeemed, redeemed_schema)
    i = 0
    for reward in redeemed:
        redeemed_data[i]["reward"] = reward_schema.dump(reward.reward)
        redeemed_data[i]["reward"]["color"] = color_schema.dump(
            reward.reward.color)
        redeemed_data[i]["reward"]["stamp"] = stamp_schema.dump(
            reward.reward.stamp)
        if redeemed_data[i]["reward"]["limit_per_member"] < 1:
            redeemed_data[i]["reward"]["limit_per_member"] = "∞"
        if redeemed_data[i]["reward"]["quantity"] < 0:
            redeemed_data[i]["reward"]["quantity"] = "∞"
        i += 1
    print("\n\nREDEEMED REWARDS LIST")
    pprint(redeemed_data)
    return jsonify(redeemed_data)
Exemplo n.º 2
0
def user_options(uid):
    """Get the available color and stamp options for a user."""
    # redeemed_options = Redeemed.query.filter(Redeemed.type == 'color' or Redeemed.type == 'stamp', Redeemed.user_id == uid).all()
    # redeemed_colors = [o.reward for o in redeemed_options if o.type == 'color']
    # redeemed_stamps = [s.reward for s in redeemed_options if s.type == 'stamp']
    # colors = Color.query.filter(Color.color in redeemed_colors).all()
    # stamps = Stamp.query.filter(Stamp.stamp in redeemed_stamps).all()
    print("\nPRE COLOR STAMPS")
    colors = Color.query.all()
    stamps = Stamp.query.all()
    # print("\nCOLORS", colors)
    colors_data = dump_data_list(colors, color_schema)
    stamps_data = dump_data_list(stamps, stamp_schema)
    print("\n\nCOLORS, STAMPS", colors_data, stamps_data)

    return jsonify(colors_data=colors_data, stamps_data=stamps_data)
Exemplo n.º 3
0
def type_rewards(type):
    """Get a list of all default-universal rewards, by type if specified."""
    if type:
        rewards = Reward.query.filter(Reward.type == type).all()
    else:
        rewards = Reward.query.filter(Reward.typ != 'custom').all()
    return jsonify(dump_data_list(rewards, reward_schema))
def member_habits(mid):
    """Get a list of a program member's habits (program-specific)."""
    habits = Habit.query.options(joinedload(Habit.stamp)).all()  # dunno
    habits_data = dump_data_list(habits, habit_schema)
    for i in range(len(habits)):
        habit_stamp = stamp_schema.dump(habits[i].stamp)
        habit_color = color_schema.dump(habits[i].color)
        habits_data[i]["stamp"] = habit_stamp
        habits_data[i]["color"] = habit_color
    return jsonify(habits_data)
Exemplo n.º 5
0
def authenticate():
    """Authenticates a user"""
    user = User.query.options( \
      joinedload(User.color), \
      joinedload(User.stamp), \
      joinedload(User.memberships), \
      ).get(current_user.id)
    if user.is_authenticated:

        user_data = user_schema.dump(current_user)
        user_data["color"] = color_schema.dump(user.color)
        user_data["stamp"] = stamp_schema.dump(user.stamp)
        print("\nauthed user")
        user_data["memberships"] = {
            m["id"]: m
            for m in dump_data_list(user.memberships, member_schema)
        }
        print("\nUSER WITH MEMBERS")
        # pprint(user_data)
        return jsonify(user_data)
    return {'errors': ['Unauthorized']}, 401
Exemplo n.º 6
0
def program_rewards(pid):
    """Get a list of a program's custom rewards."""
    rewards = Reward.query.filter(Reward.program_id == pid).options(
        joinedload(Reward.stamp), joinedload(Reward.color),
        joinedload(Reward.creator)).all()

    rewards_obj = {}
    rewards_data = dump_data_list(rewards, reward_schema)
    rewards_data = [reward for reward in rewards_data]
    i = 0
    for reward in rewards:
        rewards_data[i]["color"] = color_schema.dump(reward.color)
        rewards_data[i]["stamp"] = stamp_schema.dump(reward.stamp)
        rewards_data[i]["creator"] = user_schema.dump(reward.creator)
        rewards_obj[rewards_data[i]["id"]] = rewards_data[i]
        if rewards_data[i]["quantity"] == -1:
            rewards_data[i]["quantity"] = "∞"
        if rewards_data[i]["limit_per_member"] <= 0:
            rewards_data[i]["limit_per_member"] = "∞"
        i += 1
    print("\n\nPROGRAM REWARDS", rewards_obj)
    return jsonify(rewards_obj)
Exemplo n.º 7
0
def user_programs(uid):
    """Get a user's subscribed programs."""
    current_date = date.today()
    past_week = [(current_date - timedelta(days=i)) for i in range(7)]
    past_week = [(day.strftime('%A')[0:3], day.strftime('%Y-%m-%d'))
                 for day in past_week]

    print("\n\nUID", uid)
    user_programs = Program.query \
        .join(Member.program).filter(Member.member_id == uid) \
        .options(joinedload(Program.rewards), \
            joinedload(Program.members), \
            joinedload(Program.stamp), \
            joinedload(Program.color), \
            joinedload(Program.habits).joinedload(Habit.daily_stamps), \
            joinedload(Program.habits).joinedload(Habit.stamp), \
            joinedload(Program.habits).joinedload(Habit.color)) \
        .all()
    programs_data = dump_data_list(user_programs, program_schema)

    for i in range(len(user_programs)):
        if user_programs[i].members:
            memberships = [m.id for m in current_user.memberships]
            print("\n\nmemberships", memberships)
            try:
                [mid] = [
                    m for m in programs_data[i]["members"] if m in memberships
                ]
                programs_data[i]["habits"] = []

                for j in range(len(user_programs[i].habits)):
                    programs_data[i]["habits"].append(
                        habit_schema.dump(user_programs[i].habits[j]))
                    habit = habit_schema.dump(user_programs[i].habits[j])
                    habit["stamp"] = stamp_schema.dump(
                        user_programs[i].habits[j].stamp)
                    habit["color"] = color_schema.dump(
                        user_programs[i].habits[j].color)
                    # Daily stamps for prev week for habit
                    habit["daily_stamps"] = DailyStamp.query.filter( \
                        DailyStamp.habit_id == habit["id"], \
                        DailyStamp.member_id == mid, \
                        DailyStamp.date <= past_week[0][1], \
                        DailyStamp.date >= past_week[6][1]).all()
                    habit["daily_stamps"] = dump_data_list(
                        habit["daily_stamps"], dailystamp_schema)
                    programs_data[i]["habits"][j] = habit
            except:
                print("\nno mid probs")
        programs_data[i]["stamp"] = stamp_schema.dump(user_programs[i].stamp)
        programs_data[i]["color"] = color_schema.dump(user_programs[i].color)

    programs_fin = {}
    habits_fin = {}
    dailies_fin = {}
    for program in programs_data:
        habits_fin.update({habit["id"]: habit for habit in program["habits"]})
        program["habits"] = [habit["id"] for habit in program["habits"]]
        programs_fin.update({program["id"]: program})

    for habit in habits_fin.values():
        dailies_fin.update(
            {stamp["id"]: stamp
             for stamp in habit["daily_stamps"]})
        habit["daily_stamps"] = [
            daily["id"] for daily in habit["daily_stamps"]
        ]

    print("\nPROGRAMS DATA")
    # pprint(programs_data)
    print("\nHABITS DATA")
    # pprint(habits_fin)
    print("\nDAILIES DATA")
    # pprint(dailies_fin)

    return jsonify(programs_data=programs_fin,
                   habits_data=habits_fin,
                   dailies_data=dailies_fin,
                   past_week=past_week)
def program_members(pid):
    """Get a list of a program's members."""
    members = Member.query.filter(Member.program_id == pid).all()
    return jsonify(dump_data_list(members, member_schema))
Exemplo n.º 9
0
def program_habits(pid):
    """Get a list of a program's habits."""
    habits = Habit.query.filter(Habit.program_id == pid).all()
    return jsonify(dump_data_list(habits, habit_schema))