Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use TemplateView to allow additional context data #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions djangodav/views/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def test_get_obj(self):
self.assertEqual(resp['Last-Modified'], "Wed, 24 Dec 2014 06:00:00 +0000")
self.assertEqual(resp.content, "C" * 42)

@patch('djangodav.views.render_to_response', Mock(return_value=HttpResponse('listing')))
@patch('django.views.generic.TemplateView.get', Mock(return_value=HttpResponse('listing')))
def test_head_object(self):
path = '/object.txt'
v = DavView(path=path, base_url='/base', _allowed_methods=Mock(return_value=['ALL']), acl_class=FullAcl)
Expand All @@ -338,7 +338,7 @@ def test_head_object(self):
self.assertEqual("", resp.content)
self.assertEqual("0", resp['Content-Length'])

@patch('djangodav.views.views.render_to_response', Mock(return_value=HttpResponse('listing')))
@patch('django.views.generic.TemplateView.get', Mock(return_value=HttpResponse('listing')))
def test_get_collection(self):
path = '/collection/'
v = DavView(path=path, acl_class=FullAcl, base_url='/base', _allowed_methods=Mock(return_value=['ALL']))
Expand Down
13 changes: 9 additions & 4 deletions djangodav/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
from django.utils.decorators import method_decorator
from django.utils.functional import cached_property
from django.utils.http import parse_etags
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from django.views.generic import TemplateView

from djangodav.responses import ResponseException, HttpResponsePreconditionFailed, HttpResponseCreated, HttpResponseNoContent, \
HttpResponseConflict, HttpResponseMediatypeNotSupported, HttpResponseBadGateway, \
Expand All @@ -25,7 +24,7 @@

PATTERN_IF_DELIMITER = re.compile(r'(<([^>]+)>)|(\(([^\)]+)\))')

class DavView(View):
class DavView(TemplateView):
resource_class = None
lock_class = None
acl_class = None
Expand Down Expand Up @@ -173,6 +172,12 @@ def evaluate_conditions(self, res):
cond_if = '<*>' + cond_if
#for (tmpurl, url, tmpcontent, content) in PATTERN_IF_DELIMITER.findall(cond_if):

def get_context_data(self, **kwargs):
context = super(DavView, self).get_context_data(**kwargs)
context['resource'] = self.resource
context['base_url'] = self.base_url
return context

def get(self, request, path, head=False, *args, **kwargs):
if not self.resource.exists:
raise Http404("Resource doesn't exists")
Expand All @@ -192,7 +197,7 @@ def get(self, request, path, head=False, *args, **kwargs):
response['Content-Length'] = self.resource.getcontentlength
response.content = self.resource.read()
elif not head:
response = render_to_response(self.template_name, dict(resource=self.resource, base_url=self.base_url))
response = super(DavView, self).get(request, *args, **kwargs)
response['Last-Modified'] = self.resource.getlastmodified
return response

Expand Down