Пример #1
0
    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()
Пример #2
0
    def test_view_spot_for_reading_basic(self):
        author = User(email='test')
        author.save()
        spot = models.Spot(title='Legal ID', type='Station ID')
        spot.put()
        constraint = models.SpotConstraint(dow=1,
                                           hour=0,
                                           slot=0,
                                           spots=[spot.key()])
        constraint.put()

        spot_copy = models.SpotCopy(body='You are listening to chirpradio.org',
                                    spot=spot,
                                    author=author)
        spot_copy.put()

        resp = self.client.get(
            reverse('traffic_log.spotTextForReading', args=(spot.key(), )), {
                'hour': constraint.hour,
                'dow': constraint.dow,
                'slot': constraint.slot
            })
        context = resp.context
        self.assertEqual(context['spot_copy'].body,
                         'You are listening to chirpradio.org')
        self.assertEqual(
            context['url_to_finish_spot'],
            "/traffic_log/spot-copy/%s/finish?hour=0&dow=1&slot=0" %
            spot_copy.key())
Пример #3
0
    def test_spot_copy(self):
        author = User(email='test')
        author.save()
        spot = models.Spot(title='Legal ID', type='Station ID')
        spot.put()
        self.assertEqual(
            spot.get_add_copy_url(),
            reverse('traffic_log.views.addCopyForSpot', args=(spot.key(), )))

        constraint = models.SpotConstraint(dow=1,
                                           hour=0,
                                           slot=0,
                                           spots=[spot.key()])
        constraint.put()

        spot_copy = models.SpotCopy(
            body=('You are now and forever listening to a killer '
                  'radio station called chirpradio.org'),
            spot=spot,
            author=author)
        spot_copy.put()

        self.assertEqual(
            str(spot_copy),
            "You are now and forever listening to a killer radio...")
        self.assertEqual(
            spot_copy.get_edit_url(),
            reverse('traffic_log.editSpotCopy', args=(spot_copy.key(), )))
        self.assertEqual(
            spot_copy.get_delete_url(),
            reverse('traffic_log.deleteSpotCopy', args=(spot_copy.key(), )))
Пример #4
0
    def test_landing_page_shows_spots(self):
        user = User(email='test')
        user.save()
        spot = models.Spot(title='Legal ID', type='Station ID')
        spot_key = spot.put()
        # assign it to every day of the week at the top of the hour:
        constraint_keys = views.saveConstraint(
            dict(hour_list=range(0, 24), dow_list=range(1, 8), slot=0))
        views.connectConstraintsAndSpot(constraint_keys, spot_key)

        spot_copy = models.SpotCopy(body='body', spot=spot, author=user)
        spot_copy.put()

        resp = self.client.get(reverse('traffic_log.index'))
        context = resp.context[0]
        spot_map = {}
        constraint_map = {}
        for c in context['slotted_spots']:
            spot_map[c.hour] = list(c.iter_spots())[0]
            constraint_map[c.hour] = c

        now = time_util.chicago_now()

        # first hour:
        self.assertEqual(spot_map[now.hour].title, 'Legal ID')
        # second hour:
        self.assertEqual(
            spot_map[(now + datetime.timedelta(hours=1)).hour].title,
            'Legal ID')
Пример #5
0
    def test_delete_spot_copy(self):
        spot = models.Spot(title='Legal ID', type='Station ID')
        spot.put()
        dow = 1
        hour = 0
        slot = 0
        constraint = models.SpotConstraint(dow=dow,
                                           hour=hour,
                                           slot=slot,
                                           spots=[spot.key()])
        constraint.put()

        author = User(email='test')
        author.put()
        spot_copy = models.SpotCopy(body='First', spot=spot, author=author)
        spot_copy.put()

        self.assertEqual(spot.get_spot_copy(dow, hour, slot)[0].body, "First")

        # now edit the second one:
        resp = self.client.get(
            reverse('traffic_log.deleteSpotCopy', args=(spot_copy.key(), )))

        self.assertEqual([c for c in spot.all_spot_copy()], [])

        self.assertEqual(spot.get_spot_copy(dow, hour, slot), (None, False))
