Beispiel #1
0
def historical_zipcode_view(request, startyear, fieldname, abtest=None, years=None, year_url_bit=None, locations=None, year_range=3):
	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']
			not_locations = zipcode_radius_form.cleaned_data['not_location']
			varieties = zipcode_radius_form.cleaned_data['variety']
			yearname = zipcode_radius_form.cleaned_data['year']
			
			hidden_zipcode_form = variety_trials_forms.SelectLocationByZipcodeForm(
				initial={
					'zipcode': zipcode,
					'scope': scope,
					'not_location': not_locations,
					'year': yearname,
					}
				)
			try:
				maxyear = int(startyear)
			except ValueError:
				maxyear = datetime.date.today().year
				
			try:
				curyear = int(yearname)
			except ValueError:
				# TODO: redirect to /view/curyear/field/?... instead
				curyear = maxyear
			
			if curyear > maxyear:
				curyear = maxyear
			
			if year_url_bit is None:
				year_url_bit = startyear
			
			if years is None:
				years = [maxyear - diff for diff in range(year_range)]
			
			try:
				page = get_page(
						zipcode, 
						scope, 
						curyear, 
						fieldname,
						year_url_bit,
						not_locations=not_locations, 
						varieties=varieties, 
						year_range=year_range, 
						locations=locations
					)
			except models.Zipcode.DoesNotExist:
				zipcode_radius_form = variety_trials_forms.SelectLocationByZipcodeForm(initial={
						#'radius': zipcode_radius_form.cleaned_data['search_radius'],
					})
				# TODO: repopulate form
				return HttpResponseRedirect("%s%s" % (HOME_URL, '/?error=bad_zipcode'))
			except (LSDProbabilityOutOfRange, TooFewDegreesOfFreedom, NotEnoughDataInYear) as error:
				page = None
				message = " ".join([ERROR_MESSAGE, error.message])
				raise
			except:
				page = None
				message = ERROR_MESSAGE
				raise
					
				
			"""
			import sys
			for table in page.tables:
				for variety, row in table.rows.items():
					sys.stdout.write('['+variety.name+'\n')
					for cell in row:
						sys.stdout.write('\t'+unicode(cell))
					sys.stdout.write(']\n')
				print table.columns
			#"""
			response = None
			if page is not None:
				try:
					response = render_to_response(
						'tabbed_view_table_ndsu.html',
						{
							'home_url': HOME_URL,
							'hidden_zipcode_form': hidden_zipcode_form,
							'zipcode_get_string': '?%s' % (urlencode( [('zipcode', zipcode)] )),
							'zipcode': zipcode,
							'scope_get_string': '&%s' % (urlencode( [('scope', scope)] )),
							'scope': scope,
							'not_location_get_string': '&%s' % (urlencode([('not_location', l) for l in not_locations])),
							'not_locations': not_locations,
							'variety_get_string': '&%s' % (urlencode([('variety', v) for v in varieties])),
							'varieties': varieties,
							'year_get_string': '&%s' % (urlencode( [('year', curyear)] )),
							'year_url_bit': year_url_bit,
							'curyear': curyear,
							'maxyear': maxyear,
							'page': page,
							'message': None,
							'years': years,
							'blurbs' : unit_blurbs,
							'curfield' : fieldname,
							'show_appendix_tables': False,
						},
						context_instance=RequestContext(request)
					)
				except: # We have no expected exceptions for this code block
					page = None
					message = ERROR_MESSAGE
					#raise
			
			if response is None:
				response = render_to_response(
						'tabbed_view_table_ndsu.html',
						{
							'home_url': HOME_URL,
							'hidden_zipcode_form': hidden_zipcode_form,
							'zipcode_get_string': '?%s' % (urlencode( [('zipcode', zipcode)] )),
							'zipcode': zipcode,
							'scope_get_string': '&%s' % (urlencode( [('scope', scope)] )),
							'scope': scope,
							'not_location_get_string': '&%s' % (urlencode([('not_location', l) for l in not_locations])),
							'not_locations': not_locations,
							'variety_get_string': '&%s' % (urlencode([('variety', v) for v in varieties])),
							'varieties': varieties,
							'year_get_string': '&%s' % (urlencode( [('year', curyear)] )),
							'year_url_bit': year_url_bit,
							'curyear': curyear,
							'maxyear': maxyear,
							'page': page,
							'message': message,
							'years': years,
							'blurbs' : unit_blurbs,
							'curfield' : fieldname,
							'show_appendix_tables': False,
						},
						context_instance=RequestContext(request)
					)
			
			# TODO: is python's refcount/garbage collection enough?
			"""
			if page is not None:
				page.clear() # clear references
				page = None
			#"""
				
			return response
