OpenSlides/openslides/utils/utils.py

133 lines
4.0 KiB
Python
Raw Normal View History

2011-07-31 10:46:29 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.utils.urls
~~~~~~~~~~~~~~~~~~~~~
2012-04-25 22:29:19 +02:00
URL functions for OpenSlides.
2011-07-31 10:46:29 +02:00
2012-04-25 22:29:19 +02:00
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
2011-07-31 10:46:29 +02:00
:license: GNU GPL, see LICENSE for more details.
"""
2011-09-02 20:46:24 +02:00
try:
import json
except ImportError:
import simplejson as json
2011-07-31 10:46:29 +02:00
from django.shortcuts import render_to_response, redirect
from django.core.urlresolvers import reverse
2011-09-02 20:46:24 +02:00
from django.http import HttpResponse, HttpResponseForbidden
2011-07-31 10:46:29 +02:00
from django.template import RequestContext
from django.template.loader import render_to_string
from django.core.context_processors import csrf
from django.contrib import messages
from django.contrib.auth.models import Permission
2012-04-14 14:31:09 +02:00
from django.db.models import signals
2012-06-10 10:43:50 +02:00
from django.utils.importlib import import_module
2011-07-31 10:46:29 +02:00
2012-03-18 17:11:58 +01:00
from openslides.utils.signals import template_manipulation
from openslides.utils.translation_ext import ugettext as _
2012-03-18 17:11:58 +01:00
from openslides import get_version
2012-06-10 10:43:50 +02:00
import settings
2011-07-31 10:46:29 +02:00
2012-03-18 17:11:58 +01:00
2011-07-31 10:46:29 +02:00
def revision(request):
return {'openslides_version': get_version()}
2011-07-31 10:46:29 +02:00
def gen_confirm_form(request, message, url):
messages.warning(request, '%s<form action="%s" method="post"><input type="hidden" value="%s" name="csrfmiddlewaretoken"><input type="submit" value="%s" /> <input type="button" value="%s"></form>' % (message, url, csrf(request)['csrf_token'], _("Yes"), _("No")))
2012-02-09 02:29:38 +01:00
def del_confirm_form(request, object, name=None, delete_link=None):
2011-07-31 10:46:29 +02:00
if name is None:
name = object
if delete_link is None:
delete_link = object.get_absolute_url('delete')
gen_confirm_form(request, _('Do you really want to delete <b>%s</b>?') % name, delete_link)
2011-07-31 10:46:29 +02:00
2012-02-09 02:29:38 +01:00
2011-07-31 10:46:29 +02:00
def render_response(req, *args, **kwargs):
kwargs['context_instance'] = RequestContext(req)
return render_to_response(*args, **kwargs)
2012-02-09 02:29:38 +01:00
2011-07-31 10:46:29 +02:00
def template(template_name):
def renderer(func):
2012-03-18 17:11:58 +01:00
def wrapper(request, *args, **kwargs):
output = func(request, *args, **kwargs)
2011-07-31 10:46:29 +02:00
if not isinstance(output, dict):
return output
2012-03-18 17:11:58 +01:00
context = {}
template_manipulation.send(sender='utils_template', request=request, context=context)
output.update(context)
response = render_to_response(template_name, output, context_instance=RequestContext(request))
if 'cookie' in output:
response.set_cookie(output['cookie'][0], output['cookie'][1])
return response
2011-07-31 10:46:29 +02:00
return wrapper
return renderer
def permission_required(perm, login_url=None):
"""
Decorator for views that checks whether a user has a particular permission
enabled, redirecting to the log-in page if necessary.
"""
def renderer(func):
def wrapper(request, *args, **kw):
if request.user.has_perm(perm):
return func(request, *args, **kw)
if request.user.is_authenticated():
return render_to_forbitten(request)
return redirect(reverse('user_login'))
return wrapper
return renderer
2012-02-09 02:29:38 +01:00
2011-07-31 10:46:29 +02:00
def render_to_forbitten(request, error=_("Sorry, you have no rights to see this page.")):
return HttpResponseForbidden(render_to_string('403.html', {'error': error}, context_instance=RequestContext(request)))
2012-02-09 02:29:38 +01:00
2012-04-14 14:31:09 +02:00
def delete_default_permissions(**kwargs):
2011-07-31 10:46:29 +02:00
for p in Permission.objects.all():
if p.codename.startswith('add') or p.codename.startswith('delete') or p.codename.startswith('change'):
p.delete()
2011-09-02 20:46:24 +02:00
2012-02-09 02:29:38 +01:00
2011-09-02 20:46:24 +02:00
def ajax_request(data):
"""
generates a HTTPResponse-Object with json-Data for a
ajax response
"""
return HttpResponse(json.dumps(data))
2012-02-09 02:29:38 +01:00
def _propper_unicode(text):
res = ''
if not isinstance(text, unicode):
res = u"%s" % text.decode('UTF-8')
else:
res = text
return res
2012-04-13 11:35:53 +02:00
def decodedict(dict):
newdict = {}
for key in dict:
newdict[key] = [dict[key][0].encode('utf-8')]
return newdict
def encodedict(dict):
newdict = {}
for key in dict:
newdict[key] = [unicode(dict[key][0].decode('utf-8'))]
return newdict
2012-07-04 12:50:33 +02:00
def html_strong(string):
return u"<strong>%s</strong>" % string