Example #1
0
def orgs_team_members(request, org, team_id, template="orgs/orgs_team_members.html"):
    #organization and teams
    organization = Organization.get_by_name(org)
    teams = Team.get_by_org(org)
    current_domains = Domain.get_by_organization(org)

    #check that the team exists
    team = Team.get(team_id)
    if team is None:
        raise Http404("Group %s does not exist" % team_id)

    #inspect the members of the team
    member_ids = team.get_member_ids()
    members = WebUser.view("_all_docs", keys=member_ids, include_docs=True).all()
    members.sort(key=lambda user: user.username)

    #inspect the domains of the team
    domain_names = team.get_domains()
    domains = list()
    for name in domain_names:
        domains.append([Domain.get_by_name(name), team.role_label(domain=name)])

    all_org_domains = Domain.get_by_organization(org)
    non_domains = [domain for domain in all_org_domains if domain.name not in domain_names]

    all_org_member_ids = organization.members
    all_org_members = WebUser.view("_all_docs", keys=all_org_member_ids, include_docs=True).all()
    non_members = [member for member in all_org_members if member.user_id not in member_ids]

    vals = dict(org=organization, team=team, teams=teams, members=members, nonmembers=non_members, domains=current_domains, team_domains=domains, team_nondomains=non_domains)
    return render_to_response(request, template, vals)
Example #2
0
def base_context(request, organization, update_form=None):
    return {
        "org":
        organization,
        "teams":
        Team.get_by_org(organization.name),
        "domains":
        sorted(Domain.get_by_organization(organization.name),
               key=lambda x: x.name),
        "members":
        organization.get_members(),
        "admin":
        request.couch_user.is_org_admin(organization.name)
        or request.couch_user.is_superuser,
        "update_form_empty":
        not update_form,
        "update_form":
        update_form or UpdateOrgInfo(
            initial={
                'org_title': organization.title,
                'email': organization.email,
                'url': organization.url,
                'location': organization.location
            })
    }
Example #3
0
def stats_data(request, org):
    params, _ = parse_args_for_es(request)
    domains = [{"name": d.name, "hr_name": d.hr_name} for d in Domain.get_by_organization(org).all()]
    histo_type = request.GET.get("histogram_type")
    period = request.GET.get("daterange", "month")

    today = date.today()
    startdate = today - timedelta(days={"month": 30, "week": 7, "quarter": 90, "year": 365}[period])

    histo_data = dict(
        [
            (
                d["hr_name"],
                es_histogram(histo_type, [d["name"]], startdate.strftime("%Y-%m-%d"), today.strftime("%Y-%m-%d")),
            )
            for d in domains
        ]
    )

    return json_response(
        {
            "histo_data": histo_data,
            "range": period,
            "startdate": [startdate.year, startdate.month, startdate.day],
            "enddate": [today.year, today.month, today.day],
        }
    )
Example #4
0
def stats_data(request, org):
    params, _ = parse_args_for_es(request)
    domains = [{
        "name": d.name,
        "hr_name": d.hr_name
    } for d in Domain.get_by_organization(org).all()]
    histo_type = request.GET.get('histogram_type')
    period = request.GET.get("daterange", 'month')

    today = date.today()
    startdate = (today - timedelta(days={
        'month': 30,
        'week': 7,
        'quarter': 90,
        'year': 365,
    }[period]))

    histo_data = dict([(d['hr_name'],
                        es_histogram(histo_type, [d["name"]],
                                     startdate.strftime('%Y-%m-%d'),
                                     today.strftime('%Y-%m-%d')))
                       for d in domains])

    return json_response({
        'histo_data':
        histo_data,
        'range':
        period,
        'startdate': [startdate.year, startdate.month, startdate.day],
        'enddate': [today.year, today.month, today.day],
    })
Example #5
0
 def get_domains(self):
     from corehq.apps.orgs.models import Organization
     from corehq.apps.domain.models import Domain
     org = self.request.GET.get('org', None)
     organization = Organization.get_by_name(org, strict=True)
     if organization and \
             (self.request.couch_user.is_superuser or self.request.couch_user.is_member_of_org(org)):
         return [d for d in Domain.get_by_organization(organization.name).all()]
     return []
