PresetsController.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:cerberus-core 作者: ovh 项目源码 文件源码
def create(body):
    """
        Create a new preset
    """
    try:
        body['codename'] = body['name'].strip().lower().replace(' ', '_')
        existing = TicketWorkflowPreset.objects.filter(
            codename=body['codename'],
            name=body['name']
        ).count()
        if existing:
            transaction.rollback()
            raise BadRequest('Preset with same codename/name exists')
        preset = TicketWorkflowPreset.objects.create(codename=body['codename'], name=body['name'])
    except (AttributeError, FieldError, ValueError) as ex:
        raise BadRequest(ex)

    try:
        preset.config = __get_preset_config(body)
    except (AttributeError, KeyError, ObjectDoesNotExist, TypeError, ValueError):
        raise BadRequest('Invalid or missing params in action')

    if body.get('templates') is not None:
        for template_id in body['templates']:
            try:
                template = MailTemplate.objects.get(id=template_id)
                preset.templates.add(template)
            except (AttributeError, KeyError, ObjectDoesNotExist, ValueError):
                raise BadRequest('Invalid template id')

    preset.roles.clear()
    for role_codename in body['roles']:
        try:
            role = Role.objects.get(codename=role_codename)
            preset.roles.add(role)
        except (AttributeError, KeyError, ObjectDoesNotExist, ValueError):
            raise BadRequest('Invalid role codename')

    preset.save()
    return {'message': 'Preset successfully updated'}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号