Beispiel #1
0
def profile_view(view):
    empty_database()
    empty_cache()
    time.sleep(5)
    request = get_request()
    cart = Cart(request.session)
    # fill cart
    api_request = RequestMinimal(query={'analysis_id': IDS})
    for result in api_request.call():
        cart.add(result)
    cart.update_stats()
    # Call view RUN_COUNT times
    for i in range(RUN_COUNT):
        print 'Run #%d ...' % (i + 1)
        runctx('run_view(view, request)',
                {'run_view': run_view},
                {'view': view, 'request': request}, 'stats')
        p = pstats.Stats('stats')
        p.sort_stats('time').print_stats(20)
    print 'done'
Beispiel #2
0
    def test_item_details_view(self):
        analysis_id = '916d1bd2-f503-4775-951c-20ff19dfe409'
        bad_analysis_id = 'badd1bd2-f503-4775-951c-123456789112'
        with self.settings(PAGINATOR_LIMITS=[10, 25, 50]):
            try:
                response = self.client.get(reverse(
                        'item_details', kwargs={'analysis_id': bad_analysis_id}))
            except URLError as e:
                self.assertIn('No results for analysis_id', str(e))

            response = self.client.get(
                            reverse('item_details',
                            kwargs={'analysis_id': analysis_id}))
            self.assertEqual(response.status_code, 200)
            result = response.context['res']
            self.assertNotContains(response, u'No data.')
            self.assertContains(response, result['center_name'])
            # not ajax
            self.assertContains(response, '<head>')
            self.assertContains(response, '<script>LoadXMLString')
            self.assertContains(response, 'Add to cart')
            self.assertNotContains(response, 'In your cart')
            # TODO: add test for reason shows only for state != live
            # try ajax request
            response = self.client.get(
                            reverse('item_details',
                            kwargs={'analysis_id': analysis_id}),
                            {'ajax': 1},
                            HTTP_X_REQUESTED_WITH='XMLHttpRequest')
            self.assertEqual(response.status_code, 200)
            self.assertContains(response, result['center_name'])
            self.assertNotContains(response, '<script>LoadXMLString')
            self.assertContains(response, 'Show metadata XML')
            self.assertContains(response, 'Add to cart')
            self.assertNotContains(response, 'In your cart')
            # test if response contains some of needed fields
            self.assertContains(response, 'Modified')
            self.assertContains(response, 'Disease')
            self.assertContains(response, 'Disease Name')
            self.assertContains(response, 'Sample Accession')
            # test raw_xml
            self.assertTrue(response.context['raw_xml'])
            # check all entries are present
            self.assertIn('run_xml', response.context['raw_xml'])
            # check reason is present
            self.assertFalse(response.context['res']['reason'] is None)
            # test reason field
            analysis_id2 = '333a5cc4-741b-445c-93f9-9fde6f64b88f' # state = bad_data
            response = self.client.get(reverse(
                    'item_details', kwargs={'analysis_id': analysis_id2}))
            self.assertEqual(response.status_code, 200)
            # show 'Add to cart' button only if item not in cart
            create_session(self)
            cart = Cart(self.session)
            cart.add(result)
            response = self.client.get(
                            reverse('item_details',
                            kwargs={'analysis_id': analysis_id}))
            self.assertEqual(response.status_code, 200)
            self.assertNotContains(response, 'Add to cart')
            self.assertContains(response, 'In your cart')
            # try ajax request
            response = self.client.get(
                            reverse('item_details',
                            kwargs={'analysis_id': analysis_id}),
                            {'ajax': 1},
                            HTTP_X_REQUESTED_WITH='XMLHttpRequest')
            self.assertEqual(response.status_code, 200)
            self.assertNotContains(response, 'Add to cart')
            self.assertContains(response, 'In your cart')
Beispiel #3
0
    def post(self, request, **kwargs):
        if 'ids' in request.POST:
            form = AnalysisIDsForm(request.POST)
            if form.is_valid():
                ids = form.cleaned_data['ids']
            else:
                return HttpResponseRedirect(reverse('batch_search_page'))

            if request.POST.get('add_to_cart'):

                cart = Cart(request.session)
                with transaction.commit_on_success():
                    for part in range(0, len(ids), settings.MAX_ITEMS_IN_QUERY):
                        query = {'analysis_id': ids[part : part + settings.MAX_ITEMS_IN_QUERY]}
                        api_request = RequestMinimal(query=query)
                        for result in api_request.call():
                            cart.add(result)
                    cart.update_stats()

                return HttpResponseRedirect(reverse('cart_page'))
            else:
                ids = sorted(ids)
                results = []
                offset, limit = paginator_params(request)
                for i in ids[offset:(offset + limit)]:
                    results.append(i)

                results = get_results_for_ids(results, sort_by='analysis_id')

                response = self.render_to_response({
                        'form': form, 'ids': ids,
                        'results': results})

                if response.status_code == 200:
                    response.set_cookie(
                            settings.PAGINATOR_LIMIT_COOKIE, limit,
                            max_age=settings.COOKIE_MAX_AGE,
                            path=reverse('home_page'))

                return response
        else:
            # submitted search form
            form = BatchSearchForm(request.POST or None, request.FILES or None)
            if form.is_valid():
                submitted_ids = form.cleaned_data['ids']
                unvalidated = form.cleaned_data.get('unvalidated_ids')
                submitted = len(submitted_ids) + len(unvalidated)

                ids = []
                found = {}
                for part in range(0, len(submitted_ids), settings.MAX_ITEMS_IN_QUERY):
                    ids_part = submitted_ids[part : part + settings.MAX_ITEMS_IN_QUERY]
                    search = SearchByIDs(ids=ids_part)
                    for attr in search.results:
                        l = len(search.results[attr])
                        if l:
                            found[attr] = found.get(attr, 0) + l
                    ids += search.get_ids()
                ids = sorted(ids)
                offset, limit = paginator_params(request)
                results = get_results_for_ids(
                        ids[offset:offset + limit],
                        sort_by='analysis_id')

                if not results:
                    form.errors['__all__'] = form.error_class(["No results found."])

                response = self.render_to_response({
                        'form': form, 'found': found, 'ids': ids,
                        'submitted': submitted, 'results': results,
                        'unvalidated': unvalidated})

                if response.status_code == 200:
                    response.set_cookie(
                            settings.PAGINATOR_LIMIT_COOKIE, limit,
                            max_age=settings.COOKIE_MAX_AGE,
                            path=reverse('home_page'))

                return response

            return self.render_to_response({
                        'form': form, 'found': None})