def uno_save(document, filename, type): """ Save document document: desktop.loadComponentFromURL filename: filename of output without ext type: extension, example odt """ from com.sun.star.beans import PropertyValue tmp = tempfile.NamedTemporaryFile() # Strictly sanitize filename since Content-Disposition is really fussy filename = re.sub( '[^A-Za-z0-9]+', '_', strip_unicode_to_ascii(filename) ) if type == "doc": properties = ( PropertyValue("Overwrite",0,True,0), PropertyValue("FilterName",0,"MS Word 97",0)) document.storeToURL("file://" + str(tmp.name), properties) content = "application/msword" filename += ".doc" if type == "docx": properties = ( PropertyValue("Overwrite",0,True,0), PropertyValue("FilterName",0,"MS Word 2007 XML",0)) document.storeToURL("file://" + str(tmp.name), properties) content = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" filename += ".docx" elif type == "pdf": properties = ( PropertyValue("Overwrite",0,True,0), PropertyValue("FilterName",0,"writer_pdf_Export",0)) document.storeToURL("file://" + str(tmp.name), properties) content = "application/pdf" filename += ".pdf" elif type == "ods": document.storeToURL("file://" + str(tmp.name), ()) content = "application/vnd.oasis.opendocument.spreadsheet" filename += ".ods" elif type == "xlsx": properties = ( PropertyValue("Overwrite",0,True,0), PropertyValue("FilterName",0,"Calc MS Excel 2007 XML",0)) document.storeToURL("file://" + str(tmp.name), properties) content = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" filename += ".xlsx" elif type == "xls": properties = ( PropertyValue("Overwrite",0,True,0), PropertyValue("FilterName",0,"MS Excel 97",0)) document.storeToURL("file://" + str(tmp.name), properties) content = "application/vnd.ms-excel" filename += ".xls" else: document.storeToURL("file://" + str(tmp.name), ()) content = "application/vnd.oasis.opendocument.text" filename += ".odt" document.close(True) return tmp, filename, content
def uno_save(document, filename, type): """ Save document document: desktop.loadComponentFromURL filename: filename of output without ext type: extension, example odt """ from com.sun.star.beans import PropertyValue tmp = tempfile.NamedTemporaryFile() # Strictly sanitize filename since Content-Disposition is really fussy filename = re.sub('[^A-Za-z0-9]+', '_', strip_unicode_to_ascii(filename)) if type == "doc": properties = (PropertyValue("Overwrite", 0, True, 0), PropertyValue("FilterName", 0, "MS Word 97", 0)) document.storeToURL("file://" + str(tmp.name), properties) content = "application/msword" filename += ".doc" if type == "docx": properties = (PropertyValue("Overwrite", 0, True, 0), PropertyValue("FilterName", 0, "MS Word 2007 XML", 0)) document.storeToURL("file://" + str(tmp.name), properties) content = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" filename += ".docx" elif type == "pdf": properties = (PropertyValue("Overwrite", 0, True, 0), PropertyValue("FilterName", 0, "writer_pdf_Export", 0)) document.storeToURL("file://" + str(tmp.name), properties) content = "application/pdf" filename += ".pdf" elif type == "ods": document.storeToURL("file://" + str(tmp.name), ()) content = "application/vnd.oasis.opendocument.spreadsheet" filename += ".ods" elif type == "xlsx": properties = (PropertyValue("Overwrite", 0, True, 0), PropertyValue("FilterName", 0, "Calc MS Excel 2007 XML", 0)) document.storeToURL("file://" + str(tmp.name), properties) content = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" filename += ".xlsx" elif type == "xls": properties = (PropertyValue("Overwrite", 0, True, 0), PropertyValue("FilterName", 0, "MS Excel 97", 0)) document.storeToURL("file://" + str(tmp.name), properties) content = "application/vnd.ms-excel" filename += ".xls" else: document.storeToURL("file://" + str(tmp.name), ()) content = "application/vnd.oasis.opendocument.text" filename += ".odt" document.close(True) return tmp, filename, content
def as_download(self): """ Returns a django HttpResponse with the xlsx file """ myfile = StringIO.StringIO() myfile.write(save_virtual_workbook(self.workbook)) response = HttpResponse( myfile.getvalue(), content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') safe_filename = re.sub( '[^A-Za-z0-9]+', '_', strip_unicode_to_ascii(self.file_name) ) response['Content-Disposition'] = 'attachment; filename=%s.xlsx' % safe_filename response['Content-Length'] = myfile.tell() return response
def pod_save(self, template, ext=None, get_tmp_file=False): """ Use Appy POD to make the report Returns a HttpRepsonse with the file. """ import time if ext == None: #ext = "." + self.file_format ext = ".odt" # Strictly sanitize filename since Content-Disposition is really fussy filename = re.sub( '[^A-Za-z0-9]+', '_', strip_unicode_to_ascii(self.filename) ) file_name = tempfile.gettempdir() + '/appy' + str(time.time()) + ext renderer = Renderer(template, self.data, file_name) renderer.run() if ext == ".doc": content = "application/msword" elif ext == ".docx": content = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" elif ext == ".pdf": content = "application/pdf" elif ext == ".rtf": content = "application/rtf" elif ext == '.ods': content = "application/vnd.oasis.opendocument.spreadsheet" else: # odt, prefered content = "application/vnd.oasis.opendocument.text" if get_tmp_file: return file_name wrapper = FileWrapper(file(file_name)) # notice not using the tmp file! Not ideal. response = HttpResponse(wrapper, content_type=content) response['Content-Length'] = os.path.getsize(file_name) response['Content-Disposition'] = 'attachment; filename=' + filename + ext try: os.remove(file_name) except: pass # this sucks. But Ubuntu can't run ooo as www-data return response
def update_list(self): """ We have to use a odd not quite soap based method. Using suds or other soap library will cause errors in some funcitons, probably Groupcast's fault. This xml was captured from intercepting xml sent by Groupcast's EZ data sync .NET app. """ numbers = [] exts = [] fnames = [] lnames = [] emails = [] m1s = [] m2s = [] m3s = [] m4s = [] m5s = [] for student in Student.objects.filter(is_active=True): student_number = student.get_phone_number() if student_number: numbers += [student_number.number] exts += [student_number.ext] fnames += [student.first_name] lnames += [student.last_name] emails += [student.get_email] m1s += [student.year] m2s += [student.cache_cohort] if hasattr(student, 'studentworker') and student.studentworker: m3s += [student.studentworker.day] else: m3s += [''] m4s += ['Student'] m5s += [student_number.type] ec = student.get_primary_emergency_contact() if ec: parent_numbers = [] if ec.emergencycontactnumber_set.filter(type="H"): parent_numbers += [ec.emergencycontactnumber_set.filter(type="H")[0]] if ec.emergencycontactnumber_set.filter(type="C"): parent_numbers += [ec.emergencycontactnumber_set.filter(type="C")[0]] if ec.emergencycontactnumber_set.filter(type="W"): parent_numbers += [ec.emergencycontactnumber_set.filter(type="W")[0]] if not parent_numbers and ec.emergencycontactnumber_set.all(): # Only include this is no phone types are set parent_numbers +=[ec.emergencycontactnumber_set.all()[0]] for number in parent_numbers: numbers += [number.number] exts += [number.ext] fnames += [ec.fname] lnames += [ec.lname] emails += ['']#[ec.email] m1s += [student.year] m2s += [student.cache_cohort] if hasattr(student, 'studentworker') and student.studentworker: m3s += [student.studentworker.day] else: m3s += [''] m4s += [student] m5s += [number.type] # Make data arrays into xml strings xml_numbers = '' for data_string in numbers: xml_numbers += '<num>%s</num>' % data_string xml_exts = '' for data_string in exts: if data_string == None: data_string = '' xml_exts += '<ext>%s</ext>' % re.sub("[^0-9]", "", data_string) xml_fnames= '' for data_string in fnames: xml_fnames += '<first>%s</first>' % strip_unicode_to_ascii(data_string) xml_lnames = '' for data_string in lnames: xml_lnames += '<last>%s</last>' % strip_unicode_to_ascii(data_string) xml_emails = '' for data_string in emails: xml_emails += '<email>%s</email>' % data_string xml_m1s = '' for data_string in m1s: xml_m1s += '<mField1>%s</mField1>' % strip_unicode_to_ascii(data_string) xml_m2s = '' for data_string in m2s: xml_m2s += '<mField2>%s</mField2>' % strip_unicode_to_ascii(data_string) xml_m3s = '' for data_string in m3s: xml_m3s += '<mField3>%s</mField3>' % strip_unicode_to_ascii(data_string) xml_m4s = '' for data_string in m4s: xml_m4s += '<mField4>%s</mField4>' % strip_unicode_to_ascii(data_string) xml_m5s = '' for data_string in m5s: xml_m5s += '<mField5>%s</mField5>' % strip_unicode_to_ascii(data_string) url = 'https://app.groupcast.com/WARP/GroupcastWARP.asmx?op=SetList&wsdl' xml_dict = { 'userid': settings.SCHOOLREACH_USERID, 'pin': settings.SCHOOLREACH_PIN, 'list_id': settings.SCHOOLREACH_LIST_ID, 'xml_numbers': xml_numbers.replace("-", ""), 'xml_exts': xml_exts, 'xml_fnames': xml_fnames, 'xml_lnames': xml_lnames, 'xml_emails': xml_emails, 'xml_m1s': xml_m1s, 'xml_m2s': xml_m2s, 'xml_m3s': xml_m3s, 'xml_m4s': xml_m4s, 'xml_m5s': xml_m5s, } xml = """<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <setList2010 xmlns="https://app.groupcast.com/WARP/messages"> <appId>1</appId> <verId>3.0.11</verId> <userId>%(userid)s</userId> <ownerPin>%(pin)s</ownerPin> <affectedPin>%(pin)s</affectedPin> <PhoneNumberArray> %(xml_numbers)s </PhoneNumberArray> <Extension> %(xml_exts)s </Extension> <FName> %(xml_fnames)s </FName> <LName> %(xml_lnames)s </LName> <EMail> %(xml_emails)s </EMail> <MetaField1> %(xml_m1s)s </MetaField1> <MetaField2> %(xml_m2s)s </MetaField2> <MetaField3> %(xml_m3s)s </MetaField3> <MetaField4> %(xml_m4s)s </MetaField4> <MetaField5> %(xml_m5s)s </MetaField5> <append>0</append> <useShortCode>1</useShortCode> <ListNumber>%(list_id)s</ListNumber> <isIntl>0</isIntl></setList2010></soap:Body></soap:Envelope>""" % xml_dict header = { 'SOAPAction': "app.GroupCast.com:setList2010In", 'Content-Type': 'text/xml; charset=utf-8', 'Expect': '100-continue', 'Host': 'app.GroupCast.com', } result = requests.post(url, data=xml, headers=header) return result
def create_new_naviance_students(): """ Naviance has no update or create. So this must be seperate. We just run each one and half will always fail. """ data = [['Student_ID','Class_Year','Last Name','First Name','Middle Name','Gender','Birthdate','GPA']] for student in Student.objects.filter(inactive=False): row = [] if settings.NAVIANCE_SWORD_ID == "username": row += [student.username] elif settings.NAVIANCE_SWORD_ID == "unique_id": row += [student.unique_id] else: row += [student.id] if student.class_of_year: row += [str(student.class_of_year.year)] else: row += [''] row += [ strip_unicode_to_ascii(student.lname), strip_unicode_to_ascii(student.fname), strip_unicode_to_ascii(student.mname), student.sex, ] if student.bday: row += [student.bday.strftime('%Y%m%d')] else: row += [''] row += [student.gpa] data += [row] temp = tempfile.TemporaryFile() wr = csv.writer(temp,quoting=csv.QUOTE_ALL) wr.writerows(data) temp.seek(0) params = { 'account':settings.NAVIANCE_ACCOUNT, 'username':settings.NAVIANCE_IMPORT_USERNAME, 'key':settings.NAVIANCE_IMPORT_KEY, 'type':'1', 'format':'CSV', 'header':'Yes', 'email':settings.NAVIANCE_EMAILS, 'description':'django-sis import', } files = {'datafile': ('import.csv',temp)} response = requests.post('https://services.naviance.com/school_import.php',data=params,files=files) if response.text != 'Success.\n': raise Exception("Error in Naviance Data create import: %s" % (response.text,)) temp.seek(0) files = {'datafile': ('import.csv',temp)} params = { 'account':settings.NAVIANCE_ACCOUNT, 'username':settings.NAVIANCE_IMPORT_USERNAME, 'key':settings.NAVIANCE_IMPORT_KEY, 'type':'11', 'format':'CSV', 'header':'Yes', 'email':settings.NAVIANCE_EMAILS, 'description':'django-sis import', } response = requests.post('https://services.naviance.com/school_import.php',data=params,files=files) if response.text != 'Success.\n': raise Exception("Error in Naviance Data update import: %s" % (response.text,))
def create_new_naviance_students(): """ Naviance has no update or create. So this must be seperate. We just run each one and half will always fail. """ if config.NAVIANCE_IMPORT_KEY: data = [[ 'Student_ID', 'Class_Year', 'Last Name', 'First Name', 'Middle Name', 'Gender', 'Birthdate', 'GPA' ]] for student in Student.objects.filter(is_active=True): row = [] if config.NAVIANCE_SWORD_ID == "username": row += [student.username] elif config.NAVIANCE_SWORD_ID == "unique_id": row += [student.unique_id] else: row += [student.id] if student.class_of_year: row += [str(student.class_of_year.year)] else: row += [''] row += [ strip_unicode_to_ascii(student.last_name), strip_unicode_to_ascii(student.first_name), strip_unicode_to_ascii(student.mname), student.sex, ] if student.bday: row += [student.bday.strftime('%Y%m%d')] else: row += [''] row += [student.gpa] data += [row] temp = tempfile.TemporaryFile() wr = csv.writer(temp, quoting=csv.QUOTE_ALL) wr.writerows(data) temp.seek(0) params = { 'account': config.NAVIANCE_ACCOUNT, 'username': config.NAVIANCE_IMPORT_USERNAME, 'key': config.NAVIANCE_IMPORT_KEY, 'type': '1', 'format': 'CSV', 'header': 'Yes', 'email': config.NAVIANCE_EMAILS, 'description': 'django-sis import', } files = {'datafile': ('import.csv', temp)} response = requests.post( 'https://services.naviance.com/school_import.php', data=params, files=files) if response.text != 'Success.\n': raise Exception("Error in Naviance Data create import: %s" % (response.text, )) temp.seek(0) files = {'datafile': ('import.csv', temp)} params = { 'account': config.NAVIANCE_ACCOUNT, 'username': config.NAVIANCE_IMPORT_USERNAME, 'key': config.NAVIANCE_IMPORT_KEY, 'type': '11', 'format': 'CSV', 'header': 'Yes', 'email': config.NAVIANCE_EMAILS, 'description': 'django-sis import', } response = requests.post( 'https://services.naviance.com/school_import.php', data=params, files=files) if response.text != 'Success.\n': raise Exception("Error in Naviance Data update import: %s" % (response.text, ))
def make_pdf(instance): global entire_testtag, id teacher_section_required = False doc = minidom.Document() testtag = doc.createElement("test") id = doc.createElement("id") testtag.appendChild(id) if instance: idtext = doc.createTextNode(str(instance.id)) else: idtext = doc.createTextNode(str(test.id)) id.setAttribute("testid",str(test.id)) id.appendChild(idtext) titletag = doc.createElement("title") id.appendChild(titletag) titletext = doc.createTextNode(test.name) titletag.appendChild(titletext) studentsection = doc.createElement("section") studentsection.setAttribute("type","student") id.appendChild(studentsection) studentnametag = doc.createElement("name") studentsection.appendChild(studentnametag) if instance: studentsection.setAttribute("studentid",str(instance.id)) studentname = doc.createTextNode(strip_unicode_to_ascii(instance.student.first_name + " " + instance.student.last_name)) studentnametag.appendChild(studentname) else: studentsection.setAttribute("studentid","0") studentname = doc.createTextNode(" ") studentnametag.appendChild(studentname) questions = test.question_set.order_by('order') essays = [] i = 1 # Question number for human use only for q in questions: questiontag = doc.createElement("question") questiontag.setAttribute("varName",str(q.id)) studentsection.appendChild(questiontag) question_number = doc.createElement("text") questiontag.appendChild(question_number) if q.type == "Essay": essays.append([q,q.id,i]) teacher_section_required = True text = str(i) + ". Essay Question" else: text = str(i) + ". " answers = [] choices = q.answer_set.order_by('id') if q.type == "Multiple Choice": ct=0 alphabet=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] for answer in choices: answers.append((answer.id,str(alphabet[ct]))) ct=ct+1 elif q.type == "True/False": idlist = [] for answer in choices: idlist.append(answer.id) label = ["T","F"] answers.append((idlist[0],label[0])) answers.append((idlist[1],label[1])) for answer_id, choice in answers: choicetag = doc.createElement("choice") questiontag.appendChild(choicetag) choicetagtext = doc.createTextNode(str(choice)) choicetag.appendChild(choicetagtext) choicevaluetag = doc.createElement("value") choicevalue = doc.createTextNode(str(answer_id)) choicetag.appendChild(choicevaluetag) choicevaluetag.appendChild(choicevalue) question_numbertext = doc.createTextNode(text) question_number.appendChild(question_numbertext) i=i+1 if teacher_section_required: teachersection = doc.createElement("section") teachersection.setAttribute("type","teacher") id.appendChild(teachersection) teachertexttag = doc.createElement("name") teachersection.appendChild(teachertexttag) teachertext = doc.createTextNode("For Teacher Use Only") teachertexttag.appendChild(teachertext) for q,qid,number in essays: teacher_question = doc.createElement("question") teacher_question.setAttribute("varName",str(qid)) teachersection.appendChild(teacher_question) teacher_question_number = doc.createElement("text") teacher_question.appendChild(teacher_question_number) teacher_question_numbertext = doc.createTextNode(str(number) + ". ") teacher_question_number.appendChild(teacher_question_numbertext) options = Answer.objects.filter(question=q) for choice in options: choicetag = doc.createElement("choice") teacher_question.appendChild(choicetag) choicetagtext = doc.createTextNode(str(choice.point_value)) choicetag.appendChild(choicetagtext) choicevaluetag = doc.createElement("value") choicevalue = doc.createTextNode(str(choice.id)) choicetag.appendChild(choicevaluetag) choicevaluetag.appendChild(choicevalue) entire_testtag.appendChild(id.cloneNode(True))
def make_pdf(instance): global entire_testtag, id teacher_section_required = False doc = minidom.Document() testtag = doc.createElement("test") id = doc.createElement("id") testtag.appendChild(id) if instance: idtext = doc.createTextNode(str(instance.id)) else: idtext = doc.createTextNode(str(test.id)) id.setAttribute("testid", str(test.id)) id.appendChild(idtext) titletag = doc.createElement("title") id.appendChild(titletag) titletext = doc.createTextNode(test.name) titletag.appendChild(titletext) studentsection = doc.createElement("section") studentsection.setAttribute("type", "student") id.appendChild(studentsection) studentnametag = doc.createElement("name") studentsection.appendChild(studentnametag) if instance: studentsection.setAttribute("studentid", str(instance.id)) studentname = doc.createTextNode( strip_unicode_to_ascii(instance.student.first_name + " " + instance.student.last_name)) studentnametag.appendChild(studentname) else: studentsection.setAttribute("studentid", "0") studentname = doc.createTextNode(" ") studentnametag.appendChild(studentname) questions = test.question_set.order_by('order') essays = [] i = 1 # Question number for human use only for q in questions: questiontag = doc.createElement("question") questiontag.setAttribute("varName", str(q.id)) studentsection.appendChild(questiontag) question_number = doc.createElement("text") questiontag.appendChild(question_number) if q.type == "Essay": essays.append([q, q.id, i]) teacher_section_required = True text = str(i) + ". Essay Question" else: text = str(i) + ". " answers = [] choices = q.answer_set.order_by('id') if q.type == "Multiple Choice": ct = 0 alphabet = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ] for answer in choices: answers.append((answer.id, str(alphabet[ct]))) ct = ct + 1 elif q.type == "True/False": idlist = [] for answer in choices: idlist.append(answer.id) label = ["T", "F"] answers.append((idlist[0], label[0])) answers.append((idlist[1], label[1])) for answer_id, choice in answers: choicetag = doc.createElement("choice") questiontag.appendChild(choicetag) choicetagtext = doc.createTextNode(str(choice)) choicetag.appendChild(choicetagtext) choicevaluetag = doc.createElement("value") choicevalue = doc.createTextNode(str(answer_id)) choicetag.appendChild(choicevaluetag) choicevaluetag.appendChild(choicevalue) question_numbertext = doc.createTextNode(text) question_number.appendChild(question_numbertext) i = i + 1 if teacher_section_required: teachersection = doc.createElement("section") teachersection.setAttribute("type", "teacher") id.appendChild(teachersection) teachertexttag = doc.createElement("name") teachersection.appendChild(teachertexttag) teachertext = doc.createTextNode("For Teacher Use Only") teachertexttag.appendChild(teachertext) for q, qid, number in essays: teacher_question = doc.createElement("question") teacher_question.setAttribute("varName", str(qid)) teachersection.appendChild(teacher_question) teacher_question_number = doc.createElement("text") teacher_question.appendChild(teacher_question_number) teacher_question_numbertext = doc.createTextNode( str(number) + ". ") teacher_question_number.appendChild( teacher_question_numbertext) options = Answer.objects.filter(question=q) for choice in options: choicetag = doc.createElement("choice") teacher_question.appendChild(choicetag) choicetagtext = doc.createTextNode(str(choice.point_value)) choicetag.appendChild(choicetagtext) choicevaluetag = doc.createElement("value") choicevalue = doc.createTextNode(str(choice.id)) choicetag.appendChild(choicevaluetag) choicevaluetag.appendChild(choicevalue) entire_testtag.appendChild(id.cloneNode(True))
def update_list(self): """ We have to use a odd not quite soap based method. Using suds or other soap library will cause errors in some funcitons, probably Groupcast's fault. This xml was captured from intercepting xml sent by Groupcast's EZ data sync .NET app. """ numbers = [] exts = [] fnames = [] lnames = [] emails = [] m1s = [] m2s = [] m3s = [] m4s = [] m5s = [] for student in Student.objects.filter(is_active=True): student_number = student.get_phone_number() if student_number: numbers += [student_number.number] exts += [student_number.ext] fnames += [student.first_name] lnames += [student.last_name] emails += [student.get_email] m1s += [student.year] m2s += [student.cache_cohort] if hasattr(student, 'studentworker') and student.studentworker: m3s += [student.studentworker.day] else: m3s += [''] m4s += ['Student'] m5s += [student_number.type] ec = student.get_primary_emergency_contact() if ec: parent_numbers = [] if ec.emergencycontactnumber_set.filter(type="H"): parent_numbers += [ec.emergencycontactnumber_set.filter(type="H")[0]] if ec.emergencycontactnumber_set.filter(type="C"): parent_numbers += [ec.emergencycontactnumber_set.filter(type="C")[0]] if ec.emergencycontactnumber_set.filter(type="W"): parent_numbers += [ec.emergencycontactnumber_set.filter(type="W")[0]] if not parent_numbers and ec.emergencycontactnumber_set.all(): # Only include this is no phone types are set parent_numbers +=[ec.emergencycontactnumber_set.all()[0]] for number in parent_numbers: numbers += [number.number] exts += [number.ext] fnames += [ec.fname] lnames += [ec.lname] emails += ['']#[ec.email] m1s += [student.year] m2s += [student.cache_cohort] if hasattr(student, 'studentworker') and student.studentworker: m3s += [student.studentworker.day] else: m3s += [''] m4s += [student] m5s += [number.type] # Make data arrays into xml strings xml_numbers = '' for data_string in numbers: xml_numbers += '<num>%s</num>' % data_string xml_exts = '' for data_string in exts: if data_string == None: data_string = '' xml_exts += '<ext>%s</ext>' % re.sub("[^0-9]", "", data_string) xml_fnames= '' for data_string in fnames: xml_fnames += '<first>%s</first>' % strip_unicode_to_ascii(data_string) xml_lnames = '' for data_string in lnames: xml_lnames += '<last>%s</last>' % strip_unicode_to_ascii(data_string) xml_emails = '' for data_string in emails: xml_emails += '<email>%s</email>' % data_string xml_m1s = '' for data_string in m1s: xml_m1s += '<mField1>%s</mField1>' % strip_unicode_to_ascii(data_string) xml_m2s = '' for data_string in m2s: xml_m2s += '<mField2>%s</mField2>' % strip_unicode_to_ascii(data_string) xml_m3s = '' for data_string in m3s: xml_m3s += '<mField3>%s</mField3>' % strip_unicode_to_ascii(data_string) xml_m4s = '' for data_string in m4s: xml_m4s += '<mField4>%s</mField4>' % strip_unicode_to_ascii(data_string) xml_m5s = '' for data_string in m5s: xml_m5s += '<mField5>%s</mField5>' % strip_unicode_to_ascii(data_string) url = 'https://app.groupcast.com/WARP/GroupcastWARP.asmx?op=SetList&wsdl' xml_dict = { 'userid': config.SCHOOLREACH_USERID, 'pin': config.SCHOOLREACH_PIN, 'list_id': config.SCHOOLREACH_LIST_ID, 'xml_numbers': xml_numbers.replace("-", ""), 'xml_exts': xml_exts, 'xml_fnames': xml_fnames, 'xml_lnames': xml_lnames, 'xml_emails': xml_emails, 'xml_m1s': xml_m1s, 'xml_m2s': xml_m2s, 'xml_m3s': xml_m3s, 'xml_m4s': xml_m4s, 'xml_m5s': xml_m5s, } xml = """<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <setList2010 xmlns="https://app.groupcast.com/WARP/messages"> <appId>1</appId> <verId>3.0.11</verId> <userId>%(userid)s</userId> <ownerPin>%(pin)s</ownerPin> <affectedPin>%(pin)s</affectedPin> <PhoneNumberArray> %(xml_numbers)s </PhoneNumberArray> <Extension> %(xml_exts)s </Extension> <FName> %(xml_fnames)s </FName> <LName> %(xml_lnames)s </LName> <EMail> %(xml_emails)s </EMail> <MetaField1> %(xml_m1s)s </MetaField1> <MetaField2> %(xml_m2s)s </MetaField2> <MetaField3> %(xml_m3s)s </MetaField3> <MetaField4> %(xml_m4s)s </MetaField4> <MetaField5> %(xml_m5s)s </MetaField5> <append>0</append> <useShortCode>1</useShortCode> <ListNumber>%(list_id)s</ListNumber> <isIntl>0</isIntl></setList2010></soap:Body></soap:Envelope>""" % xml_dict header = { 'SOAPAction': "app.GroupCast.com:setList2010In", 'Content-Type': 'text/xml; charset=utf-8', 'Expect': '100-continue', 'Host': 'app.GroupCast.com', } result = requests.post(url, data=xml, headers=header) return result