Skip to content

Commit

Permalink
now the app can get the lat and log and store in the db
Browse files Browse the repository at this point in the history
  • Loading branch information
amaravindhan committed Nov 5, 2020
1 parent 601c74e commit fe48582
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
media/
**/__pycache__
**/migrations
**/migrations
completeApp.log
Binary file modified property/__pycache__/models.cpython-36.pyc
Binary file not shown.
Binary file modified property/__pycache__/utils.cpython-36.pyc
Binary file not shown.
39 changes: 39 additions & 0 deletions property/locationApi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import googlemaps, logging
from django.conf import settings

gmapsClient = googlemaps.Client(key=settings.GOOGLE_MAPS_API_KEY)

logging.basicConfig(level=logging.DEBUG, filename='completeApp.log', format='%(asctime)s - %(name)s - %(process)d - %(levelname)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')

# logging.warning('This will get logged to a file')
# logging.debug('This will get logged to a file')
# logging.info('This will get logged to a file')
# logging.error('This will get logged to a file')
# logging.critical('This will get logged to a file')

def get_lat_long_from_address(address):
""" function to get lat and long based on property address"""
status = True
placeId = ""
locationType = ""
location = {}

try:
geocodeResult = gmapsClient.geocode(address=address)
if geocodeResult:
geometry = geocodeResult[0].get('geometry', None)
placeId = geocodeResult[0].get('place_id', None)
locationType = geometry.get('location_type', None)
location = geometry.get('location', None)
if locationType == 'APPROXIMATE':
logging.warning('Could not fetch exact address for - {}.'.format(address))
status = False
else:
logging.warning('Geocode returned empty for the address - {}.'.format(address))
status = False
except Exception as e:
print(e)
logging.error('Error occurred when fetching geocode api. Address is - {} and error is - {}'.format(address, e))
status = False

return placeId, locationType, location, status
18 changes: 17 additions & 1 deletion property/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
from django.core.exceptions import ValidationError
from django.dispatch import receiver
from django.db.models.signals import pre_save, post_delete
from django.contrib.gis.geos import fromstr

import datetime, os

from users.models import UserLandLord, UserStudent
from .utils import unique_slug_generator, unique_file_path_generator
from .locationApi import *


# Create your models here.
Expand Down Expand Up @@ -149,7 +151,21 @@ def clean(self):
errorMess['toDate'] = ValidationError(('To Date cannot be less than or equal to From Date.'), code='error')

if self.address:
pass
fullAddress = '{}, {} {}'.format(self.address, self.city, self.zipcode)
placeId, locationType, location, status = get_lat_long_from_address(fullAddress)
# print(placeId, locationType, location, status)
if status:
if placeId:
self.placeId = placeId
if locationType:
self.locationType = locationType
if location:
longitude = location.get('lng', 0)
latitude = location.get('lat', 0)
self.location = fromstr(f'POINT({longitude} {latitude})', srid=4326)
else:
hasError = True
errorMess['address'] = ValidationError(('We are unable to locate the exact location. Please enter the address correctly.'), code='error')

if hasError:
raise ValidationError(errorMess)
Expand Down
34 changes: 34 additions & 0 deletions property/templates/property/properties.html
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,40 @@ <h2 class="pxp-content-side-h2">{{total_count}} Results</h2>
document.addEventListener("DOMContentLoaded", setValue2);
range2.addEventListener('input', setValue2);

</script>
<script>

var
range3 = document.getElementById('range3'),
rangeV3 = document.getElementById('rangeV3'),
setValue = ()=>{
var
newValue3 = Number( (range3.value - range3.min) * 100 / (range3.max - range3.min) ),
newPosition3 = 10 - (newValue3 * 0.2);
rangeV3.innerHTML = `<span>${range3.value} Mile</span>`;
rangeV3.style.left = `calc(${newValue3}% + (${newPosition3}px))`;
};
document.addEventListener("DOMContentLoaded", setValue);
range3.addEventListener('input', setValue);

</script>


<script>

var
range4 = document.getElementById('range4'),
rangeV4 = document.getElementById('rangeV4'),
setValue2 = ()=>{
var
newValue4 = Number( (range4.value - range4.min) * 100 / (range4.max - range4.min) ),
newPosition4 = 10 - (newValue4 * 0.2);
rangeV4.innerHTML = `<span>${range4.value} Mile</span>`;
rangeV4.style.left = `calc(${newValue4}% + (${newPosition4}px))`;
};
document.addEventListener("DOMContentLoaded", setValue2);
range4.addEventListener('input', setValue2);

</script>

<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=AIzaSyDIKVsUE3OpqZ7o5oun4HHrVG1X6eGKUec&sensor=false&v=3&libraries=places"></script>
Expand Down
3 changes: 0 additions & 3 deletions property/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,3 @@ def landlordAccessTest(user):
return True
except:
raise Http404

def get_lat_long_from_address(address, city):
pass

0 comments on commit fe48582

Please sign in to comment.