Beispiel #1
0
def add_template(template, **kwargs):
    """
        Add template and a type and typeattrs.
    """
    tmpl = Template()
    tmpl.name = template.name
    tmpl.created_by = kwargs.get('user_id')
    if template.parent_id:
        tmpl.parent_id = template.parent_id
    if template.description:
        tmpl.description = template.description
    if template.layout:
        tmpl.layout = get_json_as_string(template.layout)
    if template.project_id:
        tmpl.project_id = template.project_id

    db.DBSession.add(tmpl)

    if template.templatetypes is not None:
        types = template.templatetypes
        for templatetype in types:
            ttype = _update_templatetype(templatetype, **kwargs)
            tmpl.templatetypes.append(ttype)

    db.DBSession.flush()

    log.info("[Added template]\n{}".format(template))

    return tmpl
Beispiel #2
0
def _set_cols(source, target, reference=None):
    """
        Set the value on the column of a target row.
        This checks if the value is the same as that of another reference row,
        and if it is the same, it sets that column to None.
        Ex:
            reference is: {'status': 'x'} and value is 'x'}, then
            target will be set to {'status': None}. If status is 'y',
            then target will be {'status', 'y'}
        Args:
            target: DB row to write the column to
            source: The incoming object (a JSONObject) containing the request data
            reference: DB row used for comparison
            colnames: The column names to set
            value: The value to set.
    """
    for colname in source:

        if colname not in target.__table__.columns:
            continue

        if colname in ['cr_date', 'updated_at']:
            continue

        if hasattr(reference, '_protected_columns')\
           and colname in reference._protected_columns:
            #as a child, you can't change stuff like IDS, cr dates etc.
            continue

        if target.parent_id is not None\
           and hasattr(reference, '_hierarchy_columns')\
           and colname in reference._hierarchy_columns:
            #as a child, you can't change stuff like IDS, cr dates etc.
            continue

        newval = getattr(source, colname)

        if colname == 'layout':
            newval = get_json_as_string(newval)

        if reference is None:
            setattr(target, colname, newval)
            continue

        refval = getattr(reference, colname)

        if newval != refval:
            setattr(target, colname, newval)
Beispiel #3
0
def update_template(template, auto_delete=False, **kwargs):
    """
        Update template and a type and typeattrs.
        args:
            template (JSONObject): The template to update
            auto_delete (bool): A flag to indicated whether missing types from `template.templatetypes`
                                should be deleted automatically. This flag is also
                                used when updating the typeattrs of type. Defaults to False.
    """
    tmpl = db.DBSession.query(Template).filter(
        Template.id == template.id).one()
    tmpl.name = template.name

    if template.status is not None:
        tmpl.status = template.status

    if template.description:
        tmpl.description = template.description

    template_types = tmpl.get_types()

    if template.layout:
        tmpl.layout = get_json_as_string(template.layout)

    type_dict = dict([(t.id, t) for t in template_types])

    #a list of all the templatetypes in the incoming template object
    req_templatetype_ids = []

    if template.types is not None or template.templatetypes is not None:
        types = template.types if template.types is not None else template.templatetypes
        for templatetype in types:

            if templatetype.id is not None and templatetype.template_id != tmpl.id:
                log.debug("Type %s is a part of a parent template. Ignoring.",
                          templatetype.id)
                req_templatetype_ids.append(type_i.id)
                continue

            if templatetype.id is not None:
                type_i = type_dict[templatetype.id]
                _update_templatetype(templatetype,
                                     auto_delete=auto_delete,
                                     **kwargs)
                req_templatetype_ids.append(type_i.id)
            else:
                #Give it a template ID if it doesn't have one
                templatetype.template_id = template.id
                new_templatetype_i = _update_templatetype(
                    templatetype, auto_delete=auto_delete, **kwargs)
                req_templatetype_ids.append(new_templatetype_i.id)

    if auto_delete is True:
        for ttype in template_types:
            if ttype.id not in req_templatetype_ids:
                delete_templatetype(ttype.id, **kwargs)

    db.DBSession.flush()

    updated_templatetypes = tmpl.get_types()

    tmpl_j = JSONObject(tmpl)

    tmpl_j.templatetypes = updated_templatetypes

    #db.DBSession.expunge(tmpl)

    return tmpl_j