Advertisement
Guest User

Untitled

a guest
Jun 26th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. """
  2. Utilities for all views
  3.  
  4. Ben Adida (12-30-2008)
  5. """
  6.  
  7. from django.template import Context, Template, loader
  8. from django.http import HttpResponse, Http404
  9. from django.shortcuts import render_to_response
  10.  
  11. import utils
  12.  
  13. from helios import datatypes
  14.  
  15. # nicely update the wrapper function
  16. from functools import update_wrapper
  17.  
  18. from helios_auth.security import get_user
  19.  
  20. import helios
  21.  
  22. from django.conf import settings
  23.  
  24. ##
  25. ## BASICS
  26. ##
  27.  
  28. SUCCESS = HttpResponse("SUCCESS")
  29.  
  30. # FIXME: error code
  31. FAILURE = HttpResponse("FAILURE")
  32.  
  33. ##
  34. ## template abstraction
  35. ##
  36. def prepare_vars(request, vars):
  37.   vars_with_user = vars.copy()
  38.   vars_with_user['user'] = get_user(request)
  39.   vars_with_user['THEME_URL'] = 'http://tendenci:9000/themes/nova/'
  40.  
  41.   # csrf protection
  42.   if request.session.has_key('csrf_token'):
  43.     vars_with_user['csrf_token'] = request.session['csrf_token']
  44.    
  45.   vars_with_user['utils'] = utils
  46.   vars_with_user['settings'] = settings
  47.   vars_with_user['HELIOS_STATIC'] = '/static/helios/helios'
  48.   vars_with_user['TEMPLATE_BASE'] = helios.TEMPLATE_BASE
  49.   vars_with_user['CURRENT_URL'] = request.path
  50.   vars_with_user['SECURE_URL_HOST'] = settings.SECURE_URL_HOST
  51.  
  52.   return vars_with_user
  53.  
  54. def render_template(request, template_name, vars = {}, include_user=True):
  55.   t = loader.get_template(template_name + '.html')
  56.  
  57.   vars_with_user = prepare_vars(request, vars)
  58.   vars_with_user['THEME_URL'] = 'http://tendenci:9000/themes/nova/'
  59.  
  60.   if not include_user:
  61.     del vars_with_user['user']
  62.  
  63.   return render_to_response('helios/templates/%s.html' % template_name, vars_with_user)
  64.  
  65. def render_template_raw(request, template_name, vars={}):
  66.   t = loader.get_template(template_name)
  67.  
  68.   # if there's a request, prep the vars, otherwise can't do it.
  69.   if request:
  70.     full_vars = prepare_vars(request, vars)
  71.     vars_with_user['THEME_URL'] = 'http://tendenci:9000/themes/nova/'
  72.   else:
  73.     full_vars = vars
  74.  
  75.   c = Context(full_vars)  
  76.   return t.render(c)
  77.  
  78.  
  79. def render_json(json_txt):
  80.   return HttpResponse(json_txt, "application/json")
  81.  
  82. # decorator
  83. def return_json(func):
  84.     """
  85.    A decorator that serializes the output to JSON before returning to the
  86.    web client.
  87.    """
  88.     def convert_to_json(self, *args, **kwargs):
  89.       return_val = func(self, *args, **kwargs)
  90.       try:
  91.         return render_json(utils.to_json(return_val))
  92.       except Exception, e:
  93.         import logging
  94.         logging.error("problem with serialization: " + str(return_val) + " / " + str(e))
  95.         raise e
  96.  
  97.     return update_wrapper(convert_to_json,func)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement