コード例 #1
0
ファイル: random_data.py プロジェクト: jjelosua/newslynx-core
def gen_built_in_recipes(org):
    recipes = []
    metrics = []
    u = choice(org.users)
    # add default recipes
    for recipe in load_default_recipes():
        sous_chef_slug = recipe.pop('sous_chef')
        if not sous_chef_slug:
            raise RecipeSchemaError(
                'Default recipe "{}" is missing a "sous_chef" slug.'.format(
                    recipe.get('name', '')))
        sc = SousChef.query\
            .filter_by(slug=sous_chef_slug)\
            .first()
        if not sc:
            raise RecipeSchemaError(
                '"{}" is not a valid SousChef slug or the SousChef does not yet exist.'
                .format(sous_chef_slug))
        recipe = recipe_schema.validate(recipe, sc.to_dict())
        recipe['user_id'] = u.id
        recipe['org_id'] = org.id
        r = Recipe(sc, **recipe)
        db_session.add(r)
        db_session.commit()
        recipes.append(r)

        # if the recipe creates metrics add them in here.
        if 'metrics' in sc.creates:
            for name, params in sc.metrics.items():
                m = Metric(name=name, recipe_id=r.id, org_id=org.id, **params)
                metrics.append(m)
                db_session.add(m)
                db_session.commit()
    return recipes, metrics
コード例 #2
0
ファイル: random_data.py プロジェクト: newslynx/newslynx-core
def gen_built_in_recipes(org):
    recipes = []
    metrics = []
    u = choice(org.users)
    # add default recipes
    for recipe in load_default_recipes():
        sous_chef_slug = recipe.pop('sous_chef')
        if not sous_chef_slug:
            raise RecipeSchemaError(
                'Default recipe "{}" is missing a "sous_chef" slug.'.format(
                    recipe.get('name', '')))
        sc = SousChef.query\
            .filter_by(slug=sous_chef_slug)\
            .first()
        if not sc:
            raise RecipeSchemaError(
                '"{}" is not a valid SousChef slug or the SousChef does not yet exist.'
                .format(sous_chef_slug))
        recipe = recipe_schema.validate(recipe, sc.to_dict())
        recipe['user_id'] = u.id
        recipe['org_id'] = org.id

        r = Recipe.query\
            .filter_by(org_id=org.id, name=recipe['name'])\
            .first()

        if not r:
            # add to database here.
            r = Recipe(sc, **recipe)
        else:
            for name, value in recipe.items():
                if name != 'options':
                    setattr(r, name, value)
                else:
                    r.set_options(value)
        db.session.add(r)
        db.session.commit()
        recipes.append(r)

        # if the recipe creates metrics add them in here.
        if 'metrics' in sc.creates:
            for name, params in sc.metrics.items():
                m = Metric.query\
                    .filter_by(org_id=org.id, recipe_id=r.id, name=name, type=params['type'])\
                    .first()
                # print "METRICS PARAMS", params
                if not m:
                    m = Metric(name=name,
                               recipe_id=r.id,
                               org_id=org.id,
                               **params)

                else:
                    for k, v in params.items():
                        setattr(m, k, v)

                metrics.append(m)
                db.session.add(m)
                db.session.commit()
    return recipes, metrics
コード例 #3
0
ファイル: orgs_api.py プロジェクト: eads/newslynx-core
def org_create(user):

    req_data = request_data()

    if not user.super_user:
        raise ForbiddenError(
            'You must be the super user to create an Org')

    if 'name' not in req_data \
       or 'timezone' not in req_data:
        raise RequestError(
            "An Org requires a 'name' and 'timezone")

    org = Org.query\
        .filter_by(name=req_data['name'])\
        .first()

    # if the org doesnt exist, create it.
    if org:
        raise RequestError(
            "Org '{}' already exists"
            .format(req_data['name']))

    # add the requesting user to the org
    org = Org(
        name=req_data['name'],
        timezone=req_data['timezone']
    )
    org.users.append(user)
    db.session.add(org)
    db.session.commit()

    # add default tags
    for tag in load_default_tags():
        tag['org_id'] = org.id
        t = Tag(**tag)
        db.session.add(t)

    # add default recipes
    for recipe in load_default_recipes():

        # fetch it's sous chef.
        sous_chef_slug = recipe.pop('sous_chef')
        if not sous_chef_slug:
            raise RecipeSchemaError(
                "Default recipe '{}' is missing a 'sous_chef' slug."
                .format(recipe.get('slug', '')))

        sc = SousChef.query\
            .filter_by(slug=sous_chef_slug)\
            .first()

        if not sc:
            raise RecipeSchemaError(
                '"{}" is not a valid SousChef slug or the '
                'SousChef does not yet exist.'
                .format(sous_chef_slug))

        # validate the recipe
        recipe = recipe_schema.validate(recipe, sc.to_dict())

        # fill in relations
        recipe['user_id'] = user.id
        recipe['org_id'] = org.id

        # add to database
        r = Recipe(sc, **recipe)
        db.session.add(r)
        db.session.commit()

        # if the recipe creates metrics create them here.
        if 'metrics' in sc.creates:
            for name, params in sc.metrics.items():
                m = Metric(
                    name=name,
                    recipe_id=r.id,
                    org_id=org.id,
                    **params)
                db.session.add(m)
    db.session.commit()
    return jsonify(org)
