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.
|
|
|
|
"""
|
|
|
|
|
2012-11-24 14:01:21 +01:00
|
|
|
import sys
|
|
|
|
|
2011-09-02 20:46:24 +02:00
|
|
|
try:
|
|
|
|
import json
|
2012-11-24 14:01:21 +01:00
|
|
|
except ImportError: # For python 2.5 support
|
2011-09-02 20:46:24 +02:00
|
|
|
import simplejson as json
|
|
|
|
|
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
|
2012-07-23 23:06:36 +02:00
|
|
|
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
|
|
|
|
|
2012-02-09 13:43:39 +01:00
|
|
|
from openslides import get_version
|
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):
|
2012-02-09 13:43:39 +01:00
|
|
|
return {'openslides_version': get_version()}
|
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.
|
|
|
|
"""
|
2012-11-24 14:01:21 +01:00
|
|
|
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")))
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-02-09 02:29:38 +01:00
|
|
|
|
2012-02-15 13:44:55 +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
|
2012-02-15 13:44:55 +01:00
|
|
|
if delete_link is None:
|
|
|
|
delete_link = object.get_absolute_url('delete')
|
2012-11-24 14:01:21 +01:00
|
|
|
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 = {}
|
2012-11-24 14:01:21 +01:00
|
|
|
template_manipulation.send(
|
|
|
|
sender='utils_template', request=request, context=context)
|
2012-03-18 17:11:58 +01:00
|
|
|
output.update(context)
|
2012-11-24 14:01:21 +01:00
|
|
|
response = render_to_response(
|
|
|
|
template_name, output, context_instance=RequestContext(request))
|
2011-09-10 00:16:39 +02:00
|
|
|
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():
|
2012-10-24 11:39:27 +02:00
|
|
|
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
|
|
|
|
2012-11-24 14:01:21 +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.
|
2012-11-24 14:01:21 +01:00
|
|
|
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():
|
2012-11-24 14:01:21 +01:00
|
|
|
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))
|
2011-11-29 12:10:06 +01:00
|
|
|
|
2012-02-09 02:29:38 +01:00
|
|
|
|
2011-11-29 12:10:06 +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')
|
2011-11-29 12:10:06 +01:00
|
|
|
else:
|
2012-11-26 15:48:23 +01:00
|
|
|
return text
|
2011-11-29 12:10:06 +01:00
|
|
|
|
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):
|
2012-07-07 10:35:29 +02:00
|
|
|
return u"<strong>%s</strong>" % string
|