Beispiel #1
0
    def perform(self):
        global header

        curl = self.curl

        if sys.version_info >= (3, ):
            buffer = BytesIO()
        else:
            buffer = StringIO()
        curl.setopt(pycurl.WRITEFUNCTION, buffer.write)

        header = []
        try:
            curl.perform()
        except pycurl.error as pe:
            raise occi.TransportError(pe)

        # 'Server: Apache/2.2.22 (Debian)\r\n'
        h = {}
        http_status = None
        for item in header:
            m = Transport.reHeader.match(item.rstrip())
            if m and m.groups >= 2:
                key = m.group(1)
                value = m.group(2)
                h[key.lower()] = value
            else:
                if Transport.reStatus.match(item):
                    http_status = item.rstrip()
        content_type = None
        if 'content-type' in h:
            content_type = re.split(';', h['content-type'])[0]

        body = buffer.getvalue()
        buffer.close()
        if sys.version_info >= (3, ):
            encoding = 'iso-8859-1'
            if content_type:
                match = Transport.reEncoding.search(h['content-type'])
                if match:
                    encoding = match.group(1)
            body = body.decode(encoding)

        return [body, header, http_status, content_type, h]
Beispiel #2
0
    def perform(self):
        global header

        curl = self.curl

        if sys.version_info >= (3,):
            buffer = BytesIO()
        else:
            buffer = StringIO()
        curl.setopt(pycurl.WRITEFUNCTION, buffer.write)

        header = []
        try:
            curl.perform()
        except pycurl.error as pe:
            raise occi.TransportError(pe)

        # 'Server: Apache/2.2.22 (Debian)\r\n'
        h = {}
        http_status = None
        for item in header:
            m = Transport.reHeader.match(item.rstrip())
            if m and m.groups >= 2:
                key = m.group(1)
                value = m.group(2)
                h[key.lower()] = value
            else:
                if Transport.reStatus.match(item):
                    http_status = item.rstrip()
        content_type = None
        if 'content-type' in h:
            content_type = re.split(';', h['content-type'])[0]

        body = buffer.getvalue()
        buffer.close()
        if sys.version_info >= (3,):
            encoding = 'iso-8859-1'
            if content_type:
                match = Transport.reEncoding.search(h['content-type'])
                if match:
                    encoding = match.group(1)
            body = body.decode(encoding)

        return [body, header, http_status, content_type, h]
Beispiel #3
0
def create_pdf(form=None, sig_image=None):
    """
	given form results - 
	"""
    def get_from_form(value):
        if form:
            return form.cleaned_data.get(value, "")
        else:
            return ""

    email = get_from_form("email")
    phone_number = get_from_form("phone")
    postcode = get_from_form("postcode")
    surname = get_from_form("surname")
    first_names = get_from_form("first_names")
    add_1 = get_from_form("add_1")
    add_2 = get_from_form("add_2")
    city = get_from_form("city")
    county = get_from_form("county")
    alt_add_1 = get_from_form("alt_add_1")
    alt_add_2 = get_from_form("alt_add_2")
    alt_postcode = get_from_form("alt_postcode")
    alt_reason = get_from_form("reason")

    file_name = file_name_safe("{0}_{1}".format(surname, first_names).lower())

    council = get_from_form("council")
    multi_council = get_from_form("multi_council")

    if city and county:
        add_3 = city + ", " + county
    elif city:
        add_3 = city
    elif county:
        add_3 = county

    until_further_notice = get_from_form("universal")
    one_date = get_from_form("single_day")
    date_range = get_from_form("time_range")
    date_of_birth = get_from_form("dob")

    packet = BytesIO()
    # create a new PDF with Reportlab

    can = canvas.Canvas(packet, pagesize=letter)

    # add signature of present
    if sig_image:
        can.drawImage(ImageReader(sig_image), 293, 155, mask='auto')
    # core address info

    can.drawString(40, 667, surname.upper())
    can.drawString(40, 620, first_names.upper())

    can.drawString(40, 561, add_1.upper())
    can.drawString(40, 541, add_2.upper())
    can.drawString(40, 521, add_3.upper())

    can.drawString(40, 390, email.upper())
    can.drawString(40, 451, phone_number)
    can.drawString(100, 499, postcode.upper())

    # alt address

    can.drawString(285, 646, alt_add_1.upper())
    can.drawString(285, 626, alt_add_2.upper())
    can.drawString(350, 606, alt_postcode.upper())
    can.drawString(285, 548, alt_reason.upper())

    # for how long we want this on

    if until_further_notice:
        can.drawString(30, 278, "X")
    if one_date:
        can.drawString(30, 248, "X")
        write_date(can.drawString, 153, 213, one_date)
    if date_range:
        can.drawString(30, 181, "X")
        write_date(can.drawString, 153, 156, date_range[0])
        write_date(can.drawString, 153, 129, date_range[1])

    # today's date

    write_date(can.drawString, 457, 44, datetime.datetime.now())

    # birthdate

    can.setFont("Helvetica", 30)
    write_date(can.drawString,
               310,
               350,
               date_of_birth,
               25,
               extra_spacing=[1, 3])

    can.save()

    packet.seek(0)
    new_pdf = PdfFileReader(packet)

    front_page = create_front_page(council, postcode, multi_council)

    source_file = os.path.join(settings.PROJECT_PATH, "resources", "form.pdf")
    existing_pdf = PdfFileReader(open(source_file, "rb"))

    output = PdfFileWriter()

    # add the several pages objects to one pdf

    page = existing_pdf.getPage(0)
    page.mergePage(new_pdf.getPage(0))
    output.addPage(front_page)
    output.addPage(page)

    # send the stream into a response and return it to the view

    outputStream = BytesIO()
    output.write(outputStream)

    response = HttpResponse(content_type='application/pdf')
    response[
        'Content-Disposition'] = 'attachment; filename="postal_vote_{0}.pdf"'.format(
            file_name)
    response.write(outputStream.getvalue())

    outputStream.close()
    if council:
        council.increment_count()
    return response
Beispiel #4
0
def point_cloud_from_buffer(buf):
    fileobj = BytesIO(buf)
    pc = point_cloud_from_fileobj(fileobj)
    fileobj.close()  # necessary?
    return pc