Esempio n. 1
0
def password_reset(request):
    if request.method == 'POST':
        form = PasswordResetForm(data=request.POST)

        if form.is_valid():
            team = form.cleaned_data.get('team')
            uid = urlsafe_base64_encode(force_bytes(team.user.pk))
            token = default_token_generator.make_token(team.user)
            reset_link = request.build_absolute_uri(
                reverse(
                    'password_reset_confirm',
                    kwargs={
                        'uidb64': uid,
                        'token': token
                    },
                ))

            send_mail_wrapper('Password reset', 'password_reset_email', {
                'team_name': team.team_name,
                'reset_link': reset_link
            }, team.get_emails())
            return redirect('password_reset_done')
    else:
        form = PasswordResetForm()

    return render(request, 'password_reset.html', {'form': form})
Esempio n. 2
0
def register(request):
    team_members_formset = formset_factory(
        TeamMemberForm,
        formset=TeamMemberFormset,
        extra=0,
        min_num=1,
        max_num=MAX_MEMBERS_PER_TEAM,
        validate_max=True,
    )

    if request.method == 'POST':
        form = RegisterForm(request.POST)
        formset = team_members_formset(request.POST)

        if form.is_valid() and formset.is_valid():
            data = form.cleaned_data
            formset_data = formset.cleaned_data

            user = User.objects.create_user(
                data.get('team_id'),
                password=data.get('password'),
                first_name=data.get('team_name'),
            )
            team = Team.objects.create(
                user=user,
                team_name=data.get('team_name'),
            )
            for team_member in formset_data:
                TeamMember.objects.create(
                    team=team,
                    name=team_member.get('name'),
                    email=team_member.get('email'),
                )

            login(request, user)
            send_mail_wrapper(
                'Team created', 'registration_email', {
                    'team_name':
                    data.get('team_name'),
                    'team_link':
                    request.build_absolute_uri(reverse('teams')) + '?' +
                    urlencode({'team': data.get('team_name')}),
                }, team.get_emails())
            return redirect('index')
    else:
        form = RegisterForm()
        formset = team_members_formset()

    return render(request, 'register.html', {
        'form': form,
        'team_members_formset': formset,
    })
Esempio n. 3
0
def notify_on_hint_update(sender, instance, created, update_fields, **kwargs):
    # The .save() calls when updating certain Hint fields pass update_fields
    # to control which fields are written, which can be checked here. This is
    # to be safe and prevent overtriggering of these handlers, e.g. spamming
    # the team with more emails if an answered hint is somehow claimed again.
    if not update_fields:
        update_fields = ()
    if instance.status == Hint.NO_RESPONSE:
        if 'discord_id' not in update_fields:
            discord_interface.update_hint(instance)
    else:
        if 'discord_id' not in update_fields:
            discord_interface.clear_hint(instance)
        if 'response' in update_fields:
            send_mail_wrapper(
                'Hint answered for {}'.format(instance.puzzle),
                'hint_answered_email', {'hint': instance}, instance.recipients())
Esempio n. 4
0
def notify_on_answer_submission(sender, instance, created, **kwargs):
    if created:
        now = timezone.localtime()
        def format_time_ago(timestamp):
            if not timestamp:
                return ''
            diff = now - timestamp
            parts = ['', '', '', '']
            if diff.days > 0:
                parts[0] = '%dd' % diff.days
            seconds = diff.seconds
            parts[3] = '%02ds' % (seconds % 60)
            minutes = seconds // 60
            if minutes:
                parts[2] = '%02dm' % (minutes % 60)
                hours = minutes // 60
                if hours:
                    parts[1] = '%dh' % hours
            return ' {} ago'.format(''.join(parts))
        hints = Hint.objects.filter(team=instance.team, puzzle=instance.puzzle)
        hint_line = ''
        if len(hints):
            hint_line = '\nHints:' + ','.join('%s (%s%s)' % (
                format_time_ago(hint.submitted_datetime),
                hint.status,
                format_time_ago(hint.answered_datetime),
            ) for hint in hints)
        if instance.used_free_answer:
            dispatch_free_answer_alert(
                ':question: {} Team {} used a free answer on {}!{}'.format(
                    instance.puzzle.emoji, instance.team, instance.puzzle, hint_line))
        else:
            sigil = ':x:'
            if instance.is_correct:
                sigil = {
                    1: ':first_place:', 2: ':second_place:', 3: ':third_place:'
                }.get(AnswerSubmission.objects.filter(
                    puzzle=instance.puzzle,
                    is_correct=True,
                    used_free_answer=False,
                ).count(), ':white_check_mark:')
            dispatch_submission_alert(
                '{} {} Team {} submitted `{}` for {}: {}{}'.format(
                    sigil, instance.puzzle.emoji, instance.team,
                    instance.submitted_answer, instance.puzzle,
                    'Correct!' if instance.is_correct else 'Incorrect!!',
                    hint_line,
                ),
                username='******' if instance.is_correct else 'GPH FailBot')
        if not instance.is_correct:
            return
        if instance.puzzle.slug == INTRO_META_SLUG:
            send_mail_wrapper('Intro Meta', 'FIXME', {}, instance.team.get_emails())
        Hint.objects.filter(
            team=instance.team,
            puzzle=instance.puzzle,
            status=Hint.NO_RESPONSE,
        ).update(
            status=Hint.OBSOLETE,
            answered_datetime=now,
        )
Esempio n. 5
0
def notify_on_meta_unlock(sender, instance, created, **kwargs):
    if created:
        if instance.puzzle.slug == INTRO_META_SLUG:
            send_mail_wrapper('Intro Meta', 'FIXME', {}, instance.team.get_emails())
        elif instance.puzzle.slug == META_META_SLUG:
            send_mail_wrapper('Meta Meta', 'FIXME', {}, instance.team.get_emails())