Пример #6
0
    def test_make_spot_copy_expire(self):
        spot = models.Spot(title='Legal ID', type='Station ID')
        spot.put()
        constraint = models.SpotConstraint(dow=1,
                                           hour=0,
                                           slot=0,
                                           spots=[spot.key()])
        constraint.put()

        author = User(email='test')
        author.put()
        spot_copy = models.SpotCopy(body='First', spot=spot, author=author)
        spot_copy.put()

        resp = self.client.post(
            reverse('traffic_log.editSpotCopy', args=(spot_copy.key(), )),
            {
                'spot_key': spot.key(),
                'body': 'Something else entirely',
                'underwriter': 'another underwriter',
                'expire_on': '2/5/2010'  # any date in the past
            })
        self.assertNoFormErrors(resp)

        spot_copy = [c for c in spot.all_spot_copy()]
        self.assertEqual(spot_copy, [])
Пример #7
0
    def test_create_irregular_spot(self, time_util, global_time_util):
        for obj in [time_util, global_time_util]:
            obj.provides('chicago_now').returns(
                datetime.datetime.strptime('2011-09-14 05:10',
                                           '%Y-%m-%d %H:%M'))
        resp = self.client.post(
            reverse('traffic_log.createSpot'), {
                'title': 'Legal ID',
                'type': 'Station ID',
                'hour_list': ['2', '5', '7', '13', '23'],
                'dow_list': ['1', '3', '7'],
                'slot': '24',
            })
        self.assertNoFormErrors(resp)
        spot = models.Spot.all().filter("title =", "Legal ID").fetch(1)[0]
        dow = set()
        hours = set()
        constraint_map = {}
        for constraint in spot.constraints:
            dow.add(constraint.dow)
            hours.add(constraint.hour)
            constraint_map[(constraint.dow, constraint.hour,
                            constraint.slot)] = constraint
        self.assertEqual(dow, set([1L, 3L, 7L]))
        self.assertEqual(hours, set([2L, 5L, 7L, 13L, 23L]))

        # Check with Wednesday at 5:24am
        author = User(email='test')
        author.put()
        spot_copy = models.SpotCopy(body='body', spot=spot, author=author)
        spot_copy.put()
        spot.random_spot_copies = [spot_copy.key()]
        spot.save()

        self.assertEqual(
            constraint_map[(3L, 5L, 24L)].url_to_finish_spot(spot),
            "/traffic_log/spot-copy/%s/finish?hour=5&dow=3&slot=24" %
            spot_copy.key())

        self.assertEqual(constraint_map[(3L, 5L, 24L)].as_query_string(),
                         "hour=5&dow=3&slot=24")

        # spot shows up in DJ view:
        resp = self.client.get(reverse('traffic_log.index'))
        context = resp.context[0]
        slotted_spots = [c for c in context['slotted_spots']]
        spots = [s.title for s in slotted_spots[0].iter_spots()]
        for s in spots:
            self.assertEqual(s, 'Legal ID')

        # spot shows up in admin view:
        resp = self.client.get(reverse('traffic_log.listSpots'))
        context = resp.context[0]
        spots = [c.title for c in context['spots']]
        self.assertEqual(spots, ['Legal ID'])
Пример #8
0
    def test_delete_spot(self):
        spot = models.Spot(title='Legal ID', type='Station ID')
        spot.put()
        self.assertEqual(spot.active, True)

        author = User(email='test')
        author.put()
        spot_copy = models.SpotCopy(body='body', spot=spot, author=author)
        spot_copy.put()

        # assign it to every day of the week at the top of the hour:
        constraint_keys = views.saveConstraint(
            dict(hour_list=range(0, 24), dow_list=range(1, 8), slot=0))
        views.connectConstraintsAndSpot(constraint_keys, spot.key())

        resp = self.client.get(reverse('traffic_log.index'))
        context = resp.context[0]
        slotted_spots = [c for c in context['slotted_spots']]
        spots = [s.title for s in slotted_spots[0].iter_spots()]
        self.assertEqual(spots[0], spot.title)

        resp = self.client.get(
            reverse('traffic_log.deleteSpot', args=[spot.key()]))

        # datastore was cleaned up:
        saved_spot = models.Spot.get(spot.key())
        self.assertEqual(saved_spot.active, False)

        saved_constaints = [
            s for s in models.SpotConstraint.get(constraint_keys)
        ]
        active_spots = []
        for constraint in saved_constaints:
            for spot in constraint.iter_spots():
                active_spots.append(spot)
        self.assertEqual(len(active_spots), 0)

        # spot is hidden from landing page:
        resp = self.client.get(reverse('traffic_log.index'))
        context = resp.context[0]
        slotted_spots = [c for c in context['slotted_spots']]
        active_spots = []
        for slot in slotted_spots:
            for spot in slot.iter_spots_at_constraint():
                active_spots.append(spot)
        self.assertEqual(active_spots, [])

        # spot is hidden from admin view:
        resp = self.client.get(reverse('traffic_log.listSpots'))
        context = resp.context[0]
        spots = [c.title for c in context['spots']]
        self.assertEqual(spots, [])
