Skip to content

Commit

Permalink
Styling fixes for tastypie.
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanHayes committed Sep 16, 2015
1 parent 20e1829 commit de750a9
Show file tree
Hide file tree
Showing 22 changed files with 341 additions and 229 deletions.
4 changes: 1 addition & 3 deletions tastypie/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ def urls(self):
warnings.warn("'override_urls' is a deprecated method & will be removed by v1.0.0. Please rename your method to ``prepend_urls``.")
urlpatterns += overridden_urls

urlpatterns += patterns('',
*pattern_list
)
urlpatterns += patterns('', *pattern_list)
return urlpatterns

def top_level(self, request, api_name=None):
Expand Down
4 changes: 2 additions & 2 deletions tastypie/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def is_authenticated(self, request, **kwargs):
return self._unauthorized()

try:
(auth_type, data) = request.META['HTTP_AUTHORIZATION'].split(' ', 1)
auth_type, data = request.META['HTTP_AUTHORIZATION'].split(' ', 1)

if auth_type.lower() != 'digest':
return self._unauthorized()
Expand Down Expand Up @@ -426,7 +426,7 @@ def __init__(self, **kwargs):
raise ImproperlyConfigured("The 'django-oauth-plus' package could not be imported. It is required for use with the 'OAuthAuthentication' class.")

def is_authenticated(self, request, **kwargs):
from oauth_provider.store import store, InvalidTokenError
from oauth_provider.store import store

if self.is_valid_request(request):
oauth_request = oauth_provider.utils.get_oauth_request(request)
Expand Down
32 changes: 26 additions & 6 deletions tastypie/authorization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import unicode_literals

from tastypie.exceptions import TastypieError, Unauthorized
from tastypie.compat import get_module_name


class Authorization(object):
"""
A base class that provides no permissions checking.
Expand Down Expand Up @@ -172,7 +174,10 @@ def create_list(self, object_list, bundle):
if klass is False:
return []

permission = '%s.add_%s' % (klass._meta.app_label, get_module_name(klass._meta))
permission = '%s.add_%s' % (
klass._meta.app_label,
get_module_name(klass._meta)
)

if not bundle.request.user.has_perm(permission):
return []
Expand All @@ -185,7 +190,10 @@ def create_detail(self, object_list, bundle):
if klass is False:
raise Unauthorized("You are not allowed to access that resource.")

permission = '%s.add_%s' % (klass._meta.app_label, get_module_name(klass._meta))
permission = '%s.add_%s' % (
klass._meta.app_label,
get_module_name(klass._meta)
)

if not bundle.request.user.has_perm(permission):
raise Unauthorized("You are not allowed to access that resource.")
Expand All @@ -198,7 +206,10 @@ def update_list(self, object_list, bundle):
if klass is False:
return []

permission = '%s.change_%s' % (klass._meta.app_label, get_module_name(klass._meta))
permission = '%s.change_%s' % (
klass._meta.app_label,
get_module_name(klass._meta)
)

if not bundle.request.user.has_perm(permission):
return []
Expand All @@ -211,7 +222,10 @@ def update_detail(self, object_list, bundle):
if klass is False:
raise Unauthorized("You are not allowed to access that resource.")

permission = '%s.change_%s' % (klass._meta.app_label, get_module_name(klass._meta))
permission = '%s.change_%s' % (
klass._meta.app_label,
get_module_name(klass._meta)
)

if not bundle.request.user.has_perm(permission):
raise Unauthorized("You are not allowed to access that resource.")
Expand All @@ -224,7 +238,10 @@ def delete_list(self, object_list, bundle):
if klass is False:
return []

permission = '%s.delete_%s' % (klass._meta.app_label, get_module_name(klass._meta))
permission = '%s.delete_%s' % (
klass._meta.app_label,
get_module_name(klass._meta)
)

if not bundle.request.user.has_perm(permission):
return []
Expand All @@ -237,7 +254,10 @@ def delete_detail(self, object_list, bundle):
if klass is False:
raise Unauthorized("You are not allowed to access that resource.")

permission = '%s.delete_%s' % (klass._meta.app_label, get_module_name(klass._meta))
permission = '%s.delete_%s' % (
klass._meta.app_label,
get_module_name(klass._meta)
)

if not bundle.request.user.has_perm(permission):
raise Unauthorized("You are not allowed to access that resource.")
Expand Down
7 changes: 5 additions & 2 deletions tastypie/compat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import unicode_literals
from django.conf import settings

import django
from django.conf import settings


__all__ = ['get_user_model', 'get_username_field', 'AUTH_USER_MODEL']

Expand All @@ -24,8 +26,9 @@ def get_user_model():
def get_username_field():
return 'username'


def get_module_name(meta):
return getattr(meta, 'model_name', None) or getattr(meta, 'module_name')

# commit_on_success replaced by atomic in Django >=1.8
atomic_decorator = getattr(django.db.transaction, 'atomic', None) or getattr(django.db.transaction, 'commit_on_success')
atomic_decorator = getattr(django.db.transaction, 'atomic', None) or getattr(django.db.transaction, 'commit_on_success')
6 changes: 4 additions & 2 deletions tastypie/contrib/contenttypes/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class GenericResource(ModelResource):
Provides a stand-in resource for GFK relations.
"""
def __init__(self, resources, *args, **kwargs):
self.resource_mapping = dict((r._meta.resource_name, r) for r in resources)
self.resource_mapping = dict(
(r._meta.resource_name, r) for r in resources
)
super(GenericResource, self).__init__(*args, **kwargs)