Example #6
0
def stats_data(request, org):
    params, _ = parse_args_for_es(request)
    domains = [{"name": d.name, "hr_name": d.hr_name} for d in Domain.get_by_organization(org).all()]
    histo_type = request.GET.get('histogram_type')

    enddate = request.GET.get('enddate')
    enddate = datetime.strptime(enddate, "%Y-%m-%d") if enddate else date.today()
    startdate = request.GET.get('startdate')
    startdate = datetime.strptime(startdate, "%Y-%m-%d") if startdate else enddate - timedelta(days=30)

    histo_data = dict([(d['hr_name'],
                        es_histogram(histo_type, [d["name"]], request.datespan.startdate_display, request.datespan.enddate_display))
                        for d in domains])

    def _total_forms_until_date(dom, date):
        key = ["submission", dom]
        r = get_db().view('reports_forms/all_forms',
            startkey=key+[""],
            endkey=key+[json_format_datetime(date)],
            group=False
        ).one()
        return r['value'] if r else 0

    def _total_cases_until_date(dom, date):
        key = ["", dom]
        r = get_db().view('reports/case_activity',
            startkey=key + [""],
            endkey=key + [json_format_datetime(date), '{}']).one()
        return r['value'] if r else 0

    def _total_users_until_date(dom, date):
        from corehq.apps.appstore.views import es_query
        q = {
            "query": {"term": {"domain": dom}},
            "filter": {
                "and": [
                    {"range": {"created_on": {"lt": date.strftime('%Y-%m-%d')}}},
                    {"term": {"doc_type": "CommCareUser"}},
                ],
            },
        }
        return es_query(q=q, es_url=USER_INDEX + '/user/_search')["hits"]["total"]


    init_val_fn = {
        "forms": _total_forms_until_date,
        "cases": _total_cases_until_date,
        "users": _total_users_until_date,
    }[histo_type]

    return json_response({
        'histo_data': histo_data,
        'initial_values': dict([(dom["name"], init_val_fn(dom["name"], startdate)) for dom in domains]),
        'startdate': request.datespan.startdate_key_utc,
        'enddate': request.datespan.enddate_key_utc,
    })
Example #7
0
def add_domain_to_team(request, org, team_id):
    domain = request.POST.get("project_name", None)
    if not domain:
        messages.error(request, "You must specify a project name")
    elif domain not in [d.name for d in Domain.get_by_organization(org)]:
        messages.error(request, "You cannot add a domain that isn't managed by this organization")
    else:
        team = Team.get(team_id)
        team.add_domain_membership(domain)
        team.save()
    return HttpResponseRedirect(reverse(request.POST.get("redirect_url", "orgs_team_members"), args=(org, team_id)))
Example #8
0
def base_context(request, organization, update_form=None):
    return {
        "org": organization,
        "teams": Team.get_by_org(organization.name),
        "domains": sorted(Domain.get_by_organization(organization.name), key=lambda x: x.name),
        "members": organization.get_members(),
        "admin": request.couch_user.is_org_admin(organization.name) or request.couch_user.is_superuser,
        "update_form_empty": not update_form,
        "update_form": update_form or UpdateOrgInfo(initial={'org_title': organization.title, 'email': organization.email,
                                                            'url': organization.url, 'location': organization.location})
    }
Example #9
0
def stats_data(request, org):
    domains = [{
        "names": [d.name],
        "display_name": d.hr_name
    } for d in Domain.get_by_organization(org)]
    histo_type = request.GET.get('histogram_type')
    interval = request.GET.get('interval', 'day')
    stats_data = get_general_stats_data(domains,
                                        histo_type,
                                        request.datespan,
                                        interval=interval)
    return json_response(stats_data)
Example #10
0
def add_domain_to_team(request, org, team_id):
    domain = request.POST.get("project_name", None)
    if not domain:
        messages.error(request, "You must specify a project name")
    elif domain not in [d.name for d in Domain.get_by_organization(org)]:
        messages.error(request, "You cannot add a domain that isn't managed by this organization")
    else:
        team = Team.get(team_id)
        team.add_domain_membership(domain)
        read_only_role = UserRole.by_domain_and_name(domain, 'Read Only').one()
        team.set_role(domain, 'user-role:%s' % read_only_role.get_id)
        team.save()
    return HttpResponseRedirect(reverse(request.POST.get('redirect_url', 'orgs_team_members'), args=(org, team_id)))
Example #11
0
def add_domain_to_team(request, org, team_id):
    domain = request.POST.get("project_name", None)
    if not domain:
        messages.error(request, "You must specify a project name")
    elif domain not in [d.name for d in Domain.get_by_organization(org)]:
        messages.error(request, "You cannot add a domain that isn't managed by this organization")
    else:
        team = Team.get(team_id)
        team.add_domain_membership(domain)
        read_only_role = UserRole.by_domain_and_name(domain, 'Read Only').one()
        team.set_role(domain, 'user-role:%s' % read_only_role.get_id)
        team.save()
    return HttpResponseRedirect(reverse(request.POST.get('redirect_url', 'orgs_team_members'), args=(org, team_id)))