Пример #9
0
    def test_create_spot(self):
        resp = self.client.post(
            reverse('traffic_log.createSpot'), {
                'title': 'Legal ID',
                'type': 'Station ID',
                'hour_list': [str(d) for d in range(0, 24)],
                'dow_list': [str(d) for d in range(1, 8)],
                'slot': '0'
            })
        self.assertNoFormErrors(resp)
        spot = models.Spot.all().filter("title =", "Legal ID").fetch(1)[0]
        dow = set()
        hours = set()
        constraint_map = {}
        for constraint in spot.constraints:
            dow.add(constraint.dow)
            hours.add(constraint.hour)
            constraint_map[(constraint.dow, constraint.hour,
                            constraint.slot)] = constraint
        self.assertEqual(sorted(dow), range(1, 8))
        self.assertEqual(sorted(hours), range(0, 24))

        # check with Sunday 12:00pm
        author = User(email='test')
        author.put()
        spot_copy = models.SpotCopy(body='body', spot=spot, author=author)
        spot_copy.put()
        spot.random_spot_copies = [spot_copy.key()]
        spot.save()

        self.assertEqual(
            constraint_map[(1L, 12L, 0L)].url_to_finish_spot(spot),
            "/traffic_log/spot-copy/%s/finish?hour=12&dow=1&slot=0" %
            spot_copy.key())

        self.assertEqual(constraint_map[(1L, 12L, 0L)].as_query_string(),
                         "hour=12&dow=1&slot=0")

        # spot shows up in DJ view:
        resp = self.client.get(reverse('traffic_log.index'))
        context = resp.context[0]
        slotted_spots = [c for c in context['slotted_spots']]
        spots = [s.title for s in slotted_spots[0].iter_spots()]
        self.assertEqual(spots[0], spot.title)

        # spot shows up in admin view:
        resp = self.client.get(reverse('traffic_log.listSpots'))
        context = resp.context[0]
        spots = [c.title for c in context['spots']]
        self.assertEqual(spots, ['Legal ID'])
Пример #10
0
    def test_edit_spot_copy(self):
        spot = models.Spot(title='Legal ID', type='Station ID')
        spot.put()
        constraint = models.SpotConstraint(dow=1,
                                           hour=0,
                                           slot=0,
                                           spots=[spot.key()])
        constraint.put()

        author = User(email='test')
        author.put()
        spot_copy = models.SpotCopy(body='First', spot=spot, author=author)
        spot_copy.put()
        spot_copy2 = models.SpotCopy(
            body='You are listening to chirpradio.org',
            spot=spot,
            author=author)
        spot_copy2.put()

        # now edit the second one:
        resp = self.client.post(
            reverse('traffic_log.editSpotCopy', args=(spot_copy2.key(), )), {
                'spot_key': spot.key(),
                'body': 'Something else entirely',
                'underwriter': 'another underwriter',
                'expire_on': ''
            })
        self.assertNoFormErrors(resp)

        spot_copy = [c for c in spot.all_spot_copy()]
        self.assertEqual(sorted([c.body for c in spot_copy]),
                         ['First', 'Something else entirely'])
        self.assertEqual(sorted([c.underwriter for c in spot_copy]),
                         [None, 'another underwriter'])
        self.assertEqual(sorted([c.author.email for c in spot_copy]),
                         ['test', '*****@*****.**'])
