Example #1
0
    def testConnection(self):
        conn = Connection.get_conn()
        conn.set('asd', '123')
        conn.set('qwe', 123)
        self.assertEqual('123', conn.get('asd'))
        self.assertEqual(123, int(conn.get('qwe')))
        self.assertEqual(None, conn.get('sss'))

        conn.delete('asd')
        conn.delete('qwe')
            
Example #2
0
def del_set(request, day, outer_index, inner_index):
    if request.method == 'DELETE':
        inner_index = int(inner_index)
        outer_index = int(outer_index)

        if inner_index >= 0 and outer_index >= 0:
            conn = Connection.get_conn()
            list_name = 'workout:' + day

            try:
                l = eval(conn.lindex(list_name, outer_index))
                if isinstance(l, list) and inner_index < len(l):
                    del l[inner_index]
                    conn.lset(list_name, outer_index, l)
            except Exception, e:
                print str(e)
Example #3
0
def manage_set(request, cmd):
    if request.method != 'POST' \
            or not request.POST.has_key('day') \
            or not request.POST.has_key('index'):
        return HttpReponseRedirect('/')

    index = int(request.POST['index'])
    day = request.POST['day']

    list_name = 'workout:' + day    

    try:        
        conn = Connection.get_conn()
        list_len = conn.llen(list_name)
        if list_len > 1:
            def process_adjacent(ind1, ind2):
                try:
                    pipe = conn.pipeline()
                    if ind1 > 0:
                        curr_list = eval(conn.lindex(list_name, ind1))
                        curr = models.ExercizeSet.load_from_str(curr_list[0])
                        prev_list = eval(conn.lindex(list_name, ind1-1))
                        prev = models.ExercizeSet.load_from_str(prev_list[0])
                        if prev.exercize == curr.exercize:
                            curr_list = prev_list + curr_list
                            pipe.lset(list_name, ind1, curr_list)
                            pipe.lset(list_name, ind1-1, "TO_REMOVE")
                    pipe.execute()
                finally:
                    pipe.reset()

                try:
                    pipe = conn.pipeline()
                    if ind2 < list_len - 1:
                        curr_list = eval(conn.lindex(list_name, ind2))
                        curr = models.ExercizeSet.load_from_str(curr_list[0])
                        next_list = eval(conn.lindex(list_name, ind2+1))
                        next = models.ExercizeSet.load_from_str(next_list[0])
                        if next.exercize == curr.exercize:
                            curr_list = curr_list + next_list
                            pipe.lset(list_name, ind2, curr_list)
                            pipe.lset(list_name, ind2+1, "TO_REMOVE")
                    pipe.execute()
                finally:
                    pipe.reset()

                conn.lrem(list_name, 0, "TO_REMOVE")

            if cmd == 'up' and index > 0:
                first = conn.lindex(list_name, index-1)
                second = conn.lindex(list_name, index)
                conn.lset(list_name, index-1, second)
                conn.lset(list_name, index, first)

                process_adjacent(index-1, index)
            elif cmd == 'down' and index < list_len-1:
                first = conn.lindex(list_name, index)
                second = conn.lindex(list_name, index+1)
                conn.lset(list_name, index, second)
                conn.lset(list_name, index+1, first)

                process_adjacent(index, index+1)
    except Exception, ex:
        print str(ex)
        pass