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'}
评论列表
文章目录