def setUp(self): assert self.client.login(email="*****@*****.**", roles=[roles.TRAFFIC_LOG_ADMIN]) author = User(email='test') author.save() self.author = author spot = models.Spot(title='Legal ID', type='Station ID', author=author) self.spot = spot spot.put() self.now = time_util.chicago_now() self.today = self.now.date() self.dow = self.today.isoweekday() constraint = self.add_spot_to_constraint(spot) self.constraint = constraint spot_copy = models.SpotCopy(body='You are listening to chirpradio.org', spot=spot, author=author) self.spot_copy = spot_copy spot_copy.put() spot.random_spot_copies = [spot_copy.key()] spot.save() logged_spot = models.TrafficLogEntry(log_date=self.today, spot=spot_copy.spot, spot_copy=spot_copy, dow=self.dow, hour=self.now.hour, slot=0, scheduled=constraint, readtime=time_util.chicago_now(), reader=author) logged_spot.put()
def finishReadingSpotCopy(request, spot_copy_key=None): dow, hour, slot = _get_slot_from_request(request) # Check if a single spot constraint exists for the dow, hour, slot. q = (models.SpotConstraint.all().filter("dow =", dow).filter( "hour =", hour).filter("slot =", slot)) count = AutoRetry(q).count(1) if count == 0: raise ValueError( "No spot constraint found for dow=%r, hour=%r, slot=%r" % (dow, hour, slot)) elif count > 1: # kumar: not sure if this will actually happen raise ValueError( "Multiple spot constraints found for dow=%r, hour=%r, slot=%r" % (dow, hour, slot)) constraint = AutoRetry(q).fetch(1)[0] spot_copy = AutoRetry(models.SpotCopy).get(spot_copy_key) # Check if spot has already been read (i.e., logged). today = time_util.chicago_now().date() q = (models.TrafficLogEntry.all().filter("log_date =", today).filter( "spot =", spot_copy.spot).filter("dow =", dow).filter("hour =", hour).filter("slot =", slot)) if AutoRetry(q).count(1): existing_logged_spot = AutoRetry(q).fetch(1)[0] raise RuntimeError( "This spot %r at %r has already been read %s" % (spot_copy.spot, constraint, existing_logged_spot.reader)) # Remove spot copy from the spot's list. spot_copy.spot.finish_spot_copy() # Log spot read. logged_spot = models.TrafficLogEntry(log_date=today, spot=spot_copy.spot, spot_copy=spot_copy, dow=dow, hour=hour, slot=slot, scheduled=constraint, readtime=time_util.chicago_now(), reader=auth.get_current_user(request)) AutoRetry(logged_spot).put() return { 'spot_copy_key': str(spot_copy.key()), 'spot_constraint_key': str(constraint.key()), 'logged_spot': str(logged_spot.key()) }
def test_filter_by_type(self): # Make another type of spot: spot = models.Spot(title='PSA', type='Live Read PSA', author=self.author) spot.put() constraint = self.add_spot_to_constraint(spot) spot_copy = models.SpotCopy( body='Save the children from bad music. Listen to CHIRP', spot=spot, author=self.author) spot_copy.put() today = self.now.date() current_hour = self.now.hour hour = current_hour logged_spot = models.TrafficLogEntry(log_date=self.today, spot=spot_copy.spot, spot_copy=spot_copy, dow=self.dow, hour=self.now.hour, slot=0, scheduled=constraint, readtime=time_util.chicago_now(), reader=self.author) logged_spot.put() from_date = datetime.date.today() - timedelta(days=1) to_date = datetime.date.today() + timedelta(days=1) params = { 'start_date': from_date.strftime("%Y-%m-%d"), 'end_date': to_date.strftime("%Y-%m-%d"), # Live Read PSA: 'type': constants.SPOT_TYPE_CHOICES[3], 'underwriter': '', 'download': 'Download' } response = self.get_job_product('build-trafficlog-report', params) report = csv.reader(StringIO(response.content)) header = report.next() row = report.next() self.assertEquals(row[4], spot.title) self.assertEquals(row[5], spot.type) self.assertEquals(row[6], spot_copy.body)
def test_filter_by_underwriter(self): # Make another type of spot: spot = models.Spot(title='PSA', type='Live Read PSA', author=self.author) spot.put() constraint = self.add_spot_to_constraint(spot) spot_copy = models.SpotCopy( body='Pst, Reckless Records has killer hip hop in the cutout bin', spot=spot, author=self.author, underwriter='reckless') spot_copy.put() today = self.now.date() current_hour = self.now.hour hour = current_hour logged_spot = models.TrafficLogEntry(log_date=self.today, spot=spot_copy.spot, spot_copy=spot_copy, dow=self.dow, hour=self.now.hour, slot=0, scheduled=constraint, readtime=time_util.chicago_now(), reader=self.author) logged_spot.put() from_date = datetime.date.today() - timedelta(days=1) to_date = datetime.date.today() + timedelta(days=1) params = { 'start_date': from_date.strftime("%Y-%m-%d"), 'end_date': to_date.strftime("%Y-%m-%d"), # All: 'type': constants.SPOT_TYPE_CHOICES[0], 'underwriter': 'reckless', 'download': 'Download' } response = self.get_job_product('build-trafficlog-report', params) report = csv.reader(StringIO(response.content)) header = report.next() underwriters = set([row[3] for row in report]) self.assertEquals(underwriters, set(['reckless']))
def test_many_spots(self): raise SkipTest('Something in DB sorting probably broke this test') copy = [] for i in range(65): txt = 'PSA %s' % i copy.append(txt) spot_copy = models.SpotCopy(body=txt, spot=self.spot, author=self.author) spot_copy.put() logged_spot = models.TrafficLogEntry( log_date=self.today, spot=spot_copy.spot, spot_copy=spot_copy, dow=self.dow, hour=self.now.hour, slot=0, scheduled=self.constraint, readtime=time_util.chicago_now() - timedelta(days=1), reader=self.author) logged_spot.put() from_date = datetime.date.today() - timedelta(days=30) to_date = datetime.date.today() params = { 'start_date': from_date.strftime("%Y-%m-%d"), 'end_date': to_date.strftime("%Y-%m-%d"), # All: 'type': constants.SPOT_TYPE_CHOICES[0], 'underwriter': '', 'download': 'Download' } response = self.get_job_product('build-trafficlog-report', params) report = csv.reader(StringIO(response.content)) header = report.next() self.assertEquals([r[6] for r in report], ['You are listening to chirpradio.org'] + copy)
def getOrCreateTrafficLogEntry(date, hour, slot): entry = models.TrafficLogEntry.gql( "where log_date = :1 and hour = :2 and slot = :3", date, hour, slot) if AutoRetry(entry).count(): return AutoRetry(entry).get() else: constraint = models.SpotConstraint.gql( "where dow = :1 and hour = :2 and slot = :3", date.isoweekday(), hour, slot) if AutoRetry(constraint).count(): constraint = AutoRetry(constraint).get() spot = randomSpot(constraint.spots) if spot: new_entry = models.TrafficLogEntry(log_date=date, spot=spot, hour=hour, slot=slot, scheduled=constraint) AutoRetry(new_entry).put() return new_entry else: return