Example #1
0
def zipcode_view(request, year_range, fieldname, abtest=None):
	if request.method != 'GET':
		# Redirect to home if they try to POST
		# TODO: what is the behavior of HEAD?
		return HttpResponseRedirect(HOME_URL)
	else:
		zipcode_radius_form = variety_trials_forms.SelectLocationByZipcodeForm(request.GET)
		if not zipcode_radius_form.is_valid():
			# TODO: Have this view point to / , and if successful redirect them
			#   to the proper view with the URL filled out
			# OR: Have a zipcode form on the /view/year/field/ page
			return HttpResponseRedirect("%s%s" % (HOME_URL, '/?error=no_zipcode'))
		else:
			zipcode = zipcode_radius_form.cleaned_data['zipcode']
			scope = zipcode_radius_form.cleaned_data['scope']
			
			try:
				locations = get_locations(zipcode, scope)
			except models.Zipcode.DoesNotExist:
				zipcode_radius_form = variety_trials_forms.SelectLocationByZipcodeForm(initial={
						'scope': scope,
					})
				# TODO: repopulate form
				return HttpResponseRedirect("%s%s" % (HOME_URL, '/?error=bad_zipcode'))
			
			result = 0;
			curyear = datetime.date.today().year
			while result < 1 and curyear > 1900:
				result = models.TrialEntry.objects.filter(
						location__in = locations
					).filter(
							harvest_date__in = models.Date.objects.filter(
								date__range=(
										datetime.date(curyear,1,1),
										datetime.date(curyear,12,31)
									)
							)
					).filter(
						hidden=False
					).count()
				if result < 1: 
					curyear -= 1
				
			try:
				year_range = int(year_range)
			except ValueError:
				year_range = 3
				
			years = [curyear - diff for diff in range(year_range)]
			
			return historical_zipcode_view(
					request, 
					curyear, 
					fieldname, 
					abtest=abtest, 
					years=years, 
					year_url_bit='last_%d_years' % (year_range), 
					locations=locations, 
					year_range=year_range
				)
Example #2
0
def zipcode_near_json(request, zipcode):
	try:
		locations = variety_trials_util.get_locations(zipcode)
	except models.Zipcode.DoesNotExist:
		locations = []

	return json_response(request, locations)
Example #3
0
def trial_entry_near_json(request, zipcode):
	#TODO: the logic here should follow views.zipcode_view
	min_year=2009
	max_year=2011
	try:
		locations = variety_trials_util.get_locations(zipcode)
	except models.Zipcode.DoesNotExist:
		locations=models.Location.objects.all()
	
	d=models.Trial_Entry.objects.select_related(depth=3).filter(
				location__in=locations[0:8]
			).filter(
				harvest_date__in=models.Date.objects.filter(
					date__range=(datetime.date(min_year,1,1), datetime.date(max_year,12,31))
				)
			).filter(
				hidden=False
			)

	fields = (
		'pk',
		'model',
		'variety',
		'location',
		'name',
		'bushels_acre',
		'protein_percent',
		'test_weight'
		)
	
	
	return json_response(request, d, needed_fields=fields)
def zipcode_view(request, year_range, fieldname, abtest=None):
    if request.method != 'GET':
        # Redirect to home if they try to POST
        # TODO: what is the behavior of HEAD?
        return HttpResponseRedirect(HOME_URL)
    else:
        zipcode_radius_form = variety_trials_forms.SelectLocationByZipcodeForm(
            request.GET)
        if not zipcode_radius_form.is_valid():
            # TODO: Have this view point to / , and if successful redirect them
            #   to the proper view with the URL filled out
            # OR: Have a zipcode form on the /view/year/field/ page
            return HttpResponseRedirect("%s%s" %
                                        (HOME_URL, '/?error=no_zipcode'))
        else:
            zipcode = zipcode_radius_form.cleaned_data['zipcode']
            scope = zipcode_radius_form.cleaned_data['scope']

            try:
                locations = get_locations(zipcode, scope)
            except models.Zipcode.DoesNotExist:
                zipcode_radius_form = variety_trials_forms.SelectLocationByZipcodeForm(
                    initial={
                        'scope': scope,
                    })
                # TODO: repopulate form
                return HttpResponseRedirect("%s%s" %
                                            (HOME_URL, '/?error=bad_zipcode'))

            result = 0
            curyear = datetime.date.today().year
            while result < 1 and curyear > 1900:
                result = models.TrialEntry.objects.filter(
                    location__in=locations).filter(
                        harvest_date__in=models.Date.objects.filter(
                            date__range=(
                                datetime.date(curyear, 1, 1),
                                datetime.date(curyear, 12, 31)))).filter(
                                    hidden=False).count()
                if result < 1:
                    curyear -= 1

            try:
                year_range = int(year_range)
            except ValueError:
                year_range = 3

            years = [curyear - diff for diff in range(year_range)]

            return historical_zipcode_view(request,
                                           curyear,
                                           fieldname,
                                           abtest=abtest,
                                           years=years,
                                           year_url_bit='last_%d_years' %
                                           (year_range),
                                           locations=locations,
                                           year_range=year_range)
Example #5
0
def trial_entry_near_ids_json(request, zipcode):
	min_year=2009
	max_year=2011
	list=[]
	try:
		locations = variety_trials_util.get_locations(zipcode)
	except models.Zipcode.DoesNotExist:
		locations=models.Location.objects.all()
	
	d=models.Trial_Entry.objects.select_related(depth=3).filter(
				location__in=locations
			).filter(
				harvest_date__in=models.Date.objects.filter(
					date__range=(datetime.date(min_year,1,1), datetime.date(max_year,12,31))
				)
			).filter(
				hidden=False
			)
	for trial in d:
		list.append(trial.pk)
	
	# TODO: json_response() wasn't flexible enough, so this is copy+pasted. FIX
	response = HttpResponse()
	if request.method == 'GET':
		try:
			jsonp_callback = request.GET['callback']
		except:
			jsonp_callback = None
			
		if not jsonp_callback:
			try:
				jsonp_callback = request.GET['jsonp']
			except:
				jsonp_callback = None

	if jsonp_callback:
		response.write(jsonp_callback)
		response.write("(")
			
	json.dump(list, response)
		
	if jsonp_callback:
		response.write(")")
	
	return response