예제 #1
0
    def test_basic_update_with_popularity(self, rpost):

        posts = []

        def mocked_post(url, **options):
            assert settings.AUTOCOMPETER_URL in url
            data = json.loads(options['data'])
            posts.append(data)
            return Response(
                'OK',
                201
            )

        rpost.side_effect = mocked_post

        event = Event.objects.get(title='Test event')
        # also change to a non-public privacy setting
        event.privacy = Event.PRIVACY_CONTRIBUTORS
        event.save()
        EventHitStats.objects.create(
            event=event,
            total_hits=100
        )
        autocompeter.update()

        document = posts[0]['documents'][0]
        eq_(document['popularity'], 100)
        eq_(document['group'], Event.PRIVACY_CONTRIBUTORS)
예제 #2
0
    def test_basic_update(self, rpost):

        posts = []

        def mocked_post(url, **options):
            assert settings.AUTOCOMPETER_URL in url
            data = json.loads(options['data'])
            posts.append(data)
            return Response(
                'OK',
                201
            )

        rpost.side_effect = mocked_post

        autocompeter.update()
        # nothing should happen because there are no recently modified events
        ok_(not posts)

        event = Event.objects.get(title='Test event')
        event.save()
        autocompeter.update()
        eq_(len(posts), 1)

        # In the posted data should be a thing called 'documents'
        # which is a list of every document.
        assert len(posts[0]['documents']) == 1
        document = posts[0]['documents'][0]
        eq_(document['url'], reverse('main:event', args=(event.slug,)))
        eq_(document['title'], event.title)
        eq_(document['group'], '')
        eq_(document['popularity'], 0)
예제 #3
0
    def test_basic_update_all_with_flush(self, rpost, rdelete):

        posts = []
        deletes = []

        def mocked_post(url, **options):
            assert settings.AUTOCOMPETER_URL in url
            data = json.loads(options['data'])
            posts.append(data)
            return Response(
                'OK',
                201
            )

        rpost.side_effect = mocked_post

        def mocked_delete(url, **options):
            assert settings.AUTOCOMPETER_URL in url
            deletes.append(url)
            return Response(
                'OK',
                204
            )

        rdelete.side_effect = mocked_delete

        autocompeter.update(all=True, flush_first=True)
        ok_(deletes)
        ok_(posts)
예제 #4
0
    def test_basic_update_all_with_popularity(self, rpost):

        posts = []

        def mocked_post(url, **options):
            assert settings.AUTOCOMPETER_URL in url
            data = json.loads(options['data'])
            posts.append(data)
            return Response(
                'OK',
                201
            )

        rpost.side_effect = mocked_post
        EventHitStats.objects.create(
            event=Event.objects.get(title='Test event'),
            total_hits=200
        )

        autocompeter.update(all=True)

        assert len(posts[0]['documents']) == 1
        document = posts[0]['documents'][0]
        eq_(document['title'], 'Test event')
        eq_(document['popularity'], 200)
예제 #5
0
def autocompeter_home(request):
    if request.method == 'POST':
        form = forms.AutocompeterUpdateForm(request.POST)
        if form.is_valid():
            options = form.cleaned_data
            out = StringIO()
            options['out'] = out
            update(**options)
            messages.success(
                request,
                out.getvalue()
            )
            return redirect('manage:autocompeter')

    else:
        initial = {
            'verbose': True,
            'max_': 100,
            'since': 10,
        }
        form = forms.AutocompeterUpdateForm(initial=initial)

    context = {
        'form': form,
    }
    return render(request, 'manage/autocompeter.html', context)
예제 #6
0
 def handle(self, **options):
     verbosity = int(options['verbosity'])
     verbose = verbosity > 1
     max_ = options['max']
     all = options['all']
     flush_first = options['flush_first']
     autocompeter.update(
         verbose=verbose,
         max_=max_,
         all=all,
         flush_first=flush_first,
     )
예제 #7
0
    def test_basic_update_with_repeated_titles(self, rpost):

        posts = []

        def mocked_post(url, **options):
            assert settings.AUTOCOMPETER_URL in url
            data = json.loads(options['data'])
            posts.append(data)
            return Response(
                'OK',
                201
            )

        rpost.side_effect = mocked_post

        event = Event.objects.get(title='Test event')
        # also change to a non-public privacy setting
        event.privacy = Event.PRIVACY_CONTRIBUTORS
        event.save()
        EventHitStats.objects.create(
            event=event,
            total_hits=100
        )
        event2 = Event.objects.create(
            slug='something-else',
            title=event.title,
            status=event.status,
            privacy=event.privacy,
            start_time=event.start_time + datetime.timedelta(days=40),
            description=event.description,
            placeholder_img=event.placeholder_img,
            archive_time=event.archive_time,
        )
        EventHitStats.objects.create(
            event=event2,
            total_hits=100
        )
        assert Event.objects.approved().count() == 2
        autocompeter.update()

        documents = posts[0]['documents']
        titles = [x['title'] for x in documents]
        titles.sort()
        title1 = event.start_time.strftime('Test event %d %b %Y')
        title2 = event2.start_time.strftime('Test event %d %b %Y')
        eq_(titles, [title1, title2])