Пример #11
0
    def test_move_spot_copy_to_another_spot(self):

        # See http://code.google.com/p/chirpradio/issues/detail?id=124
        dow = 1
        hour = 0
        slot = 0

        spot1 = models.Spot(title='First Spot', type='Station ID')
        spot1.put()
        spot1_key = spot1.key()
        constraint = models.SpotConstraint(dow=dow,
                                           hour=hour,
                                           slot=slot,
                                           spots=[spot1.key()])
        constraint.put()

        spot2 = models.Spot(title='Second Spot', type='Station ID')
        spot2.put()
        constraint = models.SpotConstraint(dow=dow,
                                           hour=hour,
                                           slot=slot,
                                           spots=[spot2.key()])
        constraint.put()

        # assign it to the first one:
        author = User(email='test')
        author.put()
        spot_copy = models.SpotCopy(body='First', spot=spot1, author=author)
        spot_copy.put()

        self.assertEqual(spot1.get_spot_copy(dow, hour, slot)[0].body, "First")

        # now move it to the second spot:
        resp = self.client.post(
            reverse('traffic_log.editSpotCopy', args=(spot_copy.key(), )), {
                'spot_key': spot2.key(),
                'body': 'Second',
                'underwriter': '',
                'expire_on': ''
            })
        self.assertNoFormErrors(resp)

        self.assertEqual(
            spot2.get_spot_copy(dow, hour, slot)[0].body, "Second")

        spot1 = models.Spot.get(spot1_key)
        self.assertEqual(spot1.get_spot_copy(dow, hour, slot)[0], None)
Пример #12
0
    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)
Пример #13
0
    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']))
Пример #14
0
    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)
Пример #15
0
    def test_finish_spot(self):
        self.assertEqual(list(models.TrafficLogEntry.all().fetch(5)), [])

        author = User(email='test')
        author.save()
        spot = models.Spot(title='Legal ID', type='Station ID', author=author)
        spot.put()

        # make a constraint closest to now:
        now = time_util.chicago_now()
        today = now.date()
        current_hour = now.hour
        constraint = models.SpotConstraint(dow=today.isoweekday(),
                                           hour=current_hour,
                                           slot=0,
                                           spots=[spot.key()])
        constraint.put()

        spot_copy = models.SpotCopy(body='You are listening to chirpradio.org',
                                    spot=spot,
                                    author=author)
        spot_copy.put()

        spot.random_spot_copies = [spot_copy.key()]
        spot.save()

        resp = self.client.get(reverse('traffic_log.index'))
        # unfinished spot should have been marked in static HTML:
        assert '<tr class="new">' in resp.content

        resp = self.client.get(
            reverse('traffic_log.finishReadingSpotCopy',
                    args=(spot_copy.key(), )), {
                        'hour': constraint.hour,
                        'dow': constraint.dow,
                        'slot': constraint.slot
                    })
        logged = models.TrafficLogEntry.all().fetch(1)[0]
        self.assertEqual(logged.reader.email, "*****@*****.**")
        self.assertEqual(logged.readtime.timetuple()[0:5],
                         datetime.datetime.now().timetuple()[0:5])
        self.assertEqual(logged.log_date, time_util.chicago_now().date())
        self.assertEqual(logged.spot.key(), spot.key())
        self.assertEqual(logged.spot_copy.key(), spot_copy.key())
        self.assertEqual(logged.scheduled.key(), constraint.key())
        self.assertEqual(logged.hour, constraint.hour)
        self.assertEqual(logged.dow, constraint.dow)

        resp = self.client.get(reverse('traffic_log.index'))
        # finished spot should have been marked in static HTML:
        assert '<tr class="finished">' in resp.content

        resp = self.client.get(
            reverse('traffic_log.spotTextForReading', args=(spot.key(), )), {
                'hour': constraint.hour,
                'dow': constraint.dow,
                'slot': constraint.slot
            })
        context = resp.context
        # already finished, no need for finish URL:
        self.assertEqual(context['url_to_finish_spot'], None)
        assert '(already finished)' in resp.content