Exemplo n.º 1
0
    def list(self, request):

        ''' Return a list of categories. '''

        categories = Category.query().fetch()
        messages = []
        for category in categories:
            messages.append(category.to_message())
        return common_messages.Categories(categories=messages)
Exemplo n.º 2
0
    def test_category_delete_method(self):

        ''' Add a category and then delete it through the api. '''

        slug = 'test-slug'
        category_key = db_loader.create_category(slug=slug)
        params = {
            'key': encrypt(category_key.urlsafe()),
        }
        response = generic_service_method_success_test(self, 'category', 'delete', params=params)
        self.assertEqual(response['response']['type'], 'Echo',
            'Category put service method failed.')
        self.assertEqual(len(Category.query().fetch(1)), 0, 'Failed to delete category.')
Exemplo n.º 3
0
    def put(self, request):

        ''' Create or edit a category. '''

        if not request.key:
            # Create a new category.
            category = Category(key=ndb.Key('Category', request.slug))
        else:
            # Get the category to edit.
            category_key = ndb.Key(urlsafe=self.decrypt(request.key))
            category = category_key.get()

        if not category:
            # TODO: What to do on error?
            return common_messages.Category()

        # Update the category.
        category.mutate_from_message(request)
        category.put()

        return category.to_message()
Exemplo n.º 4
0
 def test_insert_entity(self):
     category_key = db_loader.create_category()
     self.assertTrue(category_key, "Failed to create an entity and return a key.")
     self.assertEqual(1, len(Category.query().fetch(2)), "Failed to retrieve a stored entity.")