Permalink
Browse files

flake8 all of the things

  • Loading branch information...
wraithan committed Apr 8, 2013
1 parent 1c0dfc5 commit d256625d837cd4ac7168cf93cd446d9a0674b15a
View
@@ -17,9 +17,10 @@
from projects import tasks
from djangome import views as djangome
+from .utils import SearchMixin, PostAuthentication, EnhancedModelResource
+
log = logging.getLogger(__name__)
-from .utils import SearchMixin, PostAuthentication, EnhancedModelResource
class ProjectResource(ModelResource, SearchMixin):
users = fields.ToManyField('api.base.UserResource', 'users')
@@ -60,20 +61,28 @@ def post_list(self, request, **kwargs):
If a new resource is created, return ``HttpCreated`` (201 Created).
"""
- deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))
+ deserialized = self.deserialize(
+ request, request.raw_post_data,
+ format=request.META.get('CONTENT_TYPE', 'application/json')
+ )
# Force this in an ugly way, at least should do "reverse"
- deserialized["users"] = ["/api/v1/user/%s/" % request.user.id,]
+ deserialized["users"] = ["/api/v1/user/%s/" % request.user.id]
bundle = self.build_bundle(data=dict_strip_unicode_keys(deserialized))
self.is_valid(bundle, request)
updated_bundle = self.obj_create(bundle, request=request)
return HttpCreated(location=self.get_resource_uri(updated_bundle))
def override_urls(self):
return [
- url(r"^(?P<resource_name>%s)/schema/$" % self._meta.resource_name, self.wrap_view('get_schema'), name="api_get_schema"),
- url(r"^(?P<resource_name>%s)/search%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_search'), name="api_get_search"),
- url(r"^(?P<resource_name>%s)/(?P<slug>[a-z-_]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
+ url(r"^(?P<resource_name>%s)/schema/$" % self._meta.resource_name,
+ self.wrap_view('get_schema'), name="api_get_schema"),
+ url(r"^(?P<resource_name>%s)/search%s$" % (
+ self._meta.resource_name, trailing_slash()),
+ self.wrap_view('get_search'), name="api_get_search"),
+ url((r"^(?P<resource_name>%s)/(?P<slug>[a-z-_]+)/$")
+ % self._meta.resource_name, self.wrap_view('dispatch_detail'),
+ name="api_dispatch_detail"),
]
@@ -93,13 +102,16 @@ class Meta:
"active": ALL,
}
- #Find a better name for this before including it.
- #def dehydrate(self, bundle):
- #bundle.data['subdomain'] = "http://%s/en/%s/" % (bundle.obj.project.subdomain, bundle.obj.slug)
- #return bundle
+ # Find a better name for this before including it.
+ # def dehydrate(self, bundle):
+ # bundle.data['subdomain'] = "http://%s/en/%s/" % (
+ # bundle.obj.project.subdomain, bundle.obj.slug
+ # )
+ # return bundle
def get_object_list(self, request):
- self._meta.queryset = Version.objects.public(user=request.user, only_active=False)
+ self._meta.queryset = Version.objects.public(user=request.user,
+ only_active=False)
return super(VersionResource, self).get_object_list(request)
def version_compare(self, request, **kwargs):
@@ -113,15 +125,15 @@ def version_compare(self, request, **kwargs):
}
if highest[0]:
ret_val['url'] = highest[0].get_absolute_url()
- ret_val['slug'] = highest[0].slug,
+ ret_val['slug'] = highest[0].slug,
if base and base != 'latest':
try:
ver_obj = project.versions.get(slug=base)
base_ver = mkversion(ver_obj)
if base_ver:
- #This is only place where is_highest can get set.
- #All error cases will be set to True, for non-
- #standard versions.
+ # This is only place where is_highest can get set. All
+ # error cases will be set to True, for non- standard
+ # versions.
ret_val['is_highest'] = base_ver >= highest[1]
else:
ret_val['is_highest'] = True
@@ -138,13 +150,31 @@ def build_version(self, request, **kwargs):
def override_urls(self):
return [
- url(r"^(?P<resource_name>%s)/schema/$" % self._meta.resource_name, self.wrap_view('get_schema'), name="api_get_schema"),
- url(r"^(?P<resource_name>%s)/(?P<project_slug>[a-z-_]+)/highest/(?P<base>.+)/$" % self._meta.resource_name, self.wrap_view('version_compare'), name="version_compare"),
- url(r"^(?P<resource_name>%s)/(?P<project_slug>[a-z-_]+)/highest/$" % self._meta.resource_name, self.wrap_view('version_compare'), name="version_compare"),
- url(r"^(?P<resource_name>%s)/(?P<project__slug>[a-z-_]+[a-z0-9-_]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_list'), name="api_version_list"),
- url(r"^(?P<resource_name>%s)/(?P<project_slug>[a-z-_]+)/(?P<version_slug>[a-z0-9-_.]+)/build/$" % self._meta.resource_name, self.wrap_view('build_version'), name="api_version_build_slug"),
+ url(r"^(?P<resource_name>%s)/schema/$"
+ % self._meta.resource_name,
+ self.wrap_view('get_schema'),
+ name="api_get_schema"),
+ url((r"^(?P<resource_name>%s)/(?P<project_slug>[a-z-_]+)/highest/"
+ r"(?P<base>.+)/$")
+ % self._meta.resource_name,
+ self.wrap_view('version_compare'),
+ name="version_compare"),
+ url(r"^(?P<resource_name>%s)/(?P<project_slug>[a-z-_]+)/highest/$"
+ % self._meta.resource_name,
+ self.wrap_view('version_compare'),
+ name="version_compare"),
+ url(r"^(?P<resource_name>%s)/(?P<project__slug>[a-z-_]+[a-z0-9-_]+)/$" # noqa
+ % self._meta.resource_name,
+ self.wrap_view('dispatch_list'),
+ name="api_version_list"),
+ url((r"^(?P<resource_name>%s)/(?P<project_slug>[a-z-_]+)/(?P"
+ r"<version_slug>[a-z0-9-_.]+)/build/$")
+ % self._meta.resource_name,
+ self.wrap_view('build_version'),
+ name="api_version_build_slug"),
]
+
class BuildResource(EnhancedModelResource):
project = fields.ForeignKey('api.base.ProjectResource', 'project')
version = fields.ForeignKey('api.base.VersionResource', 'version')
@@ -164,10 +194,17 @@ class Meta:
def override_urls(self):
return [
- url(r"^(?P<resource_name>%s)/schema/$" % self._meta.resource_name, self.wrap_view('get_schema'), name="api_get_schema"),
- url(r"^(?P<resource_name>%s)/(?P<project__slug>[a-z-_]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_list'), name="build_list_detail"),
+ url(r"^(?P<resource_name>%s)/schema/$"
+ % self._meta.resource_name,
+ self.wrap_view('get_schema'),
+ name="api_get_schema"),
+ url(r"^(?P<resource_name>%s)/(?P<project__slug>[a-z-_]+)/$" %
+ self._meta.resource_name,
+ self.wrap_view('dispatch_list'),
+ name="build_list_detail"),
]
+
class FileResource(EnhancedModelResource, SearchMixin):
project = fields.ForeignKey(ProjectResource, 'project', full=True)
@@ -182,9 +219,18 @@ class Meta:
def override_urls(self):
return [
- url(r"^(?P<resource_name>%s)/schema/$" % self._meta.resource_name, self.wrap_view('get_schema'), name="api_get_schema"),
- url(r"^(?P<resource_name>%s)/search%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_search'), name="api_get_search"),
- url(r"^(?P<resource_name>%s)/anchor%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_anchor'), name="api_get_anchor"),
+ url(r"^(?P<resource_name>%s)/schema/$" %
+ self._meta.resource_name,
+ self.wrap_view('get_schema'),
+ name="api_get_schema"),
+ url(r"^(?P<resource_name>%s)/search%s$" %
+ (self._meta.resource_name, trailing_slash()),
+ self.wrap_view('get_search'),
+ name="api_get_search"),
+ url(r"^(?P<resource_name>%s)/anchor%s$" %
+ (self._meta.resource_name, trailing_slash()),
+ self.wrap_view('get_anchor'),
+ name="api_get_anchor"),
]
def get_anchor(self, request, **kwargs):
@@ -195,7 +241,8 @@ def get_anchor(self, request, **kwargs):
query = request.GET.get('q', '')
redis_data = djangome.r.keys("*redirects:v4*%s*" % query)
#-2 because http:
- urls = [''.join(data.split(':')[6:]) for data in redis_data if 'http://' in data]
+ urls = [''.join(data.split(':')[6:]) for data in redis_data
+ if 'http://' in data]
"""
paginator = Paginator(urls, 20)
@@ -208,7 +255,7 @@ def get_anchor(self, request, **kwargs):
objects = [result for result in page.object_list]
object_list = { 'objects': objects, }
"""
- object_list = { 'objects': urls }
+ object_list = {'objects': urls}
self.log_throttled_access(request)
return self.create_response(request, object_list)
@@ -225,6 +272,12 @@ class Meta:
def override_urls(self):
return [
- url(r"^(?P<resource_name>%s)/schema/$" % self._meta.resource_name, self.wrap_view('get_schema'), name="api_get_schema"),
- url(r"^(?P<resource_name>%s)/(?P<username>[a-z-_]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
+ url(r"^(?P<resource_name>%s)/schema/$" %
+ self._meta.resource_name,
+ self.wrap_view('get_schema'),
+ name="api_get_schema"),
+ url(r"^(?P<resource_name>%s)/(?P<username>[a-z-_]+)/$" %
+ self._meta.resource_name,
+ self.wrap_view('dispatch_detail'),
+ name="api_dispatch_detail"),
]
View
@@ -17,6 +17,7 @@
log = logging.getLogger(__name__)
+
class SearchMixin(object):
'''
Adds a search api to any ModelResource provided the model is indexed.
@@ -39,12 +40,11 @@ def get_search(self, request, **kwargs):
self.method_check(request, allowed=['get'])
self.is_authenticated(request)
self.throttle_check(request)
- object_list = self._search(request,
- self._meta.queryset.model,
- facets = getattr(self._meta, 'search_facets', []),
- page_size = getattr(self._meta, 'search_page_size', 20),
- highlight = getattr(self._meta, 'search_highlight', True),
- )
+ object_list = self._search(
+ request, self._meta.queryset.model,
+ facets=getattr(self._meta, 'search_facets', []),
+ page_size=getattr(self._meta, 'search_page_size', 20),
+ highlight=getattr(self._meta, 'search_highlight', True))
self.log_throttled_access(request)
return self.create_response(request, object_list)
@@ -65,7 +65,8 @@ def _url_template(self, query, selected_facets):
})
return url_template + '?' + query_string
- def _search(self, request, model, facets=None, page_size=20, highlight=True):
+ def _search(self, request, model, facets=None, page_size=20,
+ highlight=True):
'''
`facets`
A list of facets to include with the results
@@ -75,7 +76,7 @@ def _search(self, request, model, facets=None, page_size=20, highlight=True):
form = FacetedSearchForm(request.GET, facets=facets or [],
models=(model,), load_all=True)
if not form.is_valid():
- return self.error_response({'errors': form.errors }, request)
+ return self.error_response({'errors': form.errors}, request)
results = form.search()
paginator = Paginator(results, page_size)
@@ -98,7 +99,8 @@ def _search(self, request, model, facets=None, page_size=20, highlight=True):
bundle.data['text'] = text
objects.append(bundle)
- url_template = self._url_template(query, form['selected_facets'].value())
+ url_template = self._url_template(query,
+ form['selected_facets'].value())
page_data = {
'number': page.number,
'per_page': paginator.per_page,
@@ -108,9 +110,11 @@ def _search(self, request, model, facets=None, page_size=20, highlight=True):
'url_template': url_template,
}
if page.has_next():
- page_data['url_next'] = url_template.format(page.next_page_number())
+ page_data['url_next'] = url_template.format(
+ page.next_page_number())
if page.has_previous():
- page_data['url_prev'] = url_template.format(page.previous_page_number())
+ page_data['url_prev'] = url_template.format(
+ page.previous_page_number())
object_list = {
'page': page_data,
@@ -120,7 +124,6 @@ def _search(self, request, model, facets=None, page_size=20, highlight=True):
object_list.update({'facets': results.facet_counts()})
return object_list
-
# XXX: This method is available in the latest tastypie, remove
# once available in production.
def error_response(self, errors, request):
@@ -129,13 +132,16 @@ def error_response(self, errors, request):
else:
desired_format = self._meta.default_format
serialized = self.serialize(request, errors, desired_format)
- response = http.HttpBadRequest(content=serialized, content_type=build_content_type(desired_format))
+ response = http.HttpBadRequest(
+ content=serialized,
+ content_type=build_content_type(desired_format))
raise ImmediateHttpResponse(response=response)
class PostAuthentication(BasicAuthentication):
def is_authenticated(self, request, **kwargs):
- val = super(PostAuthentication, self).is_authenticated(request, **kwargs)
+ val = super(PostAuthentication, self).is_authenticated(request,
+ **kwargs)
if request.method == "GET":
return True
return val
@@ -160,7 +166,10 @@ def obj_get_list(self, request=None, **kwargs):
try:
return self.get_object_list(request).filter(**applicable_filters)
except ValueError, e:
- raise NotFound(ugettext("Invalid resource lookup data provided (mismatched type).: %(error)s") % {'error': e})
+ raise NotFound(ugettext("Invalid resource lookup data provided "
+ "(mismatched type).: %(error)s") % {
+ 'error': e
+ })
class OwnerAuthorization(Authorization):
@@ -1,17 +1,21 @@
"""Django admin interface for `~builds.models.Build` and related models.
+
"""
from django.contrib import admin
from builds.models import Build, VersionAlias, Version
from guardian.admin import GuardedModelAdmin
+
class BuildAdmin(admin.ModelAdmin):
list_display = ('project', 'date', 'success', 'type', 'state')
+
class VersionAdmin(GuardedModelAdmin):
search_fields = ('slug', 'project__name')
list_filter = ('project', 'privacy_level')
+
admin.site.register(Build, BuildAdmin)
admin.site.register(VersionAlias)
admin.site.register(Version, VersionAdmin)
@@ -12,25 +12,33 @@
BUILD_TYPES = ANY_REPO + constants.BUILD_TYPES
+
class VersionSlugFilter(django_filters.FilterSet):
- slug = django_filters.CharFilter(label=_("Name"), name='slug', lookup_type='icontains')
- tag = django_filters.CharFilter(label=_("Tag"), name='tags', lookup_type='name__icontains')
+ slug = django_filters.CharFilter(label=_("Name"), name='slug',
+ lookup_type='icontains')
+ tag = django_filters.CharFilter(label=_("Tag"), name='tags',
+ lookup_type='name__icontains')
class Meta:
model = Version
fields = ['slug', 'tag']
+
class VersionFilter(django_filters.FilterSet):
- project = django_filters.CharFilter(name='project__name', lookup_type="icontains")
- slug = django_filters.CharFilter(label=_("Name"), name='slug', lookup_type='icontains')
+ project = django_filters.CharFilter(name='project__name',
+ lookup_type="icontains")
+ slug = django_filters.CharFilter(label=_("Name"), name='slug',
+ lookup_type='icontains')
class Meta:
model = Version
fields = ['project', 'slug']
+
class BuildFilter(django_filters.FilterSet):
date = django_filters.DateRangeFilter(label=_("Build Date"), name="date")
- type = django_filters.ChoiceFilter(label=_("Build Type"), choices=BUILD_TYPES)
+ type = django_filters.ChoiceFilter(label=_("Build Type"),
+ choices=BUILD_TYPES)
class Meta:
model = Build
@@ -3,14 +3,16 @@
from builds.models import VersionAlias, Version
from projects.models import Project
+
class AliasForm(forms.ModelForm):
class Meta:
model = VersionAlias
def __init__(self, instance=None, *args, **kwargs):
super(AliasForm, self).__init__(instance=instance, *args, **kwargs)
if instance:
- self.fields['project'].queryset = Project.objects.filter(pk=instance.project.pk)
+ self.fields['project'].queryset = (Project.objects
+ .filter(pk=instance.project.pk))
class VersionForm(forms.ModelForm):
Oops, something went wrong.

1 comment on commit d256625

@jezdez

This comment has been minimized.

Show comment Hide comment
@jezdez

jezdez Apr 8, 2013

Contributor

❤️ ❤️ ❤️

Contributor

jezdez commented on d256625 Apr 8, 2013

❤️ ❤️ ❤️

Please sign in to comment.