Anpassungen des Codes an Django 1.11 mit Python 3

Grapelli wurde entfernt, das neue Django Admin ist hübsch genug.
This commit is contained in:
2017-05-10 10:15:39 +02:00
parent 5437b7b8de
commit 321531c160
25 changed files with 219 additions and 121 deletions

View File

@@ -3,7 +3,7 @@ import re
from django.core.urlresolvers import reverse
from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse
from django.utils.encoding import smart_unicode, force_unicode
from django.utils.encoding import smart_text, force_text
try:
@@ -29,13 +29,13 @@ class LookupBase(object):
return []
def get_item_label(self, item):
return smart_unicode(item)
return smart_text(item)
def get_item_id(self, item):
return smart_unicode(item)
return smart_text(item)
def get_item_value(self, item):
return smart_unicode(item)
return smart_text(item)
def get_item(self, value):
return value
@@ -84,7 +84,7 @@ class LookupRegistry(object):
def register(self, lookup):
self.validate(lookup)
name = force_unicode(lookup.name())
name = force_text(lookup.name())
if name in self._registry:
raise LookupAlreadyRegistered(u'The name %s is already registered'
% name)
@@ -92,7 +92,7 @@ class LookupRegistry(object):
def unregister(self, lookup):
self.validate(lookup)
name = force_unicode(lookup.name())
name = force_text(lookup.name())
if name not in self._registry:
raise LookupNotRegistered(u'The name %s is not registered' % name)
del self._registry[name]

View File

@@ -7,7 +7,7 @@ from django.conf import settings
from django.forms import widgets
from django.forms.utils import flatatt
from django.utils import formats
from django.utils.encoding import force_unicode, force_text
from django.utils.encoding import force_text
from django.utils.html import conditional_escape
from django.utils.http import urlencode
from django.utils.safestring import mark_safe
@@ -98,7 +98,7 @@ class CheckboxInput(widgets.CheckboxInput):
if not (
value is True or value is False or value is None or value == ''): # @IgnorePep8
# Only add the 'value' attribute if a value is non-empty.
final_attrs['value'] = force_unicode(value)
final_attrs['value'] = force_text(value)
return mark_safe(u'<input%s /> %s' % (flatatt(final_attrs), title))
@@ -161,7 +161,7 @@ class Textarea(widgets.Widget):
final_attrs = self.build_attrs(attrs, name=name)
return mark_safe(u'<textarea%s>%s</textarea>' % (flatatt(final_attrs),
conditional_escape(
force_unicode(
force_text(
value))))
@@ -208,7 +208,7 @@ class LookupMultipleHiddenInput(widgets.MultipleHiddenInput):
if model and isinstance(v, model):
item = v
v = lookup.get_item_id(item)
input_attrs = dict(value=force_unicode(v), **final_attrs)
input_attrs = dict(value=force_text(v), **final_attrs)
if id_:
# An ID attribute was given. Add a numeric index as a suffix
# so that the inputs don't all have the same ID attribute.

View File

@@ -3,7 +3,8 @@ Created on 19.10.2011
@author: christian
"""
from BeautifulSoup import BeautifulSoup
from bs4 import BeautifulSoup
#TODO: Nach BeatutifulSoup 4 convertieren
class HtmlCleaner(object):
@@ -38,7 +39,7 @@ class HtmlCleaner(object):
tag_removed = False
def clean_attributes(self, tag):
for attr in tag._getAttrMap().keys():
for attr in list(tag.attrs.keys()):
if attr not in self.ACCEPTABELE_ATTRIBUTES:
del tag[attr]
elif tag[attr].count('script:'):
@@ -57,14 +58,14 @@ class HtmlCleaner(object):
@param fragment:
"""
while True:
soup = BeautifulSoup(fragment)
soup = BeautifulSoup(fragment, "html.parser")
self.tag_removed = False
self.counter += 1
for tag in soup.findAll(True):
for tag in soup.find_all(True):
self.clean_tag(tag)
fragment = unicode(soup)
fragment = str(soup)
if self.tag_removed:
continue
else:
break
return fragment
return fragment.strip()

View File

@@ -219,9 +219,9 @@ class Command(BaseCommand):
css_output = '%s/%s.css' % (CSS_ROOT,
site.domain.replace('.', '_'))
print _("Compressing CSS for %s") % site.name
print "Input Dir: %s" % css_input
print "Output File: %s" % css_output
print("Compressing CSS for {}".format(site.name))
print("Input Dir: {}".format(css_input))
print("Output File: {}".format(css_output))
try:
os.makedirs(css_input)
@@ -232,7 +232,7 @@ class Command(BaseCommand):
and append their content"""
for file_name in fnmatch.filter(sorted(os.listdir(css_input)),
'*.css'):
print ' Adding: %s' % file_name
print('Adding: {}'.format(file_name))
with open(os.path.join(css_input, file_name), 'r') as css_file:
css += css_file.read()
with open(css_output, 'w') as css_file:

View File

@@ -26,9 +26,9 @@ class Command(BaseCommand):
js_input = os.path.join(JS_ROOT, site.domain)
js_output = '%s/%s.js' % (JS_ROOT, site.domain.replace('.', '_'))
print _("Compressing JavaScript for %s") % site.name
print "Input Dir: %s" % js_input
print "Output File: %s" % js_output
print("Compressing JavaScript for {}".format(site.name))
print("Input Dir: {}".format(js_input))
print("Output File: %s".format(js_output))
try:
os.makedirs(js_input)
@@ -39,7 +39,7 @@ class Command(BaseCommand):
# content
js_files = fnmatch.filter(sorted(os.listdir(js_input)), '*.js')
for file_name in js_files:
print " Adding: %s..." % file_name
print(" Adding: {}...".format(file_name))
input_file_name = os.path.join(js_input, file_name)
with open(input_file_name, 'r') as input_file:
output += input_file.read()

46
src/utils/tests.py Normal file
View File

@@ -0,0 +1,46 @@
import unittest
import html_cleaner
class HtmlTestCase(unittest.TestCase):
known_html = (
('<STRONG>Fett!</STRONG>', '<strong>Fett!</strong>'),
('<a href="link.html" onclick="do_evil()">Bad Link</a>', '<a href="link.html">Bad Link</a>'),
('''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 5.50.4919.2200" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Hey Brad;</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2> 1) From your FAQ page at Webalizer.com the README
file link in question 19 to <A
href="ftp://ftp.mrunix.net/pub/webalizer/README">ftp://ftp.mrunix.net/pub/webalizer/README</A>&nbsp;brings
a 404 Page Not Found error message.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2> 2) </FONT><FONT face=Arial size=2>My host recently
added Webalizer and I am trying to find the best way to keep the program running
correctly and to deny outsiders the ability to look at it. I would prefer not to
give others access to my stats.&nbsp; I do web design and at any given time have
client demos in various stages of completion and other things I'd prefer not to
have the general public see popping up in my stats.&nbsp; Does Webalizer have a
password protection mode or anything?</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>XXXXXX XXXXXXX<BR>XXXXXXXXX Web Design<BR><A
href="http://www.xxxxxxxxxxxx.com">http://www.xxxxxxxxxxxx.com</A><BR>(xxx)
xxx-xxxx</FONT></DIV></BODY></HTML>
''',
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'
)
)
def test_html_cleaner(self):
"""Ugly HTML Code has been made tidy"""
cleaner = html_cleaner.HtmlCleaner()
for original, tidy in self.known_html:
self.assertEqual(cleaner.clean_html(original), tidy)
if __name__ == '__main__':
unittest.main()