Skip to main content

Affiliate system for django

Project description

Affiliate system for django

Build Status Coverage Status

Affiliate system to reward partners with their visitors payments. Contains abstract models with basic functionality, so it is easy to subclass them and add your custom logic and relations.

Note: version 0.2.0 is backwards incompatible with previous versions.

Example

Visitor goes to site with affiliate code:

http://site.com/?aid=12345

This code is saved in his cookies (also another way is supported: keep aid=12345 at every url). Now each request object has property request.affiliate, so you can access to affiliate, that attract current visitor, and therefore reward him at needed event.

Property request.affiliate is lazy. To check, that affiliate exist, do this:

if request.affiliate.exists():
    # optionally check, that he is active:
    if request.affiliate.is_active:
        # request comes from affiliate with code
        # affiliate.aid

You can find example project in this repository.

Used by projects

Requirements

  • python (2.7, 3.4)

  • django (1.6, 1.7, 1.8)

Quick start

  1. Install this package to your python distribution

    pip install django-affiliate
  2. Add ‘affiliate’ to INSTALLED_APP:

    INSTALLED_APPS = [
        # ...
        'affiliate',
    ]
  3. Add ‘affiliate.middleware.AffiliateMiddleware’ to MIDDLEWARE_CLASSES:

    MIDDLEWARE_CLASSES = (
        # ...
        'affiliate.middleware.AffiliateMiddleware',
    )
  4. Define your custom affiliate model (similar to custom user model):

    # our_app/models.py
    from django.db import models
    from affiliate.models import AbstractAffiliate
    
    
    class Affiliate(AbstractAffiliate):
        pass  # or add some your custom fields here
    
    # settngs.py
    AFFILIATE_AFFILIATE_MODEL = 'our_app.Affiliate'
  5. Create tables

    # django <= 1.6
    python manage.py syncdb
    
    # django <= 1.6 & south
    python manage.py schemamigration our_app --auto
    python manage.py migrate our_app
    
    # django >= 1.7
    python manage.py makemigrations our_app
    python manage.py migrate our_app
  6. Finally, reward affiliate

    from django.views.generic import FormView
    from affiliate.tools import get_affiliate_model
    
    Affiliate = get_affiliate_model()
    
    class SomeView(FormView):
        # ...
    
        def form_valid(self.form):
            product = self.get_product()
            if self.request.affiliate.exists() and self.request.affiliate.is_active:
                # reward affiliate here, your custom logic is here
                Transaction.objects.create(
                    user=self.affiliate.user,
                    amount=Affiliate.calc_affiliate_reward(product.price))
                return super(SomeView, self).form_valid(form)

Optional

To always keep the aid GET parameter (maybe you don’t trust the cookies or you want to reward affiliate only if his visitor make payment at current link access, and not tomorrow)

  1. Load ‘affiliate_urls’ tags:

    {% load affiliate_urls %}
  2. Use ‘url_aff’ instead of ‘url’ template tag:

    <a href="{% url_aff 'home' %}">Home</a>

Configuration

Define in settings.py

  • AFFILIATE_AFFILIATE_MODEL - the model to use to represent an Affiliate, similar to AUTH_USER_MODEL. Mandatory, must be explicitly defined.

  • AFFILIATE_PARAM_NAME - name of affiliate GET parameter in url. Default 'aid'.

  • AFFILIATE_REWARD_AMOUNT - default affiliate reward amount. Can be set as string ('5.55') or as int (10). Default 10.

  • AFFILIATE_REWARD_PERCENTAGE - if True, AFFILIATE_REWARD_AMOUNT is treated as percentage. Otherwise as exact amount of money. Default True.

  • AFFILIATE_SAVE_IN_SESSION - save affiliate id in session or not. Default True.

  • AFFILIATE_SESSION_AGE - how long keep affiliate id in session, in seconds. Default 5 * 24 * 60 * 60 seconds (5 days).

  • AFFILIATE_DEFAULT_LINK - default link, that will be used by Affiliate.build_absolute_affiliate_uri and .build_affiliate_url. Default '/'.

  • AFFILIATE_REMOVE_PARAM_AND_REDIRECT - if True, remove affiliate param from url and redirect to same url (affiliate data will be saved in session). Default False.

django-affiliate release notes

v0.3.2 (2015-11-29)

  • if AFFILIATE_REMOVE_PARAM_AND_REDIRECT is True, perform redirect only in case of GET request method

v0.3.1 (2015-11-29)

  • add setting AFFILIATE_REMOVE_PARAM_AND_REDIRECT, that allows to remove affiliate param from url and redirect

v0.2.1 (2015-10-29)

  • add translations to pypi

v0.2.0 (2015-10-29)

  • only affiliate model defined by package

  • request now have lazy affiliate property, returns Affiliate instance (if exists)

  • django 1.7, 1.8 support

  • python 3.4 support

  • tests are added

  • backwards incompatible

Issues: #1, #3, #4, #6, #8

v0.1.1 (2015-01-15)

  • uploaded to pypi

  • small bug fixes

v0.1.0 (2014-04-29)

  • affiliate model

  • statistics models

  • means for withdraw request

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-affiliate-0.3.2.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_affiliate-0.3.2-py2.py3-none-any.whl (30.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-affiliate-0.3.2.tar.gz.

File metadata

File hashes

Hashes for django-affiliate-0.3.2.tar.gz
Algorithm Hash digest
SHA256 8392bba3e28341da924eae044a407a63f2630df39dae2f0c0c686fff3785efd1
MD5 bb4429f575cb9a89fdbb95f77b0e4daa
BLAKE2b-256 479d05d8c796bb6b7e4346ec79302b1458210a58c65aec513c44c7c5d7e674e1

See more details on using hashes here.

File details

Details for the file django_affiliate-0.3.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_affiliate-0.3.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ea0ee6cc807c4ad81e0fe7a9914a2769cf379f573d61ab8beb3ff74a018e2cfa
MD5 a2def917aeab10baf836882cafe73a72
BLAKE2b-256 fa640498293f90cd17f724dec64fd04f65dd7001f17a5cf973af88aa54833a16

See more details on using hashes here.

Supported by

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