コード例 #4
0
def gen_built_in_recipes(org):
    recipes = []
    metrics = []
    u = choice(org.users)
    # add default recipes
    for recipe in load_default_recipes():
        sous_chef_slug = recipe.pop('sous_chef')
        if not sous_chef_slug:
            raise RecipeSchemaError(
                'Default recipe "{}" is missing a "sous_chef" slug.'
                .format(recipe.get('name', '')))
        sc = SousChef.query\
            .filter_by(slug=sous_chef_slug)\
            .first()
        if not sc:
            raise RecipeSchemaError(
                '"{}" is not a valid SousChef slug or the SousChef does not yet exist.'
                .format(sous_chef_slug))
        recipe = recipe_schema.validate(recipe, sc.to_dict())
        recipe['user_id'] = u.id
        recipe['org_id'] = org.id

        r = Recipe.query\
            .filter_by(org_id=org.id, name=recipe['name'])\
            .first()

        if not r:
            # add to database here.
            r = Recipe(sc, **recipe)
        else:
            for name, value in recipe.items():
                if name != 'options':
                    setattr(r, name, value)
                else:
                    r.set_options(value)
        db.session.add(r)
        db.session.commit()
        recipes.append(r)

        # if the recipe creates metrics add them in here.
        if 'metrics' in sc.creates:
            for name, params in sc.metrics.items():
                m = Metric.query\
                    .filter_by(org_id=org.id, recipe_id=r.id, name=name, type=params['type'])\
                    .first()
                # print "METRICS PARAMS", params
                if not m:
                    m = Metric(
                        name=name,
                        recipe_id=r.id,
                        org_id=org.id,
                        **params)

                else:
                    for k, v in params.items():
                        setattr(m, k, v)

                metrics.append(m)
                db.session.add(m)
                db.session.commit()
    return recipes, metrics
コード例 #5
0
ファイル: default.py プロジェクト: abelsonlive/newslynx-core
def recipes(org):
    """
    (Re)load all default recipes.
    """
    # add default recipes
    for recipe in init.load_default_recipes():

        # fetch it's sous chef.
        sous_chef_slug = recipe.pop('sous_chef')
        if not sous_chef_slug:
            raise RecipeSchemaError(
                "Default recipe '{}' is missing a 'sous_chef' slug."
                .format(recipe.get('slug', '')))
        sc = SousChef.query\
            .filter_by(slug=sous_chef_slug, org_id=org.id)\
            .first()
        if not sc:
            raise RecipeSchemaError(
                '"{}" is not a valid SousChef slug or the '
                'SousChef does not yet exist for "{}"'
                .format(sous_chef_slug, org.slug))

        # validate the recipe
        recipe = recipe_schema.validate(recipe, sc.to_dict())

        # fill in relations
        recipe['user_id'] = org.super_user.id
        recipe['org_id'] = org.id

        r = Recipe.query\
            .filter_by(org_id=org.id, name=recipe['name'])\
            .first()

        if not r:
            log.info('Creating recipe: "{slug}"'.format(**recipe))
            # add to database here.
            r = Recipe(sc, **recipe)
        else:
            log.warning('Updating recipe: "{slug}"'.format(**recipe))
            for name, value in recipe.items():
                if name != 'options':
                    setattr(r, name, value)
                else:
                    r.set_options(value)

        db.session.add(r)
        db.session.commit()

        # if the recipe creates metrics create them here.
        if 'metrics' in sc.creates and r.status == 'stable':
            for name, params in sc.metrics.items():
                m = Metric.query\
                    .filter_by(org_id=org.id, recipe_id=r.id, name=name, type=params['type'])\
                    .first()
                # print "METRICS PARAMS", params
                if not m:
                    log.info('Creating metric: "{}"'.format(name))
                    m = Metric(
                        name=name,
                        recipe_id=r.id,
                        org_id=org.id,
                        **params)

                else:
                    log.warning('Updating metric: "{}"'.format(name))
                    for k, v in params.items():
                        setattr(m, k, v)

                db.session.add(m)
        db.session.commit()

    return org
