Example #1
0
def gen_subject_tags(org, n_subject_tags):
    subject_tags = []
    for tag in load_default_tags():
        if tag['type'] == 'subject':
            tag['org_id'] = org.id
            t = db.session.query(Tag)\
                .filter_by(org_id=org.id, name=tag['name'])\
                .first()
            if not t:
                t = Tag(**tag)
                db.session.add(t)
                db.session.commit()
            subject_tags.append(t)
    return subject_tags
Example #2
0
def gen_subject_tags(org, n_subject_tags):
    subject_tags = []
    for tag in load_default_tags():
        if tag['type'] == 'subject':
            tag['org_id'] = org.id
            t = db.session.query(Tag)\
                .filter_by(org_id=org.id, name=tag['name'])\
                .first()
            if not t:
                t = Tag(**tag)
                db.session.add(t)
                db.session.commit()
            subject_tags.append(t)
    return subject_tags
Example #3
0
def tags(org):
    """
    (Re)load all default tags.
    """
    # add default tags
    for tag in init.load_default_tags():
        tag['org_id'] = org.id
        t = Tag.query\
            .filter_by(org_id=tag['org_id'], slug=tag['slug'], type=tag['type'])\
            .first()
        if not t:
            log.info('Creating tag: "{slug}"'.format(**tag))
            t = Tag(**tag)
        else:
            log.warning('Updating tag: "{slug}"'.format(**tag))
            for k, v in tag.items():
                setattr(t, k, v)
        db.session.add(t)
    db.session.commit()
    return org
Example #4
0
def tags(org):
    """
    (Re)load all default tags.
    """
    # add default tags
    for tag in init.load_default_tags():
        tag['org_id'] = org.id
        t = Tag.query\
            .filter_by(org_id=tag['org_id'], slug=tag['slug'], type=tag['type'])\
            .first()
        if not t:
            log.info('Creating tag: "{slug}"'.format(**tag))
            t = Tag(**tag)
        else:
            log.warning('Updating tag: "{slug}"'.format(**tag))
            for k, v in tag.items():
                setattr(t, k, v)
        db.session.add(t)
    db.session.commit()
    return org
Example #5
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)
Example #6
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)