Beispiel #1
0
 def test_url_contains_multiple_locations(self):
     c1 = Coordinate(0.0, 1.0)
     c2 = Coordinate(1.0, 2.0)
     adapter = MapquestAdapter('SecretKey')
     url = adapter.get_url([c1, c2])
     self.assertIn("{},{}".format(c1.latitude, c1.longitude), url)
     self.assertIn("{},{}".format(c2.latitude, c2.longitude), url)
Beispiel #2
0
    def test_consolidate_same_multiple_location(self):
        locations = [
            Coordinate(39.7160439, 32.7059948),
            Coordinate(38.963745, 35.243322),
            Coordinate(38.963745, 35.243322),
            Coordinate(38.963745, 35.243322)
        ]
        adapter = MapquestAdapter('SecretKey')

        url = adapter.get_url(locations)

        self.assertIn('39.7160439,32.7059948|marker-1', url)
        self.assertIn('38.963745,35.243322|marker-3', url)
def _render_project_report(request,
                           project_id,
                           with_map=False,
                           with_disaggregation=False):
    show_comment = True if request.GET.get('comment',
                                           '').strip() == 'true' else False
    start_date = utils.parse_date(
        request.GET.get('period_start', '').strip(), datetime(1900, 1, 1))
    end_date = utils.parse_date(
        request.GET.get('period_end', '').strip(), datetime(2999, 12, 31))

    project = get_object_or_404(Project.objects.prefetch_related(
        'locations', 'partners', 'related_projects', 'related_to_projects',
        'results', 'results__indicators', 'results__indicators__periods',
        'results__indicators__periods__disaggregations'),
                                pk=project_id)
    project_location = project.primary_location
    coordinates = None

    if with_map:
        locations = project.locations.all()
        coordinates = [
            Coordinate(loc.latitude, loc.longitude) for loc in locations if loc
        ]

    now = datetime.today()
    project_view = build_view_object(project, start_date, end_date)

    html = render_to_string(
        'reports/project-results-indicators-map-overview.html',
        context={
            'project':
            project_view,
            'location':
            ", ".join([
                _f for _f in [
                    project_location.city,
                    getattr(project_location.country, 'name', None)
                ] if _f
            ]) if project_location else "",
            'staticmap':
            get_staticmap_url(coordinates, Size(900, 600))
            if with_map else None,
            'show_comment':
            show_comment,
            'show_disaggregations':
            with_disaggregation,
            'use_indicator_target':
            project_view.use_indicator_target,
            'today':
            now.strftime('%d-%b-%Y'),
        })

    if request.GET.get('show-html', ''):
        return HttpResponse(html)

    filename = '{}-{}-results-indicators{}-overview.pdf'.format(
        now.strftime('%Y%b%d'), project.id, '-map' if with_map else '')

    return utils.make_pdf_response(html, filename)
def render_organisation_projects_results_indicators_map_overview(
        request, program_id):
    country = request.GET.get('country', '').strip()
    if not country:
        return HttpResponseBadRequest('Please provide the country code!')

    show_comment = True if request.GET.get('comment',
                                           '').strip() == 'true' else False
    start_date = utils.parse_date(
        request.GET.get('period_start', '').strip(), datetime(1900, 1, 1))
    end_date = utils.parse_date(
        request.GET.get('period_end', '').strip(), datetime(2999, 12, 31))

    country = get_object_or_404(Country, iso_code=country)
    project_hierarchy = get_object_or_404(ProjectHierarchy,
                                          root_project=program_id)
    organisation = get_object_or_404(Organisation.objects.prefetch_related(
        'projects', 'projects__results', 'projects__results__indicators',
        'projects__results__indicators__periods'),
                                     pk=project_hierarchy.organisation.id)
    projects = organisation.all_projects().filter(
        primary_location__country=country)
    coordinates = [
        Coordinate(p.primary_location.latitude, p.primary_location.longitude)
        for p in projects if p.primary_location
    ]

    now = datetime.today()

    html = render_to_string(
        'reports/organisation-projects-results-indicators-map-overview.html',
        context={
            'title':
            'Results and indicators overview for projects in {}'.format(
                country.name),
            'staticmap':
            get_staticmap_url(coordinates, Size(900, 600)),
            'projects':
            [build_view_object(p, start_date, end_date) for p in projects],
            'show_comment':
            show_comment,
            'today':
            now.strftime('%d-%b-%Y'),
        })

    if request.GET.get('show-html', ''):
        return HttpResponse(html)

    filename = '{}-{}-{}-projects-results-indicators-overview.pdf'.format(
        now.strftime('%Y%b%d'), organisation.id, country.iso_code)

    return utils.make_pdf_response(html, filename)
