コード例 #1
0
ファイル: views.py プロジェクト: sharonmleon/serendipomatic
def saveme(request):

    search_terms = request.session['search_terms']  # TODO: error handling if not set
    dpla_items = DPLA.find_items(**search_terms)
    euro_items = Europeana.find_items(**search_terms)
    flkr_items = Flickr.find_items(**search_terms)
    trove_items = Trove.find_items(**search_terms)
    sources = [DPLA, Europeana, Trove]
    items = [x for t in zip(dpla_items, euro_items, flkr_items, trove_items) for x in t]
    return render(request, 'core/saveme.html',
                  {'items': items, 'query_terms': search_terms, 'sources': sources})
コード例 #2
0
ファイル: views.py プロジェクト: mbwolff/serendipomatic
def saveme(request):
    # TODO: save results needs to be built into result page so
    # we can guarantee items match, are listed in the same order, etc

    search_terms = request.session['search_terms']  # TODO: error handling if not set
    dpla_items = DPLA.find_items(**search_terms)
    euro_items = Europeana.find_items(**search_terms)
    flkr_items = Flickr.find_items(**search_terms)
    trove_items = Trove.find_items(**search_terms)
    sources = [DPLA, Europeana, Trove]
    items = [x for t in zip(dpla_items, euro_items, flkr_items, trove_items) for x in t]
    return render(request, 'core/saveme.html',
                  {'items': items, 'query_terms': search_terms, 'sources': sources})
コード例 #3
0
ファイル: views.py プロジェクト: sthommes/serendipomatic
def saveme(request):
    # TODO: save results needs to be built into result page so
    # we can guarantee items match, are listed in the same order, etc

    search_terms = request.session[
        'search_terms']  # TODO: error handling if not set
    dpla_items = DPLA.find_items(**search_terms)
    euro_items = Europeana.find_items(**search_terms)
    flkr_items = Flickr.find_items(**search_terms)
    trove_items = Trove.find_items(**search_terms)
    sources = [DPLA, Europeana, Trove]
    items = [
        x for t in zip(dpla_items, euro_items, flkr_items, trove_items)
        for x in t
    ]
    return render(request, 'core/saveme.html', {
        'items': items,
        'query_terms': search_terms,
        'sources': sources
    })
コード例 #4
0
ファイル: views.py プロジェクト: sharonmleon/serendipomatic
def view_items(request):
    search_terms = request.session.get('search_terms', None)

    # if no search terms, return to site index
    if search_terms is None or not search_terms['keywords']:
        # TODO: add a django session message here,
        # especially if they posted data and we didn't get any keywords
        return HttpResponseRedirect(reverse('site-index'))

    # clear the session
    for key, value in request.session.items():
        if key != 'search_terms': del request.session[key]

    # sanitize the search terms for API queries

    # encode the search terms for safety
    search_terms['keywords'] = [sanitizeString(s) for s in search_terms['keywords']]

    start = time.time()
    dpla_items = DPLA.find_items(**search_terms)
    euro_items = Europeana.find_items(**search_terms)
    # added Flickr
    flkr_items = Flickr.find_items(**search_terms)
    trove_items = Trove.find_items(**search_terms)
    logger.info('Queried 4 sources in %.2f sec' % (time.time() - start))

    sources = [DPLA, Europeana, Flickr, Trove]

    # combine all results into a single list and then shuffle them together
    logger.info('Number of items by source: DPLA=%d, Europeana=%d, Flickr=%d, Trove=%d' % \
                 (len(dpla_items), len(euro_items), len(flkr_items), len(trove_items)))
    items = dpla_items + euro_items + flkr_items + trove_items
    # shuffle images so we get a more even mix, esp. if one source
    # (such as flickr) returns more items than the others
    random.shuffle(items)

    # NOTE: we may need to clear the cache when we do a 'start over'....

    return render(request, 'core/view.html',
                  {'items': items, 'query_terms': search_terms, 'sources': sources})
