Many fixes (encoding/Python3)

This commit is contained in:
Gregory Soutade 2022-06-19 10:12:51 +02:00
parent fa98b0b0e0
commit 1b0c92e45a
11 changed files with 87 additions and 86 deletions

View File

@ -77,7 +77,7 @@ class Atom(RSS):
return
f = open(filename, 'rb')
post_content = '<![CDATA[' + f.read() + ']]>'
post_content = '<![CDATA[' + f.read().decode('utf-8') + ']]>'
f.close()
if post.content_format == Post.CONTENT_TEXT:

View File

@ -32,7 +32,7 @@ class StrictUTF8Writer(codecs.StreamWriter):
value = ''
def __init__(self):
self.value = u''
self.value = ''
def write(self, object):
object = object.replace('&lt;', '<')
@ -41,17 +41,17 @@ class StrictUTF8Writer(codecs.StreamWriter):
object = object.replace('&apos;', "'")
object = object.replace('&amp;', '&')
if not type(object) == unicode:
self.value = self.value + unicode(object, 'utf-8')
if type(object) == bytes:
self.value = self.value + object.decode('utf-8')
else:
self.value = self.value + object
return self.value
def reset(self):
self.value = u''
self.value = ''
def getvalue(self):
return self.value
return self.value.encode('utf-8')
class DynastieGenerator:
@ -94,7 +94,7 @@ class DynastieGenerator:
writer = StrictUTF8Writer()
node.writexml(writer)
content = writer.getvalue().encode('utf-8')
content = writer.getvalue()
dst_md5 = hashlib.md5()
dst_md5.update(content)
@ -190,7 +190,7 @@ class DynastieGenerator:
if node.prefix == 'dyn':
if node.localName in values:
content = values[node.localName]
if isinstance(content, basestring):
if isinstance(content, str):
new_elem = self.createElement(dom, node.localName, content)
else:
new_elem = self.createElement(dom, node.localName)
@ -249,7 +249,7 @@ class DynastieGenerator:
dom2 = root
try:
dom2 = parse(src + '/' + filename)
except ExpatError, e:
except ExpatError as e:
self.addError('Error parsing ' + src + '/' + filename)
return root

View File

@ -292,7 +292,7 @@ class Index(DynastieGenerator):
filename = filename2
if not filename in self.hash_posts_content:
f = codecs.open(filename, 'rb', 'utf-8')
f = codecs.open(filename, 'r', 'utf-8')
post_content = f.read()
f.close()
self.parent_posts.append(post.id)
@ -491,19 +491,20 @@ class Index(DynastieGenerator):
writer = StrictUTF8Writer()
node.firstChild.writexml(writer)
code = writer.getvalue().encode('utf-8')
code = writer.getvalue().decode('utf-8')
r,w = os.pipe()
r,w=os.fdopen(r,'r',0), os.fdopen(w,'w',0)
r,w=os.fdopen(r,'rb'), os.fdopen(w,'wb')
highlight(code, lexer, formatter, w)
w.close()
code = r.read()
code = code.decode('utf-8')
r.close()
# Remove <pre> after <div class="highlight">
code = code[28:-13]
code = u'<div class="highlight">' + unicode(code, 'utf-8') + u'</div>'
code = '<div class="highlight">' + code + u'</div>'
return code

View File

@ -1252,8 +1252,8 @@ class Markdown(object):
if is_img:
start_idx -= 1
is_inline_img = start_idx > 0 and text[start_idx-1] == "#"
if is_inline_img:
is_inline_img = start_idx > 0 and text[start_idx-1] == "#"
if is_inline_img:
start_idx -= 1
is_img = 1
@ -1795,13 +1795,13 @@ class Markdown(object):
if "code-friendly" in self.extras:
text = self._code_friendly_strong_re.sub(r"<strong>\1</strong>", text)
text = self._code_friendly_em_re.sub(r"<em>\1</em>", text)
text = self._code_friendly_line_re.sub(r"<span style='text-decoration:line-through'>\1</span>", text)
text = self._code_friendly_underline_re.sub(r"<span style='text-decoration:underline'>\1</span>", text)
text = self._code_friendly_line_re.sub(r"<span style='text-decoration:line-through'>\1</span>", text)
text = self._code_friendly_underline_re.sub(r"<span style='text-decoration:underline'>\1</span>", text)
else:
text = self._strong_re.sub(r"<strong>\2</strong>", text)
text = self._em_re.sub(r"<em>\2</em>", text)
text = self._code_friendly_line_re.sub(r"<span style='text-decoration:line-through'>\1</span>", text)
text = self._code_friendly_underline_re.sub(r"<span style='text-decoration:underline'>\1</span>", text)
text = self._code_friendly_line_re.sub(r"<span style='text-decoration:line-through'>\1</span>", text)
text = self._code_friendly_underline_re.sub(r"<span style='text-decoration:underline'>\1</span>", text)
return text
# "smarty-pants" extra: Very liberal in interpreting a single prime as an

