OpenSlides/openslides/utils/utils.py

55 lines
1.5 KiB
Python
Raw Normal View History

import difflib
2014-04-27 21:01:23 +02:00
import roman
2011-09-02 20:46:24 +02:00
2012-07-10 13:19:12 +02:00
from django.contrib.auth.models import Permission
2011-07-31 10:46:29 +02: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.
"""
# TODO: Find a way not to create the permissions in the first place.
# Meta.default_permissions does not work, because django will
# nevertheless create permissions for its own models like "group"
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 "<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
2014-04-27 21:01:23 +02:00
def to_roman(number):
2014-04-27 21:01:23 +02:00
"""
Converts an arabic number within range from 1 to 4999 to the corresponding roman number.
Returns None on error conditions.
"""
try:
return roman.toRoman(number)
except (roman.NotIntegerError, roman.OutOfRangeError):
return None