Skip to content
This repository was archived by the owner on Mar 21, 2022. It is now read-only.

Commit fff09dd

Browse files
committed
Lint codebase using flake8 plugins and add it to CI.
1 parent cdaf8f5 commit fff09dd

17 files changed

+169
-165
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ sudo: no
33
language: python
44

55
env:
6+
- TOXENV=flake8
67
- TOXENV=py27-1.4
78
- TOXENV=py27-1.5
89
- TOXENV=py27-1.6

drip/admin.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import base64
21
import json
32

43
from django import forms
4+
from django.conf.urls import url
55
from django.contrib import admin
6+
from django.http import HttpResponse
7+
from django.shortcuts import get_object_or_404, render
68

7-
from drip.models import Drip, SentDrip, QuerySetRule
89
from drip.drips import configured_message_classes, message_class_for
9-
from drip.utils import get_user_model
10+
from drip.models import Drip, QuerySetRule, SentDrip
11+
from drip.utils import get_simple_fields, get_user_model
1012

1113

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

1618
class DripForm(forms.ModelForm):
1719
message_class = forms.ChoiceField(
18-
choices=((k, '%s (%s)' % (k, v)) for k, v in configured_message_classes().items())
20+
choices=((k, '%s (%s)' % (k, v)) for k, v in configured_message_classes().items()),
1921
)
22+
2023
class Meta:
2124
model = Drip
2225
exclude = []
@@ -29,13 +32,13 @@ class DripAdmin(admin.ModelAdmin):
2932
]
3033
form = DripForm
3134

32-
av = lambda self, view: self.admin_site.admin_view(view)
35+
def av(self, view):
36+
return self.admin_site.admin_view(view)
37+
3338
def timeline(self, request, drip_id, into_past, into_future):
3439
"""
3540
Return a list of people who should get emails.
3641
"""
37-
from django.shortcuts import render, get_object_or_404
38-
3942
drip = get_object_or_404(Drip, id=drip_id)
4043

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

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

5356
def view_drip_email(self, request, drip_id, into_past, into_future, user_id):
54-
from django.shortcuts import render, get_object_or_404
55-
from django.http import HttpResponse
5657
drip = get_object_or_404(Drip, id=drip_id)
5758
User = get_user_model()
5859
user = get_object_or_404(User, id=user_id)
@@ -72,7 +73,6 @@ def view_drip_email(self, request, drip_id, into_past, into_future, user_id):
7273
return HttpResponse(html, content_type=mime)
7374

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

8989
def get_urls(self):
90-
from django.conf.urls import patterns, url
9190
urls = super(DripAdmin, self).get_urls()
92-
my_urls = patterns('',
91+
my_urls = [
9392
url(
9493
r'^(?P<drip_id>[\d]+)/timeline/(?P<into_past>[\d]+)/(?P<into_future>[\d]+)/$',
9594
self.av(self.timeline),
96-
name='drip_timeline'
95+
name='drip_timeline',
9796
),
9897
url(
9998
r'^(?P<drip_id>[\d]+)/timeline/(?P<into_past>[\d]+)/(?P<into_future>[\d]+)/(?P<user_id>[\d]+)/$',
10099
self.av(self.view_drip_email),
101-
name='view_drip_email'
102-
)
103-
)
100+
name='view_drip_email',
101+
),
102+
]
104103
return my_urls + urls
104+
105+
105106
admin.site.register(Drip, DripAdmin)
106107

107108

108109
class SentDripAdmin(admin.ModelAdmin):
109110
list_display = [f.name for f in SentDrip._meta.fields]
110111
ordering = ['-id']
112+
113+
111114
admin.site.register(SentDrip, SentDripAdmin)

drip/drips.py

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import operator
21
import functools
2+
import logging
3+
import operator
34

45
from django.conf import settings
6+
from django.core.mail import EmailMultiAlternatives
57
from django.db.models import Q
68
from django.template import Context, Template
7-
from django.utils.importlib import import_module
8-
from django.core.mail import EmailMultiAlternatives
99
from django.utils.html import strip_tags
10+
from django.utils.importlib import import_module
1011

