def update_queue_entry(sess,clusterurl,qitem,db,status,info,response):
 try:
  thisrev = api_utils.get_doc_rev(sess,clusterurl+'/'+db,qitem['_id'])
  thisdoc = qitem
  if len(thisrev) > 0:
   thisdoc['_rev'] = thisrev
  thisdoc['status'] = status
  if info is not None:
   thisdoc['info'] = info
  if response is not None:
   thisdoc['response'] = response
  now = datetime.datetime.now()
  thisdoc['updated'] = now.strftime("%Y-%m-%d %H:%M:%S.%f")
  if status == 'success' or status == 'failed':
   thisdoc['completed'] = now.strftime("%Y-%m-%d %H:%M:%S.%f")
  doc_response = sess.put(clusterurl+'/'+db+'/'+qitem['_id'],data=json.dumps(thisdoc),headers={'content-type':'application/json'}) 
  if doc_response is None:
        logging.warn("{worker queue update} Update Status Error: No response")
        return False
  elif doc_response.status_code > 250:
        logging.warn("{worker queue update} Update Status Error : " + str(doc_response.status_code))
        return False
  else:
        return True 
 except Exception as e:
  logging.warn("{worker queue update} Update Status Error : " + str(e))
  return False
def delnew(sess, clusterurl, qitem):
    try:
        logging.warn("{View Migrater} DeleteTemps Step (New): Starting")
        newurl = clusterurl + '/' + qitem['db'] + '/_design/' + qitem[
            'ddid'] + '_NEW'
        new = '_design/' + qitem['ddid'] + '_NEW'
        newrev = api_utils.get_doc_rev(sess, clusterurl + '/' + qitem['db'],
                                       new)
        delnew_response = sess.delete(newurl + '?rev=' + newrev)
        if delnew_response is None:
            logging.warn(
                "{View Migrater} Deltemps Step (New) Error: No response")
            update_queue_entry(sess, clusterurl, qitem, 'apimigratequeue',
                               "failed", "Deltemps Step Error : No response",
                               None)
            return False
        elif delnew_response.status_code > 250:
            logging.warn(
                "{View Migrater} Deltemps Step Session (New) Error : " +
                str(delnew_response.status_code))
            update_queue_entry(
                sess, clusterurl, qitem, 'apimigratequeue', "failed",
                "Deltemps Step Session (New) Error : " +
                str(delnew_response.status_code), None)
            return False
        else:
            return True
    except Exception as e:
        logging.warn("{View Migrater} Deltemps Step (New) Error : " + str(e))
        return False
def delold(sess, clusterurl, qitem):
    try:
        logging.warn("{View Migrater} DeleteTemps Step (Old): Starting")
        old = '_design/' + qitem['ddid'] + '_OLD'
        oldurl = clusterurl + '/' + qitem['db'] + '/_design/' + qitem[
            'ddid'] + '_OLD'
        oldrev = api_utils.get_doc_rev(sess, clusterurl + '/' + qitem['db'],
                                       old)
        delold_response = sess.delete(oldurl + '?rev=' + oldrev)
        if delold_response is None:
            logging.warn(
                "{View Migrater} Deltemps Step (Old) Error: No response")
            update_queue_entry(sess, clusterurl, qitem, 'apimigratequeue',
                               "failed",
                               "Deltemps Step (Old) Error : No response", None)
            return False
        elif delold_response.status_code > 250:
            logging.warn(
                "{View Migrater} Deltemps Step (Old) Session Error : " +
                str(delold_response.status_code))
            update_queue_entry(
                sess, clusterurl, qitem, 'apimigratequeue', "failed",
                "Deltemps Step (Old) Session Error : " +
                str(delold_response.status_code), None)
            return False
        else:
            return True
    except Exception as e:
        logging.warn("{View Migrater} Deltemps Step (Old) Error : " + str(e))
        return False
def copyfinal(sess, clusterurl, qitem):
    try:
        # note issue 97653 : Destination should not include the db or a leading /
        newurl = clusterurl + '/' + qitem['db'] + '/_design/' + qitem[
            'ddid'] + '_NEW'
        finalrev = api_utils.get_doc_rev(sess, clusterurl + '/' + qitem['db'],
                                         '_design/' + qitem['ddid'])
        final = '_design/' + qitem['ddid'] + '?rev=' + finalrev
        logging.warn("{View Migrater} CopyFinal Step : Starting")
        copyfinal_response = sess.request('COPY',
                                          newurl,
                                          headers={'Destination': final},
                                          verify=False)
        if copyfinal_response is None:
            logging.warn("{View Migrater} CopyFinal Step Error: No response")
            update_queue_entry(sess, clusterurl, qitem, 'apimigratequeue',
                               "failed", "CopyFinal Step Error : No response",
                               None)
            return False
        elif copyfinal_response.status_code > 250:
            logging.warn("{View Migrater} CopyFinal Step Session Error : " +
                         str(copyfinal_response.status_code))
            update_queue_entry(
                sess, clusterurl, qitem, 'apimigratequeue', "failed",
                "CopyFinal Step Session Error : " +
                str(copyfinal_response.status_code), None)
            return False
        else:
            return True
    except Exception as e:
        logging.warn("{View Migrater} CopyFinal Step Error : " + str(e))
        return False
def process_delete(request,sess,dbid,ddid):
     thisurl = request.url_root+dbid+'/_design/'+ddid
     thisrev = api_utils.get_doc_rev(sess,request.url_root+dbid,'_design/'+ddid)
     print(thisurl+'?rev='+thisrev)
     del_response = sess.delete(thisurl+'?rev='+thisrev)
     if del_response is None:
      return {'error': 'API Access Error'},400
     elif del_response.status_code > 250:
      return {'error': 'API Access Session Error ('+str(del_response.status_code)+')'},del_response.status_code
     else:
      data = del_response.json()
      datastring = json.dumps(data)
      return datastring,None