def get_via_uri(self, uri, request=None):
Expand All @@ -27,7 +29,7 @@ def get_via_uri(self, uri, request=None):
chomped_uri = uri

if prefix and chomped_uri.startswith(prefix):
chomped_uri = chomped_uri[len(prefix)-1:]
chomped_uri = chomped_uri[len(prefix) - 1:]

try:
view, args, kwargs = resolve(chomped_uri)
Expand Down
11 changes: 6 additions & 5 deletions tastypie/fields.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import unicode_literals

import datetime
from dateutil.parser import parse
import decimal
from decimal import Decimal

try:
import importlib
except ImportError:
Expand All @@ -25,7 +27,7 @@ def __str__(self):
# All the ApiField variants.

class ApiField(object):
"""The base implementation of a field used by the resources."""
"The base implementation of a field used by the resources."
is_m2m = False
is_related = False
dehydrated_type = 'string'
Expand Down Expand Up @@ -158,7 +160,7 @@ def hydrate(self, bundle):
"""
if self.readonly:
return None
if not self.instance_name in bundle.data:
if self.instance_name not in bundle.data:
if self.is_related and not self.is_m2m:
# We've got an FK (or alike field) & a possible parent object.
# Check for it.
Expand Down Expand Up @@ -509,7 +511,7 @@ def get_related_resource(self, related_instance):

# Fix the ``api_name`` if it's not present.
if related_resource._meta.api_name is None:
if self._resource and not self._resource._meta.api_name is None:
if self._resource and self._resource._meta.api_name is not None:
related_resource._meta.api_name = self._resource._meta.api_name

self._rel_resources[related_class] = related_resource
Expand Down Expand Up @@ -707,7 +709,6 @@ def __init__(self, to, attribute, related_name=None, default=NOT_PROVIDED,

def dehydrate(self, bundle, for_list=True):
foreign_obj = None
error_to_raise = None

if callable(self.attribute):
previous_obj = bundle.obj
Expand All @@ -728,7 +729,6 @@ def dehydrate(self, bundle, for_list=True):
raise ApiFieldError("The related resource for resource %s could not be found." % (previous_obj))
else:
raise ApiFieldError("The model '%r' has an empty attribute '%s' and doesn't allow a null value." % (previous_obj, attr))

return None

fk_resource = self.get_related_resource(foreign_obj)
Expand All @@ -743,6 +743,7 @@ def hydrate(self, bundle):

return self.build_related_resource(value, request=bundle.request)


class ForeignKey(ToOneField):
"""
A convenience subclass for those who prefer to mirror ``django.db.models``.
Expand Down
1 change: 0 additions & 1 deletion tastypie/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,3 @@ class HttpApplicationError(HttpResponse):

class HttpNotImplemented(HttpResponse):
status_code = 501

2 changes: 1 addition & 1 deletion tastypie/management/commands/backfill_api_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Command(NoArgsCommand):
help = "Goes through all users and adds API keys for any that don't have one."

def handle_noargs(self, **options):
"""Goes through all users and adds API keys for any that don't have one."""
"Goes through all users and adds API keys for any that don't have one."
self.verbosity = int(options.get('verbosity', 1))

User = get_user_model()
Expand Down
6 changes: 5 additions & 1 deletion tastypie/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import unicode_literals

import hmac
import time

from django.conf import settings
from django.db import models
from django.utils.encoding import python_2_unicode_compatible

from tastypie.utils import now

try:
Expand All @@ -12,6 +15,7 @@
import sha
sha1 = sha.sha


@python_2_unicode_compatible
class ApiAccess(models.Model):
"""A simple model for use with the ``CacheDBThrottle`` behaviors."""
Expand All @@ -31,6 +35,7 @@ def save(self, *args, **kwargs):
if 'django.contrib.auth' in settings.INSTALLED_APPS:
import uuid
from tastypie.compat import AUTH_USER_MODEL

class ApiKey(models.Model):
user = models.OneToOneField(AUTH_USER_MODEL, related_name='api_key')
key = models.CharField(max_length=128, blank=True, default='', db_index=True)
Expand All @@ -54,7 +59,6 @@ def generate_key(self):
class Meta:
abstract = getattr(settings, 'TASTYPIE_ABSTRACT_APIKEY', False)


def create_api_key(sender, **kwargs):
"""
A signal for hooking up automatic ``ApiKey`` creation.
Expand Down
4 changes: 2 additions & 2 deletions tastypie/paginator.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def get_previous(self, limit, offset):
if offset - limit < 0:
return None

return self._generate_uri(limit, offset-limit)
return self._generate_uri(limit, offset - limit)

def get_next(self, limit, offset, count):
"""
Expand All @@ -146,7 +146,7 @@ def get_next(self, limit, offset, count):
if offset + limit >= count:
return None

return self._generate_uri(limit, offset+limit)
return self._generate_uri(limit, offset + limit)

def _generate_uri(self, limit, offset):
if self.resource_uri is None:
Expand Down
Loading

0 comments on commit de750a9

Please sign in to comment.