mirror of
https://git.linux-kernel.at/oliver/ivatar.git
synced 2025-11-18 22:18:02 +00:00
Reduce occurrences of bare exceptions
* replace bare exceptions with specific ones, if possible
* use "exc" instead of "e" as exception variable
("exc" is the most popular choice in the standard library)
This commit is contained in:
@@ -107,7 +107,7 @@ DEFAULT_FROM_EMAIL = 'ivatar@mg.linux-kernel.at'
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
from ivatar.settings import DATABASES
|
from ivatar.settings import DATABASES
|
||||||
except Exception: # pragma: no cover
|
except ImportError: # pragma: no cover
|
||||||
DATABASES = [] # pragma: no cover
|
DATABASES = [] # pragma: no cover
|
||||||
|
|
||||||
if 'default' not in DATABASES:
|
if 'default' not in DATABASES:
|
||||||
|
|||||||
@@ -176,9 +176,9 @@ class Photo(BaseAccountModel):
|
|||||||
try:
|
try:
|
||||||
img = Image.open(BytesIO(self.data))
|
img = Image.open(BytesIO(self.data))
|
||||||
# Testing? Ideas anyone?
|
# Testing? Ideas anyone?
|
||||||
except Exception as e: # pylint: disable=invalid-name,broad-except
|
except Exception as exc: # pylint: disable=broad-except
|
||||||
# For debugging only
|
# For debugging only
|
||||||
print('Exception caught: %s' % e)
|
print('Exception caught: %s' % exc)
|
||||||
return False
|
return False
|
||||||
self.format = file_format(img.format)
|
self.format = file_format(img.format)
|
||||||
if not self.format:
|
if not self.format:
|
||||||
@@ -537,8 +537,7 @@ class DjangoOpenIDStore(OpenIDStore):
|
|||||||
try:
|
try:
|
||||||
# pylint: disable=no-member
|
# pylint: disable=no-member
|
||||||
expires = association.getExpiresIn()
|
expires = association.getExpiresIn()
|
||||||
# pylint: disable=invalid-name,broad-except,unused-variable
|
except AttributeError:
|
||||||
except Exception as e:
|
|
||||||
expires = association.expiresIn
|
expires = association.expiresIn
|
||||||
if expires == 0:
|
if expires == 0:
|
||||||
self.removeAssociation(server_url, assoc.handle)
|
self.removeAssociation(server_url, assoc.handle)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
Reading libravatar export
|
Reading libravatar export
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
import binascii
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
import gzip
|
import gzip
|
||||||
import xml.etree.ElementTree
|
import xml.etree.ElementTree
|
||||||
@@ -46,22 +47,23 @@ def read_gzdata(gzdata=None):
|
|||||||
if photo.tag == '{%s}photo' % SCHEMAROOT:
|
if photo.tag == '{%s}photo' % SCHEMAROOT:
|
||||||
try:
|
try:
|
||||||
data = base64.decodebytes(bytes(photo.text, 'utf-8'))
|
data = base64.decodebytes(bytes(photo.text, 'utf-8'))
|
||||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
except binascii.Error as exc:
|
||||||
print('Cannot decode photo; Encoding: %s, Format: %s: %s' % (
|
print('Cannot decode photo; Encoding: %s, Format: %s: %s' % (
|
||||||
photo.attrib['encoding'], photo.attrib['format'], e))
|
photo.attrib['encoding'], photo.attrib['format'], exc))
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
Image.open(BytesIO(data))
|
Image.open(BytesIO(data))
|
||||||
|
except Exception as exc: # pylint: disable=broad-except
|
||||||
|
print('Cannot decode photo; Encoding: %s, Format: %s: %s' % (
|
||||||
|
photo.attrib['encoding'], photo.attrib['format'], exc))
|
||||||
|
continue
|
||||||
|
else:
|
||||||
# If it is a working image, we can use it
|
# If it is a working image, we can use it
|
||||||
photo.text.replace('\n', '')
|
photo.text.replace('\n', '')
|
||||||
photos.append({
|
photos.append({
|
||||||
'data': photo.text,
|
'data': photo.text,
|
||||||
'format': photo.attrib['format'],
|
'format': photo.attrib['format'],
|
||||||
})
|
})
|
||||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
|
||||||
print('Cannot decode photo; Encoding: %s, Format: %s: %s' % (
|
|
||||||
photo.attrib['encoding'], photo.attrib['format'], e))
|
|
||||||
continue
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'emails': emails,
|
'emails': emails,
|
||||||
|
|||||||
@@ -4,9 +4,11 @@ View classes for ivatar/ivataraccount/
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from urllib.request import urlopen
|
from urllib.request import urlopen
|
||||||
import base64
|
import base64
|
||||||
|
import binascii
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
|
from django.db.models import ProtectedError
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
@@ -298,7 +300,7 @@ class ImportPhotoView(SuccessMessageMixin, TemplateView):
|
|||||||
if 'email_id' in kwargs:
|
if 'email_id' in kwargs:
|
||||||
try:
|
try:
|
||||||
addr = ConfirmedEmail.objects.get(pk=kwargs['email_id']).email
|
addr = ConfirmedEmail.objects.get(pk=kwargs['email_id']).email
|
||||||
except Exception: # pylint: disable=broad-except
|
except ConfirmedEmail.ObjectDoesNotExist:
|
||||||
messages.error(
|
messages.error(
|
||||||
self.request,
|
self.request,
|
||||||
_('Address does not exist'))
|
_('Address does not exist'))
|
||||||
@@ -316,10 +318,12 @@ class ImportPhotoView(SuccessMessageMixin, TemplateView):
|
|||||||
email=addr,
|
email=addr,
|
||||||
default=404,
|
default=404,
|
||||||
)
|
)
|
||||||
try:
|
if libravatar_service_url:
|
||||||
if libravatar_service_url:
|
try:
|
||||||
# if it doesn't work, it will be caught by except
|
|
||||||
urlopen(libravatar_service_url)
|
urlopen(libravatar_service_url)
|
||||||
|
except OSError as exc:
|
||||||
|
print('Exception caught during photo import: {}'.format(exc))
|
||||||
|
else:
|
||||||
context['photos'].append({
|
context['photos'].append({
|
||||||
'service_url': libravatar_service_url,
|
'service_url': libravatar_service_url,
|
||||||
'thumbnail_url': libravatar_service_url + '?s=80',
|
'thumbnail_url': libravatar_service_url + '?s=80',
|
||||||
@@ -328,9 +332,6 @@ class ImportPhotoView(SuccessMessageMixin, TemplateView):
|
|||||||
'height': 80,
|
'height': 80,
|
||||||
'service_name': 'Libravatar',
|
'service_name': 'Libravatar',
|
||||||
})
|
})
|
||||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
|
||||||
print('Exception caught during photo import: %s' % e)
|
|
||||||
pass
|
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
@@ -423,7 +424,7 @@ class DeletePhotoView(SuccessMessageMixin, View):
|
|||||||
photo = self.model.objects.get( # pylint: disable=no-member
|
photo = self.model.objects.get( # pylint: disable=no-member
|
||||||
pk=kwargs['pk'], user=request.user)
|
pk=kwargs['pk'], user=request.user)
|
||||||
photo.delete()
|
photo.delete()
|
||||||
except Exception: # pylint: disable=broad-except
|
except (self.model.DoesNotExist, ProtectedError):
|
||||||
messages.error(
|
messages.error(
|
||||||
request,
|
request,
|
||||||
_('No such image or no permission to delete it'))
|
_('No such image or no permission to delete it'))
|
||||||
@@ -558,12 +559,12 @@ class RedirectOpenIDView(View):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
auth_request = openid_consumer.begin(user_url)
|
auth_request = openid_consumer.begin(user_url)
|
||||||
except consumer.DiscoveryFailure as e: # pylint: disable=invalid-name
|
except consumer.DiscoveryFailure as exc:
|
||||||
messages.error(request, _('OpenID discovery failed: %s' % e))
|
messages.error(request, _('OpenID discovery failed: %s' % exc))
|
||||||
return HttpResponseRedirect(reverse_lazy('profile'))
|
return HttpResponseRedirect(reverse_lazy('profile'))
|
||||||
except UnicodeDecodeError as e: # pragma: no cover pylint: disable=invalid-name
|
except UnicodeDecodeError as exc: # pragma: no cover
|
||||||
msg = _('OpenID discovery failed (userid=%s) for %s: %s' %
|
msg = _('OpenID discovery failed (userid=%s) for %s: %s' %
|
||||||
(request.user.id, user_url.encode('utf-8'), e))
|
(request.user.id, user_url.encode('utf-8'), exc))
|
||||||
print(msg)
|
print(msg)
|
||||||
messages.error(request, msg)
|
messages.error(request, msg)
|
||||||
|
|
||||||
@@ -684,14 +685,14 @@ class CropPhotoView(TemplateView):
|
|||||||
if 'email' in request.POST:
|
if 'email' in request.POST:
|
||||||
try:
|
try:
|
||||||
email = ConfirmedEmail.objects.get(email=request.POST['email'])
|
email = ConfirmedEmail.objects.get(email=request.POST['email'])
|
||||||
except Exception: # pylint: disable=broad-except
|
except ConfirmedEmail.DoesNotExist:
|
||||||
pass # Ignore automatic assignment
|
pass # Ignore automatic assignment
|
||||||
|
|
||||||
if 'openid' in request.POST:
|
if 'openid' in request.POST:
|
||||||
try:
|
try:
|
||||||
openid = ConfirmedOpenId.objects.get( # pylint: disable=no-member
|
openid = ConfirmedOpenId.objects.get( # pylint: disable=no-member
|
||||||
openid=request.POST['openid'])
|
openid=request.POST['openid'])
|
||||||
except Exception: # pylint: disable=broad-except
|
except ConfirmedOpenId.DoesNotExist:
|
||||||
pass # Ignore automatic assignment
|
pass # Ignore automatic assignment
|
||||||
|
|
||||||
return photo.perform_crop(request, dimensions, email, openid)
|
return photo.perform_crop(request, dimensions, email, openid)
|
||||||
@@ -747,15 +748,16 @@ class UploadLibravatarExportView(SuccessMessageMixin, FormView):
|
|||||||
email,
|
email,
|
||||||
_('address added successfully,\
|
_('address added successfully,\
|
||||||
confirmation mail sent')))
|
confirmation mail sent')))
|
||||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
except Exception as exc: # pylint: disable=broad-except
|
||||||
# DEBUG
|
# DEBUG
|
||||||
print('Exception during adding mail address (%s): %s' % (email, e))
|
print('Exception during adding mail address (%s): %s'
|
||||||
|
% (email, exc))
|
||||||
|
|
||||||
if arg.startswith('photo'):
|
if arg.startswith('photo'):
|
||||||
try:
|
try:
|
||||||
data = base64.decodebytes(bytes(request.POST[arg], 'utf-8'))
|
data = base64.decodebytes(bytes(request.POST[arg], 'utf-8'))
|
||||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
except binascii.Error as exc:
|
||||||
print('Cannot decode photo: %s' % e)
|
print('Cannot decode photo: %s' % exc)
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
pilobj = Image.open(BytesIO(data))
|
pilobj = Image.open(BytesIO(data))
|
||||||
@@ -768,8 +770,8 @@ class UploadLibravatarExportView(SuccessMessageMixin, FormView):
|
|||||||
photo.format = file_format(pilobj.format)
|
photo.format = file_format(pilobj.format)
|
||||||
photo.data = out.read()
|
photo.data = out.read()
|
||||||
photo.save()
|
photo.save()
|
||||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
except Exception as exc: # pylint: disable=broad-except
|
||||||
print('Exception during save: %s' % e)
|
print('Exception during save: %s' % exc)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
return HttpResponseRedirect(reverse_lazy('profile'))
|
return HttpResponseRedirect(reverse_lazy('profile'))
|
||||||
@@ -799,17 +801,18 @@ class ResendConfirmationMailView(View):
|
|||||||
try:
|
try:
|
||||||
email = self.model.objects.get( # pylint: disable=no-member
|
email = self.model.objects.get( # pylint: disable=no-member
|
||||||
user=request.user, id=kwargs['email_id'])
|
user=request.user, id=kwargs['email_id'])
|
||||||
|
except self.model.DoesNotExist: # pragma: no cover # pylint: disable=no-member
|
||||||
|
messages.error(request, _('ID does not exist'))
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
email.send_confirmation_mail(
|
email.send_confirmation_mail(
|
||||||
url=request.build_absolute_uri('/')[:-1])
|
url=request.build_absolute_uri('/')[:-1])
|
||||||
messages.success(
|
messages.success(
|
||||||
request, '%s: %s' %
|
request, '%s: %s' %
|
||||||
(_('Confirmation mail sent to'), email.email))
|
(_('Confirmation mail sent to'), email.email))
|
||||||
except Exception as e: # pylint: disable=broad-except,invalid-name
|
except Exception as exc: # pylint: disable=broad-except
|
||||||
messages.error(
|
messages.error(
|
||||||
request, '%s %s: %s' %
|
request, '%s %s: %s' %
|
||||||
(_('Unable to send confirmation email for'),
|
(_('Unable to send confirmation email for'),
|
||||||
email.email, e))
|
email.email, exc))
|
||||||
except self.model.DoesNotExist: # pragma: no cover # pylint: disable=no-member
|
|
||||||
messages.error(request, _('ID does not exist'))
|
|
||||||
return HttpResponseRedirect(reverse_lazy('profile'))
|
return HttpResponseRedirect(reverse_lazy('profile'))
|
||||||
|
|||||||
Reference in New Issue
Block a user