diff --git a/bloclink/apps/bis/views.py b/bloclink/apps/bis/views.py index 737dc82e3ba842b9039ba29c0287ca74c200f777..ebb7c15c26a89cbeae0917aba3273dfbc54a4bdd 100644 --- a/bloclink/apps/bis/views.py +++ b/bloclink/apps/bis/views.py @@ -10,6 +10,7 @@ from django.forms.models import model_to_dict from django.template.loader import render_to_string from django.core.mail import EmailMessage import re +from uszipcode import SearchEngine import boto3 from bpeng.bis.report import generate_pns_report @@ -26,42 +27,51 @@ class SubmitBuilding(View): # Get Survey ID from entered address address = put['address'].split(",") - if (len(address) > 3): - country = address[-1] - zip = int(address[-2].split(" ")[-1]) - if country == " United States": - if 10001 <= zip <= 11697: - surveyId = int(SurveyAreas.objects.using('bis').filter(area_name='NYC')[0].id) - elif 94501 <= zip <= 94705: - surveyId = int(SurveyAreas.objects.using('bis').filter(area_name='Oakland')[0].id) - elif 53201 <= zip <= 53295: - surveyId = int(SurveyAreas.objects.using('bis').filter(area_name='Milwaukee')[0].id) - else: - return JsonResponse({ - 'success': False, - 'message': 'Sorry! Area not covered at the moment.' - }, status=400) - else: + nyc_area = ['New York County', 'Kings County', 'Queens County', 'Bronx County', 'Richmond County'] + if len(address) <= 3: + return JsonResponse({ + 'success': False, + 'message': 'Please enter street address.' + }, status=400) + + zip = address[-2].split(" ")[-1] + if not (zip.isnumeric() and address[-1] == " United States"): return JsonResponse({ - 'success': False, - 'message': 'Please enter street address.' + 'success': False, + 'message': 'Please enter proper address.' }, status=400) - - data['surveyId'] = surveyId - Id_array = list(Surveys.objects.using('bis').filter(survey_area_id=surveyId)) + + search = SearchEngine(simple_zipcode=True) + zipcode = search.by_zipcode(zip) + surveyAreas = SurveyAreas.objects.using('bis') + + area_name = '' + if zipcode.county in nyc_area and zipcode.state == 'NY': + area_name='NYC' + elif zipcode.major_city == 'Oakland' and zipcode.state == 'CA': + area_name='Oakland' + elif zipcode.major_city == 'Milwaukee' and zipcode.state == 'WI': + area_name='Milwaukee' + + if area_name == '': + return JsonResponse({ + 'success': False, + 'message': 'Sorry! Area not covered at the moment.' + }, status=400) + + data['surveyId'] = int(surveyAreas.filter(area_name=area_name)[0].id) + Id_array = list(Surveys.objects.using('bis').filter(survey_area_id=data['surveyId'])) for Id in Id_array: - queID = Id.que_id - ansId = Id.ans_id + queIDs = Id.que_id + ansIds = Id.ans_id - for j in queID: - questions = Questions.objects.using('bis').filter(id=j) - for question in questions: - data['questions'][question.id] = question.question + questions = Questions.objects.using('bis').filter(id__in=queIDs) + for question in questions: + data['questions'][question.id] = question.question - for k in ansId: - answers = Answers.objects.using('bis').filter(id=k) - for answer in answers: - data['answers'][answer.id] = answer.answer + answers = Answers.objects.using('bis').filter(id__in=ansIds) + for answer in answers: + data['answers'][answer.id] = answer.answer # Get buildingId from the address try: diff --git a/requirements.txt b/requirements.txt index 5cccf6da0f7f3e29981cf4187d727c0f93a53d47..82b1092cbbc5074a08eefdeea03fe9958eeaf3e9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,3 +13,4 @@ python-pptx==0.6.18 django-cors-headers==2.1.0 djangorestframework==3.10.2 boto3==1.9.214 +uszipcode==0.2.4