def patch_action_type(request, act_id): action_type = get_object_or_404(ActionType, id=int(act_id)) name = request.data.get("name") entity_status = request.data.get("entity_status") description = request.data.get("description") label = request.data.get("label") format_data = request.data.get("format") action_controller = ActionController(action_type) result = {'id': action_type.id} if entity_status is not None and action_type.entity_status != entity_status: action_type.set_status(entity_status) result['entity_status'] = entity_status action_type.update_field('entity_status') if name is not None: action_type.name = name result['name'] = name action_type.update_field('name') if description is not None: action_type.description = description result['description'] = description action_type.update_field('description') if label is not None: lang = translation.get_language() action_type.set_label(lang, label) result['label'] = label action_type.update_field('label') if format_data is not None: if 'steps' not in format_data: raise SuspiciousOperation(_("Invalid format")) action_type.format['steps'] = [] for step in format_data['steps']: # step format validation ActionStepFormatManager.check(action_controller, step) action_type.format['steps'].append(step) result['format'] = action_type.format action_type.update_field('format') action_type.save() return HttpResponseRest(request, result)
def create_action_type(request): """ Create a new action type """ # check name uniqueness if ActionType.objects.filter(name=request.data['name']).exists(): raise SuspiciousOperation( _('An action with a similar name already exists')) description = request.data.get("description") label = request.data.get("label") format_data = request.data.get("format", {'type': 'undefined'}) lang = translation.get_language() # create the action type action_type = ActionType() action_type.name = request.data['name'] action_type.set_label(lang, label) action_type.description = description action_type.format = format_data action_controller = ActionController(action_type, request.user) if format_data['type'] != 'undefined': # format validation ActionStepFormatManager.check(action_controller, format_data) action_type.save() result = { 'id': action_type.id, 'name': action_type.name, 'label': action_type.get_label(), 'format': action_type.format, 'description': action_type.description } return HttpResponseRest(request, result)