Beispiel #2
0
import os; os.environ.setdefault("DJANGO_SETTINGS_MODULE", "variety_trials_website.settings"); from variety_trials_data.variety_trials_util import get_page; p = get_page("55108", "NEAR", 2012, "bushels_acre", ""); print p
def historical_zipcode_view(request,
                            startyear,
                            fieldname,
                            abtest=None,
                            years=None,
                            year_url_bit=None,
                            locations=None,
                            year_range=3):
    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']
            not_locations = zipcode_radius_form.cleaned_data['not_location']
            varieties = zipcode_radius_form.cleaned_data['variety']
            yearname = zipcode_radius_form.cleaned_data['year']

            hidden_zipcode_form = variety_trials_forms.SelectLocationByZipcodeForm(
                initial={
                    'zipcode': zipcode,
                    'scope': scope,
                    'not_location': not_locations,
                    'year': yearname,
                })
            try:
                maxyear = int(startyear)
            except ValueError:
                maxyear = datetime.date.today().year

            try:
                curyear = int(yearname)
            except ValueError:
                # TODO: redirect to /view/curyear/field/?... instead
                curyear = maxyear

            if curyear > maxyear:
                curyear = maxyear

            if year_url_bit is None:
                year_url_bit = startyear

            if years is None:
                years = [maxyear - diff for diff in range(year_range)]

            try:
                page = get_page(zipcode,
                                scope,
                                curyear,
                                fieldname,
                                year_url_bit,
                                not_locations=not_locations,
                                varieties=varieties,
                                year_range=year_range,
                                locations=locations)
            except models.Zipcode.DoesNotExist:
                zipcode_radius_form = variety_trials_forms.SelectLocationByZipcodeForm(
                    initial={
                        #'radius': zipcode_radius_form.cleaned_data['search_radius'],
                    })
                # TODO: repopulate form
                return HttpResponseRedirect("%s%s" %
                                            (HOME_URL, '/?error=bad_zipcode'))
            except (LSDProbabilityOutOfRange, TooFewDegreesOfFreedom,
                    NotEnoughDataInYear) as error:
                page = None
                message = " ".join([ERROR_MESSAGE, error.message])
                raise
            except:
                page = None
                message = ERROR_MESSAGE
                raise
            """
			import sys
			for table in page.tables:
				for variety, row in table.rows.items():
					sys.stdout.write('['+variety.name+'\n')
					for cell in row:
						sys.stdout.write('\t'+unicode(cell))
					sys.stdout.write(']\n')
				print table.columns
			#"""
            response = None
            if page is not None:
                try:
                    response = render(
                        request, 'tabbed_view_table_ndsu.html', {
                            'home_url':
                            HOME_URL,
                            'hidden_zipcode_form':
                            hidden_zipcode_form,
                            'zipcode_get_string':
                            '?%s' % (urlencode([('zipcode', zipcode)])),
                            'zipcode':
                            zipcode,
                            'scope_get_string':
                            '&%s' % (urlencode([('scope', scope)])),
                            'scope':
                            scope,
                            'not_location_get_string':
                            '&%s' % (urlencode([('not_location', l)
                                                for l in not_locations])),
                            'not_locations':
                            not_locations,
                            'variety_get_string':
                            '&%s' % (urlencode([('variety', v)
                                                for v in varieties])),
                            'varieties':
                            varieties,
                            'year_get_string':
                            '&%s' % (urlencode([('year', curyear)])),
                            'year_url_bit':
                            year_url_bit,
                            'curyear':
                            curyear,
                            'maxyear':
                            maxyear,
                            'page':
                            page,
                            'message':
                            None,
                            'years':
                            years,
                            'blurbs':
                            unit_blurbs,
                            'curfield':
                            fieldname,
                            'show_appendix_tables':
                            False,
                        })
                except:  # We have no expected exceptions for this code block
                    page = None
                    message = ERROR_MESSAGE
                    raise

            if response is None:
                response = render(
                    request, 'tabbed_view_table_ndsu.html', {
                        'home_url':
                        HOME_URL,
                        'hidden_zipcode_form':
                        hidden_zipcode_form,
                        'zipcode_get_string':
                        '?%s' % (urlencode([('zipcode', zipcode)])),
                        'zipcode':
                        zipcode,
                        'scope_get_string':
                        '&%s' % (urlencode([('scope', scope)])),
                        'scope':
                        scope,
                        'not_location_get_string':
                        '&%s' % (urlencode([('not_location', l)
                                            for l in not_locations])),
                        'not_locations':
                        not_locations,
                        'variety_get_string':
                        '&%s' % (urlencode([('variety', v)
                                            for v in varieties])),
                        'varieties':
                        varieties,
                        'year_get_string':
                        '&%s' % (urlencode([('year', curyear)])),
                        'year_url_bit':
                        year_url_bit,
                        'curyear':
                        curyear,
                        'maxyear':
                        maxyear,
                        'page':
                        page,
                        'message':
                        message,
                        'years':
                        years,
                        'blurbs':
                        unit_blurbs,
                        'curfield':
                        fieldname,
                        'show_appendix_tables':
                        False,
                    })

            # TODO: is python's refcount/garbage collection enough?
            """
			if page is not None:
				page.clear() # clear references
				page = None
			#"""

            return response
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE",
                      "variety_trials_website.settings")
from variety_trials_data.variety_trials_util import get_page
p = get_page("55108", "NEAR", 2012, "bushels_acre", "")
print p