Add package_count to Category objects

Partially implements blueprint enable-category-management
Change-Id: I8639586d6dad1b398d54ee6f4ac5755cb38ed8b0
This commit is contained in:
Kirill Zaitsev 2015-04-04 01:57:12 +03:00
parent 7a08d3b81f
commit 5542f47b61
5 changed files with 28 additions and 8 deletions

View File

@ -441,13 +441,15 @@ List categories
"id": "0420045dce7445fabae7e5e61fff9e2f",
"updated": "2014-12-26T13:57:04",
"name": "Web",
"created": "2014-12-26T13:57:04"
"created": "2014-12-26T13:57:04",
"package_count": 1
},
{
"id": "3dd486b1e26f40ac8f35416b63f52042",
"updated": "2014-12-26T13:57:04",
"name": "Databases",
"created": "2014-12-26T13:57:04"
"created": "2014-12-26T13:57:04",
"package_count": 0
}]
}
@ -490,7 +492,8 @@ Get category details
}
],
"name": "Web",
"created": "2015-01-28T17:00:19"
"created": "2015-01-28T17:00:19",
"package_count": 1
}
+----------------+-----------------------------------------------------------+
@ -541,6 +544,7 @@ Add new category
"name": "category_name",
"created": "2013-11-30T03:23:42Z",
"updated": "2013-11-30T03:23:44Z",
"package_count": 0
}

View File

@ -265,7 +265,6 @@ class Controller(object):
def delete(self, req, package_id):
target = {'package_id': package_id}
policy.check("delete_package", req.context, target)
db_api.package_delete(package_id, req.context)
def get_category(self, req, category_id):

View File

@ -416,6 +416,9 @@ def category_add(category_name):
with session.begin():
category.update({'name': category_name})
# NOTE(kzaitsev) update package_count, so we can safely access from
# outside the session
category.package_count = 0
category.save(session)
return category

View File

@ -278,6 +278,17 @@ class Category(Base, TimestampMixin):
default=uuidutils.generate_uuid)
name = sa.Column(sa.String(80), nullable=False, index=True, unique=True)
package_count = sa_orm.column_property(
sa.select([sa.func.count(package_to_category.c.package_id)]).
where(package_to_category.c.category_id == id).
correlate_except(package_to_category)
)
def to_dict(self):
d = super(Category, self).to_dict()
d['package_count'] = self.package_count
return d
class Tag(Base, TimestampMixin):
"""Represents tags in the datastore."""

View File

@ -148,9 +148,12 @@ Content-Type: application/json
fake_now = timeutils.utcnow()
timeutils.utcnow.override_time = fake_now
expected = {'name': 'new_category',
expected = {
'name': 'new_category',
'created': timeutils.isotime(fake_now)[:-1],
'updated': timeutils.isotime(fake_now)[:-1]}
'updated': timeutils.isotime(fake_now)[:-1],
'package_count': 0,
}
body = {'name': 'new_category'}
req = self._post('/catalog/categories', json.dumps(body))