utils.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import datetime
  2. from django.utils import timezone
  3. from django.db.models import Max
  4. from .models import YelpReview, YelpLocation
  5. def date_string2timezone(date):
  6. month, day, year = map(int, date.split('/'))
  7. date = datetime.datetime(day=day, month=month, year=year)
  8. time_zone = timezone.make_aware(date)
  9. return time_zone
  10. def get_max_date(yelp_location):
  11. max_date = yelp_location.yelpreview_set.all()\
  12. .aggregate(Max('date_posted'))['date_posted__max']
  13. return max_date if max_date is not None else date_string2timezone('7/2/1995')
  14. def store_into_database(reviews, location):
  15. for rev in reviews:
  16. name = rev.get('name')
  17. profile = rev.get('profile')
  18. rating = rev.get('rating')
  19. date_posted = rev.get('date_posted')
  20. comment = rev.get('comment')
  21. # store into database
  22. obj, created = YelpReview.objects.update_or_create(
  23. reviewer_name=name,
  24. profile=profile,
  25. rating=rating,
  26. date_posted=date_posted,
  27. comment=comment,
  28. location=location
  29. )
  30. if created:
  31. print(f'A new review object has been created for {location}!')