예제 #8
0
    def test_basic_update_all(self, rpost):

        posts = []

        def mocked_post(url, **options):
            assert settings.AUTOCOMPETER_URL in url
            data = json.loads(options['data'])
            posts.append(data)
            return Response(
                'OK',
                201
            )

        rpost.side_effect = mocked_post

        autocompeter.update(all=True)

        assert len(posts[0]['documents']) == 1
        document = posts[0]['documents'][0]
        eq_(document['title'], 'Test event')
        eq_(document['popularity'], 0)
예제 #9
0
    def test_basic_update_all(self, rpost):

        posts = []

        def mocked_post(url, **options):
            assert settings.AUTOCOMPETER_URL in url
            data = json.loads(options['data'])
            posts.append(data)
            return Response(
                'OK',
                201
            )

        rpost.side_effect = mocked_post

        autocompeter.update(all=True)

        main_channel, = Channel.objects.all()
        assert main_channel.slug == settings.DEFAULT_CHANNEL_SLUG
        channel = Channel.objects.create(
            name='Food',
            slug='food'
        )

        assert len(posts[0]['documents']) == 1, len(posts[0]['documents'])
        document = posts[0]['documents'][0]
        eq_(document['title'], 'Test event')
        eq_(document['popularity'], 0)

        event = Event.objects.get(title='Test event')
        event.channels.add(channel)

        autocompeter.update(all=True)
        assert len(posts[1]['documents']) == 2, len(posts[0]['documents'])
        document = posts[1]['documents'][1]
        eq_(document['title'], 'Food (Channel)')
        eq_(document['url'], reverse('main:home_channels', args=('food',)))
        eq_(document['popularity'], 0.0)
예제 #10
0
    def test_basic_update_upcoming_event(self, rpost):

        posts = []

        def mocked_post(url, **options):
            assert settings.AUTOCOMPETER_URL in url
            data = json.loads(options['data'])
            posts.append(data)
            return Response(
                'OK',
                201
            )

        rpost.side_effect = mocked_post

        event = Event.objects.get(title='Test event')
        EventHitStats.objects.create(
            event=event,
            total_hits=100
        )
        autocompeter.update()

        future = timezone.now() + datetime.timedelta(days=1)
        Event.objects.create(
            slug='aaa',
            title='Other',
            start_time=future,
            status=event.status,
        )
        assert Event.objects.approved().count() == 2
        autocompeter.update()
        assert len(posts[0]['documents']) == 1
        document = posts[0]['documents'][0]
        eq_(document['title'], 'Other')
        # picks this up from the median
        eq_(document['popularity'], 100)
예제 #11
0
    def test_basic_update_all_with_unapproved(self, rpost):

        posts = []

        def mocked_post(url, **options):
            assert settings.AUTOCOMPETER_URL in url
            data = json.loads(options['data'])
            posts.append(data)
            return Response(
                'OK',
                201
            )

        rpost.side_effect = mocked_post
        event = Event.objects.get(title='Test event')

        EventHitStats.objects.create(
            event=event,
            total_hits=200
        )

        autocompeter.update(all=True)

        assert len(posts[0]['documents']) == 1
        document = posts[0]['documents'][0]
        eq_(document['title'], 'Test event')
        eq_(document['group'], '')

        app = Approval.objects.create(event=event)
        autocompeter.update(all=True)

        document = posts[1]['documents'][0]
        eq_(document['title'], 'Test event')
        eq_(document['group'], 'contributors')

        app.approved = True
        app.save()
        autocompeter.update(all=True)

        document = posts[2]['documents'][0]
        eq_(document['title'], 'Test event')
        eq_(document['group'], '')
예제 #12
0
 def test_update_without_key(self):
     with self.settings(AUTOCOMPETER_KEY=None):
         # This would simply fail if autocompeter.py wasn't smart
         # enough to notice that the AUTOCOMPETER_KEY was not set.
         autocompeter.update()
예제 #13
0
 def test_update_without_key(self):
     with self.settings(AUTOCOMPETER_KEY=None):
         # This would simply fail if autocompeter.py wasn't smart
         # enough to notice that the AUTOCOMPETER_KEY was not set.
         autocompeter.update()