Last active
December 10, 2015 01:38
-
-
Save hirokiky/4360091 to your computer and use it in GitHub Desktop.
Django's Class base view, checking params and dispatch to either valid or invalid case method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponseRedirect | |
from django.views.generic.base import TemplateResponseMixin, View | |
class ParamMixin(object): | |
failed_url = None | |
allowed_params = None | |
def get_failed_url(self): | |
return self.failed_url | |
def is_param_valid(self, **kwargs): | |
for k in kwargs.keys(): | |
if k not in self.allowed_params: | |
return False | |
return True | |
def param_valid(self, **kwargs): | |
context = self.get_context_data(**kwargs) | |
return self.render_to_response(context) | |
def param_invalid(self): | |
return HttpResponseRedirect(self.get_failed_url()) | |
def get_context_data(self, **kwargs): | |
return kwargs | |
class ProcessParamView(View): | |
def get(self, request, *args, **kwargs): | |
kwargs = request.GET | |
self.failed_url = request.path | |
if self.is_param_valid(**kwargs): | |
return self.param_valid(**kwargs) | |
else: | |
return self.param_invalid() | |
class ParamView(ParamMixin, TemplateResponseMixin, ProcessParamView): | |
""" | |
""" | |
class MyParamView(ParamView): | |
template_name = 'myparams.html' | |
allowed_params = ('spam', 'ham') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment