def get_attachments(self, fields, request): """Return all attachments uploaded in form. """ attachments = [] # if requested, generate CSV attachment of form values sendCSV = getattr(self, 'sendCSV', None) if sendCSV: csvdata = [] sendXML = getattr(self, 'sendXML', None) if sendXML: xmlRoot = ET.Element("form") for fname in fields: field = fields[fname] showFields = getattr(self, 'showFields', []) or [] if sendCSV: if not is_file_data(field) and (getattr(self, 'showAll', True) or fname in showFields): csvdata.append(field) if sendXML: if not is_file_data(field) and (getattr(self, 'showAll', True) or fname in showFields): ET.SubElement(xmlRoot, "field", name=fname).text \ = self.serialize(field) if is_file_data(field) and (getattr(self, 'showAll', True) or fname in showFields): data = field.data filename = field.filename mimetype, enc = guess_content_type(filename, data, None) attachments.append((filename, mimetype, enc, data)) if sendCSV: output = BytesIO() writer = csvwriter(output) writer.writerow(csvdata) csv = output.getvalue() now = DateTime().ISO().replace(' ', '-').replace(':', '') filename = 'formdata_{0}.csv'.format(now) attachments.append((filename, 'text/plain', 'utf-8', csv)) if sendXML: xmlstr = ET.tostring(xmlRoot, encoding='utf8', method='xml') now = DateTime().ISO().replace(' ', '-').replace(':', '') filename = 'formdata_{0}.xml'.format(now) attachments.append((filename, 'text/xml', 'utf-8', xmlstr)) return attachments
def get_data(row, i): data = row.get(i, '') if is_file_data(data): data = data.filename if isinstance(data, unicode): return data.encode('utf-8') return data
def get_data(row, i): data = row.get(i, "") if is_file_data(data): data = data.filename if six.PY2 and isinstance(data, six.text_type): return data.encode("utf-8") return data
def filter_fields(data, schema, subscriber_fields): data = OrderedDict([(x[0], data[x[0]]) for x in getFieldsInOrder(schema) if x[0] in data], ) fields = [f for f in data if not (is_file_data(data[f]))] # remove standard fields (in subscriber schema) fields = [f for f in fields if f not in subscriber_fields] # remove empties fields = [f for f in fields if data[f]] ret = OrderedDict([(f, data[f]) for f in fields]) return ret
def get_attachments(self, fields, request): """Return all attachments uploaded in form. """ attachments = [] # if requested, generate CSV attachment of form values sendCSV = getattr(self, 'sendCSV', None) if sendCSV: csvdata = [] for fname in fields: field = fields[fname] showFields = getattr(self, 'showFields', []) or [] if sendCSV: if not is_file_data(field) and (getattr(self, 'showAll', True) or fname in showFields): csvdata.append(field) if is_file_data(field) and (getattr(self, 'showAll', True) or fname in showFields): data = field.data filename = field.filename mimetype, enc = guess_content_type(filename, data, None) attachments.append((filename, mimetype, enc, data)) if sendCSV: output = BytesIO() writer = csvwriter(output) writer.writerow(csvdata) csv = output.getvalue() now = DateTime().ISO().replace(' ', '-').replace(':', '') filename = 'formdata_{0}.csv'.format(now) attachments.append((filename, 'text/plain', 'utf-8', csv)) return attachments
def get_attachments(self, fields, request): """Return all attachments uploaded in form. """ attachments = [] # if requested, generate CSV attachment of form values sendCSV = getattr(self, "sendCSV", None) if sendCSV: csvdata = () sendXML = getattr(self, "sendXML", None) if sendXML: xmlRoot = ET.Element("form") showFields = getattr(self, "showFields", []) or [] for fname in fields: field = fields[fname] if sendCSV: if not is_file_data(field) and (getattr(self, "showAll", True) or fname in showFields): val = self.serialize(field) if six.PY2: val = val.encode("utf-8") csvdata += (val, ) if sendXML: if not is_file_data(field) and (getattr(self, "showAll", True) or fname in showFields): ET.SubElement(xmlRoot, "field", name=fname).text = self.serialize( field) # noqa if is_file_data(field) and (getattr(self, "showAll", True) or fname in showFields): data = field.data filename = field.filename mimetype, enc = guess_content_type(filename, data, None) attachments.append((filename, mimetype, enc, data)) if sendCSV: output = StringIO() writer = csvwriter(output) writer.writerow(csvdata) csv = output.getvalue() if six.PY3: csv = csv.encode("utf-8") now = DateTime().ISO().replace(" ", "-").replace(":", "") filename = "formdata_{0}.csv".format(now) # Set MIME type of attachment to 'application' so that it will be encoded with base64 attachments.append((filename, "application/csv", "utf-8", csv)) if sendXML: # use ET.write to get a proper XML Header line output = BytesIO() doc = ET.ElementTree(xmlRoot) doc.write(output, encoding="utf-8", xml_declaration=True) xmlstr = output.getvalue() now = DateTime().ISO().replace(" ", "-").replace(":", "") filename = "formdata_{0}.xml".format(now) # Set MIME type of attachment to 'application' so that it will be encoded with base64 attachments.append((filename, "application/xml", "utf-8", xmlstr)) return attachments