View File

@ -243,8 +243,8 @@ class Post(Index):
if not the_class in post_transform:
continue
if the_class == 'post_content':
s = u'<div>' + post_content + u'</div>'
new_node = parseString(s.encode('utf-8'))
s = '<div>' + post_content + u'</div>'
new_node = parseString(s)
for n in new_node.childNodes[0].childNodes:
content_node.appendChild(n)
break
@ -286,4 +286,4 @@ class Post(Index):
writer = StrictUTF8Writer()
nodes[0].writexml(writer)
return writer.getvalue().encode('utf-8')
return writer.getvalue().decode('utf-8')

View File

@ -31,7 +31,7 @@ class RSS(DynastieGenerator):
def appendElement(self, dom, root, name='', content='', attributes=None):
elem = dom.createElement(name)
if attributes:
for k, v in attributes.iteritems():
for k, v in attributes.items():
elem.setAttribute(k, v)
if content != '':
elem.appendChild(dom.createTextNode(content))
@ -77,7 +77,7 @@ class RSS(DynastieGenerator):
return
f = open(filename, 'rb')
post_content = f.read()
post_content = f.read().decode('utf-8')
f.close()
if post.content_format == Post.CONTENT_TEXT:

View File

@ -71,5 +71,5 @@ class Search(Index):
writer = StrictUTF8Writer()
nodes[0].writexml(writer)
return writer.getvalue().encode('utf-8')
return writer.getvalue().decode('utf-8')

View File

@ -34,7 +34,7 @@ from dynastie.generators import *
def slugify(name):
name = name.strip()
name = normalize('NFKD', name).encode('ascii', 'ignore').replace(' ', '-').lower()
name = normalize('NFKD', name).replace(' ', '-').lower()
#remove `other` characters
name = sub('[^a-zA-Z0-9_-]', '', name)
#nomalize dashes
@ -83,7 +83,7 @@ class Blog(models.Model):
continue
engine = line.strip()
if not engine in globals():
print 'Engine ' + engine + ' doesn\'t exists'
print('Engine ' + engine + ' doesn\'t exists')
else:
self.engines.append(globals()[engine])
f.close()
@ -162,11 +162,11 @@ class Blog(models.Model):
# XXX What about devices, sockets etc.?
except (IOError, os.error), why:
except (IOError, os.error) as why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Exception, err:
except Exception as err:
errors.extend(err.args[0])
if errors:
raise Exception(errors)
@ -186,6 +186,7 @@ class Blog(models.Model):
if inspect.isclass(obj) and obj.__module__.startswith("dynastie.generators"):
if obj.__module__ in generated: continue
e = obj(hash_posts, hash_posts_content)
print('Go for {}'.format(e))
r = e.generate(self, self.src_path, self.output_path)
generated.append(obj.__module__)
if not r is None:
@ -225,9 +226,9 @@ class Editor(models.Model):
class Category(models.Model):
name = models.CharField(max_length=255, unique=True)
name_slug = models.CharField(max_length=255)
parent = models.ForeignKey('self', blank=True, null=True)
parent = models.ForeignKey('self', blank=True, null=True, on_delete=models.CASCADE)
description = models.TextField(max_length=255, blank=True)
blog = models.ForeignKey(Blog)
blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
def save(self):
self.name_slug = slugify(self.name)
@ -243,7 +244,7 @@ class Category(models.Model):
class Tag(models.Model):
name = models.CharField(max_length=255, unique=True)
name_slug = models.CharField(max_length=255)
blog = models.ForeignKey(Blog)
blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
def save(self):
self.name_slug = slugify(self.name)
@ -275,7 +276,7 @@ class Post(models.Model):
description = models.TextField(max_length=255, blank=True)
keywords = models.TextField(blank=True)
tags = models.ManyToManyField(Tag, blank=True, null=True)
blog = models.ForeignKey(Blog)
blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
CONTENT_HTML = 0
CONTENT_TEXT = 1
CONTENT_FORMAT = (
@ -353,7 +354,6 @@ class Post(models.Model):
os.mkdir(output + '/_post')
filename = output + '/_post/' + str(self.pk)
content = unicode(content)
content = content.encode('utf-8')
f = open(filename, 'wb')
@ -405,7 +405,6 @@ class Draft(Post):
os.mkdir(output + '/_draft')
filename = output + '/_draft/' + str(self.pk)
content = unicode(content)
content = content.encode('utf-8')
modif = True
@ -444,8 +443,8 @@ class Draft(Post):
super(Draft, self).save()
class Comment(models.Model):
post = models.ForeignKey(Post)
parent = models.ForeignKey('self', null=True, blank=True)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE)
date = models.DateTimeField()
author = models.CharField(max_length=255)
email = models.EmailField(max_length=255, blank=True)

