def data_pipeline():
    reqdat = dict(request.form)
    print(reqdat)
    print(reqdat['record'])
    # Check the format returned sent by the REDCap server to determine if the values in the dict are lists or strings
    if type(reqdat['record']) is str:
      recordid = reqdat['record']
      instr = reqdat['instrument']
    elif type(reqdat['record']) is list:
      recordid = reqdat['record'][0]
      instr = reqdat['instrument'][0]
    if instr == 'demographics' or instr == 'enrollment':
      # Check to see if a new family member has been added by pulling the Enrollment table from the Data Collection project
      rep = reqdat['redcap_repeat_instance'][0]
      enroll = NDDdb.pullRCRecords(rc_api_url, rc_data_apikey, 'enrollment', recordid, 'redcap_id,demo_dateadded,demo_relation')
      enroll.set_index(keys='redcap_repeat_instance', drop = False, inplace = True, verify_integrity = True)
      lkid = enroll.loc[rep,'labkey_subjid']
      reln = enroll.loc[rep,'demo_relation']
      if reln!='':
        if lkid=='': # Assign LabKey ID by querying LabKey for next available integer
          lkid = str(NDDdb.getNextLabKeyID(server_context))
          NDDdb.assignLabKeyID(rc_api_url, rc_data_apikey, server_context, recordid, rep, lkid, reln_map[reln], enroll.loc[rep,'demo_dateadded'])
        idnum = enroll.loc[rep,'idnum']
        if idnum=='': # Assign individual number by pulling family record and checking for next available value--or, if relation is one of {Proband, Mother, Father}, assign designated numbers
          famdat = NDDdb.pullRCRecords(rc_api_url, rc_data_apikey, 'family_enrollment', recordid, '')
          fnrow = famdat.loc[famdat['redcap_event_name']=='family_data_arm_1',].index
          fnum = famdat.loc[fnrow,'fnum'].values[0]
          if fnum != '':
            print('Checking for next Ind num')
            idnum = NDDdb.getNextIndNum(reln,enroll['idnum'])
            print('Assigninf F-idnum')
            NDDdb.assignFindivID(rc_api_url, rc_refer_apikey, rc_data_apikey, server_context, None, recordid, rep, lkid, fnum, idnum)
    # Check to see if 'copy contact from' field has been set
    if instr == 'demographics':
      demo = NDDdb.pullRCRecords(rc_api_url, rc_data_apikey, 'demographics', recordid,'redcap_id')
      reptocopy = demo.loc[demo['redcap_repeat_instance']==rep]['demo_copycontactfrom'].values[0]
      if reptocopy != '':
        copyfrom = demo.loc[demo['redcap_repeat_instance']==reptocopy]
        pasteto = copyfrom[['redcap_id','demo_address1','demo_address2','demo_apt','demo_city','demo_state','demo_zip','demo_email','demo_email2','demo_phone','demo_phone2']]
        pasteto['demo_copycontactfrom'] = ''
        pasteto['redcap_event_name'] = 'family_member_arm_1'
        pasteto['redcap_repeat_instance'] = rep
        updateres = NDDdb.pushRCRecord(rc_api_url, rc_data_apikey, '', pasteto)
      # If the saved record is the proband's, copy his/her email to the family contact email
      if rep == '1':
        NDDdb.setFamEmail(rc_api_url, rc_data_apikey, demo.loc[demo['redcap_repeat_instance']=='1','demo_email'].values[0], recordid)
    sys.stdout.flush()
    return
