Skip to main content

Django App for Vingd integration

Project description

Django Vingd enables integrating Vingd into django project.

Basic setup

  1. Add “django_vingd” to INSTALLED_APPS.

  2. Configure django settings:

VINGD_SETTINGS = {
    'sandbox': {
        'VINGD_USR': 'test@knopso.com',
        'VINGD_PWD': '123',
    }
}
VINGD_MODE = 'sandbox'
  1. In any django app that uses vingd define Vingd Orders details (models.py):

# Imaginary scenario where users are voting for candidates via vingd.

from django.db import models
from django.http import HttpResponseRedirect
from django_vingd.models import VingdOrder

class Candidate(models.Model):
    name = models.CharField(max_length=128)
    votes = models.IntegerField(default=0)

    def add_vote(self):
        Candidate.objects.filter(id=self.id).update(votes=models.F('votes')+1)
        self.votes += 1

class VoteOrder(VingdOrder):
    candidate = models.ForeignKey(Candidate)

    # Short object description
    def get_display_name(self):
        return 'Vote for candidate'

    # Handle form submission (before sending user to vingd)
    def take_order(self):
        self.candidate_id = self.request.POST.get('candidate_id')

    # Determine vingd price
    def get_vingd_amount(self):
        return 75   # 0.75 vingd

    # Access is confirmed (user has returned back from vingd)
    def accept_order(self):
        self.candidate.add_vote()

    # Serve requested content to user
    def success_response(self):
        return HttpResponseRedirect('/')
  1. Register order classes in your urls (urls.py):

from django_vingd.models import VingdMeta
from base.models import VoteOrder

VingdMeta.register([VoteOrder, ])

urlpatterns = patterns('',
    (r'^vgd/', include('django_vingd.urls')),
)
  1. In HTML template place vingd order forms:

{% for candidate in candidates %}
    <form action="{% url vingd_order "VoteOrder" %}" method="POST">
        {% csrf_token %}
        {{ candidate.name }}: {{ candidate.votes }}
        <input type="hidden" name="candidate_id" value="{{ candidate.id }}">
        <input type="submit" value="vote">
    </form>
{% endfor %}

Sync database and start your engines!

Deny access to content

In some situations user should not be allowed to access content. Such cases should be handled both for:

  • denying access at vingd ordering time (before sending user to vingd)

  • denying access at vingd verification time (after user has returned from vingd).

In those cases one should raise exception within take_order and accept_order respectively.

Handling exceptions

To gracefully handle any kind of exception one should use VingdOrder handle_exception function:

def handle_exception(self, e):
    # log exception
    # inform user
    return HttpResponse("Inform user that something has gone wrong.")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-vingd-0.1.1.tar.gz (6.7 kB view hashes)

Uploaded Source

Built Distribution

django-vingd-0.1.1.linux-x86_64.exe (72.8 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page