Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
fix some bugs @zalun found
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy McKay committed Jun 29, 2012
1 parent 00104e0 commit 8ad1743
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
25 changes: 21 additions & 4 deletions mkt/api/resources.py
Expand Up @@ -23,7 +23,7 @@
from mkt.developers.forms import NewManifestForm
from mkt.developers.forms import PreviewForm
from mkt.api.forms import PreviewJSONForm
from mkt.webapps.models import Webapp
from mkt.webapps.models import Addon
from mkt.submit.forms import AppDetailsBasicForm

log = commonware.log.getLogger('z.api')
Expand Down Expand Up @@ -87,7 +87,7 @@ class AppResource(MarketplaceResource):
'previews', readonly=True)

class Meta:
queryset = Webapp.objects.all().no_transforms()
queryset = Addon.objects.filter(type=amo.ADDON_WEBAPP)
fields = ['id', 'name', 'description', 'device_types',
'homepage', 'privacy_policy',
'status', 'summary', 'support_email', 'support_url',
Expand All @@ -114,7 +114,7 @@ def obj_create(self, bundle, request, **kwargs):
plats = [Platform.objects.get(id=amo.PLATFORM_ALL.id)]

# Create app, user and fetch the icon.
bundle.obj = Webapp.from_upload(form.obj, plats)
bundle.obj = Addon.from_upload(form.obj, plats)
AddonUser(addon=bundle.obj, user=request.amo_user).save()
tasks.fetch_icon.delay(bundle.obj)
log.info('App created: %s' % bundle.obj.pk)
Expand Down Expand Up @@ -148,7 +148,7 @@ def obj_update(self, bundle, request, **kwargs):
data = bundle.data
try:
obj = self.get_object_list(bundle.request).get(**kwargs)
except Webapp.DoesNotExist:
except Addon.DoesNotExist:
raise ImmediateHttpResponse(response=http.HttpNotFound())

if not AppOwnerAuthorization().is_authorized(request, object=obj):
Expand Down Expand Up @@ -197,6 +197,23 @@ class Meta:
serializer = Serializer(formats=['json'])


class DeviceTypeResource(MarketplaceResource):

class Meta:
queryset = DeviceType.objects.all()
list_allowed_methods = ['get']
allowed_methods = ['get']
fields = ['name', 'id']
always_return_data = True
resource_name = 'devicetype'
serializer = Serializer(formats=['json'])

def dehydrate(self, bundle):
with no_translation():
bundle.data['name'] = str(bundle.obj.name).lower()
return bundle


class PreviewResource(MarketplaceResource):
image_url = fields.CharField(attribute='image_url', readonly=True)
thumbnail_url = fields.CharField(attribute='thumbnail_url', readonly=True)
Expand Down
25 changes: 25 additions & 0 deletions mkt/api/tests/test_handlers.py
Expand Up @@ -365,6 +365,31 @@ def test_get_other_category(self):
eq_(res.status_code, 404)


class TestDeviceTypeHandler(BaseOAuth):

def setUp(self):
super(TestDeviceTypeHandler, self).setUp()

self.dt = DeviceType.objects.create(name='Phone')
self.dt.name = {'fr': 'Le phone'}
self.dt.save()
self.list_url = ('api_dispatch_list', {'resource_name': 'devicetype'})
self.get_url = ('api_dispatch_detail',
{'resource_name': 'devicetype', 'pk': self.dt.pk})

self.client = OAuthClient(None)

def test_verbs(self):
self._allowed_verbs(self.list_url, ['get'])
self._allowed_verbs(self.get_url, ['get'])

def test_get_devicetypes(self):
res = self.client.get(self.list_url)
data = json.loads(res.content)
eq_(data['meta']['total_count'], 1)
eq_(data['objects'][0]['name'], 'phone')


@patch.object(settings, 'SITE_URL', 'http://api/')
class TestPreviewHandler(BaseOAuth, AMOPaths):
fixtures = ['base/users', 'base/user_2519', 'webapps/337141-steamcube']
Expand Down
2 changes: 2 additions & 0 deletions mkt/api/urls.py
Expand Up @@ -3,12 +3,14 @@
from tastypie.api import Api
from mkt.search.api import SearchResource
from mkt.api.resources import (AppResource, CategoryResource,
DeviceTypeResource,
PreviewResource, ValidationResource)

api = Api(api_name='apps')
api.register(ValidationResource())
api.register(AppResource())
api.register(CategoryResource())
api.register(DeviceTypeResource())
api.register(SearchResource())
api.register(PreviewResource())

Expand Down

0 comments on commit 8ad1743

Please sign in to comment.