OpenSlides/openslides/utils/utils.py

163 lines
4.7 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.
"""
import sys
2013-01-05 15:29:57 +01:00
import json
2011-09-02 20:46:24 +02:00
2012-07-10 13:19:12 +02:00
from django.contrib import messages
from django.contrib.auth.models import Permission
from django.core.context_processors import csrf
2011-07-31 10:46:29 +02:00
from django.core.urlresolvers import reverse
2011-09-02 20:46:24 +02:00
from django.http import HttpResponse, HttpResponseForbidden
2012-07-10 13:19:12 +02:00
from django.shortcuts import render_to_response, redirect
2011-07-31 10:46:29 +02:00
from django.template import RequestContext
from django.template.loader import render_to_string
from django.utils.translation import ugettext as _, ugettext_lazy
2011-07-31 10:46:29 +02:00
2012-03-18 17:11:58 +01:00
from openslides.utils.signals import template_manipulation
2011-07-31 10:46:29 +02:00
def gen_confirm_form(request, message, url):
2012-07-10 13:19:12 +02:00
"""
Generate a message-form.
Deprecated. Use Class base Views instead.
"""
messages.warning(
request,
"""
%s
<form action="%s" method="post">
<input type="hidden" value="%s" name="csrfmiddlewaretoken">
<button type="submit" name="submit" class="btn btn-mini">%s</button>
<button name="cancel" class="btn btn-mini">%s</button>
</form>
"""
% (message, url, csrf(request)['csrf_token'], _("Yes"), _("No")))
2011-07-31 10:46:29 +02:00
2012-02-09 02:29:38 +01:00
def del_confirm_form(request, object, name=None, delete_link=None):
2012-07-10 13:19:12 +02:00
"""
Creates a question to delete an object.
Deprecated. Use Class base Views instead.
"""
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 %s?')
2012-07-10 13:19:12 +02:00
% html_strong(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 template(template_name):
2012-11-24 01:42:10 +01:00
"""
Decorator to set a template for a view.
Deprecated. Use class based views instead.
"""
2011-07-31 10:46:29 +02:00
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)
2012-03-18 17:11:58 +01:00
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.
2012-11-24 01:42:10 +01:00
Deprecated.
2011-07-31 10:46:29 +02:00
"""
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_forbidden(request)
2011-07-31 10:46:29 +02:00
return redirect(reverse('user_login'))
return wrapper
return renderer
2012-02-09 02:29:38 +01:00
def render_to_forbidden(request,
error=ugettext_lazy("Sorry, you have no rights to see this page.")):
2012-11-24 01:42:10 +01:00
# TODO: Integrate this function into the PermissionMixin once the
# above function is deleted.
return HttpResponseForbidden(render_to_string(
'403.html', {'error': error}, context_instance=RequestContext(request)))
2011-07-31 10:46:29 +02:00
2012-02-09 02:29:38 +01:00
2012-04-14 14:31:09 +02:00
def delete_default_permissions(**kwargs):
2012-07-10 13:19:12 +02:00
"""
Deletes the permissions, django creates by default for the admin.
"""
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')):
2011-07-31 10:46:29 +02:00
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
2012-11-24 01:42:10 +01:00
ajax response.
Deprecated.
2011-09-02 20:46:24 +02:00
"""
return HttpResponse(json.dumps(data))
2012-02-09 02:29:38 +01:00
def _propper_unicode(text):
if not isinstance(text, unicode):
2012-11-26 15:48:23 +01:00
return u"%s" % text.decode('UTF-8')
else:
2012-11-26 15:48:23 +01:00
return text
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
def htmldiff(self, rev1, rev2):
"""Return string of html diff between two strings (rev1 and rev2)"""
diff = difflib.HtmlDiff(wrapcolumn=60)
return diff.make_table(rev1.splitlines(), rev2.splitlines())