Пример #1
0
def update_score(req):
    if req.method != 'POST':
        return HttpResponseNotFound()
    user = UserProfile.get_user_profile_or_none(req.user)
    game = get_object_or_404(Game, pk=req.POST['id'])
    if user and user.is_player:
        Result(user=user, game=game, score=req.POST['score']).save()
    return HttpResponse()
Пример #2
0
def play(req, id):
    user = UserProfile.get_user_profile_or_none(req.user)
    game = get_object_or_404(Game, pk=id)
    if user and user.is_player:
        if Purchase.objects.get_paid_purchase(user, game):
            return render(req, 'play.html', {
                'game': game,
                'user_profile': user,
                'is_paid': True,
            })
    return redirect("/game/{}".format(game.id))
Пример #3
0
def save_game(req):
    if req.method != 'POST':
        return HttpResponseNotFound()
    user = UserProfile.get_user_profile_or_none(req.user)
    game = get_object_or_404(Game, pk=req.POST['id'])
    if user and user.is_player:
        if GameState.objects.filter(user=user, game=game):
            GameState.objects.get(user=user, game=game).delete()
        game_state = GameState(user=user,
                               game=game,
                               game_state=req.POST['game_state'])
        game_state.save()
    return HttpResponse()
Пример #4
0
def generate_access_token(req):
    if req.method != 'POST':
        return HttpResponse(status=404)

    profile_form = ProfileForm(instance=req.user)
    password_form = PasswordChangeForm(user=req.user)

    user = UserProfile.get_user_profile_or_none(req.user)
    if user and user.is_developer:
        token, _ = Token.objects.get_or_create(user=req.user)
        access_token_form = AccessTokenForm(
            initial={'access_token': token.key})
    else:
        return HttpResponse(status=403)

    return render(
        req, 'profile.html', {
            'profile_form': profile_form,
            'password_form': password_form,
            'access_token_form': access_token_form,
            'user_profile': UserProfile.get_user_profile_or_none(req.user),
        })
Пример #5
0
def cancel_purchase(req, id, pid):
    user_profile = UserProfile.get_user_profile_or_none(req.user)

    if user_profile is None:
        return HttpResponseForbidden()

    purchase = get_object_or_404(Purchase,
                                 id=uuid.UUID(pid),
                                 user=user_profile,
                                 status=TransactionStatus.Pending.value)
    purchase.status = TransactionStatus.Canceled.value
    purchase.save()

    return redirect('/game/{}'.format(id))
Пример #6
0
def purchase(req, id):
    user_profile = UserProfile.get_user_profile_or_none(req.user)
    if user_profile is None or user_profile.is_developer:
        logger.error('The user is not allowed to purchase a game: {}'.format(
            req.user.username))
        return HttpResponseForbidden()  # FIXME: Handle it the other way

    if Purchase.objects.filter(
            user=user_profile, game=id,
            status=TransactionStatus.Succeeded.value).exists():
        return redirect('/game/{}'.format(id))

    game = get_object_or_404(Game, pk=id)

    try:
        purchase = Purchase.objects.filter(
            user=user_profile,
            game=game,
            status=TransactionStatus.Pending.value)[0:1].get()
        purchase.price = game.price
    except Purchase.DoesNotExist:
        purchase = Purchase(user=user_profile, game=game, price=game.price)

    purchase.save()

    formatted_pid = purchase.id.hex
    sid = os.environ.get('PAYMENT_SID', '')
    secret_key = os.environ.get('PAYMENT_SECRET_KEY', '')
    redirect_url = '{}/payment/result'.format(settings.HOST)

    checksum_input = 'pid={}&sid={}&amount={}&token={}'.format(
        formatted_pid, sid, game.price, secret_key)
    m = md5(checksum_input.encode('ascii'))
    checksum = m.hexdigest()

    form = PurchaseForm()

    return render(
        req, 'purchase.html', {
            'form': form,
            'game': game,
            'pid': formatted_pid,
            'sid': sid,
            'amount': game.price,
            'redirect_url': redirect_url,
            'checksum': checksum,
        })
