def inventory_roles_add_view(request, inventory_id): try: if request.method == 'POST': data = request.POST target = data.get('target') role_id = data.get('role_id') if target == 'user': user_id_list = data.getlist('user_id') for user_id in user_id_list: status, msg = UserAndTeamRoleAPI.add_role_to_user( request.user, int(user_id), int(role_id) ) if not status: return HttpResponse(msg) return HttpResponseRedirect( reverse( 'inventory:inventory_roles', kwargs={'inventory_id': inventory_id} ) ) elif target == 'team': team_id_list = data.getlist('team_id') for team_id in team_id_list: status, msg = UserAndTeamRoleAPI.add_role_to_team( request.user, int(team_id), int(role_id) ) if not status: return HttpResponse(msg) return HttpResponseRedirect( reverse( 'inventory:inventory_roles', kwargs={'inventory_id': inventory_id} ) ) else: return HttpResponse('404') except Exception as e: return HttpResponse(str(e))
def project_roles_add_view(request, project_id): try: if request.method == 'POST': data = request.POST target = data.get('target') role_id = data.get('role_id') if target == 'user': user_id_list = data.getlist('user_id') for user_id in user_id_list: status, errmsg = UserAndTeamRoleAPI.add_role_to_user( request.user, int(user_id), int(role_id)) if not status: return render(request, 'error.html', {ARK_ERRMSG: errmsg}) return HttpResponseRedirect( reverse('project:project_roles', kwargs={'project_id': project_id})) elif target == 'team': team_id_list = data.getlist('team_id') for team_id in team_id_list: status, errmsg = UserAndTeamRoleAPI.add_role_to_team( request.user, int(team_id), int(role_id)) if not status: return render(request, 'error.html', {ARK_ERRMSG: errmsg}) return HttpResponseRedirect( reverse('project:project_roles', kwargs={'project_id': project_id})) else: return HttpResponse('404') except Exception as e: return render(request, 'error.html', {ARK_ERRMSG: str(e)})
def team_info_role_add_view(request, team_id): user = request.user context = dict() try: if request.method == 'GET': status, errmsg, team = TeamAPI.get(user=user, team_id=team_id) if not status: context = {'errmsg': errmsg} return render(request, 'error.html', context) # 项目 status, errmsg, projects = ProjectAPI.all(user) if not status: context = {'errmsg': errmsg} return render(request, 'error.html', context) projects = projects.filter( organization=team.organization).distinct() # 仓库 status, errmsg, inventories = InventoryAPI.all(user) if not status: context = {'errmsg': errmsg} return render(request, 'error.html', context) inventories = inventories.filter(organization=team.organization).\ distinct() # 模板 status, errmsg, templates = TemplateAPI.all(user) if not status: context = {'errmsg': errmsg} return render(request, 'error.html', context) templates = templates.filter(project__organization=team.organization).\ distinct() context = { 'app': app, 'projects': projects, 'inventories': inventories, 'templates': templates, 'team': team } return render(request, 'organization/team_info_role_add.html', context) if request.is_ajax(): if request.method == 'POST': ajax_data = request.POST.get('data') data = json.loads(ajax_data) project_ids = data.get('project_ids') project_role = data.get('project_role') for project_id in project_ids: role = InternalAPI.get_role(resource_type=RS_PROJECT, resource_name=project_role, resource_id=int(project_id)) status, errmsg = UserAndTeamRoleAPI.add_role_to_team( user, team_id, role.id) if not status: return JsonResponse({ ARK_STATUS: False, ARK_ERRMSG: errmsg }) inventory_ids = data.get('inventory_ids') inventory_role = data.get('inventory_role') for inventory_id in inventory_ids: role = InternalAPI.get_role(resource_type=RS_INVENTORY, resource_name=inventory_role, resource_id=int(inventory_id)) status, errmsg = UserAndTeamRoleAPI.add_role_to_team( user, team_id, role.id) if not status: return JsonResponse({ ARK_STATUS: False, ARK_ERRMSG: errmsg }) template_ids = data.get('template_ids') template_role = data.get('template_role') for template_id in template_ids: role = InternalAPI.get_role(resource_type=RS_TEMPLATE, resource_name=template_role, resource_id=int(template_id)) status, errmsg = UserAndTeamRoleAPI.add_role_to_team( user, team_id, role.id) if not status: return JsonResponse({ ARK_STATUS: False, ARK_ERRMSG: errmsg }) return JsonResponse({ARK_STATUS: True}) except Exception as e: context['errmsg'] = str(e) return render(request, 'error.html', context)