replace except: pass with suppress() context manager
This commit is contained in:
parent
23694b207b
commit
341c2f98e5
5 changed files with 12 additions and 22 deletions
|
@ -1,3 +1,4 @@
|
|||
from contextlib import suppress
|
||||
from functools import wraps
|
||||
|
||||
from django.apps import apps
|
||||
|
@ -107,12 +108,10 @@ def edit(request, pk=None, model=None, section=None, space=None, explicit_edit=F
|
|||
'title': obj.title if obj else None,
|
||||
}
|
||||
|
||||
try:
|
||||
with suppress(FieldDoesNotExist):
|
||||
ctx.update({
|
||||
'geomtype': model._meta.get_field('geometry').geomtype,
|
||||
})
|
||||
except FieldDoesNotExist:
|
||||
pass
|
||||
|
||||
if model == Section:
|
||||
ctx.update({
|
||||
|
|
|
@ -25,13 +25,9 @@ class MapdataConfig(AppConfig):
|
|||
from c3nav.mapdata.models.geometry.base import GeometryMixin, GEOMETRY_MODELS
|
||||
for cls in self._get_submodels(GeometryMixin):
|
||||
GEOMETRY_MODELS[cls.__name__] = cls
|
||||
geometry = None
|
||||
try:
|
||||
geometry = cls._meta.get_field('geometry')
|
||||
cls._meta.get_field('geometry')
|
||||
except FieldDoesNotExist:
|
||||
pass
|
||||
|
||||
if geometry is None:
|
||||
raise TypeError(_('Model %s has GeometryMixin as base class but has no geometry field.') % cls)
|
||||
|
||||
from c3nav.mapdata.models.locations import Location, LOCATION_MODELS
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from contextlib import suppress
|
||||
|
||||
from django.apps import apps
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
@ -23,10 +25,8 @@ class LocationSlug(SerializableMixin, models.Model):
|
|||
def get_child(self):
|
||||
# todo: cache this
|
||||
for model in LOCATION_MODELS+[LocationRedirect]:
|
||||
try:
|
||||
with suppress(AttributeError):
|
||||
return getattr(self, model._meta.default_related_name)
|
||||
except AttributeError:
|
||||
pass
|
||||
return None
|
||||
|
||||
def get_slug(self):
|
||||
|
|
|
@ -3,6 +3,7 @@ import configparser
|
|||
import os
|
||||
import string
|
||||
import sys
|
||||
from contextlib import suppress
|
||||
|
||||
from django.contrib.messages import constants as messages
|
||||
from django.utils.crypto import get_random_string
|
||||
|
@ -155,19 +156,13 @@ MIDDLEWARE = [
|
|||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
]
|
||||
|
||||
try:
|
||||
with suppress(ImportError):
|
||||
import debug_toolbar # noqa
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
INSTALLED_APPS.append('debug_toolbar')
|
||||
MIDDLEWARE.append('debug_toolbar.middleware.DebugToolbarMiddleware')
|
||||
|
||||
try:
|
||||
with suppress(ImportError):
|
||||
import htmlmin # noqa
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
MIDDLEWARE += [
|
||||
'htmlmin.middleware.HtmlMinifyMiddleware',
|
||||
'htmlmin.middleware.MarkRequestMiddleware',
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from contextlib import suppress
|
||||
|
||||
from django.conf import settings
|
||||
from django.conf.urls import include, url
|
||||
from django.contrib import admin
|
||||
|
@ -15,8 +17,6 @@ urlpatterns = [
|
|||
]
|
||||
|
||||
if settings.DEBUG:
|
||||
try:
|
||||
with suppress(ImportError):
|
||||
import debug_toolbar
|
||||
urlpatterns.insert(0, url(r'^__debug__/', include(debug_toolbar.urls)))
|
||||
except ImportError:
|
||||
pass
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue