Skip to content

Commit

Permalink
Merge pull request #31 from rootstrap/enhancement/indentation
Browse files Browse the repository at this point in the history
Change indentation according to standard definition
  • Loading branch information
brunomichetti authored Aug 18, 2020
2 parents f1c4a35 + ea15df9 commit c38a309
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 119 deletions.
30 changes: 20 additions & 10 deletions drip/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ 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:
Expand All @@ -30,7 +32,8 @@ class DripAdmin(admin.ModelAdmin):
]
form = DripForm

def av(self, view): return 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):
"""
Expand All @@ -46,12 +49,17 @@ def timeline(self, request, drip_id, into_past, into_future):
into_past=int(into_past), into_future=int(into_future)+1
):
shifted_drip.prune()
shifted_drips.append({
'drip': shifted_drip,
'qs': shifted_drip.get_queryset().exclude(id__in=seen_users)
})
shifted_drips.append(
{
'drip': shifted_drip,
'qs': shifted_drip.get_queryset().exclude(
id__in=seen_users,
),
},
)
seen_users.update(
shifted_drip.get_queryset().values_list('id', flat=True))
shifted_drip.get_queryset().values_list('id', flat=True)
)

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

Expand All @@ -66,7 +74,8 @@ def get_mime_html_from_alternatives(self, alternatives):

def get_mime_html(self, drip, user):
drip_message = message_class_for(
drip.message_class)(drip.drip, user)
drip.message_class,
)(drip.drip, user)
if drip_message.message.alternatives:
return self.get_mime_html_from_alternatives(
drip_message.message.alternatives
Expand Down Expand Up @@ -98,7 +107,8 @@ def build_extra_context(self, extra_context):

def add_view(self, request, extra_context=None):
return super(DripAdmin, self).add_view(
request, extra_context=self.build_extra_context(extra_context))
request, extra_context=self.build_extra_context(extra_context),
)

def change_view(self, request, object_id, extra_context=None):
return super(DripAdmin, self).change_view(
Expand Down
48 changes: 34 additions & 14 deletions drip/drips.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ def context(self):
def subject(self):
if not self._subject:
self._subject = Template(
self.drip_base.subject_template).render(self.context)
self.drip_base.subject_template,
).render(self.context)
return self._subject

@property
def body(self):
if not self._body:
self._body = Template(
self.drip_base.body_template).render(self.context)
self.drip_base.body_template,
).render(self.context)
return self._body

@property
Expand All @@ -84,7 +86,9 @@ def plain(self):
def get_from_(self):
if self.drip_base.from_email_name:
from_ = "%s <%s>" % (
self.drip_base.from_email_name, self.drip_base.from_email)
self.drip_base.from_email_name,
self.drip_base.from_email,
)
else:
from_ = self.drip_base.from_email
return from_
Expand All @@ -95,7 +99,10 @@ def message(self):
from_ = self.get_from_()

self._message = EmailMultiAlternatives(
self.subject, self.plain, from_, [self.user.email])
self.subject,
self.plain, from_,
[self.user.email],
)

# check if there are html tags in the rendered template
if len(self.plain) != len(self.body):
Expand Down Expand Up @@ -123,9 +130,13 @@ def __init__(self, drip_model, *args, **kwargs):
self.name = kwargs.pop('name', self.name)
self.from_email = kwargs.pop('from_email', self.from_email)
self.from_email_name = kwargs.pop(
'from_email_name', self.from_email_name)
'from_email_name',
self.from_email_name,
)
self.subject_template = kwargs.pop(
'subject_template', self.subject_template)
'subject_template',
self.subject_template,
)
self.body_template = kwargs.pop('body_template', self.body_template)

if not self.name:
Expand Down Expand Up @@ -159,9 +170,11 @@ def walk(self, into_past=0, into_future=0):
"""
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})
kwargs = dict(
drip_model=self.drip_model,
name=self.name,
now_shift_kwargs={'days': shift},
)
walked_range.append(self.__class__(**kwargs))
return walked_range

