Example #1
0
def query_handler(query):
    '''Manage query and return taxa from uBio.'''
    # Get taxa from pickled objects or via uBio.
    try:
        # Queries are cached as pickled objects.
        taxapic = open('queries/' + query, 'rb')
        taxa = pickle.load(taxapic)
        taxapic.close()
    except:
        #TODO Handle connection problems, better at ubio.py.
        ubio = uBio()
        taxa = ubio.search_name(query)

        # Save query as pickled object.
        taxapic = open('queries/' + query, 'wb')
        pickle.dump(taxa, taxapic)
        taxapic.close()
    return taxa
Example #2
0
def taxon_page(request, slug):
    '''Taxon page.'''
    # Search form.
    form = SearchForm()

    # Get or create taxon instance.
    try:
        taxon = Taxon.objects.select_related().get(slug=slug)
    except:
        # Pretend it is a regular query.
        query = slug.replace('-', ' ')
        # Call uBio to help.
        ubio = uBio()
        taxa = ubio.search_name(query)
        # Uppercase the first letter of the first word.
        words = query.split(' ')
        words[0] = words[0].title()
        name = ' '.join(words)
        # Scan query results to check if the slug (taxon) exists.
        # It must be an exact match to a known uBio entry to create the taxon.
        for entry in taxa:
            if entry['name'].lower() == query.lower():
                taxon = Taxon(name=name)
                taxon.save()
            else:
                #XXX What happens when taxon object is not created?
                pass

    # Get all related articles.
    articles = taxon.articles.all()

    # Check if a query object was created recently.
    try:
        last_query = Query.objects.filter(taxon=taxon).order_by('-timestamp')[0]
    except:
        last_query = ''

    # Top ten authors in number of publications with the taxon.
    #TODO Explore possibilities.
    try:
        top_authors = articles.values('authors__forename', 'authors__surname').annotate(Count('authors')).order_by('-authors__count')[:10]
    except:
        top_authors = []

    # Number of publications per year.
    if articles:
        data = get_yearly(articles)
    else:
        data = {}

    # Calls for reference fetching.
    fetching = False
    if not last_query:
        fetching = True
    else:
        now = datetime.now()
        if (now - last_query.timestamp) > timedelta(minutes=1):
            fetching = True

    # Calculate the ratio of existent and fetched articles.
    if last_query:
        fetch_ratio = get_ratio(articles.count(), last_query.total_results)
    else:
        fetch_ratio = 0

    user_list = taxon.taxon_users.values_list('id', flat=True)

    variables = RequestContext(request, {
        'taxon': taxon,
        'last_query': last_query,
        'articles': articles,
        'top_authors': top_authors,
        'fetching': fetching,
        'fetch_ratio': fetch_ratio,
        'form': form,
        'user_list': user_list,
        'data': data,
        })
    return render_to_response('taxon.html', variables)