View File

@ -61,7 +61,7 @@ class Search:
def _saveDatabase(self, blog, hashtable):
d = pickle.dumps(hashtable)
f = open(blog.src_path + '/_search.db', 'w')
f = open(blog.src_path + '/_search.db', 'wb')
f.write(d)
f.close()
@ -69,7 +69,7 @@ class Search:
filename = blog.src_path + '/_search.db'
if not os.path.exists(filename):
print 'No search index !'
print('No search index !')
return None
f = open(filename, 'rb')
@ -100,7 +100,7 @@ class Search:
def _prepare_string(self, content):
content = self._remove_tag(content)
content = self._strip_accents(unicode(content, 'utf8'))
content = self._strip_accents(content)
return content
@ -128,12 +128,12 @@ class Search:
except:
return
f = open(filename, 'r')
content = f.read()
f = open(filename, 'rb')
content = f.read().decode('utf-8')
f.close()
self._indexContent(hashtable, index, content, 1)
self._indexContent(hashtable, index, post.title.encode('utf-8'), 5)
self._indexContent(hashtable, index, post.title, 5)
def create_index(self, blog):
hashtable = {}

View File

@ -18,51 +18,52 @@
along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
"""
from django.conf.urls import patterns, include, url
from django.conf.urls import re_path
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
from dynastie.views import *
urlpatterns = patterns('',
url(r'^index$', 'dynastie.views.index', name='index'),
url(r'^$', 'dynastie.views.index', name='index'),
url(r'^disconnect$', 'dynastie.views.disconnect', name='disconnect'),
url(r'^user$', 'dynastie.views.user', name='user'),
url(r'^user/add$', 'dynastie.views.add_user', name='add_user'),
url(r'^user/edit/(\d+)$', 'dynastie.views.edit_user', name='edit_user'),
url(r'^user/(\d+)$', 'dynastie.views.edit_user', name='view_user'),
url(r'^category/(\d+)$', 'dynastie.views.category', name='category'),
url(r'^category/add/(\d+)$', 'dynastie.views.add_category', name='add_category'),
url(r'^category/edit/(\d+)$', 'dynastie.views.edit_category', name='edit_category'),
url(r'^category/delete/(\d+)$', 'dynastie.views.delete_category', name='delete_category'),
url(r'^blog$', 'dynastie.views.blog', name='blog'),
url(r'^blog/add$', 'dynastie.views.add_blog', name='add_blog'),
url(r'^blog/(\d+)$', 'dynastie.views.view_blog', name='view_blog'),
url(r'^blog/edit/(\d+)$', 'dynastie.views.edit_blog', name='edit_blog'),
url(r'^blog/search/(\d+)$', 'dynastie.views.search_blog', name='search_blog'),
url(r'^post/add/(\d+)$', 'dynastie.views.add_post', name='add_post'),
url(r'^post/edit/(\d+)$', 'dynastie.views.edit_post', name='edit_post'),
url(r'^post/delete/(\d+)$', 'dynastie.views.delete_post', name='delete_post'),
url(r'^draft/edit/(\d+)$', 'dynastie.views.edit_draft', name='edit_draft'),
url(r'^draft/delete/(\d+)$', 'dynastie.views.delete_draft', name='delete_draft'),
url(r'^generate/(\d+)$', 'dynastie.views.generate', name='generate'),
url(r'^generate/(\d+)/(\d+)$','dynastie.views.generate_post',name='generate_post'),
url(r'^preview/(\d+)$', 'dynastie.views.preview', name='preview'),
url(r'^tinyMCEExternalList/post/add/(\d+)$', 'dynastie.views.tinymcelist_add', name='tinymce'),
url(r'^tinyMCEExternalList/post/edit/(\d+)$', 'dynastie.views.tinymcelist_edit', name='tinymce'),
url(r'^comment/add/(\d+)/(\d+)$', 'dynastie.views.add_comment', name='add_comment'),
url(r'^comment/edit/(\d+)$', 'dynastie.views.edit_comment', name='edit_comment'),
url(r'^comment/delete/(\d+)$','dynastie.views.delete_comment',name='delete_comment'),
url(r'^tag/(\d+)$', 'dynastie.views.tag', name='tag'),
url(r'^tag/edit/(\d+)$', 'dynastie.views.edit_tag', name='edit_tag'),
url(r'^tag/delete/(\d+)$', 'dynastie.views.delete_tag', name='delete_tag'),
url(r'^search/generate/(\d+)$', 'dynastie.views.generate_search',name='generate_search'),
url(r'^search/(\d+)$', 'dynastie.views.search', name='search'),
urlpatterns = [
re_path(r'^index$', index, name='index'),
re_path(r'^$', index, name='index'),
re_path(r'^disconnect$', disconnect, name='disconnect'),
re_path(r'^user$', user, name='user'),
re_path(r'^user/add$', add_user, name='add_user'),
re_path(r'^user/edit/(\d+)$', edit_user, name='edit_user'),
re_path(r'^user/(\d+)$', edit_user, name='view_user'),
re_path(r'^category/(\d+)$', category, name='category'),
re_path(r'^category/add/(\d+)$', add_category, name='add_category'),
re_path(r'^category/edit/(\d+)$', edit_category, name='edit_category'),
re_path(r'^category/delete/(\d+)$', delete_category, name='delete_category'),
re_path(r'^blog$', blog, name='blog'),
re_path(r'^blog/add$', add_blog, name='add_blog'),
re_path(r'^blog/(\d+)$', view_blog, name='view_blog'),
re_path(r'^blog/edit/(\d+)$', edit_blog, name='edit_blog'),
re_path(r'^blog/search/(\d+)$', search_blog, name='search_blog'),
re_path(r'^post/add/(\d+)$', add_post, name='add_post'),
re_path(r'^post/edit/(\d+)$', edit_post, name='edit_post'),
re_path(r'^post/delete/(\d+)$', delete_post, name='delete_post'),
re_path(r'^draft/edit/(\d+)$', edit_draft, name='edit_draft'),
re_path(r'^draft/delete/(\d+)$', delete_draft, name='delete_draft'),
re_path(r'^generate/(\d+)$', generate, name='generate'),
re_path(r'^generate/(\d+)/(\d+)$',generate_post,name='generate_post'),
re_path(r'^preview/(\d+)$', preview, name='preview'),
re_path(r'^tinyMCEExternalList/post/add/(\d+)$', tinymcelist_add, name='tinymce'),
re_path(r'^tinyMCEExternalList/post/edit/(\d+)$', tinymcelist_edit, name='tinymce'),
re_path(r'^comment/add/(\d+)/(\d+)$', add_comment, name='add_comment'),
re_path(r'^comment/edit/(\d+)$', edit_comment, name='edit_comment'),
re_path(r'^comment/delete/(\d+)$',delete_comment,name='delete_comment'),
re_path(r'^tag/(\d+)$', tag, name='tag'),
re_path(r'^tag/edit/(\d+)$', edit_tag, name='edit_tag'),
re_path(r'^tag/delete/(\d+)$', delete_tag, name='delete_tag'),
re_path(r'^search/generate/(\d+)$', generate_search,name='generate_search'),
re_path(r'^search/(\d+)$', search, name='search'),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
)
]

View File

@ -107,7 +107,7 @@ def createNavigationBar(blog_id, cur_page, nb_pages):
def index(request):
if request.user.is_authenticated():
if request.user.is_authenticated:
return HttpResponseRedirect('/blog')
login_failed = False
@ -526,7 +526,7 @@ def edit_post(request, post_id):
filename = b.src_path + '/_post/' + str(post.pk)
if os.path.exists(filename):
f = open(filename, 'rb')
content = f.read()
content = f.read().decode('utf-8')
f.close()
else:
content = 'Empty post'
@ -586,7 +586,7 @@ def edit_draft(request, draft_id):
filename = b.src_path + '/_draft/' + str(draft.pk)
if os.path.exists(filename):
f = open(filename, 'rb')
content = f.read()
content = f.read().decode('utf-8')
f.close()
else:
content = 'Empty draft'
@ -734,7 +734,7 @@ def preview(request, blog_id):
os.unlink(path)
f = open(path, 'wb')
f.write(content)
f.write(content.encode('utf-8'))
f.close()
c = {'content' : content}
@ -806,9 +806,9 @@ def add_comment(request, post_id, parent_id):
# Anti robot !!
if not 'email' in request.POST or request.POST['email'] != '':
if 'HTTP_X_REAL_IP' in request.META:
print 'Dynastie : fucking robot %s' % (request.META['HTTP_X_REAL_IP'])
print('Dynastie : fucking robot %s' % (request.META['HTTP_X_REAL_IP']))
else:
print 'Dynastie : fucking robot %s' % (request.META['REMOTE_ADDR'])
print('Dynastie : fucking robot %s' % (request.META['REMOTE_ADDR']))
return HttpResponseRedirect(ref)
post = Post.objects.get(pk=post_id)