def create_new_project(request, project_name: str, project_description: str, is_public: bool): project = Project() if create_project_co_cous(project_uuid=project.uuid, project_name=project_name): project.created_by = request.user.email project.name = project_name project.description = project_description project.is_public = is_public project.save() # add pi_admin role for created_by user cou_name = str(project.uuid) + os.getenv('COU_FLAG_PI_ADMIN') ns_cou = ComanageCou.objects.filter(name=cou_name).first() if create_co_person_role(co_person_id=request.user.co_person_id, co_cou_id=ns_cou.co_cou_id): project.comanage_pi_admins.add(request.user) project.save() else: messages.error( request, 'ERROR! Unable to assign {0} to Project {1} ...'.format( os.getenv('COU_FLAG_PI_ADMIN'), project_name)) return str(project.uuid) else: messages.error( request, 'ERROR! Unable to create Project {0} ...'.format(project_name)) redirect('projects')
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')