Git Product home page Git Product logo

django-segments's Introduction

Description

The django-segments module allows you to slice and dice your Django user models into segments with Redis backed SQL queries.

Assume your Django user model has an integer primary key called 'id'.

Create a segment, and use the mixin with your user class:

from django.contrib.auth.models import AbstractUser
from segments.models import SegmentMixin

class SegmentableUser(AbstractUser, SegmentMixin):
    pass

...

u = SegmentableUser()
s = Segment(definition = "select id from %s" % SegmentableUser._meta.db_table)
s.refresh() # Run the query and save the results to Redis
print u.is_member(s)  # "True"

You can use it for targeting marketing offers at certain users, building mailing lists, identifying "good" vs. "bad" customers, and quickly adding all sorts of properties on user records into the django admin without having to write or deploy code.

For instance:

class Offer(models.Model)
    priority = models.IntegerField()
    discount = models.DecimalField()
    segment = models.ForeignKey(Segment)

    class Meta:
        ordering = ('priority', )

    @classmethod
    def get_offer_for_user(cls, user)
        for offer in cls.objects.all():
            if offer.segment.has_member(user):
                return offer

The code is thoroughly documented and tested.

Installation

To use, first install (pypi package coming soon):

pip install -e git+https://github.com/groveco/django-segments#egg=segments

Then add the following to your settings.py:

INSTALLED_APPS = (
    ...
    'segments',
)

# This is the name of the connection Segments will use to evaluate segment SQL
# Recommended to set this to a readonly DB role. Defaults to 'default'.
SEGMENTS_EXEC_CONNECTION = 'readonly'

You're ready to go!

Tests

>>> python manage.py test --settings=segments.tests.settings

django-segments's People

Contributors

anton-shutik avatar chrisclark avatar dray-grove avatar executivejanitorgrove avatar johncgrove avatar katekligman avatar stephenmcrowe avatar tochiaigrove avatar unphonetic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-segments's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.