コード例 #5
0
ファイル: views.py プロジェクト: mbwolff/serendipomatic
def view_items(request):
    search_terms = request.session.get('search_terms', None)

    # if no search terms, return to site index
    if search_terms is None or not search_terms['keywords']:
        if search_terms is not None and 'keywords' in search_terms:
            messages.error(request, '''Whoops! Somehow we didn't come up with any search terms for you''')

        # TODO: add a django session message here,
        # especially if they posted data and we didn't get any keywords
        return HttpResponseRedirect(reverse('site-index'))

    # clear the session
    # TODO: should probably be more specific what we need to clear (zotero auth info?)
    # since other items may be added to the session at some point (e.g. messages)
    for key, value in request.session.items():
        if key != 'search_terms':
            del request.session[key]

    # sanitize the search terms for API queries

    # encode the search terms for safety
    search_terms['keywords'] = [sanitizeString(s) for s in search_terms['keywords']]

    start = time.time()
    error = False
    try:
        dpla_items = DPLA.find_items(**search_terms)
    except Exception as err:
        logger.error('Error querying DPLA - %s' % err)
        error = True
        dpla_items = []

    try:
        euro_items = Europeana.find_items(**search_terms)
    except Exception as err:
        logger.error('Error querying Europeana - %s' % err)
        error = True
        euro_items = []

    try:
        flkr_items = Flickr.find_items(**search_terms)
    except Exception as err:
        logger.error('Error querying Flickr - %s' % err)
        error = True
        flkr_items = []

    try:
        trove_items = Trove.find_items(**search_terms)
    except Exception as err:
        logger.error('Error querying Trove - %s' % err)
        error = True
        trove_items = []

    logger.info('Queried 4 sources in %.2f sec' % (time.time() - start))

    # TODO: should we remove from source list if we failed?
    sources = [DPLA, Europeana, Flickr, Trove]

    # combine all results into a single list and then shuffle them together
    logger.info('Number of items by source: DPLA=%d, Europeana=%d, Flickr=%d, Trove=%d' % \
                 (len(dpla_items), len(euro_items), len(flkr_items), len(trove_items)))
    items = dpla_items + euro_items + flkr_items + trove_items

    # if none of the API calls worked, message & return to home page
    if not items:
        msg = '''We couldn't find anything for you. Please try again.'''
        # if error is true, at least one of the api calls failed
        # otherwise, presumably we callled all apis and didn't get any results (?)
        messages.error(request, msg)
        return HttpResponseRedirect(reverse('site-index'))

    # shuffle images so we get a more even mix, esp. if one source
    # (such as flickr) returns more items than the others
    random.shuffle(items)

    # NOTE: we may need to clear the cache when we do a 'start over'....

    return render(request, 'core/view.html',
                  {'items': items, 'query_terms': search_terms, 'sources': sources})
コード例 #6
0
ファイル: views.py プロジェクト: sthommes/serendipomatic
def view_items(request):
    search_terms = request.session.get('search_terms', None)

    # if no search terms, return to site index
    if search_terms is None or not search_terms['keywords']:
        if search_terms is not None and 'keywords' in search_terms:
            messages.error(
                request,
                '''Whoops! Somehow we didn't come up with any search terms for you'''
            )

        # TODO: add a django session message here,
        # especially if they posted data and we didn't get any keywords
        return HttpResponseRedirect(reverse('site-index'))

    # clear the session
    # TODO: should probably be more specific what we need to clear (zotero auth info?)
    # since other items may be added to the session at some point (e.g. messages)
    for key, value in request.session.items():
        if key != 'search_terms':
            del request.session[key]

    # sanitize the search terms for API queries

    # encode the search terms for safety
    search_terms['keywords'] = [
        sanitizeString(s) for s in search_terms['keywords']
    ]

    start = time.time()
    error = False
    try:
        dpla_items = DPLA.find_items(**search_terms)
    except Exception as err:
        logger.error('Error querying DPLA - %s' % err)
        error = True
        dpla_items = []

    try:
        euro_items = Europeana.find_items(**search_terms)
    except Exception as err:
        logger.error('Error querying Europeana - %s' % err)
        error = True
        euro_items = []

    try:
        flkr_items = Flickr.find_items(**search_terms)
    except Exception as err:
        logger.error('Error querying Flickr - %s' % err)
        error = True
        flkr_items = []

    try:
        trove_items = Trove.find_items(**search_terms)
    except Exception as err:
        logger.error('Error querying Trove - %s' % err)
        error = True
        trove_items = []

    logger.info('Queried 4 sources in %.2f sec' % (time.time() - start))

    # TODO: should we remove from source list if we failed?
    sources = [DPLA, Europeana, Flickr, Trove]

    # combine all results into a single list and then shuffle them together
    logger.info('Number of items by source: DPLA=%d, Europeana=%d, Flickr=%d, Trove=%d' % \
                 (len(dpla_items), len(euro_items), len(flkr_items), len(trove_items)))
    items = dpla_items + euro_items + flkr_items + trove_items

    # if none of the API calls worked, message & return to home page
    if not items:
        msg = '''We couldn't find anything for you. Please try again.'''
        # if error is true, at least one of the api calls failed
        # otherwise, presumably we callled all apis and didn't get any results (?)
        messages.error(request, msg)
        return HttpResponseRedirect(reverse('site-index'))

    # shuffle images so we get a more even mix, esp. if one source
    # (such as flickr) returns more items than the others
    random.shuffle(items)

    # NOTE: we may need to clear the cache when we do a 'start over'....

    return render(request, 'core/view.html', {
        'items': items,
        'query_terms': search_terms,
        'sources': sources
    })