123456789101112131415161718192021222324252627282930313233343536373839 |
- import datetime
- from django.utils import timezone
- from django.db.models import Max
- from .models import YelpReview, YelpLocation
- def date_string2timezone(date):
- month, day, year = map(int, date.split('/'))
- date = datetime.datetime(day=day, month=month, year=year)
- time_zone = timezone.make_aware(date)
- return time_zone
- def get_max_date(yelp_location):
- max_date = yelp_location.yelpreview_set.all()\
- .aggregate(Max('date_posted'))['date_posted__max']
- return max_date if max_date is not None else date_string2timezone('7/2/1995')
- def store_into_database(reviews, location):
- for rev in reviews:
- name = rev.get('name')
- profile = rev.get('profile')
- rating = rev.get('rating')
- date_posted = rev.get('date_posted')
- comment = rev.get('comment')
- # store into database
- obj, created = YelpReview.objects.update_or_create(
- reviewer_name=name,
- profile=profile,
- rating=rating,
- date_posted=date_posted,
- comment=comment,
- location=location
- )
- if created:
- print(f'A new review object has been created for {location}!')
|