1300
1301
'only one of "db" or "collection" can be provided')
1301
1302
if collection is None:
1302
1303
collection = db.bundles
1303
cursor = collection.find(query, limit=2)
1304
if cursor.count() > 1:
1305
raise ValueError('more than one matching bundle')
1306
elif cursor.count() == 0:
1304
cursor = collection.find(query).sort(
1305
'basket_revision', pymongo.DESCENDING).limit(1)
1306
if cursor.count() == 0:
1307
1307
# No bundles match the query.
1309
1309
return cls(cursor.next())
1319
1319
raise AttributeError
1322
def construct_id(owner, basket, name, use_revision=True):
1323
if not use_revision:
1324
basket = basket.split('/')[0]
1322
def construct_id(owner, basket, name, revision=None):
1323
if revision is not None:
1324
basket = '/'.join([basket, str(revision)])
1325
1325
return "~%s/%s/%s" % (owner, basket, name)
1328
def basket_name(self):
1329
if self.basket is None:
1331
return self.basket.split('/')[0]
1334
def basket_rev(self):
1336
return self.basket.split('/')[1]
1337
except (IndexError, AttributeError):
1342
return self.construct_id(self.owner, self.basket, self.name)
1345
def construct_search_id(cls, owner, basket_id, name):
1346
return cls.construct_id(owner, basket_id, name, use_revision=False)
1329
return self.construct_id(
1330
self.owner, self.basket_name, self.name, self.basket_revision)
1349
1333
def search_id(self):
1350
1334
"""Return the (revisionless) ID used to index the bundle for searches.
1352
return self.construct_search_id(self.owner, self.basket, self.name)
1336
return self.construct_id(
1337
self.owner, self.basket_name, self.name)
1355
1340
def short_url(self):
1359
1344
~sinzui/bundles/mysql/tiny to support a full URL of
1360
1345
http://manage.jujucharms.com/~sinzui/bundles/mysql/tiny
1362
if not (self.owner and self.basket_name and self.name):
1347
if not all([self.owner, self.basket_name, self.name]):
1364
1349
url = '~{owner}/bundles/{basket_name}/{name}'.format(
1365
1350
owner=self.owner,
1522
1507
def make_bundle_doc(data, owner, basket_id, bundle_name):
1523
_id = Bundle.construct_id(owner, basket_id, bundle_name)
1508
basket_name, basket_revision = basket_id.split('/')
1509
_id = Bundle.construct_id(owner, basket_name, bundle_name, basket_revision)
1526
1512
'name': bundle_name,
1527
1513
'owner': owner,
1528
'basket': basket_id,
1514
'basket_name': basket_name,
1515
'basket_revision': int(basket_revision),