def country_population_timeline(request, country_code): country = Country.find_by_code(country_code) if not country: raise Http404("No country for specified code") response = HttpResponse(content_type="text/csv") writer = csv.writer(response) writer.writerow([ "Date", "Country", "Government", "Location", "Population", "Declaration Status", "Declared Population", ]) for pc in country.popcounts: area = pc.declaration.area writer.writerow([ pc.declaration.event_date, country.name, area.name, area.location, area.population, pc.status, pc.population, ]) return response
def country_regenerate_timeline(request, country_code): status = 403 if request.user.is_authenticated: country = Country.find_by_code(country_code) if not country: raise Http404("No country for specified code") country.generate_population_count() return HttpResponse(str(country.current_popcount), content_type="text/plain") else: return HttpResponse(status=status)
def country_trigger_recount(request, country_code): status = 403 if request.user.is_authenticated: country = Country.find_by_code(country_code) if not country: raise Http404("No country for specified code") logger.info(f"triggering recount for country {country}") response = country.trigger_population_recount() return JsonResponse(dict(response), content_type="application/json") else: return HttpResponse(status=status)
def handle(self, *args, **options): self.stdout.write( self.style.SUCCESS("About to generate population count for some country") ) try: country_code = options.get("country_code", "VUT") country = Country.find_by_code(country_code) country.generate_population_count() except Country.DoesNotExist: raise CommandError('Country "%s" does not exist' % country_code) self.stdout.write( self.style.SUCCESS( 'Generated population count for country "%s"' % country_code ) )
def country_declarations(request, country_code): country = Country.find_by_code(country_code) if not country: raise Http404("No country for specified code") response = HttpResponse(content_type="text/csv") writer = csv.writer(response) writer.writerow([ "Area", "Location", "Population", "Date Declared", "Declared Ancestors" ]) query_args = { "order_by": "date", } before_date = request.GET.get("before") after_date = request.GET.get("after") if before_date: date_obj = None try: date_obj = datetime.datetime.strptime(before_date, DATE_FORMAT) except ValueError as ex: return HttpResponseBadRequest("Bad date format") query_args["before"] = before_date if after_date: date_obj = None try: date_obj = datetime.datetime.strptime(after_date, DATE_FORMAT) except ValueError as ex: return HttpResponseBadRequest("Bad date format") query_args["after"] = after_date declist = country.declarations(**query_args) for dec in declist: writer.writerow([ dec.area.name, dec.area.location, dec.area.population, dec.display_event_date(), dec.area.num_declared_ancestors(), ]) return response
def country_pop_by_location(request, country_code): country = Country.find_by_code(country_code) if not country: raise Http404("No country for specified code") response = HttpResponse(content_type="text/plain") writer = csv.writer(response) writer.writerow(["Date", "Country", "Location", "Declared Population"]) cpops = country.popcounts start_date = cpops[0].declaration.event_date logger.info(f"start date {start_date}") current_pop = 0 last_date = cpops[0].declaration.event_date day_diff = timedelta(days=1) for pc in cpops: dec_date = pc.date while dec_date > last_date + day_diff: last_date += day_diff writer.writerow([last_date, country.name, "", current_pop]) area = pc.declaration.area writer.writerow([pc.date, country.name, area.location, pc.population]) current_pop = pc.population last_date = dec_date return response
def country_population(request, country_code): country = Country.find_by_code(country_code) if not country: raise Http404("No country for specified code") return HttpResponse(str(country.current_popcount), content_type="text/plain")