コード例 #1
0
    def get(self, week_num):
        user = current_user()

        if not user:
            return '', 403

        challenges = sorted(user.school.active_challenges(),
                            key=attrgetter('start_date'))

        curr_week_num = current_week_num(challenges)
        challenge = challenges[week_num - 1]

        # If requesting future check_in or
        # requesting first check_in but challenges haven't started yet, error out
        if week_num > curr_week_num or (
                week_num == 1 and date.today() < challenge.start_date.date()):
            return '', 403

        # Get the CheckIn from the Challenge
        check_in = challenge.check_in

        # Get formatted question-answers for this user
        questions = format_questions(check_in, user)

        return {
            'id': check_in.id,
            'challengeName': challenge.name,
            'questions': questions
        }
コード例 #2
0
def formatted_winners(challenges):
    curr_week_num = current_week_num(challenges)
    launched = True

    if date.today() < challenges[0].start_date.date():
        launched = False

    resp = {'launched': launched, 'weekNum': curr_week_num}

    winners_data = []
    i = 1
    for c in challenges:
        data = {'challenge': {'id': c.id, 'name': c.name}}

        user_info = []
        if i <= curr_week_num:
            prizes = c.prizes

            if prizes:
                for p in prizes:
                    winners = p.winners

                    if not winners:
                        continue

                    users = dbi.find_all(User,
                                         {'id': [w.user_id for w in winners]})

                    sponsor = p.sponsor

                    for u in users:
                        user_info.append({
                            'user': {
                                'name': u.name,
                                'email': u.email
                            },
                            'prize': {
                                'name': p.name
                            },
                            'sponsor': {
                                'name': sponsor.name,
                                'logo': sponsor.logo
                            }
                        })
            else:
                data['noPrizes'] = True

        data['winners'] = user_info

        winners_data.append(data)

        i += 1

    resp['winners'] = winners_data

    return resp
コード例 #3
0
    def get(self):
        user = current_user()

        if not user:
            return '', 403

        challenges = sorted(user.school.active_challenges(),
                            key=attrgetter('start_date'))
        challenge_ids = [c.id for c in challenges]

        curr_week_num = current_week_num(challenges)

        # TODO: Eager-load all of this
        check_ins = dbi.find_all(CheckIn, {'challenge_id': challenge_ids})
        check_ins_map = {c.challenge_id: c for c in check_ins}

        check_in_answers_map = {
            a.check_in_question_id: a
            for a in user.check_in_answers
        }

        formatted_check_ins = []
        i = 1
        for c in challenges:
            check_in = check_ins_map[c.id]
            check_in_questions = check_in.check_in_questions
            num_questions = len(check_in_questions)

            num_answers = 0
            for q in check_in_questions:
                if check_in_answers_map.get(q.id):
                    num_answers += 1

            data = {
                'challengeName': c.name,
                'weekNum': i,
                'numQuestions': num_questions,
                'numAnswers': num_answers
            }

            formatted_check_ins.append(data)
            i += 1

        launched = True
        if date.today() < challenges[0].start_date.date():
            launched = False

        return {
            'checkIns': formatted_check_ins,
            'weekNum': curr_week_num,
            'launched': launched
        }
コード例 #4
0
ファイル: challenge.py プロジェクト: quokkateam/quokka-api
    def put(self):
        user = current_user()

        if not user or not user.is_admin:
            return '', 403

        try:
            start_date = datetime.strptime(api.payload['startDate'],
                                           '%m/%d/%y')
        except:
            return 'Invalid start date', 500

        challenge_slugs = [c['slug'] for c in api.payload['challenges']]

        school = user.school

        challenges = dbi.find_all(Challenge, {
            'school': user.school,
            'slug': challenge_slugs
        })

        i = 0
        for slug in challenge_slugs:
            challenge = [c for c in challenges if c.slug == slug][0]

            if i > 0:
                start_date = start_date + timedelta(days=7)

            end_date = start_date + timedelta(days=6)

            dbi.update(challenge, {
                'start_date': start_date,
                'end_date': end_date
            })

            i += 1

        challenges = sorted(school.active_challenges(),
                            key=attrgetter('start_date'))

        curr_week_num = current_week_num(challenges)

        challenges_data = format_challenges(challenges,
                                            user,
                                            curr_week_num=curr_week_num)

        resp = {'weekNum': curr_week_num, 'challenges': challenges_data}

        return resp
