def reload_from_analytics(cls): """Update the stats from Google Analytics.""" from sumo import googleanalytics counts = googleanalytics.pageviews_by_question(settings.GA_START_DATE, date.today()) if counts: for question_id, visits in counts.iteritems(): # We are trying to minimize db calls here. Let's try to update # first, that will be the common case. num = cls.objects.filter(question_id=question_id).update( visits=visits) # If we were able to update, we are done. if num > 0: continue # If it doesn't exist yet, create it. try: cls.objects.create(question_id=question_id, visits=visits) except IntegrityError: # The question doesn't exist anymore, move on. continue else: log.warning('Google Analytics returned no interesting data,' ' so I kept what I had.')
def reload_from_analytics(cls): """Update the stats from Google Analytics.""" from sumo import googleanalytics counts = googleanalytics.pageviews_by_question( settings.GA_START_DATE, date.today()) if counts: for question_id, visits in counts.iteritems(): # We are trying to minimize db calls here. Let's try to update # first, that will be the common case. num = cls.objects.filter( question_id=question_id).update(visits=visits) # If we were able to update, we are done. if num > 0: continue # If it doesn't exist yet, create it. try: cls.objects.create( question_id=question_id, visits=visits) except IntegrityError: # The question doesn't exist anymore, move on. continue else: log.warning('Google Analytics returned no interesting data,' ' so I kept what I had.')
def test_pageviews_by_question(self, _build_request): """Test googleanalytics.pageviews_by_question().""" execute = _build_request.return_value.get.return_value.execute execute.return_value = PAGEVIEWS_BY_QUESTION_RESPONSE pageviews = googleanalytics.pageviews_by_question( date(2013, 01, 16), date(2013, 01, 16)) eq_(3, len(pageviews)) eq_(3, pageviews[1]) eq_(2, pageviews[2]) eq_(11, pageviews[3])