def test_get_postal_code(self): self.assertEquals(Transaction.get_postal_code(address="144 Jalan Bukit Merah"), None) self.assertEquals(Transaction.get_postal_code(address="146 Simei Street 2"), None) self.assertEquals(Transaction.get_postal_code(address="10 Telok Blangah Crescent"), "090010") self.assertEquals(Transaction.get_postal_code(address="1 Haig Road"), "430001") self.assertEquals(Transaction.get_postal_code(name="Aa Centre", address="River Valley Road"), "238366") self.assertEquals(Transaction.get_postal_code(name="283 Studio", address="River Valley Road"), "238324")
def process(): transactions = Transaction.objects.filter(postal_code__isnull=True) dict = {} for trans in transactions: if trans.address in dict.keys(): trans.postal_code = dict[trans.address] else: trans.postal_code = Transaction.get_postal_code( name=trans.name, address=trans.address) dict[trans.address] = trans.postal_code if trans.postal_code: print "{0} - {1} - {2}".format(trans.name, trans.address, trans.postal_code)
def transaction_list(request, template="transaction_list.html"): """ View to display transaction list and related charts. """ MAX_LENGTH = 50 if not request.POST or request.GET: transactions = Transaction.objects.all() chart = {'by_itself': Chart.chart_retrieve(transactions)} result_count = len(transactions) if len(transactions) > MAX_LENGTH: transactions = transactions[:MAX_LENGTH] return render( request, template, { 'transactions': transactions, 'result_count': result_count, 'chart': chart, 'filter_form': FilterForm(), 'chart_form': ChartFilterForm() }) else: # Handle the POST request type = request.POST['type'] name = camelcase(request.POST['name']) postal_code = request.POST['postal_code'] address = camelcase(request.POST['address']) room_count = request.POST['room_count'] transactions = Transaction.get_transactions(type=type, room_count=room_count) # Refine request: name <--> address <--> postal_code temp = Transaction.get_transactions(transactions, name=name, postal_code=postal_code, address=address) if address == "": address = Transaction.get_address(name=name, postal_code=postal_code) if postal_code == "": postal_code = Transaction.get_postal_code(name=name, address=address) print("name = {0}, address = {1}, postal_code = {2}".format( name, address, postal_code)) chart = {} # Handle chart series chart_series = request.POST.getlist('series') if Chart.ITSELF in chart_series: chart['by_itself'] = Chart.chart_retrieve(temp) if Chart.NEIGHBOR_POSTALCODE in chart_series: chart['by_postalcode'] = Chart.chart_by_neighbor_postal_code( transactions, postal_code) if Chart.NEIGHBOR_ADDRESS in chart_series: chart['by_address'] = Chart.chart_by_neighbor_address( transactions, address) # Handle displayed list display_list = request.POST['list'] if display_list == Chart.ITSELF: transactions = temp elif display_list == Chart.NEIGHBOR_POSTALCODE: transactions = Chart.get_transactions_by_neighbor_postal_code( transactions, postal_code) else: transactions = Chart.get_transactions_by_neighbor_address( transactions, address, include=True) result_count = len(transactions) if len(transactions) > MAX_LENGTH: transactions = transactions[:MAX_LENGTH] return render( request, template, { 'transactions': transactions, 'result_count': result_count, 'filter_form': FilterForm(request.POST), 'chart_form': ChartFilterForm(request.POST), 'chart': chart })