def mission_people(request, role=None): character_query = Character.Query(request.redis_conn, request.mission.name) character_ordering = [ identifier.decode('utf-8') for identifier in list( request.redis_conn.lrange( "character-ordering:%s" % request.mission.name, 0, -1)) ] sort_characters = lambda l: sorted( list(l), key=lambda x: character_ordering.index(x.identifier) if x.identifier in character_ordering else 100) if role: people = [{ 'name': role, 'members': sort_characters(character_query.role(role)), }] more_people = False else: astronauts = list(character_query.role('astronaut')) ops = sort_characters(character_query.role('mission-ops-title')) people = [{ 'name': 'Flight Crew', 'members': astronauts, 'view': 'full' }, { 'name': 'Mission Control', 'members': ops, 'view': 'simple' }] more_people = len(list(character_query.role('mission-ops'))) return people, more_people
def people(request, role=None): character_query = Character.Query(request.redis_conn, request.mission.name) character_ordering = list(request.redis_conn.lrange("character-ordering:%s" % request.mission.name, 0, -1)) sort_characters = lambda l: sorted( list(l), key=lambda x: character_ordering.index(x.identifier) if x.identifier in character_ordering else 100 ) if role: people = [ { 'name': role, 'members': sort_characters(character_query.role(role)), } ] more_people = False else: all_people = sort_characters(character_query) astronauts = list(character_query.role('astronaut')) ops = sort_characters(character_query.role('mission-ops-title')) people = [ { 'name': 'Flight Crew', 'members': astronauts, 'view': 'full' }, { 'name': 'Mission Control', 'members': ops, 'view': 'simple' } ] more_people = len(list(character_query.role('mission-ops'))) # 404 if we have no content if 1 == len(people) and 0 == len(people[0]['members']): raise Http404( "No people were found" ) return render_to_response( 'people/people.html', { 'role': role, 'people': people, 'more_people': more_people, }, context_instance = RequestContext(request), )
def __init__(self, redis_conn, mission_name, transcript_name, parser): self.redis_conn = redis_conn self.mission_name = mission_name self.transcript_name = transcript_name self.parser = parser search_db.add_field_action( "mission", xappy.FieldActions.INDEX_EXACT, # search_by_default=False, # allow_field_specific=False, ) search_db.add_field_action( "transcript", xappy.FieldActions.INDEX_EXACT, ) # don't think we need STORE_CONTENT actions any more search_db.add_field_action( "speaker", xappy.FieldActions.STORE_CONTENT, ) # Can't use facetting unless Xapian supports it # can't be bothered to check this (xappy._checkxapian.missing_features['facets']==1) # # search_db.add_field_action( # "speaker", # xappy.FieldActions.FACET, # type='string', # ) search_db.add_field_action( "speaker", xappy.FieldActions.INDEX_FREETEXT, weight=1, language='en', search_by_default=True, allow_field_specific=True, ) search_db.add_field_action( "text", xappy.FieldActions.STORE_CONTENT, ) search_db.add_field_action( "text", xappy.FieldActions.INDEX_FREETEXT, weight=1, language='en', search_by_default=True, allow_field_specific=False, spell=True, ) search_db.add_field_action( "weight", xappy.FieldActions.SORTABLE, type='float', ) search_db.add_field_action( "speaker_identifier", xappy.FieldActions.STORE_CONTENT, ) # Add names as synonyms for speaker identifiers characters = Character.Query(self.redis_conn, self.mission_name).items() self.characters = {} for character in characters: self.characters[character.identifier] = character # for name in [character.name, character.short_name]: # for bit in name.split(): # search_db.add_synonym(bit, character.identifier) # search_db.add_synonym(bit, character.identifier, field='speaker') # Add to the mission's list of transcripts self.redis_conn.sadd( "mission:%s:transcripts" % self.mission_name, self.transcript_name, )
def get_context_data(self): # Get the query text q = self.request.GET.get('q', '') # Get the offset value try: offset = int(self.request.GET.get('offset', '0')) if offset < 0: offset = 0 except ValueError: offset = 0 # Is it a special search? special_value = self.request.redis_conn.get("special_search:%s:%s" % ( self.request.mission.name, q, )) if special_value: self.template_name = "search/special.html" return { "q": q, "text": special_value, } # Get the results from Xapian db = xappy.SearchConnection( os.path.join( settings.SITE_ROOT, '..', "xappydb", ), ) query = db.query_parse( q, default_op=db.OP_OR, deny=["mission"], ) query = db.query_filter( query, db.query_composite(db.OP_AND, [ db.query_field("mission", self.request.mission.name), db.query_field("transcript", self.request.mission.main_transcript), ])) results = db.search( query=query, startrank=offset, endrank=offset + PAGESIZE, checkatleast= -1, # everything (entire xapian db fits in memory, so this should be fine) sortby="-weight", ) # Go through the results, building a list of LogLine objects redis_conn = self.request.redis_conn log_lines = [] for result in results: transcript_name, timestamp = result.id.split(":", 1) log_line = LogLine(redis_conn, transcript_name, int(timestamp)) log_line.speaker = Character(redis_conn, transcript_name.split('/')[0], result.data['speaker_identifier'][0]) log_line.title = mark_safe( result.summarise("text", maxlen=50, ellipsis='…', strict_length=True, hl=None)) log_line.summary = mark_safe( result.summarise("text", maxlen=600, ellipsis='…', hl=('<mark>', '</mark>'))) log_lines.append(log_line) def page_url(offset): return reverse("search") + '?' + urllib.urlencode( { 'q': q.encode('utf-8'), 'offset': offset, }) if offset == 0: previous_page = False else: previous_page = page_url(offset - PAGESIZE) if offset + PAGESIZE > results.matches_estimated: next_page = False else: next_page = page_url(offset + PAGESIZE) thispage = offset / PAGESIZE maxpage = results.matches_estimated / PAGESIZE pages_to_show = set([0]) | set([thispage - 1, thispage, thispage + 1 ]) | set([maxpage]) if 0 == thispage: pages_to_show.remove(thispage - 1) if maxpage == thispage: pages_to_show.remove(thispage + 1) pages = [] class Page(object): def __init__(self, number, url, selected=False): self.number = number self.url = url self.selected = selected pages_in_order = list(pages_to_show) pages_in_order.sort() for page in pages_in_order: if len(pages) > 0 and page != pages[-1].number: pages.append('...') pages.append( Page(page + 1, page_url(page * PAGESIZE), page == thispage)) error_info = self.request.redis_conn.hgetall( "error_page:%s:%s" % ( self.request.mission.name, 'no_search_results', ), ) if not error_info: error_info = {} if error_info.has_key('classic_moment_quote'): error_quote = LogLine( self.request.redis_conn, self.request.mission.main_transcript, timestamp_to_seconds(error_info['classic_moment_quote'])) else: error_quote = None return { 'log_lines': log_lines, 'result': results, 'q': q, 'previous_page': previous_page, 'next_page': next_page, 'pages': pages, 'debug': { 'query': query, }, 'error': { 'info': error_info, 'quote': error_quote, } }