Python2/3 compatibility: Added force_str or force_bytes in some places, to ensure the argument right type

- Legacy-Id: 16450
This commit is contained in:
Henrik Levkowetz 2019-07-15 15:49:35 +00:00
parent eb42394534
commit 831fb18f8a
3 changed files with 21 additions and 5 deletions

View file

@ -1,5 +1,6 @@
# Copyright The IETF Trust 2007-2019, All Rights Reserved
# -*- coding: utf-8 -*-
#
# Portions Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
# All rights reserved. Contact: Pasi Eronen <pasi.eronen@nokia.com>
#
@ -32,6 +33,9 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from __future__ import absolute_import, print_function, unicode_literals
import importlib
from datetime import datetime as DateTime, timedelta as TimeDelta, date as Date
@ -54,6 +58,7 @@ from django.urls import reverse as urlreverse
from django.utils.safestring import mark_safe
from django.http import Http404, HttpResponseRedirect #, HttpResponse,
from django.shortcuts import render, redirect, get_object_or_404
from django.utils.encoding import force_bytes
import debug # pyflakes:ignore
@ -674,7 +679,7 @@ def apikey_disable(request):
class KeyDeleteForm(forms.Form):
hash = forms.ChoiceField(label='Key', choices=choices)
def clean_key(self):
hash = self.cleaned_data['hash']
hash = force_bytes(self.cleaned_data['hash'])
key = PersonalApiKey.validate_key(hash)
if key and key.person == request.user.person:
return hash
@ -684,7 +689,7 @@ def apikey_disable(request):
if request.method == 'POST':
form = KeyDeleteForm(request.POST)
if form.is_valid():
hash = form.data['hash']
hash = force_bytes(form.data['hash'])
key = PersonalApiKey.validate_key(hash)
key.valid = False
key.save()

View file

@ -1,4 +1,9 @@
# Copyright The IETF Trust 2014-2019, All Rights Reserved
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import datetime
import email
@ -6,6 +11,7 @@ import email
from django import forms
from django.core.validators import RegexValidator
from django.utils.safestring import mark_safe
from django.utils.encoding import force_str
import debug # pyflakes:ignore
@ -71,7 +77,7 @@ class AddEmailForm(forms.Form):
def clean_message(self):
'''Returns a ietf.message.models.Message object'''
text = self.cleaned_data['message']
message = email.message_from_string(text)
message = email.message_from_string(force_str(text))
for field in ('to','from','subject','date'):
if not message[field]:
raise forms.ValidationError('Error parsing email: {} field not found.'.format(field))

View file

@ -1,4 +1,8 @@
# Copyright The IETF Trust 2016-2019, All Rights Reserved
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import datetime
@ -8,6 +12,7 @@ from django.conf import settings
from django.contrib.auth import login
from django.http import HttpResponse
from django.shortcuts import render
from django.utils.encoding import force_bytes
import debug # pyflakes:ignore
@ -49,7 +54,7 @@ def require_api_key(f, request, *args, **kwargs):
if not hash:
return err(400, "Missing apikey parameter")
# Check hash
key = PersonalApiKey.validate_key(hash)
key = PersonalApiKey.validate_key(force_bytes(hash))
if not key:
return err(400, "Invalid apikey")
# Check endpoint