~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/models.py

  • Committer: Tarmac
  • Author(s): Brad Crittenden
  • Date: 2013-08-23 22:01:47 UTC
  • mfrom: (358.1.14 official-bundle-json)
  • Revision ID: tarmac-20130823220147-z0pg084iaro0afyh
[r=sinzui][bug=1215473][author=bac] Support official/promulgated bundles.  Change bundle to store basket_name and basket_revision separately rather than as on combined basket_id with name/rev.  Always use 'bundles' in the path for web and api requests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1270
1270
 
1271
1271
    _COMMON_REPRESENTATION = {
1272
1272
        'owner': '',
1273
 
        'basket': None,
 
1273
        'basket_name': None,
 
1274
        'basket_revision': None,
1274
1275
        'name': '',
1275
1276
        'title': '',
1276
1277
        'description': '',
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.
1308
1308
            return None
1309
1309
        return cls(cursor.next())
1319
1319
            raise AttributeError
1320
1320
 
1321
1321
    @staticmethod
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)
1326
1326
 
1327
1327
    @property
1328
 
    def basket_name(self):
1329
 
        if self.basket is None:
1330
 
            return None
1331
 
        return self.basket.split('/')[0]
1332
 
 
1333
 
    @property
1334
 
    def basket_rev(self):
1335
 
        try:
1336
 
            return self.basket.split('/')[1]
1337
 
        except (IndexError, AttributeError):
1338
 
            return None
1339
 
 
1340
 
    @property
1341
1328
    def id(self):
1342
 
        return self.construct_id(self.owner, self.basket, self.name)
1343
 
 
1344
 
    @classmethod
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)
1347
1331
 
1348
1332
    @property
1349
1333
    def search_id(self):
1350
1334
        """Return the (revisionless) ID used to index the bundle for searches.
1351
1335
        """
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)
1353
1338
 
1354
1339
    @property
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
1361
1346
        """
1362
 
        if not (self.owner and self.basket_name and self.name):
 
1347
        if not all([self.owner, self.basket_name, self.name]):
1363
1348
            return ''
1364
1349
        url = '~{owner}/bundles/{basket_name}/{name}'.format(
1365
1350
            owner=self.owner,
1520
1505
 
1521
1506
 
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)
1524
1510
    return {
1525
1511
        '_id': _id,
1526
1512
        'name': bundle_name,
1527
1513
        'owner': owner,
1528
 
        'basket': basket_id,
 
1514
        'basket_name': basket_name,
 
1515
        'basket_revision': int(basket_revision),
1529
1516
        'data': data,
1530
1517
    }
1531
1518