コード例 #1
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
コード例 #2
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
コード例 #3
0
 def valid_email(self, key, opt):
     """
     Validate a email field.
     """
     if mail.validate(opt):
         return opt
     return RecipeSchemaError(
         "{} can be an 'email' field but was passed '{}'.".format(key, opt))
コード例 #4
0
 def valid_url(self, key, opt):
     """
     Validate a url field.
     """
     if url.validate(opt):
         return opt
     return RecipeSchemaError(
         "{} should be a 'url' field but was passed '{}'.".format(key, opt))
コード例 #5
0
 def _raise_recipe_schema_error(self, message):
     """
     A helper for raising consistent error messages.
     """
     r = self.recipe.get('slug', self.recipe.get('name', ''))
     preface = "There were problems validating Recipe '{}' "\
               "which is associated with SousChef '{}' -- "\
               .format(r, self.sous_chef)
     msg = preface + message
     raise RecipeSchemaError(msg)
コード例 #6
0
 def valid_regex(self, key, opt):
     """
     Validate a email field.
     """
     try:
         return re.compile(opt)
     except:
         return RecipeSchemaError(
             "{} should be a 'regex' field but was passed '{}'."
             .format(key, opt))
コード例 #7
0
 def valid_string(self, key, opt):
     """
     Validate a string field.
     """
     try:
         return unicode(opt)
     except:
         return RecipeSchemaError(
             "{} should be a 'string' field but was passed '{}'."
             .format(key, opt))
コード例 #8
0
 def valid_numeric(self, key, opt):
     """
     Validate a numeric option.
     """
     try:
         return parse_number(opt)
     except:
         return RecipeSchemaError(
             "{} is an 'numeric' field but was passed '{}'."
             .format(key, opt))
コード例 #9
0
 def valid_datetime(self, key, opt):
     """
     Validate a iso-datetime option.
     """
     v_opt = dates.parse_iso(opt)
     if not v_opt:
         return RecipeSchemaError(
             "{} should be a 'datetime' field but was passed '{}'."
             .format(key, opt))
     return v_opt
コード例 #10
0
 def valid_searchstring(self, key, opt):
     """
     Validate a searchstring option.
     """
     try:
         return SearchString(opt)
     except SearchStringError as e:
         return RecipeSchemaError(
             "{} should be a 'searchstring' field but was passed '{}'. "
             "Here is the specific error: {}.".format(key, opt, e.message))
コード例 #11
0
 def valid_json(self, key, opt):
     """
     Validate a iso-datetime option.
     """
     try:
         obj_to_json(opt)
     except:
         return RecipeSchemaError(
             "{} should be a 'json' field but was passed '{}'."
             .format(key, opt))
     return opt
コード例 #12
0
 def valid_crontab(self, key, opt):
     """
     Validate a crontab option.
     """
     if opt is None:
         return None
     try:
         dates.cron(opt)
     except Exception as e:
         return RecipeSchemaError(
             "{} should be a 'crontab' field but was passed '{}'. "
             "Here is the error message: {}.".format(key, opt, e.message))
     return opt
コード例 #13
0
 def valid_boolean(self, key, opt):
     """
     Validate a boolean option.
     """
     try:
         opt = str(opt)
         if opt.lower() in TRUE_VALUES:
             return True
         if opt.lower() in FALSE_VALUES:
             return False
     except:
         return RecipeSchemaError(
             "{} is an 'boolean' field but was passed '{}'."
             .format(key, opt))
コード例 #14
0
    def valid_nulltype(self, key, opt):
        """

        """
        if opt is None:
            return None
        else:
            try:
                if opt.lower() in NULL_VALUES:
                    return None
            except:
                pass
        return RecipeSchemaError(
            "{} should be a 'nulltype' field but was passed '{}'."
            .format(key, opt))
コード例 #15
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
コード例 #16
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)