def org_management(request): """Management of all organizations for the given user""" # get a list of all the organizations this user helps administer organizations = get_or_create_user_profile(request.user).get_organizations() # add invitation forms to each of the organizations for org in organizations.values(): org.form = OrganizationInvitationForm(initial={"invited_by": request.user}) # handle a submitted invitation form if request.method == "POST": form = OrganizationInvitationForm(data=request.POST) if form.is_valid(): # ensure that the current user is a member of the organization to which someone is being invited if not form.instance.organization.is_member(request.user): raise PermissionDenied("Unfortunately for you, you do not have permission to do that.") # send the invitation email, and save the invitation record form.instance.send(request) form.save() return HttpResponseRedirect(reverse("org_management")) else: # we need to inject the form into the correct organization, so errors are displayed inline for pk,org in organizations.items(): if org.pk == int(request.POST.get("organization")): org.form = form return { "title": _("Account administration"), "organizations": organizations, "HEADLESS_ORG_NAME": Organization.HEADLESS_ORG_NAME, "invitations": OrganizationInvitation.objects.filter(email_to_invite=request.user.email) }
def homepage(request): # show the static landing page to users that aren't logged in if not request.user.is_authenticated(): return landing_page(request) # get a list of all the organizations this user helps administer organizations = get_or_create_user_profile(request.user).get_organizations() # add invitation forms to each of the organizations for org in organizations: org.form = OrganizationInvitationForm(initial={"invited_by": request.user}) # handle a submitted invitation form if request.method == "POST": form = OrganizationInvitationForm(data=request.POST) if form.is_valid(): # ensure that the current user is a member of the organization to which someone is being invited if not form.instance.organization.is_member(request.user): return HttpResponseNotAllowed("Unfortunately for you, you do not have permission to do that.") # send the invitation email, and save the invitation record form.instance.send(request) form.save() return HttpResponseRedirect(reverse("homepage")) else: # we need to inject the form into the correct organization, so errors are displayed inline for org in organizations: if org.pk == int(request.POST.get("organization")): org.form = form return { "organizations": organizations, "invitations": OrganizationInvitation.objects.filter(email_to_invite=request.user.email) }
def download_kalite(request, *args, **kwargs): """ A request to download KA Lite, either without zone info, or with it. If with it, then we have to make sure it's OK for this user. This endpoint is also set up to deal with platform, locale, and version, though right now only direct URLs would set this (not via the download wizard). """ # Parse args zone = get_object_or_None(Zone, id=kwargs.get('zone_id', None)) platform = kwargs.get("platform", "all") locale = kwargs.get("locale", "en") version = kwargs.get("version", kalite.VERSION) if version == "latest": version = kalite.VERSION # Make sure this user has permission to admin this zone if zone and not request.user.is_authenticated(): raise PermissionDenied(_("Requires authentication")) elif zone: zone_orgs = Organization.from_zone(zone) if not zone_orgs or not set( [org.id for org in zone_orgs]).intersection( set( get_or_create_user_profile( request.user).get_organizations().keys())): raise PermissionDenied( _("You are not authorized to access this zone information.")) # Generate the zip file. Pre-specify the zip filename, # as we won't know the output location otherwise. zip_file = tempfile.mkstemp()[1] call_command("package_for_download", file=zip_file, central_server=get_central_server_host(request), **kwargs) # Build the outgoing filename." user_facing_filename = "kalite" for val in [platform, locale, kalite.VERSION, zone.name if zone else None]: user_facing_filename += ("-%s" % val) if val not in [None, "", "all"] else "" user_facing_filename += ".zip" # Stream it back to the user zh = open(zip_file, "rb") response = HttpResponse(content=zh, mimetype='application/zip', content_type='application/zip') response[ 'Content-Disposition'] = 'attachment; filename="%s"' % user_facing_filename # Not sure if we could remove the zip file here; possibly not, # if it's a streaming response or byte-range reesponse return response
def org_management(request, org_id=None): """Management of all organizations for the given user""" # get a list of all the organizations this user helps administer organizations = get_or_create_user_profile( request.user).get_organizations() # add invitation forms to each of the organizations for org in organizations.values(): org.form = OrganizationInvitationForm( initial={"invited_by": request.user}) # handle a submitted invitation form if request.method == "POST": form = OrganizationInvitationForm(data=request.POST) if form.is_valid(): # ensure that the current user is a member of the organization to which someone is being invited if not form.instance.organization.is_member(request.user): raise PermissionDenied( "Unfortunately for you, you do not have permission to do that." ) # send the invitation email, and save the invitation record form.instance.send(request) form.save() return HttpResponseRedirect(reverse("org_management")) else: # we need to inject the form into the correct organization, so errors are displayed inline for pk, org in organizations.items(): if org.pk == int(request.POST.get("organization")): org.form = form zones = {} for org in organizations.values(): zones[org.pk] = [] for zone in org.get_zones(): zones[org.pk].append({ "id": zone.id, "name": zone.name, "is_deletable": not zone.has_dependencies(passable_classes=["Organization"]), }) return { "title": _("Account administration"), "organizations": organizations, "zones": zones, "HEADLESS_ORG_NAME": Organization.HEADLESS_ORG_NAME, "invitations": OrganizationInvitation.objects.filter( email_to_invite=request.user.email) }
def download_kalite(request, *args, **kwargs): """ A request to download KA Lite, either without zone info, or with it. If with it, then we have to make sure it's OK for this user. This endpoint is also set up to deal with platform, locale, and version, though right now only direct URLs would set this (not via the download wizard). """ # Parse args zone = get_object_or_None(Zone, id=kwargs.get('zone_id', None)) platform = kwargs.get("platform", "all") locale = kwargs.get("locale", "en") version = kwargs.get("version", kalite.VERSION) if version == "latest": version = kalite.VERSION # Make sure this user has permission to admin this zone if zone and not request.user.is_authenticated(): raise PermissionDenied(_("Requires authentication")) elif zone: zone_orgs = Organization.from_zone(zone) if not zone_orgs or not set([org.id for org in zone_orgs]).intersection(set(get_or_create_user_profile(request.user).get_organizations().keys())): raise PermissionDenied(_("You are not authorized to access this zone information.")) # Generate the zip file. Pre-specify the zip filename, # as we won't know the output location otherwise. zip_file = tempfile.mkstemp()[1] call_command( "package_for_download", file=zip_file, central_server=get_central_server_host(request), **kwargs ) # Build the outgoing filename." user_facing_filename = "kalite" for val in [platform, locale, kalite.VERSION, zone.name if zone else None]: user_facing_filename += ("-%s" % val) if val not in [None, "", "all"] else "" user_facing_filename += ".zip" # Stream it back to the user zh = open(zip_file,"rb") response = HttpResponse(content=zh, mimetype='application/zip', content_type='application/zip') response['Content-Disposition'] = 'attachment; filename="%s"' % user_facing_filename # Not sure if we could remove the zip file here; possibly not, # if it's a streaming response or byte-range reesponse return response
def org_management(request, org_id=None): """Management of all organizations for the given user""" # get a list of all the organizations this user helps administer organizations = get_or_create_user_profile(request.user).get_organizations() # add invitation forms to each of the organizations for org in organizations.values(): org.form = OrganizationInvitationForm(initial={"invited_by": request.user}) # handle a submitted invitation form if request.method == "POST": form = OrganizationInvitationForm(data=request.POST) if form.is_valid(): # ensure that the current user is a member of the organization to which someone is being invited if not form.instance.organization.is_member(request.user): raise PermissionDenied(_("Unfortunately for you, you do not have permission to do that.")) # send the invitation email, and save the invitation record form.instance.send(request) form.save() return HttpResponseRedirect(reverse("org_management")) else: # we need to inject the form into the correct organization, so errors are displayed inline for pk,org in organizations.items(): if org.pk == int(request.POST.get("organization")): org.form = form zones = {} for org in organizations.values(): zones[org.pk] = [] for zone in list(org.get_zones()): zones[org.pk].append({ "id": zone.id, "name": zone.name, "is_deletable": not zone.has_dependencies(passable_classes=["Organization"]), }) return { "title": _("Account administration"), "organizations": organizations, "zones": zones, "HEADLESS_ORG_NAME": Organization.HEADLESS_ORG_NAME, "my_invitations": list(OrganizationInvitation.objects \ .filter(email_to_invite=request.user.email) .order_by("organization__name")), "download_url": reverse("install"), }