Example #1
0
def poll_stations_for(request, const_name, page_num):
    const_name = const_name.upper()
    key = "%s-stations-info" % const_name
    #stn_info is a dict contains the list of stations and the jump to alphabetical dict 
    stn_info = memcache.get(key)
    if stn_info is None:
        logger.info("poll station list for '%s' not found in memcache." % const_name)
        list = []
        res = PollStation.gql("where belongsTo = :1 order by name ASC", const_name)
        #create a map that will hold a "jump to" page number alphabetically
        jump_to = {}
        #loop counter, start at items per page so that page number starts at 1
        counter = items_per_page
        #hold letters already seen
        letter_set = []
        for stn in res:
            counter = counter + 1
            list.append(stn)
            #populate map if first letter not present
            #find out if key exists, record only first occurrence
            if stn.nameStartsWith not in letter_set:
                letter_set.append(stn.nameStartsWith)
                page_to = counter / items_per_page
                if page_to not in jump_to:
                    #create a list that holds all letters starting on same page
                    jump_to[page_to] = [stn.nameStartsWith]
                else:#add to existing letter starting on this page
                    jump_to[page_to].append(stn.nameStartsWith)
        #logger.info(jump_to)
        memcache.add(key, {'stations':list, 'jump_to':jump_to})
    else:#retrieve from memcache dict
        list = stn_info['stations']
        jump_to = stn_info['jump_to']
    if len(list) == 0:#not found
        err_msg = "No Poll stations were found for '%s'" % const_name
        return render_to_response('kura_web/constituency-stations-view.html', {'error_message': err_msg, 'const_name': const_name})
    else:
        #poll station list found in memcache of db
        return render_to_response('kura_web/constituency-stations-view.html', 
                                  {'const_name': const_name, 'page':page_object_list(list, page_num), 'jump_to':jump_to})
Example #2
0
def retrieve_station_from_memcache_or_db(const_name, stn_name):
    """ Retrieve polling station from memcache  if it exists. If not, try the datastore """
    #check if any arg is none and return none TODO - is that the best way
    if const_name is None or stn_name is None:
        logger.error("const_name or stn_name is None. Return None")
        return None 
    #check if poll station had been fetched into memcache
    stn_key = "poll-station-%s-%s" % (stn_name, const_name)
    stn = memcache.get(stn_key)
    if (stn is None): #not in memcache, fetch from db
        logger.info("key %s not found in memcache" % stn_key),
        #expects one result TODO, more that one result should not break the app
        stn_res = PollStation.gql("where belongsTo=:cn and name=:nm", cn=const_name, nm=stn_name).fetch(1)
        if (len(stn_res)) == 0:#polling station, const combi not found
            logger.info("%s-%s not found" % (stn_name, const_name))
            return None
        else:
            #retrieve first poll station from result list
            memcache.set(stn_key, stn_res[0])
            return stn_res[0]
    else:
        return stn#from memcache