Skip to content
This repository was archived by the owner on Mar 21, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]
source = drip
branch = True
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ docs/_build/
*.egg-info/
.idea/

sqlite.db
settings.py
.tox
.coverage
41 changes: 18 additions & 23 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
language: python
sudo: no

python:
- "2.7"
- "3.3"
- "3.4"
- "pypy"
language: python

env:
- DJANGO_VERSION="1.4"
- DJANGO_VERSION="1.5"
- DJANGO_VERSION="1.6"
- DJANGO_VERSION="1.7"
- TOXENV=flake8
- TOXENV=py27-1.4
- TOXENV=py27-1.5
- TOXENV=py27-1.6
- TOXENV=py27-1.7
- TOXENV=py33-1.5
- TOXENV=py33-1.6
- TOXENV=py33-1.7
- TOXENV=py34-1.5
- TOXENV=py34-1.6
- TOXENV=py34-1.7

install:
- pip install "Django>=${DJANGO_VERSION},<${DJANGO_VERSION}.99"
- pip install -r requirements.txt
- pip install tox coveralls

script: python manage.py test drip
script:
- tox

matrix:
exclude:
- python: "3.3"
env: DJANGO_VERSION="1.3"
- python: "3.4"
env: DJANGO_VERSION="1.3"
- python: "3.3"
env: DJANGO_VERSION="1.4"
- python: "3.4"
env: DJANGO_VERSION="1.4"
after_success:
- coveralls
37 changes: 20 additions & 17 deletions drip/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import base64
import json

from django import forms
from django.conf.urls import url
from django.contrib import admin
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render

from drip.models import Drip, SentDrip, QuerySetRule
from drip.drips import configured_message_classes, message_class_for
from drip.utils import get_user_model
from drip.models import Drip, QuerySetRule, SentDrip
from drip.utils import get_simple_fields, get_user_model


class QuerySetRuleInline(admin.TabularInline):
Expand All @@ -15,8 +17,9 @@ class QuerySetRuleInline(admin.TabularInline):

class DripForm(forms.ModelForm):
message_class = forms.ChoiceField(
choices=((k, '%s (%s)' % (k, v)) for k, v in configured_message_classes().items())
choices=((k, '%s (%s)' % (k, v)) for k, v in configured_message_classes().items()),
)

class Meta:
model = Drip
exclude = []
Expand All @@ -29,13 +32,13 @@ class DripAdmin(admin.ModelAdmin):
]
form = DripForm

av = lambda self, view: self.admin_site.admin_view(view)
def av(self, view):
return self.admin_site.admin_view(view)

def timeline(self, request, drip_id, into_past, into_future):
"""
Return a list of people who should get emails.
"""
from django.shortcuts import render, get_object_or_404

drip = get_object_or_404(Drip, id=drip_id)

shifted_drips = []
Expand All @@ -44,15 +47,13 @@ def timeline(self, request, drip_id, into_past, into_future):
shifted_drip.prune()
shifted_drips.append({
'drip': shifted_drip,
'qs': shifted_drip.get_queryset().exclude(id__in=seen_users)
'qs': shifted_drip.get_queryset().exclude(id__in=seen_users),
})
seen_users.update(shifted_drip.get_queryset().values_list('id', flat=True))

return render(request, 'drip/timeline.html', locals())

def view_drip_email(self, request, drip_id, into_past, into_future, user_id):
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
drip = get_object_or_404(Drip, id=drip_id)
User = get_user_model()
user = get_object_or_404(User, id=user_id)
Expand All @@ -72,7 +73,6 @@ def view_drip_email(self, request, drip_id, into_past, into_future, user_id):
return HttpResponse(html, content_type=mime)

def build_extra_context(self, extra_context):
from drip.utils import get_simple_fields
extra_context = extra_context or {}
User = get_user_model()
extra_context['field_data'] = json.dumps(get_simple_fields(User))
Expand All @@ -87,25 +87,28 @@ def change_view(self, request, object_id, extra_context=None):
request, object_id, extra_context=self.build_extra_context(extra_context))

def get_urls(self):
from django.conf.urls import patterns, url
urls = super(DripAdmin, self).get_urls()
my_urls = patterns('',
my_urls = [
url(
r'^(?P<drip_id>[\d]+)/timeline/(?P<into_past>[\d]+)/(?P<into_future>[\d]+)/$',
self.av(self.timeline),
name='drip_timeline'
name='drip_timeline',
),
url(
r'^(?P<drip_id>[\d]+)/timeline/(?P<into_past>[\d]+)/(?P<into_future>[\d]+)/(?P<user_id>[\d]+)/$',
self.av(self.view_drip_email),
name='view_drip_email'
)
)
name='view_drip_email',
),
]
return my_urls + urls


admin.site.register(Drip, DripAdmin)


class SentDripAdmin(admin.ModelAdmin):
list_display = [f.name for f in SentDrip._meta.fields]
ordering = ['-id']


admin.site.register(SentDrip, SentDripAdmin)
40 changes: 12 additions & 28 deletions drip/drips.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import operator
import functools
import logging
import operator

from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.db.models import Q
from django.template import Context, Template
from django.utils.importlib import import_module
from django.core.mail import EmailMultiAlternatives
from django.utils.html import strip_tags
from django.utils.importlib import import_module

from drip.models import SentDrip
from drip.utils import get_user_model
Expand All @@ -18,9 +19,6 @@
conditional_now = datetime.now


import logging


def configured_message_classes():
conf_dict = getattr(settings, 'DRIP_MESSAGE_CLASSES', {})
if 'default' not in conf_dict:
Expand Down Expand Up @@ -124,11 +122,6 @@ def __init__(self, drip_model, *args, **kwargs):

self.now_shift_kwargs = kwargs.get('now_shift_kwargs', {})


#########################
### DATE MANIPULATION ###
#########################

def now(self):
"""
This allows us to override what we consider "now", making it easy
Expand All @@ -147,13 +140,13 @@ def walk(self, into_past=0, into_future=0):
"""
Walk over a date range and create new instances of self with new ranges.
"""
walked_range = []
for shift in range(-into_past, into_future):
kwargs = dict(drip_model=self.drip_model,
name=self.name,
now_shift_kwargs={'days': shift})
walked_range.append(self.__class__(**kwargs))
return walked_range
return [
self.__class__(
drip_model=self.drip_model,
name=self.name,
now_shift_kwargs={'days': shift},
) for shift in range(-into_past, into_future)
]

def apply_queryset_rules(self, qs):
"""
Expand All @@ -179,10 +172,6 @@ def apply_queryset_rules(self, qs):

return qs

##################
### MANAGEMENT ###
##################

def get_queryset(self):
try:
return self._queryset
Expand Down Expand Up @@ -239,19 +228,14 @@ def send(self):
from_email=self.from_email,
from_email_name=self.from_email_name,
subject=message_instance.subject,
body=message_instance.body
body=message_instance.body,
)
count += 1
except Exception as e:
logging.error("Failed to send drip %s to user %s: %s" % (self.drip_model.id, user, e))

return count


####################
### USER DEFINED ###
####################

def queryset(self):
"""
Returns a queryset of auth.User who meet the
Expand Down
6 changes: 3 additions & 3 deletions drip/management/commands/send_drips.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.core.management.base import BaseCommand, CommandError
from django.core.management.base import BaseCommand

from drip.models import Drip


class Command(BaseCommand):
def handle(self, *args, **options):
from drip.models import Drip

for drip in Drip.objects.filter(enabled=True):
drip.drip.run()
4 changes: 3 additions & 1 deletion drip/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
import datetime

from django.db import models

from south.db import db
from south.v2 import SchemaMigration
from django.db import models


class Migration(SchemaMigration):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
import datetime

from django.db import models

from south.db import db
from south.v2 import SchemaMigration
from django.db import models


class Migration(SchemaMigration):
Expand Down Expand Up @@ -116,4 +118,4 @@ def backwards(self, orm):
}
}

complete_apps = ['drip']
complete_apps = ['drip']
6 changes: 4 additions & 2 deletions drip/migrations/0003_auto__add_field_drip_message_class.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
import datetime

from django.db import models

from south.db import db
from south.v2 import SchemaMigration
from django.db import models


class Migration(SchemaMigration):
Expand Down Expand Up @@ -91,4 +93,4 @@ def backwards(self, orm):
}
}

complete_apps = ['drip']
complete_apps = ['drip']
Loading