コード例 #6
0
ファイル: default.py プロジェクト: newslynx/newslynx-core
def recipes(org):
    """
    (Re)load all default recipes.
    """
    # add default recipes
    for recipe in init.load_default_recipes():

        # fetch it's sous chef.
        sous_chef_slug = recipe.pop('sous_chef')
        if not sous_chef_slug:
            raise RecipeSchemaError(
                "Default recipe '{}' is missing a 'sous_chef' slug.".format(
                    recipe.get('slug', '')))
        sc = SousChef.query\
            .filter_by(slug=sous_chef_slug, org_id=org.id)\
            .first()
        if not sc:
            raise RecipeSchemaError(
                '"{}" is not a valid SousChef slug or the '
                'SousChef does not yet exist for "{}"'.format(
                    sous_chef_slug, org.slug))

        # validate the recipe
        recipe = recipe_schema.validate(recipe, sc.to_dict())

        # fill in relations
        recipe['user_id'] = org.super_user.id
        recipe['org_id'] = org.id

        r = Recipe.query\
            .filter_by(org_id=org.id, name=recipe['name'])\
            .first()

        if not r:
            log.info('Creating recipe: "{slug}"'.format(**recipe))
            # add to database here.
            r = Recipe(sc, **recipe)
        else:
            log.warning('Updating recipe: "{slug}"'.format(**recipe))
            for name, value in recipe.items():
                if name != 'options':
                    setattr(r, name, value)
                else:
                    r.set_options(value)

        db.session.add(r)
        db.session.commit()

        # if the recipe creates metrics create them here.
        if 'metrics' in sc.creates and r.status == 'stable':
            for name, params in sc.metrics.items():
                m = Metric.query\
                    .filter_by(org_id=org.id, recipe_id=r.id, name=name, type=params['type'])\
                    .first()
                # print "METRICS PARAMS", params
                if not m:
                    log.info('Creating metric: "{}"'.format(name))
                    m = Metric(name=name,
                               recipe_id=r.id,
                               org_id=org.id,
                               **params)

                else:
                    log.warning('Updating metric: "{}"'.format(name))
                    for k, v in params.items():
                        setattr(m, k, v)

                db.session.add(m)
        db.session.commit()

    return org
コード例 #7
0
def org_create(user):

    req_data = request_data()

    if not user.super_user:
        raise ForbiddenError('You must be the super user to create an Org')

    if 'name' not in req_data \
       or 'timezone' not in req_data:
        raise RequestError("An Org requires a 'name' and 'timezone")

    org = Org.query\
        .filter_by(name=req_data['name'])\
        .first()

    # if the org doesnt exist, create it.
    if org:
        raise RequestError("Org '{}' already exists".format(req_data['name']))

    # add the requesting user to the org
    org = Org(name=req_data['name'], timezone=req_data['timezone'])
    org.users.append(user)
    db.session.add(org)
    db.session.commit()

    # add default tags
    for tag in load_default_tags():
        tag['org_id'] = org.id
        t = Tag(**tag)
        db.session.add(t)

    # add default recipes
    for recipe in load_default_recipes():

        # fetch it's sous chef.
        sous_chef_slug = recipe.pop('sous_chef')
        if not sous_chef_slug:
            raise RecipeSchemaError(
                "Default recipe '{}' is missing a 'sous_chef' slug.".format(
                    recipe.get('slug', '')))

        sc = SousChef.query\
            .filter_by(slug=sous_chef_slug)\
            .first()

        if not sc:
            raise RecipeSchemaError(
                '"{}" is not a valid SousChef slug or the '
                'SousChef does not yet exist.'.format(sous_chef_slug))

        # validate the recipe
        recipe = recipe_schema.validate(recipe, sc.to_dict())

        # fill in relations
        recipe['user_id'] = user.id
        recipe['org_id'] = org.id

        # add to database
        r = Recipe(sc, **recipe)
        db.session.add(r)
        db.session.commit()

        # if the recipe creates metrics create them here.
        if 'metrics' in sc.creates:
            for name, params in sc.metrics.items():
                m = Metric(name=name, recipe_id=r.id, org_id=org.id, **params)
                db.session.add(m)
    db.session.commit()
    return jsonify(org)