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
Example #3
0
 def setUp(self):
     #const
     self.const = Constcy(name = "Emba", year = 2010, number = "999", nameStartsWith="E")
     #polymodel
     self.station = PollStation(name = "Caltex", year = 2007, belongsTo = "Emba", number = "007", nameStartsWith="C")
Example #4
0
class Test(unittest.TestCase):

    def setUp(self):
        #const
        self.const = Constcy(name = "Emba", year = 2010, number = "999", nameStartsWith="E")
        #polymodel
        self.station = PollStation(name = "Caltex", year = 2007, belongsTo = "Emba", number = "007", nameStartsWith="C")

    def tearDown(self):
        pass
    
    def test_local_consty(self):
        self.model_const_props_test(self.const)
        
    def test_local_poll(self):
        self.model_poll_props(self.station)
        
    def model_local_dirn(self):
        self.model_dirn_props(self.direction)

    def model_const_props_test(self, const):
        self.assertEqual("999", const.number)
        self.assertEqual(2010, const.year)
        self.assertEqual("Emba", const.name)
    
    def model_poll_props(self, station):
        self.assertEqual("007", station.number)
        self.assertEqual(2007, station.year)
        self.assertEqual("Caltex", station.name)
        self.assertEqual("Emba", station.belongsTo)
        
    def model_dirn_props(self, dirn):
        self.assertEquals("by the still waters\n I will walk", dirn.text)
        self.assertEquals("mimi", dirn.author)
        
    def test_put_gae_store(self):
#        key = self.const.put()
#        logging.info("create constcy with constcy_key %s" % key)
        #does not work for polymorphic classes
        #allConst = db.GqlQuery("select __key__ from Constcy where number = :1", "999")
        #allConst = Constcy.gql("where number = :1", "999")
        #self.assertEqual(1, allConst.count()) doesn't work
        #list = allConst.fetch(limit=1)
        #logging.debug(dir(list[0]))
        #self.model_const_props_test(allConst.get())
        
        #gae store
        constcy_key = self.const.put()
        logging.info("put constcy with key %s" % constcy_key)
        stn_key = self.station.put()
        logging.info("put station with key %s" % stn_key)
        #directions
        from google.appengine.api.users import User
        self.direction = Directions(text="by the still waters\n I will walk", author=User(email='*****@*****.**'),
                                     forStation=self.station)
        dirn_key = self.direction.put()
        logging.info("put direction with key %s" % dirn_key)
        #test retrieved values
        self.model_const_props_test(db.get(constcy_key))
        self.model_poll_props(db.get(stn_key))
        self.model_dirn_props(db.get(dirn_key))
        self.model_poll_props(db.get(dirn_key).forStation)#test model - station - referred to