def render_country_level_report(request, program_id):
    country = request.GET.get('country', '').strip()
    if not country:
        return HttpResponseBadRequest('Please provide the country code!')

    show_comment = True if request.GET.get('comment', '').strip() == 'true' else False
    start_date = utils.parse_date(request.GET.get('period_start', '').strip(), datetime(1900, 1, 1))
    end_date = utils.parse_date(request.GET.get('period_end', '').strip(), datetime(2999, 12, 31))

    country = get_object_or_404(Country, iso_code=country)
    project = get_object_or_404(Project, id=program_id)
    project_ids = project.descendants()\
        .exclude(pk=program_id)\
        .exclude(Q(title__icontains='test') | Q(subtitle__icontains='test'))\
        .values_list('id', flat=True)
    projects_in_country = Project.objects\
        .filter(id__in=project_ids, locations__country=country)\
        .distinct()
    coordinates = [
        Coordinate(location.latitude, location.longitude)
        for project in projects_in_country
        for location in project.locations.all()
        if location.country == country
    ]

    now = datetime.today()

    html = render_to_string('reports/nuffic-country-level-report.html', context={
        'title': 'Country level report for projects in {}'.format(country.name),
        'staticmap': get_staticmap_url(coordinates, Size(900, 600), zoom=11),
        'projects': build_view_objects(projects_in_country, start_date, end_date),
        'show_comment': show_comment,
        'today': now.strftime('%d-%b-%Y'),
    })

    if request.GET.get('show-html', ''):
        return HttpResponse(html)

    filename = '{}-{}-country-report.pdf'.format(now.strftime('%Y%b%d'), country.iso_code)

    return utils.make_pdf_response(html, filename)
def render_report(request, program_id):
    now = datetime.today()

    program = get_object_or_404(Project.objects.prefetch_related('results'),
                                pk=program_id)
    start_date = utils.parse_date(request.GET.get('period_start', '').strip())
    end_date = utils.parse_date(request.GET.get('period_end', '').strip())

    program_view = build_view_object(
        program, start_date or datetime(1900, 1, 1), end_date
        or (datetime.today() + relativedelta(years=10)))

    coordinates = [
        Coordinate(loc.latitude, loc.longitude)
        for loc in program_view.locations
        if loc and loc.latitude and loc.longitude
    ]

    html = render_to_string('reports/program-overview.html',
                            context={
                                'program':
                                program_view,
                                'staticmap':
                                get_staticmap_url(coordinates, Size(900, 600)),
                                'start_date':
                                start_date,
                                'end_date':
                                end_date,
                            })

    if request.GET.get('show-html', ''):
        return HttpResponse(html)

    filename = '{}-program-{}-overview.pdf'.format(now.strftime('%Y%b%d'),
                                                   program.id)

    return utils.make_pdf_response(html, filename)
Beispiel #7
0
 def test_url_with_size(self):
     c = Coordinate(0.0, 1.0)
     s = Size(100, 100)
     adapter = MapquestAdapter('SecretKey')
     self.assertIn("{},{}".format(s.width, s.height),
                   adapter.get_url([c], size=s))
Beispiel #8
0
 def test_url_contains_one_location(self):
     c = Coordinate(0.0, 1.0)
     adapter = MapquestAdapter('SecretKey')
     self.assertIn("{},{}".format(c.latitude, c.longitude),
                   adapter.get_url([c]))
Beispiel #9
0
 def test_url_contains_key(self):
     key = 'SecretKey'
     adapter = MapquestAdapter(key)
     self.assertIn(key, adapter.get_url([Coordinate(0.0, 0.0)]))