コード例 #5
0
ファイル: challenge.py プロジェクト: quokkateam/quokka-api
    def get(self):
        user = current_user()

        if not user:
            return '', 403

        # Get challenges for school, sorted by date
        challenges = sorted(user.school.active_challenges(),
                            key=attrgetter('start_date'))

        curr_week_num = current_week_num(challenges)

        challenges_data = format_challenges(challenges,
                                            user,
                                            curr_week_num=curr_week_num)

        resp = {'weekNum': curr_week_num, 'challenges': challenges_data}

        return resp
コード例 #6
0
def format_response_overviews(challenges):
    curr_week_num = current_week_num(challenges)
    launched = True

    if date.today() < challenges[0].start_date.date():
        launched = False

    resp = {'launched': launched, 'weekNum': curr_week_num}

    i = 1
    overviews = []
    for c in challenges:
        data = {'challenge': {'name': c.name, 'slug': c.slug}}

        overview = {}
        if i <= curr_week_num:
            check_in = c.check_in

            check_in_answers = dbi.find_all(
                CheckInAnswer, {
                    'check_in_question_id':
                    [q.id for q in check_in.check_in_questions]
                })

            overview['checkInId'] = check_in.id
            overview['respCount'] = len(check_in_answers)

        data['overview'] = overview

        overviews.append(data)

        i += 1

    resp['weeklyResponses'] = overviews

    return resp
コード例 #7
0
ファイル: challenge.py プロジェクト: quokkateam/quokka-api
    def get(self, week_num):
        user = current_user()

        if not user:
            return '', 403

        school = user.school
        week_index = week_num - 1

        # Get challenges for school, sorted by date
        challenges = sorted(school.active_challenges(),
                            key=attrgetter('start_date'))

        if week_num < 1 or week_num > len(challenges):
            return {
                'error': 'Challenge does not exist',
                'code': CHALLENGE_NOT_EXIST
            }, 400

        curr_week_num = current_week_num(challenges)

        # if this is a future week and the user isn't an admin, prevent access
        if week_num > curr_week_num and not user.is_admin:
            return {
                'error': 'Week not yet available to access',
                'code': INVALID_CHALLENGE_ACCESS
            }, 400

        # Find the challenge requested by week index
        challenge = challenges[week_index]

        if week_index == 0:
            prev_habit = None
            next_habit = {'weekNum': 2, 'name': challenges[1].name}
        elif week_index == len(challenges) - 1:
            prev_habit = {
                'weekNum': week_index,
                'name': challenges[week_index - 1].name
            }
            next_habit = None
        else:
            prev_habit = {
                'weekNum': week_index,
                'name': challenges[week_index - 1].name
            }
            next_habit = {
                'weekNum': week_num + 1,
                'name': challenges[week_num].name
            }

        # if this is the current week and the user isn't an admin, he/she shouldn't have a link to the next week yet
        if week_num == curr_week_num and not user.is_admin:
            next_habit = None

        universal_challenge = universal_challenge_info.get(challenge.slug)

        resp = {
            'id': challenge.id,
            'habit': {
                'name': challenge.name,
                'slug': challenge.slug,
                'icon': universal_challenge['icon'],
                'dates': {
                    'start': datetime.strftime(challenge.start_date,
                                               '%m/%d/%Y'),
                    'end': datetime.strftime(challenge.end_date, '%m/%d/%Y')
                }
            },
            'overview': universal_challenge['overview'],
            'challenge': {
                'text': challenge.text,
                'points': challenge.points
            },
            'prizes': format_prizes(challenge.active_prizes()),
            'sponsors': format_sponsors(school.sponsors),
            'suggestions': challenge.suggestions,
            'adjHabits': {
                'prev': prev_habit,
                'next': next_habit
            },
            'links': universal_challenge['links'],
            'extraInfo': universal_challenge['extra_info']
        }

        return resp