Пример #7
0
def change_password(req):
    if req.method != 'POST':
        return HttpResponse(status=404)

    form = PasswordChangeForm(req.user, req.POST)
    access_token_form = AccessTokenForm()

    if form.is_valid():
        user = form.save()
        update_session_auth_hash(req, user)
        return redirect('/profile/edit/')

    return render(
        req, 'profile.html', {
            'profile_form': ProfileForm(instance=req.user),
            'password_form': form,
            'access_token_form': access_token_form,
            'user_profile': UserProfile.get_user_profile_or_none(req.user),
        })
Пример #8
0
def edit_profile(req):
    password_form = PasswordChangeForm(user=req.user)
    access_token_form = AccessTokenForm()

    if req.method == 'POST':
        profile_form = ProfileForm(req.POST, instance=req.user)

        if profile_form.is_valid():
            user = profile_form.save()
            profile_form = ProfileForm(instance=user)
    else:
        profile_form = ProfileForm(instance=req.user)

    return render(
        req, 'profile.html', {
            'profile_form': profile_form,
            'password_form': password_form,
            'access_token_form': access_token_form,
            'user_profile': UserProfile.get_user_profile_or_none(req.user),
        })
Пример #9
0
def login(req):
    prev_path = urlparse(req.META.get('HTTP_REFERER')).path
    if req.session.has_key('redirect-url') and prev_path in [
            '/login/', '/register/'
    ]:
        next = req.session['redirect-url']
    else:
        next = req.GET.get('next', req.META.get('HTTP_REFERER', '/'))
        req.session['redirect-url'] = next

    if req.user.is_authenticated:
        req.session.pop('redirect-url', None)
        return redirect(next)

    if req.method == 'POST':
        form = AuthenticationForm(req, req.POST)

        if form.is_valid():
            user = authenticate(username=form.cleaned_data.get('username'),
                                password=form.cleaned_data.get('password'))

            if user is not None:
                user_profile = UserProfile.get_user_profile_or_none(user)

                if user_profile is None:
                    return redirect('/login/')

                if not user_profile.verified:
                    return render(req, 'verify_email.html',
                                  {'new_user': False})

                auth_login(req, user)
                req.session.pop('redirect-url', None)
                return redirect(next)
    else:
        form = AuthenticationForm()

    return render(req, 'login.html', {
        'form': form,
    })
Пример #10
0
def stats(req):
    user_profile = UserProfile.get_user_profile_or_none(req.user)
    if user_profile is None or not user_profile.is_developer:
        return HttpResponseForbidden()

    sales = Purchase.objects.get_sales(user_profile)
    sales_per_game = Purchase.objects.get_sales_per_game(user_profile)

    revenue_per_date = Purchase.objects.get_revenue_per_date(user_profile)

    dataSource = OrderedDict()
    chartConfig = OrderedDict()
    chartConfig['caption'] = 'Revenue'
    chartConfig['xAxisName'] = 'Date'
    chartConfig['yAxisName'] = 'Revenue (EUR)'
    chartConfig['decimals'] = '2'
    chartConfig['forceDecimals'] = '1'
    chartConfig['yAxisValueDecimals'] = '2'
    chartConfig['forceYAxisValueDecimals'] = '1'
    chartConfig['lineThickness'] = '2'
    chartConfig['theme'] = 'fusion'
    dataSource['chart'] = chartConfig
    dataSource['data'] = []

    for entry in revenue_per_date:
        data = {}
        data['label'] = str(entry.get('date'))
        data['value'] = float(entry.get('revenue'))
        dataSource['data'].append(data)

    chart = FusionCharts('line', 'revenue-chart', '100%', '400',
                         'revenue-chart-container', 'json', dataSource)

    return render(
        req, 'stats.html', {
            'sales': sales,
            'sales_per_game': sales_per_game,
            'chart': chart.render(),
        })