def referral_pipeline():
  reqdat = dict(request.form)
  # Check the format returned sent by the REDCap server to determine if the values in the dict are lists or strings
  if type(reqdat['record']) is str:
    recordid = reqdat['record']
    instr = reqdat['instrument']
  elif type(reqdat['record']) is list:
    recordid = reqdat['record'][0]
    instr = reqdat['instrument'][0]
  # Pull record
  referral = NDDdb.pullRCRecords(rc_api_url, rc_refer_apikey, instr, recordid,'pintoreferral_stat,apptdate,idnum,f_idnum,dataproj_id,physician_referral_form_complete')
  referral = referral.loc[referral['redcap_repeat_instrument']==''] # TODO deprecate this temporary code
  referral.set_index(keys='redcap_id', drop = False, inplace = True, verify_integrity = True)
  # Fill in provider email if it hasn't been set already
  if instr == 'physician_referral_form':
    if referral.loc[recordid,'provider_email']=='':
      if referral.loc[recordid,'sinaistatus']!='':
        if referral.loc[recordid,'sinaistatus']=='1':
          provider_email = NDDdict.mshprovider2email[referral.loc[recordid,'sinaiprovider']]
        elif referral.loc[recordid,'sinaistatus']=='2':
          provider_email = NDDdict.nyuprovider2email[referral.loc[recordid,'nyuprovider']]
        else:
          provider_email = ''
      update = pd.DataFrame(data={'redcap_id':[recordid], 'provider_email':[provider_email]})
      updatereferreq = NDDdb.pushRCRecord(rc_api_url, rc_refer_apikey, '', update)
  # Get data collection id
  dcid = referral.loc[recordid,'dataproj_id']
  # If the data collection id is blank, query RedCap for the next one and set the 'I'm new here' flag
  imnewhere = False
  if dcid == '':
    imnewhere = True
    dcid = str(NDDdb.getNextDCID(rc_api_url, rc_data_apikey))
  # Get individual #
  idnum = referral.loc[recordid,'idnum']
  # then check value of 'referral_triggerenroll' and 'referral_id'
  # finally write new project id back to 'referral_id'
  if referral.loc[recordid,'referral_triggerenroll'] == '1' and referral.loc[recordid,'verifiedunique']=='1':
    idnum = NDDdb.pushToDC(rc_api_url, rc_refer_apikey, rc_data_apikey, referral, recordid, dcid, idnum, imnewhere)
    if imnewhere:
      lkupdate = pd.DataFrame(data={'id':[dcid], 'referralid':[recordid]})
      NDDdb.LKinsertRow(lkupdate.iloc[0], 'Families', 'lists', server_context)
      lkid = str(NDDdb.getNextLabKeyID(server_context))
      NDDdb.assignLabKeyID(rc_api_url, rc_data_apikey, server_context, dcid, '1', lkid, 'Proband', dt.datetime.today().strftime('%Y-%m-%d'))
  # Check if this subject was referred to Pinto study and has an appointment date
  # how to check for fam's record here?
  apptdate = referral.loc[recordid,'apptdate']
  if referral.loc[recordid,'pintoreferral_stat']=='1' and apptdate != '' and idnum == '01':
    # Assign F# if this subject does not have one already
    dc = NDDdb.pullRCRecords(rc_api_url, rc_data_apikey, 'family_enrollment', '', '')
    if len(dc) > 0:
      dc = dc.loc[dc['redcap_event_name']=='family_data_arm_1']
    dc.set_index(keys='redcap_id', drop = False, inplace = True, verify_integrity = True)
    fnum = dc.loc[str(dcid),'fnum']
    # Assign F# if this subject does not have one already
    if fnum == '':
      myfam = NDDdb.pullRCRecords(rc_api_url, rc_data_apikey, 'enrollment', dcid, '')
      myfam.set_index(keys='idnum', drop = False, inplace = True, verify_integrity = False)
      lkid = myfam.loc[idnum,'labkey_subjid']
      fnum = NDDdb.getNextFnum(apptdate,dc)
      NDDdb.assignFID(rc_api_url, rc_data_apikey, server_context, dcid, fnum)
      NDDdb.assignFindivID(rc_api_url, rc_refer_apikey, rc_data_apikey, server_context, recordid, dcid, '1', lkid, fnum, idnum)
  sys.stdout.flush()
  return ''