Ejemplo n.º 1
0
def register_on_market(name, host, site):

    # Check that the market name is not in use
    existing = True

    if host[-1] != '/':
        host += '/'

    try:
        Marketplace.objects.get(name=name)
        Marketplace.objects.get(host=host)
    except:
        existing = False

    if existing:
        raise Exception('Marketplace already registered')

    store_name = settings.STORE_NAME

    marketadaptor = MarketAdaptor(host)

    store_info = {
        'store_name': store_name,
        'store_uri': site,
    }

    try:
        marketadaptor.add_store(store_info)
    except HTTPError:
        raise Exception('Bad Gateway')

    try:
        Marketplace.objects.create(name=name, host=host)
    except Exception, e:
        # If the marketplace model creation fails it is necesary to unregister the store
        # in order to avoid an inconsistent state
        marketadaptor.delete_store(store_name)
        raise e