def setUp(self): current.auth.override = True s3db = current.s3db ptable = s3db.pr_person ctable = s3db.pr_contact itable = s3db.pr_image # Create a person record person = { "pe_label": "KBT012", "first_name": "Example", "last_name": "Person", #"date_of_birth": ..., "comments": "This is a comment", } person_id = ptable.insert(**person) person["id"] = person_id s3db.update_super(ptable, person) self.person_id = person_id # Add a contact record contact = { "pe_id": person["pe_id"], "contact_method": "SMS", "value": "+60738172623", "comments": "This is a comment", } self.contact_id = ctable.insert(**contact) # Add an image record stream = StringIO() stream.write("TEST") filename = itable.image.store(stream, "test.txt") filepath = os.path.join(current.request.folder, "uploads", filename) image = { "pe_id": person["pe_id"], "image": filename, "description": "Example description", } self.image_id = itable.insert(**image) self.assertTrue(os.path.exists(filepath)) self.image_path = filepath
def importxml(db, xmlinput): """ Converts the XML to a CSV compatible with the import_from_csv_file of web2py @ToDo: rewrite this to go via S3Resource for proper Auth checking, Audit. @todo: deprecate """ from s3compat import StringIO import xml.dom.minidom try: doc = xml.dom.minidom.parseString(xmlinput) except: raise Exception("XML parse error") parent = doc.childNodes[0].tagName csvout = csvheader(parent, doc.childNodes[0].childNodes) for subnode in doc.childNodes: csvout = csvout + csvdata(subnode.childNodes) fh = StringIO() fh.write(csvout) fh.seek(0, 0) db[parent].import_from_csv_file(fh)
def import_xls(uploadFile): """ Import Assessment Spreadsheet """ from s3compat import StringIO if series_id is None: response.error = T("Series details missing") return openFile = StringIO() try: import xlrd from xlwt.Utils import cell_to_rowcol2 except ImportError: current.log.error("ERROR: xlrd & xlwt modules are needed for importing spreadsheets") return None workbook = xlrd.open_workbook(file_contents=uploadFile) try: sheetR = workbook.sheet_by_name("Assessment") sheetM = workbook.sheet_by_name("Metadata") except: session.error = T("You need to use the spreadsheet which you can download from this page") redirect(URL(c="survey", f="new_assessment", args=[], vars={"viewing": "survey_series.%s" % series_id})) header = "" body = "" for row in xrange(1, sheetM.nrows): header += ',"%s"' % sheetM.cell_value(row, 0) code = sheetM.cell_value(row, 0) qstn = s3.survey_getQuestionFromCode(code, series_id) type = qstn["type"] count = sheetM.cell_value(row, 1) if count != "": count = int(count) optionList = sheetM.cell_value(row, 2).split("|#|") else: count = 1 optionList = None if type == "Location" and optionList != None: answerList = {} elif type == "MultiOption": answerList = [] else: answerList = "" for col in range(count): cell = sheetM.cell_value(row, 3 + col) (rowR, colR) = cell_to_rowcol2(cell) try: cellValue = sheetR.cell_value(rowR, colR) except IndexError: cellValue = "" # BUG: The option list needs to work in different ways # depending on the question type. The question type should # be added to the spreadsheet to save extra db calls: # * Location save all the data as a hierarchy # * MultiOption save all selections # * Option save the last selection if cellValue != "": if optionList != None: if type == "Location": answerList[optionList[col]]=cellValue elif type == "MultiOption": answerList.append(optionList[col]) else: answerList = optionList[col] else: if type == "Date": try: (dtYear, dtMonth, dtDay, dtHour, dtMinute, dtSecond) = \ xlrd.xldate_as_tuple(cellValue, workbook.datemode) dtValue = datetime.date(dtYear, dtMonth, dtDay) cellValue = dtValue.isoformat() except: pass elif type == "Time": try: time = cellValue hour = int(time * 24) minute = int((time * 24 - hour) * 60) cellValue = "%s:%s" % (hour, minute) except: pass answerList += "%s" % cellValue body += ',"%s"' % answerList openFile.write(header) openFile.write("\n") openFile.write(body) openFile.seek(0) return openFile