Commits

Dmitri Lebedev committed dcc4ec6 Merge

Merge

Comments (0)

Files changed (1)

example/django_partial_page/middleware.py

 
 PARTIAL_PARAM_NAME = getattr(settings, 'PARTIAL_PARAM_NAME', 'blocks')
 _thread_locals = local()
-
+PARTIAL_ALL_PARAM = getattr(settings, 'PARTIAL_ALL_PARAM', 'as_block')
 
 def capture_blocks(func):
 	"""
 		_thread_locals.request = request
 		request.no_partial = callback_kwargs.pop('no_partial', False)
 		request.partials = []
-		if not request.no_partial and PARTIAL_PARAM_NAME in request.GET:
-			request.partials = request.GET[PARTIAL_PARAM_NAME].split(',')
+		_thread_locals.partial = {'blocks': {}, 'data': {}}
+		if not request.no_partial:
+			if PARTIAL_PARAM_NAME in request.GET:
+				request.partials = request.GET[PARTIAL_PARAM_NAME].split(',')
+			
+			elif PARTIAL_ALL_PARAM in request.GET:
+				request.as_partial = request.GET[PARTIAL_ALL_PARAM]
 		
-		_thread_locals.partial = {'blocks': {}, 'data': {}}
-		
-		current_url = resolve(request.path)
-		try:
-			_thread_locals.partial['data']['url'] = current_url.url_name
-		except AttributeError, ex:
-			pass  # not supported for Django < 1.3
+			current_url = resolve(request.path)
+			try:
+				_thread_locals.partial['data']['url'] = current_url.url_name
+			except AttributeError, ex:
+				pass  # not supported for Django < 1.3
 
 	def process_response(self, request, response):
 		"""
 		Bypasses the response or, if partial page is requested, returns JSON.
 		"""
-		if getattr(request, 'no_partial', False) or len(getattr(request, 'partials', [])) == 0:
+		if getattr(request, 'no_partial', False):
 			return response
 
-		json_obj = dict(
-			(key, _thread_locals.partial[key])
-			for key in ['blocks', 'data']
-			if len(_thread_locals.partial[key]) > 0)
+		if len(getattr(request, 'partials', [])) == 0 and not getattr(request, 'as_partial', False):
+			return response
+		
+		if getattr(request, 'as_partial', False):
+			json_obj = {'blocks': {getattr(request, PARTIAL_ALL_PARAM): response.content}}
+		else:
+			json_obj = dict(
+				(key, _thread_locals.partial[key])
+				for key in ['blocks', 'data']
+				if len(_thread_locals.partial[key]) > 0)
 
 		new_response = HttpResponse(json.dumps(json_obj), mimetype='application/json')
 		new_response.status_code = response.status_code
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.