Beispiel #1
0
def cart_add_selected_files(request):
    form = SelectedItemsForm(request.POST)
    if form.is_valid():
        try:
            message_id = add_message(
                        request, 'info',
                        settings.ADDING_TO_CART_IN_PROGRESS_NOTIFICATION)
            try:
                with transaction.commit_on_success():
                    cart = Cart(request.session)
                    for result in form.cleaned_data['selected_items']:
                        cart.add(result)
                    cart.update_stats()
                remove_message(request=request, message_id=message_id)
            except DatabaseError:
                remove_message(request=request, message_id=message_id)
                return {
                    'action': 'message',
                    'title': settings.DATABASE_ERROR_NOTIFICATION_TITLE,
                    'content': settings.DATABASE_ERROR_NOTIFICATION}
            return {'action': 'redirect', 'redirect': reverse('cart_page')}
        except Exception as e:
            cart_logger.error('Error while adding to cart: %s' % unicode(e))
    else:
        cart_logger.error('SelectedItemsForm not valid: %s' % unicode(form.errors))
Beispiel #2
0
def cart_add_all_files(request):
    # 'Add all to cart' pressed
    form = AllItemsForm(request.POST)
    if form.is_valid():
        try:
            # calculate query
            raw_filters = form.cleaned_data['filters']
            filters = get_filters_dict(raw_filters)
            q = raw_filters.get('q')
            queries = []
            if q:
                # FIXME: temporary hack to work around GNOS not quoting Solr query
                # FIXME: this is temporary hack, need for multiple requests will be fixed at CGHub
                if browser_text_search.useAllMetadataIndex:
                    query = {'all_metadata': browser_text_search.ws_query(q)}
                    query.update(filters)
                    queries = [query]
                else:
                    query = {'xml_text': u"(%s)" % q}
                    query.update(filters)
                    queries = [query, {'analysis_id': q}]
            if not queries:
                queries = [filters]

            message_id = add_message(
                        request, 'info',
                        settings.ADDING_TO_CART_IN_PROGRESS_NOTIFICATION)
            try:
                with transaction.commit_on_success():
                    cart = Cart(request.session)
                    for query in queries:
                        api_request = RequestMinimal(query=query)
                        for result in api_request.call():
                            cart.add(result)
                        cart.update_stats()
                remove_message(request, message_id)
            except DatabaseError:
                remove_message(request, message_id)
                return {
                    'action': 'message',
                    'title': settings.DATABASE_ERROR_NOTIFICATION_TITLE,
                    'content': settings.DATABASE_ERROR_NOTIFICATION}
            return {'action': 'redirect', 'redirect': reverse('cart_page')}
        except Exception as e:
            cart_logger.error('Error while adding all files to cart: %s' % unicode(e))
    else:
        cart_logger.error('AllItemsForm not valid: %s' % unicode(form.errors))