def __init__(self, view=None, *args, **kwargs):
        super(FilterForm, self).__init__(*args, **kwargs)

        if view:
            activities = view.queryset or view.search(query=view.querydict["query"])

            def get_qs_for(filter_name):
                """
                Does a search without the current filter.
                This is to allow users to change already selected filters. 
                """
                if view.querydict.get(filter_name) and view.queryset:
                    querydict = view.querydict.copy()
                    del querydict[filter_name]
                    return view.search(**querydict)
                else:
                    return activities

            qs = get_qs_for("countries")
            countries = set(project["recipient_country_code"] for project in qs if project["recipient_country_code"])
            country_choices = sorted(
                zip(countries, [iso_to_country(iso) for iso in countries]), key=lambda country: country[1]
            )
            country_choices = sorted(
                country_choices, key=lambda country: country[0] not in view.modified_request.getlist("countries")
            )

            qs = get_qs_for("budget")
            largest_budget = max(Decimal(project["total_budget"]) for project in qs) if qs else 0
            budget_choices = list(
                itertools.takewhile(
                    lambda x: x < largest_budget, [0, 10000, 50000, 100000, 500000, 1000000, 5000000, 10000000]
                )
            )
            budget_choices = zip(budget_choices, ["> " + currency(budget) for budget in budget_choices])

            qs = get_qs_for("sectors")
            sector_choices = set((project["sector_code"], project["sector"]) for project in qs if project["sector"])
            sector_choices = sorted(sector_choices, key=lambda sector: sector[1])
            sector_choices = sorted(
                sector_choices, key=lambda sector: sector[0] not in view.request.GET.getlist("sectors")
            )

            region_choices = (
                WorldBorder.objects.filter(iso2__in=countries).values_list("subregion", flat=True).distinct()
            )
            region_choices = sorted(map(lambda x: (x, SUBREGIONS[x]), region_choices), key=lambda x: x[1])
            region_choices = sorted(
                region_choices, key=lambda region: unicode(region[0]) not in view.request.GET.getlist("regions")
            )

            self.fields["countries"].choices = country_choices
            self.fields["regions"].choices = region_choices
            self.fields["budget"].choices = budget_choices
            self.fields["sectors"].choices = sector_choices
Esempio n. 2
0
    def render_to_csv_response(self, context):
        response = HttpResponse(mimetype="text/csv")
        response["Content-Disposition"] = "attachment; filename=search_results.csv"

        writer = UnicodeWriter(response)

        writer.writerow(["title", "description", "country", "start date", "budget", "principal sector"])
        for activity in context["paginator"].object_list:
            title = activity["title"]
            description = activity["description"]
            country = iso_to_country(activity["recipient_country_code"]) or "Unspecified"
            start_date = activity["start_actual"]
            budget = currency(activity["total_budget"])
            sector = activity["sector"]
            writer.writerow([title, description, country, start_date, budget, sector])
        return response
 def render_to_csv_response(self, context):
     response = HttpResponse(mimetype='text/csv')
     response['Content-Disposition'] = 'attachment; filename=search_results.csv'
     
     writer = UnicodeWriter(response)
     
     writer.writerow(['title', 'description', 'country', 'start date', 'budget', 'principal sector'])
     for activity in context['paginator'].object_list:
         title = activity['title']
         description = activity['description']
         country = iso_to_country(activity['recipient_country_code']) or "Unspecified"
         start_date = activity['start_actual']
         budget = currency(activity['total_budget'])
         sector = activity['sector']
         writer.writerow([title, description, country, start_date, budget, sector])
     return response
Esempio n. 4
0
    def render_to_csv_response(self, context):
        response = HttpResponse(mimetype='text/csv')
        response[
            'Content-Disposition'] = 'attachment; filename=search_results.csv'

        writer = UnicodeWriter(response)

        writer.writerow([
            'title', 'description', 'country', 'start date', 'budget',
            'principal sector'
        ])
        for activity in context['paginator'].object_list:
            title = activity['title']
            description = activity['description']
            country = iso_to_country(
                activity['recipient_country_code']) or "Unspecified"
            start_date = activity['start_actual']
            budget = currency(activity['total_budget'])
            sector = activity['sector']
            writer.writerow(
                [title, description, country, start_date, budget, sector])
        return response