1112
from drip.models import SentDrip
1213
from drip.utils import get_user_model
@@ -18,9 +19,6 @@
1819
conditional_now = datetime.now
1920

2021

21-
import logging
22-
23-
2422
def configured_message_classes():
2523
conf_dict = getattr(settings, 'DRIP_MESSAGE_CLASSES', {})
2624
if 'default' not in conf_dict:
@@ -124,11 +122,6 @@ def __init__(self, drip_model, *args, **kwargs):
124122

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

127-
128-
#########################
129-
### DATE MANIPULATION ###
130-
#########################
131-
132125
def now(self):
133126
"""
134127
This allows us to override what we consider "now", making it easy
@@ -147,13 +140,13 @@ def walk(self, into_past=0, into_future=0):
147140
"""
148141
Walk over a date range and create new instances of self with new ranges.
149142
"""
150-
walked_range = []
151-
for shift in range(-into_past, into_future):
152-
kwargs = dict(drip_model=self.drip_model,
153-
name=self.name,
154-
now_shift_kwargs={'days': shift})
155-
walked_range.append(self.__class__(**kwargs))
156-
return walked_range
143+
return [
144+
self.__class__(
145+
drip_model=self.drip_model,
146+
name=self.name,
147+
now_shift_kwargs={'days': shift},
148+
) for shift in range(-into_past, into_future)
149+
]
157150

158151
def apply_queryset_rules(self, qs):
159152
"""
@@ -179,10 +172,6 @@ def apply_queryset_rules(self, qs):
179172

180173
return qs
181174

182-
##################
183-
### MANAGEMENT ###
184-
##################
185-
186175
def get_queryset(self):
187176
try:
188177
return self._queryset
@@ -239,19 +228,14 @@ def send(self):
239228
from_email=self.from_email,
240229
from_email_name=self.from_email_name,
241230
subject=message_instance.subject,
242-
body=message_instance.body
231+
body=message_instance.body,
243232
)
244233
count += 1
245234
except Exception as e:
246235
logging.error("Failed to send drip %s to user %s: %s" % (self.drip_model.id, user, e))
247236

248237
return count
249238

