def can_view(request, artifact): all_versions = list(artifact.versions) if artifact.sharing_key and (request.GET.get(SHARING_KEY_PARAM) == artifact.sharing_key): return all_versions if request.user.is_authenticated(): if request.user.is_staff: return all_versions if artifact.created_by == request.user: return all_versions project_shares = ShareTarget.objects.filter(artifact=artifact, project__isnull=False) # Avoid the membership lookup if there are no sharing rules in place if project_shares: mapper = ProjectAllocationMapper(request) user_projects = [ p['chargeCode'] for p in mapper.get_user_projects(request.user.username, fetch_balance=False) ] if any(p.charge_code in user_projects for p in project_shares): return all_versions # NOTE(jason): It is important that this check go last. Visibility b/c # the artifact has a DOI is the weakest form of visibility; not all # versions are necessarily visible in this case. We return a list of # the versions that are allowed. We check for stronger visibilty rules # first, as they should ensure all versions are seen. if artifact.doi: return [v for v in all_versions if v.doi] return []
def dashboard(request): context = {} # active projects... mapper = ProjectAllocationMapper(request) active_projects = mapper.get_user_projects( request.user.username, alloc_status=["Active", "Approved", "Pending"], to_pytas_model=True, ) context["active_projects"] = active_projects context["show_migration_info"] = request.session.get( "has_legacy_account", False) # open tickets... rt = rtUtil.DjangoRt() context["open_tickets"] = rt.getUserTickets(request.user.email) # ongoing outages... outages = [ o for o in Outage.objects.order_by("-end_date", "-start_date") if not o.resolved ] # silly ORM quirk context["outages"] = outages webinars = Webinar.objects.filter(end_date__gte=timezone.now()) context["webinars"] = webinars return render(request, "dashboard.html", context)
def sync_project_memberships(request, username): """Re-sync a user's Keystone project memberships. This calls utils.auth.keystone_auth.sync_projects under the hood, which will dynamically create missing projects as well. Args: request (Request): the parent request; used for region detection. username (str): the username to sync memberships for. Return: List[keystone.Project]: a list of Keystone projects the user is a member of. """ mapper = ProjectAllocationMapper(request) try: ks_admin = admin_ks_client(request=request) ks_user = get_user(ks_admin, username) if not ks_user: logger.error(( "Could not fetch Keystone user for {}, skipping membership syncing" .format(username))) return active_projects = mapper.get_user_projects(username, alloc_status=["Active"], to_pytas_model=True) return sync_projects(ks_admin, ks_user, active_projects) except Exception as e: logger.error("Could not sync project memberships for %s: %s", username, e) return []
def has_active_allocations(request): mapper = ProjectAllocationMapper(request) user_projects = mapper.get_user_projects(request.user.username, to_pytas_model=False) for project in user_projects: for allocation in project["allocations"]: if allocation["status"].lower() == "active": return True return False
def user_projects(request): context = {} username = request.user.username mapper = ProjectAllocationMapper(request) user = mapper.get_user(username) context["is_pi_eligible"] = user["piEligibility"].lower() == "eligible" context["username"] = username context["projects"] = mapper.get_user_projects(username, to_pytas_model=True) return render(request, "projects/user_projects.html", context)
def index_all(request, collection=None): user_projects = None if request.user.is_authenticated: mapper = ProjectAllocationMapper(request) user_projects = mapper.get_user_projects(request.user.username, fetch_balance=False) charge_codes = [p['chargeCode'] for p in user_projects] projects = Project.objects.filter(charge_code__in=charge_codes) f = (ArtifactFilter.MINE(request) | ArtifactFilter.PROJECT(projects) | ArtifactFilter.PUBLIC) else: f = ArtifactFilter.PUBLIC artifacts = _fetch_artifacts(f) # Pass user_projects to list to avoid a second fetch of this data return _render_list(request, artifacts, user_projects=user_projects)
def __init__(self, request, *args, **kwargs): super(ShareArtifactForm, self).__init__(*args, **kwargs) mapper = ProjectAllocationMapper(request) user_projects = [ ( project["chargeCode"], project["nickname"] if "nickname" in project else project["chargeCode"], ) for project in mapper.get_user_projects( request.user.username, to_pytas_model=False ) ] self.fields["project"] = forms.ChoiceField( label="Belongs to project", required=False, choices=[(None, "----")] + user_projects, )
def _render_list(request, artifacts, user_projects=None): if not user_projects: if request.user.is_authenticated: mapper = ProjectAllocationMapper(request) user_projects = mapper.get_user_projects(request.user.username, fetch_balance=False) else: user_projects = [] template = loader.get_template('sharing_portal/index.html') context = { 'hub_url': settings.ARTIFACT_SHARING_JUPYTERHUB_URL, 'artifacts': artifacts.order_by('-created_at'), 'projects': user_projects, } return HttpResponse(template.render(context, request))
def user_projects(request, username): logger.info("User projects requested by admin: %s for user %s", request.user, username) resp = {"status": "error", "msg": "", "result": []} if username: try: mapper = ProjectAllocationMapper(request) user_projects = mapper.get_user_projects(username) resp["status"] = "success" resp["result"] = user_projects logger.info("Total chameleon projects for user %s: %s", username, len(user_projects)) except Exception as e: logger.debug("Error loading projects for user: %s with error %s", username, e) resp["msg"] = "Error loading projects for user: %s" % username return HttpResponse(json.dumps(resp), content_type="application/json")