Form processing
class MyForm(forms.Form):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(MyForm, self).__init__(*args, **kwargs)
def save(self):
# self‐contained, user is known
Drop-in features
class CleverPaginator(object):
paginate_by = 100
def get_count(self):
raise NotImplementedError
def get_paginate_by(self, queryset):
count = self.get_count()
if count > self.paginate_by * 1.5:
return self.paginate_by
return count
class CountryView(CleverPaginator, ListView):
def get_count():
return self.country.num_people
Slide 30
Slide 30 text
Registration
Slide 31
Slide 31 text
django-registration is
great, but…
I want more template variables
I'm not using contrib.auth
I want to send an SMS instead of an email
…
Slide 32
Slide 32 text
Writing a custom backend
is not as simple as
subclassing the default views
from le_social.registration import views
class Register(views.Register):
form_class = SMSRegistrationForm
def send_notification(self):
...
Sane, easily overridable defaults
pip install django-le-social
Slide 33
Slide 33 text
Settings
Slide 34
Slide 34 text
Does it have to be that global?
Is it a switch people will want to flip at any time?
Could it be… a class attribute/method instead?
I know, I'll add a setting
Slide 35
Slide 35 text
ACCOUNT_EXPIRES_IN
from le_social.registration import views
class Activate(views.Activate):
expires_in = 3600 * 24 * 7 # 7 days
Slide 36
Slide 36 text
SOCIAL_AUTH_LOGIN_REDIRECT_URL
SOCIAL_AUTH_BACKEND_ERROR_URL
from le_social.twitter import views
class Callback(views.Callback):
def success(self, auth):
...
def error(self, message):
...
Slide 37
Slide 37 text
CBVs are great
but don't use them
for everything
Slide 38
Slide 38 text
class Handler500(generic.TemplateView):
template_name = '500.html'