Esempio n. 5
0
    def get_context_data(self, **kwargs):
        project_id = self.kwargs.get("id")
        project = self.connect("activity/%s/" % project_id)
        project.update(organisation=self.connect("organisation/%s/" % project["organisation_id"]))
        transactions = self.connect("transaction", activity__id=project_id)
        policy_markers = self.connect("policymarker", activity__id=project_id, significance__gt=0, _order_by="code")

        commitment_list = []
        disbursement_list = []
        for t in transactions:
            if t["transaction_type"] == "Commitments":
                commitment_list.append(t)
            else:
                disbursement_list.append(t)

        context = kwargs
        context["project"] = project
        context["commitment_list"] = commitment_list
        context["disbursement_list"] = disbursement_list

        context["table"] = [
            ["Country Information"],
            ["Country", iso_to_country(project["recipient_country_code"])],
            [],
            ["Activity Information"],
            ["IATI Identifier", project["identifier"]],
            ["Reporting Organisation", project["organisation"]["name"]],
            ["Sector", project["sector"]],
            ["Sector code", project["sector_code"]],
            ["Last updated", project["last_updated"]],
            ["Start date planned", format_date(project["start_planned"])],
            ["Start date actual", format_date(project["start_actual"])],
            ["End date planned", format_date(project["end_planned"])],
            ["End date actual", format_date(project["end_actual"])],
            ["Collaboration type", project["collaboration_type"]],
            ["Flow type", project["default_flow_type"]],
            ["Aid type", project["default_aid_type"]],
            ["Finance type", project["default_finance_type"]],
            ["Tying status", project["default_tied_status"]],
            ["Activity status", project["activity_status"]],
            [],
            ["Participating Organisations"],
            ["Name", project["organisation"]["name"]],
            ["Type", project["organisation"]["type"]],
            ["Organisation reference code", project["organisation"]["ref"]],
            [],
        ]

        if commitment_list:
            context["table"] += [["Commitments"]]
            for commitment in commitment_list:
                context["table"] += [
                    ["Activity", project["title"]],
                    ["Provider org", commitment["provider_org"]],
                    ["Receiver org", commitment["receiver_org"]],
                    ["Value", currency(Decimal(commitment["value"]))],
                    ["Transaction date", format_date(commitment["transaction_date"])],
                    [],
                ]
        # TODO: DNRY
        if disbursement_list:
            context["table"] += [["Disbursements"]]
            for disbursement in disbursement_list:
                context["table"] += [
                    ["Activity", project["title"]],
                    ["Provider org", disbursement["provider_org"]],
                    ["Receiver org", disbursement["receiver_org"]],
                    ["Value", currency(Decimal(disbursement["value"]))],
                    ["Transaction date", format_date(disbursement["transaction_date"])],
                    [],
                ]

        if policy_markers:
            context["table"] += [["Policy markers"]]
            for policy_marker in policy_markers:
                context["table"] += [
                    ["Description", policy_marker["description"]],
                    ["Significance", code_to_significance(policy_marker["significance"])],
                    [],
                ]

        context["table"] = [[cell or "Unknown" for cell in row] for row in context["table"]]

        return context
