def get_positions_count(userids):
    dtt = form.getvalue("dt","-1");
    dttt = []
    dttt = dtt.split("mm")
    dtt = "between '"+py_db.sh_to_mil(dttt[0])+" "+dttt[1]+":00' and '"+py_db.sh_to_mil(dttt[2])+" "+dttt[3]+":00' "
    sqlst = "select count(*)  from `tbl_gps_positions` WHERE  g_sat_count  > 5 and  g_user_id in ({1})  and g_date {0} ".format(dtt,userids)
    print 'Content-Type: text/html; charset=utf-8'
    print ""
    print py_db.executeAndReturnRows(sqlst)[0][0].__str__()
def get_pos(sel2):
    dtt = form.getvalue("dt2","-1");
    ttl = form.getvalue("totalrows","-1")
    dttt = []
    dttt = dtt.split("mm")
    dtt = "between '"+py_db.sh_to_mil(dttt[0])+" "+dttt[1]+":00' and '"+py_db.sh_to_mil(dttt[2])+" "+dttt[3]+":00' "
  
    sqlst ="""SELECT w.* FROM (SELECT @row := @row + 1 AS _ROW, t.* FROM `tbl_gps_positions` t,(SELECT @row:=0) r WHERE g_sat_count  > 5 and  g_user_id IN ({0})  AND g_date {1} ORDER BY g_date DESC ) w WHERE MOD(_ROW,{2})=0  """.format(sel2,dtt,ttl)
    rows = py_db.executeAndReturnRows(sqlst)
    str = ""
    i=0
    for row in rows :
        if i==0:
            str = "{0}!{1}!{2}!{3}!{4}".format(row[2].__str__(),row[3].__str__(),row[4].__str__(), py_db.mil_to_sh_with_time(row[6].__str__()),row[7].__str__())
        else:
            str = str+"cz{0}!{1}!{2}!{3}!{4}".format(row[2].__str__(),row[3].__str__(),row[4].__str__(),py_db.mil_to_sh_with_time(row[6].__str__()),row[7].__str__())
        i+=1
    print 'Content-Type: text/html; charset=utf-8'
    print ""
    print str
    rows = dta.split("sazw")
    for row in rows:
        r = row.split("zzz")
        
        if r[0]=="_obj_chk_unread":
            if r[1]=="true": sds =sds+ " i_position = 1 "
        if r[0]=="_obj_chk_read":
            if r[1]=="true": sds =sds+ " or i_position = 2 "
        if r[0]=="_obj_chk_sent":
            if r[1]=="true": sds =sds+ " or i_position = 3 "
        if r[0]=="_obj_chk_baygani":
            if r[1]=="true":
                sds =sds+ " or i_active_user_link = 43160 "
                baygani=1
        
        if r[0]=="_obj_from": sds =sds+ "and i_date_of_create between '"+py_db.sh_to_mil(r[1])+"' and "
        if r[0]=="_obj_to": sds =sds+ " '"+py_db.sh_to_mil(r[1])+"' "
        if r[0]=="_obj_search": sds=sds+ """
        ) AND (    
        ((i_order LIKE '%{0}%') OR 
        (l_subject LIKE '%{0}%') OR
        (l_letter_number LIKE '%{0}%') OR
        (((SELECT tbl_secretrait_letters.sl_secretariat_no FROM tbl_secretrait_letters WHERE sl_letter_link=i_letter_link LIMIT 1)) LIKE '%{0}%') OR 
        ((SELECT u_complete_name FROM tbl_users WHERE u_name='{1}' ) LIKE '%{0}%') OR 
        ((SELECT u_complete_name FROM tbl_users WHERE u_id=i_prev_active_user_link ) LIKE '%{0}%'))  
            )""".format(r[1],py_db.username)
    if sds[0:3]==" or": sds=sds[4:len(sds)]
    if sds[0:4]==" and": sds=sds[5:len(sds)]
#    print sds