Expand All @@ -172,7 +185,8 @@ def apply_queryset_rules(self, qs):
"""
clauses = {
'filter': [],
'exclude': []}
'exclude': [],
}

for rule in self.drip_model.queryset_rules.all():

Expand All @@ -197,7 +211,8 @@ def get_queryset(self):
queryset = getattr(self, '_queryset', None)
if queryset is None:
self._queryset = self.apply_queryset_rules(
self.queryset()).distinct()
self.queryset(),
).distinct()
return self._queryset

def run(self):
Expand Down Expand Up @@ -241,8 +256,10 @@ def get_count_from_queryset(self, MessageClass):
)
count += 1
except Exception as e:
logging.error("Failed to send drip %s to user %s: %s" %
(self.drip_model.id, user, e))
logging.error(
"Failed to send drip %s to user %s: %s" %
(self.drip_model.id, user, e)
)
return count

def send(self):
Expand All @@ -256,7 +273,10 @@ def send(self):

if not self.from_email:
self.from_email = getattr(
settings, 'DRIP_FROM_EMAIL', settings.DEFAULT_FROM_EMAIL)
settings,
'DRIP_FROM_EMAIL',
settings.DEFAULT_FROM_EMAIL,
)
MessageClass = message_class_for(self.drip_model.message_class)

return self.get_count_from_queryset(MessageClass)
Expand Down
5 changes: 3 additions & 2 deletions drip/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def get_flexible_regex(string):
r'((?P<hours>-?((\d*\.\d+)|\d+))\W*h(ou)?(r(s)?)?(,)?\W*)?'
r'((?P<minutes>-?((\d*\.\d+)|\d+))\W*m(in(ute)?(s)?)?(,)?\W*)?'
r'((?P<seconds>-?((\d*\.\d+)|\d+))\W*s(ec(ond)?(s)?)?)?\W*$',
string)
string,
)
if not d:
raise TypeError("'%s' is not a valid time interval" % string)
d = d.groupdict(0)
Expand All @@ -38,7 +39,7 @@ def process_string(string):
d = re.match(
r'^((?P<days>[-+]?\d+) days?,? )?(?P<sign>[-+]?)(?P<hours>\d+):'
r'(?P<minutes>\d+)(:(?P<seconds>\d+(\.\d+)?))?$',
str(string)
str(string),
)
if d:
d = process_regex(d)
Expand Down
14 changes: 10 additions & 4 deletions drip/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,16 @@ class QuerySetRule(models.Model):
)

method_type = models.CharField(
max_length=12, default='filter', choices=METHOD_TYPES)
max_length=12,
default='filter',
choices=METHOD_TYPES,
)
field_name = models.CharField(
max_length=128, verbose_name='Field name of User')
max_length=128, verbose_name='Field name of User'
)
lookup_type = models.CharField(
max_length=12, default='exact', choices=LOOKUP_TYPES)
max_length=12, default='exact', choices=LOOKUP_TYPES
)

field_value = models.CharField(
max_length=255,
Expand All @@ -151,7 +156,8 @@ def clean(self):
self.apply(User.objects.all())
except Exception as e:
raise ValidationError(
'%s raised trying to apply rule: %s' % (type(e).__name__, e))
'%s raised trying to apply rule: %s' % (type(e).__name__, e)
)

@property
def annotated_field_name(self):
Expand Down
18 changes: 13 additions & 5 deletions drip/tests/test_custom_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ class PlainDripEmail(DripMessage):
def message(self):
if not self._message:
email = mail.EmailMessage(
self.subject, self.plain, self.from_email, [self.user.email])
self.subject,
self.plain,
self.from_email,
[self.user.email],
)
self._message = email
return self._message

Expand All @@ -24,12 +28,14 @@ def setUp(self):

self.old_msg_classes = getattr(settings, 'DRIP_MESSAGE_CLASSES', None)
self.user = self.User.objects.create(
username='customuser', email='[email protected]')
username='customuser',
email='[email protected]',
)
self.model_drip = Drip.objects.create(
name='A Custom Week Ago',
subject_template='HELLO {{ user.username }}',
body_html_template='<h2>This</h2> is an <b>example</b>'
' html <strong>body</strong>.'
' html <strong>body</strong>.',
)
QuerySetRule.objects.create(
drip=self.model_drip,
Expand Down Expand Up @@ -63,7 +69,8 @@ def test_custom_added_not_used(self):

def test_custom_added_and_used(self):
settings.DRIP_MESSAGE_CLASSES = {
'plain': 'drip.tests.test_custom_messages.PlainDripEmail'}
'plain': 'drip.tests.test_custom_messages.PlainDripEmail',
}
self.model_drip.message_class = 'plain'
self.model_drip.save()
result = self.model_drip.drip.send()
Expand All @@ -76,7 +83,8 @@ def test_custom_added_and_used(self):

def test_override_default(self):
settings.DRIP_MESSAGE_CLASSES = {
'default': 'drip.tests.test_custom_messages.PlainDripEmail'}
'default': 'drip.tests.test_custom_messages.PlainDripEmail',
}
result = self.model_drip.drip.send()
self.assertEqual(1, result)
self.assertEqual(1, len(mail.outbox))
Expand Down
Loading

0 comments on commit c38a309

Please sign in to comment.