fix pylint warnings as exercise

This commit is contained in:
Benjamin Dauvergne 2017-10-18 16:53:51 +02:00
parent 74087001d0
commit 567731d6bd
8 changed files with 32 additions and 21 deletions

View File

@ -14,10 +14,11 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import requests
import urlparse
import logging
import requests
from django.db.models.query import Q, F
from django.http import StreamingHttpResponse, HttpResponse
from django.conf import settings
@ -198,6 +199,7 @@ class PetalAPIView(APIView):
@logit
@atomic
def put(self, request, partner_name, cut_uuid, petal_name):
# pylint: disable=too-many-locals
if_match = request.META.get('HTTP_IF_MATCH')
if_none_match = request.META.get('HTTP_IF_NONE_MATCH')
content_type = request.META.get('CONTENT_TYPE')

View File

@ -14,10 +14,11 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import requests
import urlparse
import logging
import requests
from django.conf import settings
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
@ -50,7 +51,7 @@ class PetalAuthentication(BasicAuthentication):
'username': userid,
'password': password}, auth=authentic_auth, verify=False)
response.raise_for_status()
except Exception as e:
except requests.RequestException as e:
logger.warning(u'authentic check-password API failed: %s', e)
return False, 'authentic is down'
try:
@ -68,11 +69,13 @@ class PetalAuthentication(BasicAuthentication):
def authenticate_credentials(self, userid, password):
username = userid[:30]
try:
user, auth = super(PetalAuthentication, self).authenticate_credentials(username, password)
user, auth = super(PetalAuthentication, self).authenticate_credentials(username,
password)
except AuthenticationFailed:
ok, error = self.authentic_proxy(userid, password)
if not ok:
success, error = self.authentic_proxy(userid, password)
if not success:
raise AuthenticationFailed(error or _('Invalid username/password.'))
# pylint: disable=unused-variable
user, created = User.objects.get_or_create(username=username)
user.set_password(password)
auth = None

View File

@ -19,7 +19,7 @@ from rest_framework.exceptions import APIException
class PetalAPIException(APIException):
def __init__(self):
def __init__(self): # pylint: disable=super-init-not-called
pass

View File

@ -69,6 +69,7 @@ class Partner(models.Model):
and new_size > self.soft_global_max_size * 1024):
self.notify_admins(
subject=_('Partner %s space almost exhausted') % self.name,
# pylint: disable=no-member
body=_('Current size: {current_size}, Max size: {max_size}').format(
current_size=new_size,
max_size=self.hard_global_max_size * 1024),
@ -165,9 +166,11 @@ class Petal(models.Model):
if (self.size <= self.partner.soft_per_key_max_size * 1024 and content_length >
self.partner.soft_per_key_max_size * 1024):
self.partner.notify_admins(
# pylint: disable=no-member
_('Key {key} space of partner {partner} almost exhausted').format(
key=self.name,
partner=self.partner.name),
# pylint: disable=no-member
_('Current size: {current_size}, Max size: {max_size}').format(
current_size=content_length,
max_size=self.partner.hard_per_key_max_size * 1024),

View File

@ -15,7 +15,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from rest_framework.permissions import BasePermission
from rest_framework.exceptions import MethodNotAllowed
from .models import AccessControlList
from .exceptions import AccessForbidden

View File

@ -17,12 +17,15 @@
from django.conf.urls import patterns, include, url
from django.contrib import admin
from api_views import PetalAPIView, PetalAPIKeysView
from .api_views import PetalAPIView, PetalAPIKeysView
urlpatterns = patterns(
'',
url(r'^admin/', include(admin.site.urls)),
url(r'^api/(?P<partner_name>[\w,-]+)/(?P<cut_uuid>[\w,-]{,255})/$', PetalAPIKeysView.as_view(), name='api-keys'),
url(r'^api/(?P<partner_name>[\w,-]+)/(?P<cut_uuid>[\w,-]{,255})/(?P<petal_name>[\w,-]+)/$', PetalAPIView.as_view(), name='api')
url(r'^api/(?P<partner_name>[\w,-]+)/(?P<cut_uuid>[\w,-]{,255})/$',
PetalAPIKeysView.as_view(),
name='api-keys'),
url(r'^api/(?P<partner_name>[\w,-]+)/(?P<cut_uuid>[\w,-]{,255})/(?P<petal_name>[\w,-]+)/$',
PetalAPIView.as_view(), name='api')
)

View File

@ -30,28 +30,28 @@ def logit(func):
logger.info(req_url, extra={'request_url': req_url})
req_headers = ''.join(
['%s: %s | ' % (k, v) for k, v in request.META.items() if k.isupper()])
logger.debug('Request Headers: %s' % req_headers, extra={'request_headers': req_headers})
logger.debug('Request Headers: %s', req_headers, extra={'request_headers': req_headers})
response = func(self, request, *args, **kwargs)
resp_headers = ''.join(['%s: %s | ' % (k, v) for k, v in response.items()])
logger.debug('Response Headers: %s' % resp_headers,
logger.debug('Response Headers: %s', resp_headers,
extra={'response_headers': resp_headers})
if hasattr(response, 'data'):
logger.debug('Response Data: %r' % response.data,
logger.debug('Response Data: %r', response.data,
extra={'response_body': response.data})
logger.debug('Response Status Code: %s' % response.status_code,
logger.debug('Response Status Code: %s', response.status_code,
extra={'response_status_code': response.status_code})
return response
return wrapper
def etag(f):
def etag(stream):
# f must be a Django file object
digest = hashlib.new(DEFAULT_HASH_ALGO)
if hasattr(f, 'chunks'):
for chunk in f.chunks():
if hasattr(stream, 'chunks'):
for chunk in stream.chunks():
digest.update(chunk)
else:
digest.update(f)
digest.update(stream)
return '"%s:%s"' % (DEFAULT_HASH_ALGO, digest.hexdigest())
@ -62,7 +62,7 @@ class StreamingHash(object):
self.digest = hashlib.new(hash_algo)
self.size = 0
def read(self, n=-1):
def read(self, n=-1): # pylint: disable=C0103
buf = self.readable.read(n)
self.size += len(buf)
self.digest.update(buf)

View File

@ -16,7 +16,8 @@
import os
os.environ["DJANGO_SETTINGS_MODULE"] = "petale.settings"
from django.core.wsgi import get_wsgi_application
os.environ["DJANGO_SETTINGS_MODULE"] = "petale.settings"
application = get_wsgi_application()