Exemplo n.º 1
0
 def update(self, **kwargs):
     for property, value in kwargs.items():
         property_type = property_types.get(property, None)
         if property in boolean_properties:
             value = kwargs[property] in ('y', 'on', 'false', 'true', True)
         elif 'regex' in property:
             value = property in kwargs
         elif property_type == dict:
             value = loads(value) if value else {}
         elif property_type in [float, int]:
             value = property_type(value or 0)
         setattr(self, property, value)
Exemplo n.º 2
0
def save_service(cls_name):
    form = dict(request.form.to_dict())
    form['devices'] = [
        fetch(Device, id=id) for id in request.form.getlist('devices')
    ]
    form['pools'] = [
        fetch(Pool, id=id) for id in request.form.getlist('pools')
    ]
    for key in request.form:
        if property_types.get(key, None) == list:
            form[key] = request.form.getlist(key)
    for property in boolean_properties:
        if property not in form:
            form[property] = 'off'
    try:
        return jsonify(factory(service_classes[cls_name], **form).serialized)
    except JSONDecodeError:
        return jsonify('JSONDecodeError')
Exemplo n.º 3
0
 def update(self, **kwargs):
     serial = rel.get(self.__tablename__, rel['Service'])
     for property, value in kwargs.items():
         property_type = property_types.get(property, None)
         if property in serial:
             value = fetch(serial[property], id=value)
         elif property[:-1] in serial:
             value = objectify(serial[property[:-1]], value)
         elif property in boolean_properties:
             value = kwargs[property] not in (None, False)
         elif 'regex' in property:
             value = property in kwargs
         elif property_type == 'dict' and type(value) == str:
             value = loads(value) if value else {}
         elif property_type in ['float', 'int']:
             default_value = getattr(self.__table__.c, property).default
             if default_value and not value:
                 value = default_value.arg
             value = {'float': float, 'int': int}[property_type](value or 0)
         setattr(self, property, value)
Exemplo n.º 4
0
 def update(self, **kwargs: Any) -> None:
     serial = rel.get(self.__tablename__, rel["Service"])
     for property, value in kwargs.items():
         property_type = property_types.get(property, None)
         if property in serial:
             value = fetch(serial[property], id=value)
         elif property[:-1] in serial:
             value = objectify(serial[property[:-1]], value)
         elif property in boolean_properties:
             value = kwargs[property] not in (None, False)
         elif "regex" in property:
             value = property in kwargs
         elif property_type == "dict" and type(value) == str:
             value = loads(value) if value else {}
         elif property_type in ["float", "int"]:
             default_value = getattr(self.__table__.c, property).default
             if default_value and not value:
                 value = default_value.arg
             value = {"float": float, "int": int}[property_type](value or 0)
         setattr(self, property, value)
Exemplo n.º 5
0
def get_service(id_or_cls):
    service = fetch('Service', id=id_or_cls)
    cls = service_classes[service.type if service else id_or_cls]

    def build_text_box(property, name):
        return f'''
            <label>{name}</label>
            <div class="form-group">
              <input class="form-control" id="service-{property}"
              name="{property}" type="{'password'
              if property in private_properties else 'text'}">
            </div>'''

    def build_textarea_box(property, name):
        return f'''
            <label>{name}</label>
            <div class="form-group">
              <textarea style="height: 150px;" rows="30"
              class="form-control" id="service-{property}"
              name="{property}"></textarea>
            </div>'''

    def build_select_box(property, name):
        options = ''.join(f'<option value="{k}">{v}</option>'
                          for k, v in getattr(cls, f'{property}_values'))
        return f'''
            <label>{name}</label>
            <div class="form-group">
              <select class="form-control"
                id="service-{property}" name="{property}"
                {'multiple size="7"'
                if property_types[property] == 'list'
                else ''}>{options}
              </select>
            </div>'''

    def build_boolean_box(property, name):
        return '<fieldset>' + f'''
            <div class="item">
                <input id="service-{property}" name="{property}"
                type="checkbox"><label>{name}</label>
            </div>''' + '</fieldset>'

    form, list_properties, boolean_properties = '', [], []
    for property in cls_to_properties[cls.__tablename__]:
        name = pretty_names.get(property, property)
        if property in cls_to_properties['Service']:
            continue
        if property_types.get(property, None) == 'bool':
            form += build_boolean_box(property, name)
            boolean_properties.append(property)
        elif hasattr(cls, f'{property}_values'):
            form += build_select_box(property, name)
            if property_types[property] == 'list':
                list_properties.append(property)
        elif hasattr(cls, f'{property}_textarea'):
            form += build_textarea_box(property, name)
        else:
            form += build_text_box(property, name)
    return {
        'boolean_properties': ','.join(boolean_properties),
        'form': form,
        'list_properties': ','.join(list_properties),
        'service': service.serialized if service else None
    }
Exemplo n.º 6
0
def get_service(id_or_cls: str) -> dict:
    service = None
    try:
        service = fetch("Service", id=id_or_cls)
    except DataError:
        pass
    cls = service_classes[service.type if service else id_or_cls]

    def build_text_box(property: str, name: str) -> str:
        return f"""
            <label>{name}</label>
            <div class="form-group">
              <input
                class="form-control"
                id="service-{property}"
                maxlength="{getattr(cls, f'{property}_length', 524288)}"
                name="{property}"
                type="{'password' if property in private_properties else 'text'}"
              >
            </div>"""

    def build_textarea_box(property: str, name: str) -> str:
        return f"""
            <label>{name}</label>
            <div class="form-group">
              <textarea
                style="height: 150px;" rows="30"
                maxlength="{getattr(cls, f'{property}_length', 524288)}"
                class="form-control"
                id="service-{property}"
                name="{property}"
              ></textarea>
            </div>"""

    def build_select_box(property: str, name: str) -> str:
        options = "".join(f'<option value="{k}">{v}</option>'
                          for k, v in getattr(cls, f"{property}_values"))
        return f"""
            <label>{name}</label>
            <div class="form-group">
              <select
                class="form-control"
                id="service-{property}"
                name="{property}"
                {'multiple size="7"' if property_types[property] == 'list' else ''}
              >
              {options}
              </select>
            </div>"""

    def build_boolean_box(property: str, name: str) -> str:
        return ("<fieldset>" + f"""
            <div class="item">
              <input
                id="service-{property}"
                name="{property}"
                type="checkbox"
              >
              <label>{name}</label>
            </div>""" + "</fieldset>")

    form, list_properties, boolean_properties = "", [], []
    for property in cls_to_properties[cls.__tablename__]:
        name = pretty_names.get(property, property)
        if property in cls_to_properties["Service"]:
            continue
        if property_types.get(property, None) == "bool":
            form += build_boolean_box(property, name)
            boolean_properties.append(property)
        elif hasattr(cls, f"{property}_values"):
            form += build_select_box(property, name)
            if property_types[property] == "list":
                list_properties.append(property)
        elif hasattr(cls, f"{property}_textarea"):
            form += build_textarea_box(property, name)
        else:
            form += build_text_box(property, name)
    return {
        "boolean_properties": ",".join(boolean_properties),
        "form": form,
        "list_properties": ",".join(list_properties),
        "service": service.serialized if service else None,
    }