OpenSlides/openslides/utils/utils.py

84 lines
2.4 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 difflib
2011-09-02 20:46:24 +02:00
2012-07-10 13:19:12 +02:00
from django.contrib.auth.models import Permission
2013-09-25 12:53:44 +02:00
from django.shortcuts import render_to_response
2011-07-31 10:46:29 +02:00
from django.template import RequestContext
2013-09-25 12:53:44 +02:00
from .signals import template_manipulation
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.
"""
2013-09-25 12:53:44 +02:00
# TODO: Write the login page an the usersettings page with class based views
# Remove this function afterwards
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
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.
"""
2013-09-25 12:53:44 +02:00
# TODO: Create an participant app which does not create the permissions.
# Delete this function afterwards
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
2013-09-25 12:53:44 +02:00
def html_strong(string):
2011-09-02 20:46:24 +02:00
"""
2013-09-25 12:53:44 +02:00
Returns the text wrapped in an HTML-Strong element.
2011-09-02 20:46:24 +02:00
"""
return u"<strong>%s</strong>" % string
2013-03-15 02:14:15 +01:00
def htmldiff(text1, text2):
2013-09-25 12:53:44 +02:00
"""
Return string of html diff between two strings (text1 and text2)
"""
diff = difflib.HtmlDiff(wrapcolumn=60)
2013-03-15 02:14:15 +01:00
return diff.make_table(text1.splitlines(), text2.splitlines())
2013-09-25 12:53:44 +02:00
def int_or_none(var):
"""
Trys to convert 'var' into an integer. Returns None if an TypeError occures.
"""
try:
2013-09-25 12:53:44 +02:00
return int(var)
except (TypeError, ValueError):
return None