Esempio n. 1
0
def the_challenge_page(request, challenge_id, state_engine=None, state=None):
    challenge = get_object_or_404(Challenge, id=challenge_id)
    challenge_recipient = challenge.recipient
    template_data = {
        'challenge': challenge,
        'challenge_recipient': challenge_recipient}
    challenge_tree = challenge.challenge_tree
    challenge_tree_stats = extract_statistics_from_challenge_tree(challenge_tree)
    template_data['challenge_tree_stats'] = challenge_tree_stats
    try:
        if state_engine:
            if request.is_ajax() or (state in [_StateEngineStates.PAY] and request.POST):
                return challenge_state_engine(request, challenge, state_engine, state)
            if not request.current_role:
                raise Http404
            participation = ChallengeParticipation.objects.get(
                challenge=challenge,
                participating_entity_id=request.current_role.entity.id,
                participating_entity_type=request.current_role.entity_type)
            if participation.state_engine != state_engine:
                participation.state_engine = state_engine
                participation.save()
            template_data['challenge_participation'] = participation
    except Http404:
        logging.error("Tried to access a state engine page with out logging in")
        return redirect('/challenges/%s' % challenge_id)
    except ChallengeParticipation.DoesNotExist:
        logging.error("Tried to access a state engine but no participation found")
        return redirect('/challenges/%s' % challenge_id)
    return render(request, 'spudderspuds/challenges/pages/challenge_page.html', template_data)
Esempio n. 2
0
def webhook(request):
    logging.warn('Webhook received')
    logging.warn(request.raw_post_data)

    event_json = parse_webhook_request(request)
    if event_json is None:
        return HttpResponse()  # Invalid request data, disregard it

    event_type = get_event_attribute(event_json, 'type')

    if event_type is None or not CardChargeEvents.if_valid_event(event_type):
        return HttpResponse(
        )  # at this point we only care if payment for challenge pledge was successful or not

    if event_type == CardChargeEvents.SUCCEEDED:
        return HttpResponse(
        )  # Although this event tells us that the charge was successful we don't do anything - we already assumed that it will succeed

    charge_id = get_event_attribute(event_json, 'id')
    if charge_id is None:
        return HttpResponse()  # we don't care for invalid webhook requests

    try:
        challenge_participation = ChallengeParticipation.objects.get(
            charge_id=charge_id)
        challenge = challenge_participation.challenge
    except ChallengeParticipation.DoesNotExist:
        # All instances (not only the current one) can receive webhook. That's why we need to ensure that challenge
        # participation was made here. If not, just disregard the event.
        return HttpResponse()

    fake_request = {
        'method': 'GET',
        'current_role': {
            'entity': {
                'id': challenge_participation.participating_entity_id
            },
            'entity_type': challenge_participation.participating_entity_type
        }
    }

    challenge_state_engine(fake_request, challenge, None,
                           _StateEngineStates.PAY_FAILED)

    return HttpResponse()
Esempio n. 3
0
def webhook(request):
    logging.warn('Webhook received')
    logging.warn(request.raw_post_data)

    event_json = parse_webhook_request(request)
    if event_json is None:
        return HttpResponse()  # Invalid request data, disregard it

    event_type = get_event_attribute(event_json, 'type')

    if event_type is None or not CardChargeEvents.if_valid_event(event_type):
        return HttpResponse()  # at this point we only care if payment for challenge pledge was successful or not

    if event_type == CardChargeEvents.SUCCEEDED:
        return HttpResponse()  # Although this event tells us that the charge was successful we don't do anything - we already assumed that it will succeed

    charge_id = get_event_attribute(event_json, 'id')
    if charge_id is None:
        return HttpResponse()  # we don't care for invalid webhook requests

    try:
        challenge_participation = ChallengeParticipation.objects.get(charge_id=charge_id)
        challenge = challenge_participation.challenge
    except ChallengeParticipation.DoesNotExist:
        # All instances (not only the current one) can receive webhook. That's why we need to ensure that challenge
        # participation was made here. If not, just disregard the event.
        return HttpResponse()

    fake_request = {
        'method': 'GET',
        'current_role': {
            'entity': {
                'id': challenge_participation.participating_entity_id
            },
            'entity_type': challenge_participation.participating_entity_type
        }
    }

    challenge_state_engine(fake_request, challenge, None, _StateEngineStates.PAY_FAILED)

    return HttpResponse()
Esempio n. 4
0
def the_challenge_page(request, challenge_id, state_engine=None, state=None):
    challenge = get_object_or_404(Challenge, id=challenge_id)
    challenge_recipient = challenge.recipient
    template_data = {
        'challenge': challenge,
        'challenge_recipient': challenge_recipient
    }
    challenge_tree = challenge.challenge_tree
    challenge_tree_stats = extract_statistics_from_challenge_tree(
        challenge_tree)
    template_data['challenge_tree_stats'] = challenge_tree_stats
    try:
        if state_engine:
            if request.is_ajax() or (state in [_StateEngineStates.PAY]
                                     and request.POST):
                return challenge_state_engine(request, challenge, state_engine,
                                              state)
            if not request.current_role:
                raise Http404
            participation = ChallengeParticipation.objects.get(
                challenge=challenge,
                participating_entity_id=request.current_role.entity.id,
                participating_entity_type=request.current_role.entity_type)
            if participation.state_engine != state_engine:
                participation.state_engine = state_engine
                participation.save()
            template_data['challenge_participation'] = participation
    except Http404:
        logging.error(
            "Tried to access a state engine page with out logging in")
        return redirect('/challenges/%s' % challenge_id)
    except ChallengeParticipation.DoesNotExist:
        logging.error(
            "Tried to access a state engine but no participation found")
        return redirect('/challenges/%s' % challenge_id)
    return render(request, 'spudderspuds/challenges/pages/challenge_page.html',
                  template_data)