from django.views import generic
class EmailPreferenceView(generic.FormView):
form_class = EmailPreferenceForm
def get(self, *args, **kwargs):
# You can access url variables from kwargs
# url: /email_preferences/geeknam > kwargs['username'] = 'geeknam'
# Assign to self.subscriber to be used later
self.subscriber = get_subscriber(kwargs['username'])
def post(self, request, *args, **kwargs):
# Process view when the form gets POSTed
pass
def get_initial(self):
# Populate ticks in BooleanFields
initial = {}
for s in self.subscriber.events.all():
initial[s.value_id] = True
return initial
def get_form(self, form_class):
# Initialize the form with initial values and the subscriber object
# to be used in EmailPreferenceForm for populating fields
return form_class(
initial=self.get_initial(),
subscriber=self.subscriber
)
def get_success_url(self):
# Redirect to previous url
return self.request.META.get('HTTP_REFERER', None)
def form_valid(self, form):
messages.info(
self.request,
"You have successfully changed your email notifications"
)
return super(EmailPreferenceView, self).form_valid(form)
def form_invalid(self, form):
messages.info(
self.request,
"Your submission has not been saved. Try again."
)
return super(EmailPreferenceView, self).form_invalid(form)
email_preferences = EmailPreferenceView.as_view()