Example #12
0
def base_context(request, organization, update_form=None):
    return {
        "org": organization,
        "teams": Team.get_by_org(organization.name),
        "domains": Domain.get_by_organization(organization.name).all(),
        "members": organization.get_members(),
        "admin": request.couch_user.is_org_admin(organization.name) or request.couch_user.is_superuser,
        "update_form_empty": not update_form,
        "update_form": update_form
        or UpdateOrgInfo(
            initial={
                "org_title": organization.title,
                "email": organization.email,
                "url": organization.url,
                "location": organization.location,
            }
        ),
    }
Example #13
0
def stats_data(request, org):
    params, _ = parse_args_for_es(request)
    domains = [{"name": d.name, "hr_name": d.hr_name} for d in Domain.get_by_organization(org).all()]
    histo_type = request.GET.get('histogram_type')
    period = request.GET.get("daterange", 'month')

    enddate = request.GET.get('enddate')
    enddate = datetime.strptime(enddate, "%Y-%m-%d") if enddate else date.today()
    startdate = request.GET.get('startdate')
    startdate = datetime.strptime(startdate, "%Y-%m-%d") if startdate else enddate - timedelta(days=30)

    histo_data = dict([(d['hr_name'],
                        es_histogram(histo_type, [d["name"]], startdate.strftime('%Y-%m-%d'), enddate.strftime('%Y-%m-%d')))
                       for d in domains])

    return json_response({
        'histo_data': histo_data,
        'range': period,
        'startdate': [startdate.year, startdate.month, startdate.day],
        'enddate': [enddate.year, enddate.month, enddate.day],
    })
Example #14
0
def orgs_landing(request, org, template="orgs/orgs_landing.html", form=None, add_form=None, add_member_form=None, add_team_form=None, update_form=None):
    organization = Organization.get_by_name(org)

    reg_form_empty = not form
    add_form_empty = not add_form
    add_member_form_empty = not add_member_form
    add_team_form_empty = not add_team_form
    update_form_empty = not update_form

    reg_form = form or DomainRegistrationForm(initial={'org': organization.name})
    add_form = add_form or AddProjectForm(org)
    add_member_form = add_member_form or AddMemberForm(org)
    add_team_form = add_team_form or AddTeamForm(org)

    update_form = update_form or UpdateOrgInfo(initial={'org_title': organization.title, 'email': organization.email, 'url': organization.url, 'location': organization.location})

    current_teams = Team.get_by_org(org)
    current_domains = Domain.get_by_organization(org)
    members = [WebUser.get_by_user_id(user_id) for user_id in organization.members]
    vals = dict( org=organization, domains=current_domains, reg_form=reg_form,
                 add_form=add_form, reg_form_empty=reg_form_empty, add_form_empty=add_form_empty, update_form=update_form, update_form_empty=update_form_empty, add_member_form=add_member_form, add_member_form_empty=add_member_form_empty, add_team_form=add_team_form, add_team_form_empty=add_team_form_empty, teams=current_teams, members=members)
    return render_to_response(request, template, vals)
Example #15
0
def stats_data(request, org):
    params, _ = parse_args_for_es(request)
    domains = [{"name": d.name, "hr_name": d.hr_name} for d in Domain.get_by_organization(org).all()]
    histo_type = request.GET.get('histogram_type')
    period = request.GET.get("daterange", 'month')

    today = date.today()
    startdate = (today - timedelta(days={
        'month': 30,
        'week': 7,
        'quarter': 90,
        'year': 365,
    }[period]))

    histo_data = dict([(d['hr_name'], es_histogram(histo_type, [d["name"]], startdate.strftime('%Y-%m-%d'), today.strftime('%Y-%m-%d')))
                       for d in domains])

    return json_response({
        'histo_data': histo_data,
        'range': period,
        'startdate': [startdate.year, startdate.month, startdate.day],
        'enddate': [today.year, today.month, today.day],
    })
Example #16
0
def orgs_teams(request, org, template="orgs/orgs_teams.html"):
    organization = Organization.get_by_name(org)
    teams = Team.get_by_org(org)
    current_domains = Domain.get_by_organization(org)
    vals = dict(org=organization, teams=teams, domains=current_domains)
    return render_to_response(request, template, vals)
Example #17
0
def stats_data(request, org):
    domains = [{"names": [d.name], "display_name": d.hr_name} for d in Domain.get_by_organization(org)]
    histo_type = request.GET.get('histogram_type')
    interval = request.GET.get('interval', 'day')
    stats_data = get_stats_data(domains, histo_type, request.datespan, interval=interval)
    return json_response(stats_data)
Example #18
0
def stats_data(request, org):
    domains = [{"name": d.name, "hr_name": d.hr_name} for d in Domain.get_by_organization(org).all()]
    histo_type = request.GET.get('histogram_type')
    stats_data = get_stats_data(domains, histo_type, request.datespan)
    return json_response(stats_data)