Browse Source

Remove a function and and handle all errors that occure when absence of this function

Mohidul Islam 5 years ago
parent
commit
01b97b15af
3 changed files with 13 additions and 33 deletions
  1. 6 26
      gauth/location_utils.py
  2. 6 6
      review/review_utils.py
  3. 1 1
      review_automation/settings/dev.py

+ 6 - 26
gauth/location_utils.py

@@ -1,41 +1,21 @@
 from requests import get
 from .models import Location
-from django.conf import settings
+from gauth.auth_utils import get_auth_header
 from .auth_utils import get_gmb_id
 
-ACCESS_TOKEN_URI = settings.HOST_URI + '/token'
-
-
-def get_all_location_ids():
-    locations = Location.objects.only('location_id')
-    ids = [loc.location_id for loc in locations]
-    '''
-    There is a location which we don't need to add into database.
-    It is a Medical Billing Service, That's why it is nothing to do with review. 
-    '''
-    if '5397588228065547694' in ids:
-        ids.remove('5397588228065547694')
-    return ids
-
 
 def populate_locations():
-    access_token = get(ACCESS_TOKEN_URI).text
+    headers = get_auth_header()
     user, gmb_id = get_gmb_id()
     url = 'https://mybusiness.googleapis.com/v4/accounts/' + gmb_id + '/locations'
-    headers = {
-        'authorization': 'Bearer ' + access_token,
-        'content-type': 'application/json'
-    }
     res = get(url, headers=headers).json()
     locations = res['locations']
     for loc in locations:
         # loc['name'] = 'accounts/103266181421855655295/locations/8916258876770296726'
         loc_id = loc['name'].split('/')[-1]
-        location_ids = get_all_location_ids()
-        location_ids.append('5397588228065547694')
-        if loc_id in location_ids:
-            continue
         loc_name = loc['address']['locality']
         loc_website = loc['websiteUrl']
-        location = Location.objects.create(location_id=loc_id, location_name=loc_name, website_url=loc_website, owner=user)
-        location.save()
+        location, created = Location.objects.update_or_create(location_id=loc_id, location_name=loc_name,
+                                                              website_url=loc_website, owner=user)
+        if created:
+            print('A new location has been created!')

+ 6 - 6
review/review_utils.py

@@ -1,7 +1,6 @@
 import json
 from requests import get, put
 from gauth.auth_utils import get_gmb_id, get_auth_header
-from gauth.location_utils import get_all_location_ids
 from .models import Review, Reply
 from gauth.models import Location
 
@@ -108,12 +107,12 @@ def update_location_data(loc_id, average_rating, total_reviews, total_reviews_db
     loc.save()
 
 
-def fetch_last_20_reviews(loc_id):
+def fetch_last_20_reviews(loc_id, page_size=20):
     headers = get_auth_header()
-    url = get_review_list_url(loc_id)+'&pageSize=20'
+    url = get_review_list_url(loc_id)+'&pageSize='+str(page_size)
     res = get(url, headers=headers)
     data = res.json()
-    reviews = data['reviews']
+    reviews = data.get('reviews')
     average_rating = data.get('averageRating')
     total_reviews = data.get('totalReviewCount')
     if len(reviews) > 0:
@@ -124,8 +123,9 @@ def fetch_last_20_reviews(loc_id):
 
 def populate_reviews():
     start = timezone.now()
-    locations = get_all_location_ids()
-    for loc_id in locations:
+    locations = Location.objects.all().values('location_id')
+    for loc in locations:
+        loc_id = loc.get('location_id')
         fetch_last_20_reviews(loc_id)
     end = timezone.now()
     elapsed = end - start

+ 1 - 1
review_automation/settings/dev.py

@@ -13,7 +13,7 @@ DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.mysql',
         'NAME': 'review_automation',
-        'USER': 'bytetrek',
+        'USER': 'root',
         'PASSWORD': 'sad2002S1',
         'HOST': 'localhost',
         'PORT': '3306',