def save_adoption_data(xml_tree): error_msg = '' try: xml_data = xml_tree.getElementsByTagName('data') commcare_user = CommCareUser.objects.get(guid = str(xml_tree.getElementsByTagName('n0:userID')[0].childNodes[0].nodeValue)) cocouser = commcare_user.coco_user for record in xml_data: try: adoption_data = {} adoption_data['date'] = record.getElementsByTagName('selected_date')[0].firstChild.data adoption_data['selected_person'] = record.getElementsByTagName('selected_person')[0].firstChild.data adoption_data['selected_video'] = record.getElementsByTagName('selected_video')[0].firstChild.data try: AdoptionExisting = PersonAdoptPractice.objects.filter(person_id=adoption_data['selected_person'], video_id=adoption_data['selected_video'], date_of_adoption=adoption_data['date']) status = error_list['DUPLICATE_ADOPTION'] error_msg = 'Duplicate Adoption' if not(len(AdoptionExisting)): pap = PersonAdoptPractice(person_id=adoption_data['selected_person'], date_of_adoption=adoption_data['date'], video_id=adoption_data['selected_video'], partner=cocouser.partner, user_created=cocouser.user) pap.full_clean() pap.save() status = 1 error_msg = 'Successful' except ValidationError, e: status = error_list['ADOPTION_SAVE_ERROR'] error_msg = 'adoption_save_error' error = "Error in Saving Adoption : " + str(e) sendmail("Exception in Mobile COCO. Adoption save error (Line 168)", error) except Exception as ex: status = error_list['ADOPTION_READ_ERROR'] error_msg = 'adoption_read_error' error = "Error in Reading Adoption : " + str(ex) sendmail("Exception in Mobile COCO. Adoption read error (Line 152)", error) except Exception as e: status = error_list['USER_NOT_FOUND'] error_msg = 'user_read_error' error = "Error in Reading User : "******"Exception in Mobile COCO. User read error (Line 147)", error) return status, error_msg
csv_file = open('op.csv', 'wb') a = csv.writer(csv_file) a.writerow(["Did not attend the screening"]) csv_data.next() videos = [] for row in csv_data: if row[0]: for pma in Person.objects.get( id=row[0]).personmeetingattendance_set.all(): for vid in pma.screening.videoes_screened.all(): videos.append(str(vid.id)) row[1] = datetime.datetime.strptime(row[1], "%d-%b-%y").strftime("20%y-%m-%d") try: if (row[2] in videos): c = PersonAdoptPractice(person_id=int(row[0]), video_id=int(row[2]), date_of_adoption=row[1], partner_id="13", user_created_id="1") c.save() else: a.writerow(row) except Exception, err: a.writerow([str(err)]) csvfile.close() csv_file.close()
from django.core.management import setup_environ import dg.settings setup_environ(dg.settings) import csv grd_file = open('path_adoptions_errors.csv', 'wb') wrtr = csv.writer(grd_file, delimiter=',', quotechar='"') from activities.models import PersonAdoptPractice import openpyxl as xl wb = xl.load_workbook(filename='PathAdoptions.xlsx') ws = wb.worksheets[0] i = 0 for row in ws.rows[1:]: i = i + 1 print i try: adoption = PersonAdoptPractice(user_created_id=127, partner_id=13, person_id=row[1].value, video_id=row[3].value, date_of_adoption=row[2].value) adoption.save() except Exception as e: wrtr.writerow( [i, row[0].value, row[1].value, row[2].value, row[3].value, e]) grd_file.flush() grd_file.close()
from activities.models import PersonAdoptPractice from people.models import Person csvfile = open("Adoption.csv", "rb") csv_data = csv.reader(csvfile) csv_file = open('op.csv', 'wb') a = csv.writer(csv_file) a.writerow(["Did not attend the screening"]) csv_data.next() videos = [] for row in csv_data: if row[0]: for pma in Person.objects.get(id=row[0]).personmeetingattendance_set.all(): for vid in pma.screening.videoes_screened.all(): videos.append(str(vid.id)) row[1] = datetime.datetime.strptime(row[1], "%d-%b-%y").strftime("20%y-%m-%d") try: if(row[2] in videos): c = PersonAdoptPractice(person_id=int(row[0]), video_id=int(row[2]), date_of_adoption=row[1], partner_id="13", user_created_id="1") c.save() else: a.writerow(row) except Exception, err: a.writerow([str(err)]) csvfile.close() csv_file.close()
from django.core.management import setup_environ import dg.settings setup_environ(dg.settings) import csv grd_file = open('path_adoptions_errors.csv', 'wb') wrtr = csv.writer(grd_file, delimiter=',', quotechar='"') from activities.models import PersonAdoptPractice import openpyxl as xl wb = xl.load_workbook(filename='PathAdoptions.xlsx') ws = wb.worksheets[0] i=0 for row in ws.rows[1:]: i = i + 1 print i try: adoption = PersonAdoptPractice(user_created_id = 127, partner_id = 13, person_id = row[1].value, video_id = row[3].value, date_of_adoption = row[2].value) adoption.save() except Exception as e: wrtr.writerow([i, row[0].value, row[1].value, row[2].value, row[3].value, e] ) grd_file.flush() grd_file.close()
def save_adoption_data(xml_tree): error_msg = '' try: xml_data = xml_tree.getElementsByTagName('data') commcare_user = CommCareUser.objects.get(guid=str( xml_tree.getElementsByTagName('n0:userID') [0].childNodes[0].nodeValue)) cocouser = commcare_user.coco_user for record in xml_data: try: adoption_data = {} adoption_data['date'] = record.getElementsByTagName( 'selected_date')[0].firstChild.data adoption_data['selected_person'] = record.getElementsByTagName( 'selected_person')[0].firstChild.data adoption_data['selected_video'] = record.getElementsByTagName( 'selected_video')[0].firstChild.data try: adoption_data[ 'verification_date'] = record.getElementsByTagName( 'verification_date')[0].firstChild.data except Exception: adoption_data['verification_date'] = None try: AdoptionExisting = PersonAdoptPractice.objects.filter( person_id=adoption_data['selected_person'], video_id=adoption_data['selected_video'], date_of_adoption=adoption_data['date']) status = error_list['DUPLICATE_ADOPTION'] error_msg = 'Duplicate Adoption' if not (len(AdoptionExisting)): pap = PersonAdoptPractice( person_id=adoption_data['selected_person'], date_of_adoption=adoption_data['date'], video_id=adoption_data['selected_video'], partner=cocouser.partner, user_created=cocouser.user, date_of_verification=adoption_data[ 'verification_date']) pap.full_clean() pap.save() status = 1 error_msg = 'Successful' except ValidationError, e: status = error_list['ADOPTION_SAVE_ERROR'] error_msg = 'adoption_save_error' error = "Error in Saving Adoption : " + str(e) sendmail( "Exception in Mobile COCO. Adoption save error (Line 168)", error) except Exception as ex: status = error_list['ADOPTION_READ_ERROR'] error_msg = 'adoption_read_error' error = "Error in Reading Adoption : " + str(ex) sendmail( "Exception in Mobile COCO. Adoption read error (Line 152)", error) except Exception as e: status = error_list['USER_NOT_FOUND'] error_msg = 'user_read_error' error = "Error in Reading User : "******"Exception in Mobile COCO. User read error (Line 147)", error) return status, error_msg