Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplified base. Time for sleep.

  • Loading branch information...
commit 19e7174edc0e0132d0ff1e42a1fc38eff0f4e53a 1 parent 8cbbeb7
@bfirsh authored
View
46 class_based_views/base.py
@@ -14,40 +14,25 @@ def __init__(self, **kwargs):
mimetype = 'text/html',
template_loader = None,
template_name = None,
- methods = ['GET',],
- restriction_message = _('Restricted page.'),
- warning_message = _('You must define "%s" method.'),
)
if kwargs:
raise TypeError("__init__() got an unexpected keyword argument '%s'" % iter(kwargs).next())
def __call__(self, request, *args, **kwargs):
- method_name = request.method.lower()
- obj = kwargs.get('obj', self.get_object(request, *args, **kwargs))
- if self.is_restricted(request, obj, method_name):
- return HttpResponseNotAllowed(self.restriction_message)
- try:
- method = getattr(self, method_name)
- except AttributeError:
- return HttpResponseNotAllowed(self.warning_message % method_name)
- return method(request, obj, *args, **kwargs)
+ method = getattr(self, request.method.lower(), 'get')
+ return method(request, *args, **kwargs)
- def get(self, request, obj, *args, **kwargs):
+ def get(self, request, *args, **kwargs):
+ obj = self.get_object(request, *args, **kwargs)
template = self.get_template(request, obj)
context = self.get_context(request, obj)
mimetype = self.get_mimetype(request, obj)
response = self.get_response(request, obj, template, context, mimetype=mimetype)
return response
-
- def is_restricted(self, request, obj, method_name):
- """
- Return a boolean, True if the user is not able to access the page.
- """
- return method_name.upper() not in self.methods
-
+
def get_object(self, request, *args, **kwargs):
return None
-
+
def get_template(self, request, obj):
"""
Get a ``Template`` object for the given request.
@@ -56,7 +41,7 @@ def get_template(self, request, obj):
if not names:
raise ImproperlyConfigured("'%s' must provide template_name." % self.__class__.__name__)
return self.load_template(request, obj, names)
-
+
def get_template_names(self, request, obj):
"""
Return a list of template names to be used for the request. Must return
@@ -68,13 +53,13 @@ def get_template_names(self, request, obj):
return [self.template_name]
else:
return self.template_name
-
+
def load_template(self, request, obj, names=[]):
"""
Load a template, using self.template_loader or the default.
"""
return self.get_template_loader(request, obj).select_template(names)
-
+
def get_template_loader(self, request, obj):
"""
Get the template loader to be used for this request. Defaults to
@@ -82,7 +67,7 @@ def get_template_loader(self, request, obj):
"""
import django.template.loader
return self.template_loader or django.template.loader
-
+
def get_context(self, request, obj, context=None):
"""
Get the context. Must return a Context (or subclass) instance.
@@ -91,25 +76,25 @@ def get_context(self, request, obj, context=None):
if context is None:
context = {}
return RequestContext(request, context, processors)
-
+
def get_context_processors(self, request, obj):
"""
Get the context processors to be used for the given request.
"""
return self.context_processors
-
+
def get_mimetype(self, request, obj):
"""
Get the mimetype to be used for the given request.
"""
return self.mimetype
-
+
def get_response(self, request, obj, template, context, **httpresponse_kwargs):
"""
Construct an `HttpResponse` object given the template and context.
"""
return HttpResponse(template.render(context), **httpresponse_kwargs)
-
+
def _load_config_values(self, initkwargs, **defaults):
"""
Set on self some config values possibly taken from __init__, or
@@ -118,4 +103,5 @@ def _load_config_values(self, initkwargs, **defaults):
for k in defaults:
default = getattr(self.__class__, k, defaults[k])
value = initkwargs.pop(k, default)
- setattr(self, k, value)
+ setattr(self, k, value)
+
View
3  class_based_views/dates.py
@@ -20,7 +20,8 @@ def __init__(self, **kwargs):
# views weren't paginated.
self.legacy_context = False
- def get(self, request, obj, *args, **kwargs):
+ def get(self, request, *args, **kwargs):
+ obj = self.get_object(request, *args, **kwargs)
date_list, items, extra_context = self.get_dated_items(request, *args, **kwargs)
template = self.get_template(request, items)
context = self.get_context(request, items, date_list, extra_context)
View
4 class_based_views/detail.py
@@ -20,10 +20,6 @@ def __init__(self, **kwargs):
)
super(DetailView, self).__init__(**kwargs)
- def get(self, request, obj, *args, **kwargs):
- obj = self.get_object(request, *args, **kwargs)
- return super(DetailView, self).get(request, obj=obj)
-
def get_object(self, request, pk=None, slug=None, object_id=None, queryset=None):
"""
Get the object this request wraps. By default this requires
View
14 class_based_views/edit.py
@@ -2,7 +2,8 @@
from class_based_views import View, ListView, DetailView
class FormView(View):
- def post(self, request, obj, *args, **kwargs):
+ def post(self, request, *args, **kwargs):
+ obj = self.get_object(request, *args, **kwargs)
form = self.get_form(request, obj, *args, **kwargs)
if form.is_valid():
self.process_form(request, obj, form.cleaned_data)
@@ -28,23 +29,24 @@ class CreateView(ListView, FormView):
class UpdateView(DetailView, FormView):
- def put(self, request, obj, *args, **kwargs):
+ # FIXME: Er, perhaps not PUT
+ def put(self, request, *args, **kwargs):
obj = self.get_object(request, *args, **kwargs)
# Force evaluation to populate PUT
request.POST
request.PUT = request._post
del request._post
- return self.post(request, obj, *args, **kwargs)
+ return self.post(request, *args, **kwargs)
class DeleteView(DetailView):
- def delete(self, request, obj, *args, **kwargs):
+ def delete(self, request, *args, **kwargs):
obj = self.get_object(request, *args, **kwargs)
obj.delete()
return HttpResponseRedirect(self.redirect_to(request, obj))
- def post(self, request, obj, *args, **kwargs):
- return self.delete(request, obj, *args, **kwargs)
+ def post(self, request, *args, **kwargs):
+ return self.delete(request, *args, **kwargs)
def redirect_to(self, request, obj):
raise NotImplementedError
Please sign in to comment.
Something went wrong with that request. Please try again.