250-
251-
####################
252-
### USER DEFINED ###
253-
####################
254-
255239
def queryset(self):
256240
"""
257241
Returns a queryset of auth.User who meet the
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from django.core.management.base import BaseCommand, CommandError
1+
from django.core.management.base import BaseCommand
2+
3+
from drip.models import Drip
24

35

46
class Command(BaseCommand):
57
def handle(self, *args, **options):
6-
from drip.models import Drip
7-
88
for drip in Drip.objects.filter(enabled=True):
99
drip.drip.run()

drip/migrations/0001_initial.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# -*- coding: utf-8 -*-
22
import datetime
3+
4+
from django.db import models
5+
36
from south.db import db
47
from south.v2 import SchemaMigration
5-
from django.db import models
68

79

810
class Migration(SchemaMigration):

drip/migrations/0002_auto__add_field_drip_from_email__add_field_drip_from_email_name__add_f.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# -*- coding: utf-8 -*-
22
import datetime
3+
4+
from django.db import models
5+
36
from south.db import db
47
from south.v2 import SchemaMigration
5-
from django.db import models
68

79

810
class Migration(SchemaMigration):
@@ -116,4 +118,4 @@ def backwards(self, orm):
116118
}
117119
}
118120

119-
complete_apps = ['drip']
121+
complete_apps = ['drip']

drip/migrations/0003_auto__add_field_drip_message_class.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# -*- coding: utf-8 -*-
22
import datetime
3+
4+
from django.db import models
5+
36
from south.db import db
47
from south.v2 import SchemaMigration
5-
from django.db import models
68

79

810
class Migration(SchemaMigration):
@@ -91,4 +93,4 @@ def backwards(self, orm):
9193
}
9294
}
9395

94-
complete_apps = ['drip']
96+
complete_apps = ['drip']

drip/models.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
from datetime import datetime, timedelta
2-
3-
from django.db import models
4-
from django.core.exceptions import ValidationError
5-
from django.conf import settings
6-
7-
from drip.utils import get_user_model
1+
from datetime import datetime
82

93
# just using this to parse, but totally insane package naming...
104
# https://bitbucket.org/schinckel/django-timedelta-field/
115
import timedelta as djangotimedelta
6+
from django.conf import settings
7+
from django.core.exceptions import ValidationError
8+
from django.db import models
9+
10+
from drip.utils import get_user_model
1211

1312

1413
class Drip(models.Model):
@@ -23,26 +22,30 @@ class Drip(models.Model):
2322

2423
enabled = models.BooleanField(default=False)
2524

26-
from_email = models.EmailField(null=True, blank=True,
27-
help_text='Set a custom from email.')
28-
from_email_name = models.CharField(max_length=150, null=True, blank=True,
29-
help_text="Set a name for a custom from email.")
25+
from_email = models.EmailField(
26+
null=True, blank=True, help_text='Set a custom from email.',
27+
)
28+
from_email_name = models.CharField(
29+
max_length=150, null=True, blank=True, help_text="Set a name for a custom from email.",
30+
)
3031
subject_template = models.TextField(null=True, blank=True)
31-
body_html_template = models.TextField(null=True, blank=True,
32-
help_text='You will have settings and user in the context.')
32+
body_html_template = models.TextField(
33+
null=True, blank=True, help_text='You will have settings and user in the context.',
34+
)
3335
message_class = models.CharField(max_length=120, blank=True, default='default')
3436

3537
@property
3638
def drip(self):
3739
from drip.drips import DripBase
3840

39-
drip = DripBase(drip_model=self,
40-
name=self.name,
41-
from_email=self.from_email if self.from_email else None,
42-
from_email_name=self.from_email_name if self.from_email_name else None,
43-
subject_template=self.subject_template if self.subject_template else None,
44-
body_template=self.body_html_template if self.body_html_template else None)
45-
return drip
41+
return DripBase(
42+
drip_model=self,
43+
name=self.name,
44+
from_email=self.from_email if self.from_email else None,
45+
from_email_name=self.from_email_name if self.from_email_name else None,
46+
subject_template=self.subject_template if self.subject_template else None,
47+
body_template=self.body_html_template if self.body_html_template else None,
48+
)
4649

4750
def __unicode__(self):
4851
return self.name
@@ -60,14 +63,13 @@ class SentDrip(models.Model):
6063
subject = models.TextField()
6164
body = models.TextField()
6265
from_email = models.EmailField(
63-
null=True, default=None # For south so that it can migrate existing rows.
66+
null=True, default=None, # For south so that it can migrate existing rows.
6467
)
65-
from_email_name = models.CharField(max_length=150,
66-
null=True, default=None # For south so that it can migrate existing rows.
68+
from_email_name = models.CharField(
69+
max_length=150, null=True, default=None, # For south so that it can migrate existing rows.
6770
)
6871

6972

70-
7173
METHOD_TYPES = (
7274
('filter', 'Filter'),
7375
('exclude', 'Exclude'),
@@ -90,6 +92,7 @@ class SentDrip(models.Model):
9092
('iendswith', 'ends with (case insensitive)'),
9193
)
9294

95+
9396
class QuerySetRule(models.Model):
9497
date = models.DateTimeField(auto_now_add=True)
9598
lastchanged = models.DateTimeField(auto_now=True)
@@ -100,9 +103,9 @@ class QuerySetRule(models.Model):
100103
field_name = models.CharField(max_length=128, verbose_name='Field name of User')
101104
lookup_type = models.CharField(max_length=12, default='exact', choices=LOOKUP_TYPES)
102105

103-
field_value = models.CharField(max_length=255,
104-
help_text=('Can be anything from a number, to a string. Or, do ' +
105-
'`now-7 days` or `today+3 days` for fancy timedelta.'))
106+
field_value = models.CharField(max_length=255, help_text=(
107+
'Can be anything from a number, to a string. Or, do `now-7 days` or `today+3 days` for fancy timedelta.'
108+
))
106109

107110
def clean(self):
108111
User = get_user_model()

0 commit comments

Comments
 (0)