def project_delete(request, uuid): """ Delete existing project and database table relationships :param request: :param uuid: :return: """ project = get_object_or_404(Project, uuid=uuid) affiliations = project.affiliation.all().order_by('name').distinct('name') comanage_staff = project.comanage_staff.all().order_by('display_name') comanage_pi_members = project.comanage_pi_members.all().order_by( 'display_name') comanage_pi_admins = project.comanage_pi_admins.all().order_by( 'display_name') igs = project.project_igs.all().order_by('display_name') datasets = project.datasets.all().order_by('name') workflows = project.workflows.all().order_by('affiliation', 'name') if request.method == "POST": # delete workflows for workflow in workflows: project.workflows.remove(workflow) workflow.delete() # remove datasets / infrastructure for dataset in datasets: project.datasets.remove(dataset) if project.infrastructure: project.infrastructure = None # remove personnel staff_cou = ComanageCou.objects.filter( name=str(project.uuid) + os.getenv('COU_FLAG_STAFF')).first() if staff_cou: for staff in comanage_staff: rm_role = remove_co_person_role( co_person_id=staff.co_person_id, co_cou_id=staff_cou.co_cou_id) if rm_role: project.comanage_staff.remove(staff) pi_member_cou = ComanageCou.objects.filter( name=str(project.uuid) + os.getenv('COU_FLAG_PI_MEMBER')).first() if pi_member_cou: for pi_member in comanage_pi_members: rm_role = remove_co_person_role( co_person_id=pi_member.co_person_id, co_cou_id=pi_member_cou.co_cou_id) if rm_role: project.comanage_staff.remove(pi_member) pi_admin_cou = ComanageCou.objects.filter( name=str(project.uuid) + os.getenv('COU_FLAG_PI_ADMIN')).first() if pi_admin_cou: for pi_admin in comanage_pi_admins: rm_role = remove_co_person_role( co_person_id=pi_admin.co_person_id, co_cou_id=pi_admin_cou.co_cou_id) if rm_role: project.comanage_staff.remove(pi_admin) # remove IGs for ig in igs: project.project_igs.remove(ig) # remove affiliations for aff in affiliations: project.affiliation.remove(aff) # delete project if remove_co_cou(staff_cou) and remove_co_cou( pi_member_cou) and remove_co_cou(pi_admin_cou): messages.success( request, '[INFO] Project "{0}" has been deleted'.format(project.name)) project.delete() else: messages.error( request, '[ERROR] Failed to delete Project "{0}" ...'.format( project.name)) return redirect('projects') return render( request, 'project_delete.html', { 'projects_page': 'active', 'project': project, 'affiliations': affiliations, 'project_pi_admins': comanage_pi_admins, 'project_pi_members': comanage_pi_members, 'project_staff': comanage_staff, 'datasets': datasets, 'workflows': workflows, })
def project_update_admin(request, uuid): project = get_object_or_404(Project, uuid=uuid) pi_admins = project.comanage_pi_admins.all() pi_members = project.comanage_pi_members.all() comanage_pi_admins_orig = list(project.comanage_pi_admins.all()) if request.method == "POST": form = ProjectUpdateAdminForm(request.POST, instance=project) if form.is_valid(): comanage_pi_admins = list( form.cleaned_data.get('comanage_pi_admins')) comanage_pi_admins_added = list( set(comanage_pi_admins).difference( set(comanage_pi_admins_orig))) comanage_pi_admins_removed = list( set(comanage_pi_admins_orig).difference( set(comanage_pi_admins))) co_cou = ComanageCou.objects.filter( name=str(uuid) + os.getenv('COU_FLAG_PI_ADMIN')).first() # update staff for pi_admin in comanage_pi_admins_added: # create co_person role and add ns_role if create_co_person_role(co_person_id=pi_admin.co_person_id, co_cou_id=co_cou.co_cou_id): # add user to comanage_staff project.comanage_pi_admins.add(pi_admin) role_added_to_project(request=request, user=pi_admin, project=project, role='Project Manager') messages.success( request, '[INFO] "{0}" added as MANAGER to project "{1}"'. format(pi_admin.display_name, project.name)) for pi_admin in comanage_pi_admins_removed: # remove co_person role and add ns_role if remove_co_person_role(co_person_id=pi_admin.co_person_id, co_cou_id=co_cou.co_cou_id): # remove user from comanage_staff project.comanage_pi_admins.remove(pi_admin) role_removed_from_project(request=request, user=pi_admin, project=project, role='Project Manager') messages.success( request, '[INFO] "{0}" removed as MANAGER from project "{1}:'. format(pi_admin.display_name, project.name)) project.is_valid = False project.save() return redirect('project_detail', uuid=uuid) else: form = ProjectUpdateAdminForm(instance=project) return render( request, 'project_update_admin.html', { 'form': form, 'projects_page': 'active', 'project': project, 'project_pi_admins': pi_admins, 'project_pi_members': pi_members })
def nsadmin(request): if request.user.is_nsadmin(): if request.method == 'POST': for key in request.POST.keys(): if not key == 'csrfmiddlewaretoken': cur_value = request.POST.get(key) parse_key = key.rsplit('_', 1) ns_role = parse_key[0] ns_user = NotaryServiceUser.objects.filter( id=int(parse_key[1])).first() ns_cou = ComanageCou.objects.filter(name=ns_role).first() if str(cur_value) == 'True': blocked_cou_ids = [ str(os.getenv('COU_ID_ACTIVE_USER')), str(os.getenv('COU_ID_NOTARY_SERVICE_ADMINS')) ] if ns_user == request.user and str( ns_cou.co_cou_id) in blocked_cou_ids: messages.error( request, '[INFO] Cannot reemove role "{0}" from "self"'. format(ns_role)) else: remove_co_person_role( co_person_id=ns_user.co_person_id, co_cou_id=ns_cou.co_cou_id) messages.info( request, '[INFO] Remove role "{0}" from user "{1}"'. format(ns_role, ns_user.display_name)) if str(ns_cou.co_cou_id) == str( os.getenv('COU_ID_NOTARY_SERVICE_ADMINS')): remove_co_person_role( co_person_id=ns_user.co_person_id, co_cou_id=int( os.getenv( 'COU_ID_ENROLLMENT_APPROVAL'))) messages.info( request, '[INFO] Remove role "{0}" to user "{1}"'. format( os.getenv('ROLE_ENROLLMENT_APPROVAL'), ns_user.display_name)) send_nsmessage( users_to=[ns_user], users_from=[request.user], subject='[ROLE] "{0}" has been removed'.format( ns_role), body=""" Role "{0}" has been removed See /profile for your current Notary Service roles """.format(ns_role)) else: create_co_person_role( co_person_id=ns_user.co_person_id, co_cou_id=ns_cou.co_cou_id) messages.info( request, '[INFO] Add role "{0}" to user "{1}"'.format( ns_role, ns_user.display_name)) # add EnrollmentApproval if request is for NotaryServiceAdmin if str(ns_cou.co_cou_id) == str( os.getenv('COU_ID_NOTARY_SERVICE_ADMINS')): create_co_person_role( co_person_id=ns_user.co_person_id, co_cou_id=int( os.getenv('COU_ID_ENROLLMENT_APPROVAL'))) messages.info( request, '[INFO] Add role "{0}" to user "{1}"'.format( os.getenv('ROLE_ENROLLMENT_APPROVAL'), ns_user.display_name)) send_nsmessage( users_to=[ns_user], users_from=[request.user], subject='[ROLE] "{0}" has been added'.format( ns_role), body=""" Role "{0}" has been added See /profile for your current Notary Service roles """.format(ns_role)) ns_people = NotaryServiceUser.objects.all().order_by('display_name') nsmessages = NsMessages.objects.all().order_by('-created')[:25] ns_messages = [] for m in nsmessages: users_to = m.users_to.all().values_list('display_name', flat=True) users_from = m.users_from.all().values_list('display_name', flat=True) ns_messages.append({ 'uuid': m.uuid, 'subject': m.subject, 'created': m.created, 'is_read': m.is_read, 'users_to': users_to, 'users_from': users_from }) return render( request, 'nsadmin.html', { 'people': ns_people, 'nsmessages': ns_messages, 'nsadmin_page': 'active' }) else: return render(request, 'index.html')