Esempio n. 6
0
    def get_context_data(self, **kwargs):
        project_id = self.kwargs.get('id')
        project = self.connect('activity/%s/' % project_id)
        project.update(organisation=self.connect('organisation/%s/' %
                                                 project['organisation_id']))
        transactions = self.connect('transaction', activity__id=project_id)
        policy_markers = self.connect('policymarker',
                                      activity__id=project_id,
                                      significance__gt=0,
                                      _order_by='code')

        commitment_list = []
        disbursement_list = []
        for t in transactions:
            if t['transaction_type'] == 'Commitments':
                commitment_list.append(t)
            else:
                disbursement_list.append(t)

        context = kwargs
        context['project'] = project
        context['commitment_list'] = commitment_list
        context['disbursement_list'] = disbursement_list

        context['table'] = [
            ['Country Information'],
            ['Country',
             iso_to_country(project['recipient_country_code'])],
            [],
            ['Activity Information'],
            ['IATI Identifier', project['identifier']],
            ['Reporting Organisation', project['organisation']['name']],
            ['Sector', project['sector']],
            ['Sector code', project['sector_code']],
            ['Last updated', project['last_updated']],
            ['Start date planned',
             format_date(project['start_planned'])],
            ['Start date actual',
             format_date(project['start_actual'])],
            ['End date planned',
             format_date(project['end_planned'])],
            ['End date actual',
             format_date(project['end_actual'])],
            ['Collaboration type', project['collaboration_type']],
            ['Flow type', project['default_flow_type']],
            ['Aid type', project['default_aid_type']],
            ['Finance type', project['default_finance_type']],
            ['Tying status', project['default_tied_status']],
            ['Activity status', project['activity_status']],
            [],
            ['Participating Organisations'],
            ['Name', project['organisation']['name']],
            ['Type', project['organisation']['type']],
            ['Organisation reference code', project['organisation']['ref']],
            [],
        ]

        if commitment_list:
            context['table'] += [['Commitments']]
            for commitment in commitment_list:
                context['table'] += [
                    ['Activity', project['title']],
                    ['Provider org', commitment['provider_org']],
                    ['Receiver org', commitment['receiver_org']],
                    ['Value', currency(Decimal(commitment['value']))],
                    [
                        'Transaction date',
                        format_date(commitment['transaction_date'])
                    ],
                    [],
                ]
        # TODO: DNRY
        if disbursement_list:
            context['table'] += [['Disbursements']]
            for disbursement in disbursement_list:
                context['table'] += [
                    ['Activity', project['title']],
                    ['Provider org', disbursement['provider_org']],
                    ['Receiver org', disbursement['receiver_org']],
                    ['Value',
                     currency(Decimal(disbursement['value']))],
                    [
                        'Transaction date',
                        format_date(disbursement['transaction_date'])
                    ],
                    [],
                ]

        if policy_markers:
            context['table'] += [['Policy markers']]
            for policy_marker in policy_markers:
                context['table'] += [
                    ['Description', policy_marker['description']],
                    [
                        'Significance',
                        code_to_significance(policy_marker['significance'])
                    ],
                    [],
                ]

        context['table'] = [[cell or 'Unknown' for cell in row]
                            for row in context['table']]

        return context
 def get_context_data(self, **kwargs):
     project_id = self.kwargs.get('id')
     project = self.connect('activity/%s/' % project_id)
     project.update(organisation=self.connect('organisation/%s/' % project['organisation_id']))
     transactions = self.connect('transaction', activity__id=project_id)
     policy_markers = self.connect('policymarker', activity__id=project_id, significance__gt=0, _order_by='code')
     
     commitment_list = []
     disbursement_list = []
     for t in transactions:
         if t['transaction_type'] == 'Commitments':
             commitment_list.append(t)
         else:
             disbursement_list.append(t)
     
     context = kwargs
     context['project'] = project
     context['commitment_list'] = commitment_list
     context['disbursement_list'] = disbursement_list
     
     context['table'] = [
         ['Country Information'],
         ['Country', iso_to_country(project['recipient_country_code'])],
         [],
         ['Activity Information'],
         ['IATI Identifier', project['identifier']],
         ['Reporting Organisation', project['organisation']['name']], 
         ['Sector', project['sector']],
         ['Sector code', project['sector_code']],
         ['Last updated', project['last_updated']],
         ['Start date planned', format_date(project['start_planned'])],
         ['Start date actual', format_date(project['start_actual'])],
         ['End date planned', format_date(project['end_planned'])],
         ['End date actual', format_date(project['end_actual'])],
         ['Collaboration type', project['collaboration_type']],
         ['Flow type', project['default_flow_type']],
         ['Aid type', project['default_aid_type']],
         ['Finance type', project['default_finance_type']],
         ['Tying status', project['default_tied_status']],
         ['Activity status', project['activity_status']],
         [],
         ['Participating Organisations'],
         ['Name', project['organisation']['name']],
         ['Type', project['organisation']['type']],
         ['Organisation reference code', project['organisation']['ref']],
         [],
     ]
     
     if commitment_list:
         context['table'] += [
             ['Commitments']
         ]
         for commitment in commitment_list:
             context['table'] += [
                 ['Activity', project['title']],
                 ['Provider org', commitment['provider_org']],
                 ['Receiver org', commitment['receiver_org']],
                 ['Value', currency(Decimal(commitment['value']))],
                 ['Transaction date', format_date(commitment['transaction_date'])],
                 [],
             ]
     # TODO: DNRY
     if disbursement_list:
         context['table'] += [
             ['Disbursements']
         ]
         for disbursement in disbursement_list:
             context['table'] += [
                 ['Activity', project['title']],
                 ['Provider org', disbursement['provider_org']],
                 ['Receiver org', disbursement['receiver_org']],
                 ['Value', currency(Decimal(disbursement['value']))],
                 ['Transaction date', format_date(disbursement['transaction_date'])],
                 [],
             ]
     
     if policy_markers:
         context['table'] += [
             ['Policy markers']
         ]
         for policy_marker in policy_markers:
             context['table'] += [
                 ['Description', policy_marker['description']],
                 ['Significance', code_to_significance(policy_marker['significance'])],
                 [],
             ]
     
     context['table'] = [[cell or 'Unknown' for cell in row] for row in context['table']]
     
     return context