Ejemplo n.º 1
0
    async def post(self, *args, **kwargs):
        try:
            self.set_header("Content-Type", "application/json; charset=utf-8")
            if self.user is None:
                raise AuthenticationException()

            self.authorize()
            body = self.request.body.decode('utf-8')
            if body is None or body == '':
                raise ActionEmpty()

            response = await self.execute(json.loads(body))

            if response is None:
                response = {}

            self.write({**{"success": True}, **response})

        except BmsException as bex:
            print(traceback.print_exc())
            self.write({
                "success": False,
                "message": str(bex),
                "error": bex.code,
                "data": bex.data
            })

        except Exception as ex:
            print(traceback.print_exc())
            self.write({"success": False, "message": str(ex)})

        self.finish()
Ejemplo n.º 2
0
    async def post(self, *args, **kwargs):
        try:
            self.set_header("Content-Type", "text/csv")
            if self.user is None:
                raise AuthenticationException()

            self.authorize()
            body = self.request.body.decode('utf-8')
            if body is None or body == '':
                raise ActionEmpty()

            async with self.pool.acquire() as conn:
                action = ExportBorehole(conn)
                result = await action.execute(**json.loads(body))
                data = result['data']
                if len(data) > 0:
                    csvfile = StringIO()
                    cw = csv.writer(
                        csvfile,
                        delimiter=';',
                        quotechar='"'
                    )
                    cols = data[0].keys()
                    cw.writerow(cols)

                    for row in data:
                        r = []
                        for col in cols:
                            r.append(row[col])
                        cw.writerow(r)

                    self.write(csvfile.getvalue())

                else:
                    self.write("no data")

        except BmsException as bex:
            print(traceback.print_exc())
            self.write({
                "success": False,
                "message": str(bex),
                "error": bex.code
            })
        except Exception as ex:
            print(traceback.print_exc())
            self.write({
                "success": False,
                "message": str(ex)
            })
        self.finish()
Ejemplo n.º 3
0
    async def get(self, *args, **kwargs):
        try:

            if self.user is None:
                raise AuthenticationException()

            self.authorize()

            arguments = {}
            for key in self.request.query_arguments.keys():
                if self.request.query_arguments[key][0] != b'null':
                    if key == 'extent':
                        coords = self.get_argument(key).split(',')
                        for idx in range(0, len(coords)):
                            coords[idx] = float(coords[idx])
                        arguments[key] = coords
                    elif key == 'format':
                        arguments[key] = self.get_argument(key).split(',')
                    else:
                        arguments[key] = self.get_argument(key)

            if 'format' not in arguments.keys():
                raise MissingParameter("format")

            now = datetime.datetime.now()

            self.set_header(
                "Expires",
                datetime.datetime.utcnow() +
                datetime.timedelta(seconds=1)
            )

            self.set_header(
                "Cache-Control",
                "max-age=" + str(1)
            )

            async with self.pool.acquire() as conn:

                output = None
                output_stream = None

                if len(arguments['format']) > 1:

                    self.set_header(
                        "Content-Type",
                        "application/zip"
                    )
                    self.set_header(
                        "Content-Disposition",
                        "inline; filename=export-%s.zip" % now.strftime(
                                "%Y%m%d%H%M%S"
                        )
                    )

                    output = BytesIO()
                    output_stream = zipfile.ZipFile(
                        output,
                        mode="w",
                        compression=zipfile.ZIP_DEFLATED
                    )

                if 'csv' in arguments['format']:

                    # action = ExportSimpleCsv(conn)
                    action = ExportCsv(conn)
                    if arguments is None:
                        csvfile = await action.execute(
                            user=self.user
                        )

                    else:
                        csvfile = await action.execute(
                            filter=arguments,
                            user=self.user
                        )


                    if output_stream is not None:
                        output_stream.writestr(
                            'export-%s.csv' % now.strftime(
                                    "%Y%m%d%H%M%S"
                            ),
                            csvfile.getvalue()
                        )

                    else:
                        self.set_header(
                            "Content-Type",
                            "text/csv"
                        )
                        self.set_header(
                            "Content-Disposition",
                            "inline; filename=export-%s.csv" % now.strftime(
                                    "%Y%m%d%H%M%S"
                            )
                        )
                        self.write(csvfile.getvalue())

                if 'fullcsv' in arguments['format']:

                    # action = ExportSimpleCsv(conn)
                    action = ExportCsvFull(conn)
                    if arguments is None:
                        csvfile = await action.execute(
                            user=self.user
                        )

                    else:
                        csvfile = await action.execute(
                            filter=arguments,
                            user=self.user
                        )

                    if output_stream is not None:
                        output_stream.writestr(
                            'full-export-%s.csv' % now.strftime(
                                    "%Y%m%d%H%M%S"
                            ),
                            csvfile.getvalue()
                        )

                    else:
                        self.set_header(
                            "Content-Type",
                            "text/csv"
                        )
                        self.set_header(
                            "Content-Disposition",
                            "inline; filename=full-export-%s.csv" % now.strftime(
                                    "%Y%m%d%H%M%S"
                            )
                        )
                        self.write(csvfile.getvalue())

                if 'pdf' in arguments['format']:

                    lan = arguments['lang'] if 'lang' in arguments else 'en'
                    idsty =6
                    schema ='bdms'

                    if 'id' in arguments:

                        pdfs = []

                        for cid in arguments['id'].split(','):
                            cid = cid.split(':')
                            if len(cid)==2:
                                bid = int(cid[0])
                                sid = int(cid[1])

                                # TODO: test if passed sid belongs to passed bid
                                # else raise error
                            elif len(cid)==1:
                                bid = int(cid[0])
                                sid = await conn.fetchval("""
                                    SELECT
                                        id_sty
                                    FROM
                                        bdms.stratigraphy
                                    WHERE
                                        id_bho_fk = $1
                                    AND
                                        primary_sty IS TRUE
                                """, bid)
                            else:
                                raise ValueError("id parameters are {berehole id}:{stratigraphy id}")

                            if sid is not None:
                                res = await conn.fetchval("""
                                    SELECT
                                        row_to_json(t2)
                                    FROM (
                                        SELECT
                                            id_bho as idb,
                                            'Switzerland' as country,
                                            cant_j.name as canton,
                                            mun_j.name as city,
                                            address_bho as address,
                                            cli_kind.text_cli_{} as kind,
                                            location_x_bho as location_e,
                                            location_y_bho as location_n,
                                            COALESCE(elevation_z_bho, 0) as elevation_z,
                                            cli_srs.text_cli_{} as srs,
                                            cli_hrs.text_cli_{} as hrs,
                                            length_bho as length,
                                            drilling_date_bho as drilling_date,
                                            cli_restriction.text_cli_{} as restriction,
                                            to_char(
                                                restriction_until_bho,
                                                'YYYY-MM-DD'
                                            ) as restrictoin_until,
                                            cli_purpose.text_cli_{} as purpose,
                                            cli_landuse.text_cli_{} as landuse,
                                            cli_cuttings.text_cli_{} as cuttings,
                                            cli_method.text_cli_{} as method,
                                            cli_status.text_cli_{} as status,
                                            drill_diameter_bho as drill_diameter,
                                            bore_inc_bho as bore_inc,
                                            bore_inc_dir_bho as bore_inc_dir,
                                            project_name_bho as project_name,
                                            '12345' as auth_n,
                                            original_name_bho as original_name,
                                            public_name_bho as public_name,
                                            strat_j.name_sty as strataname,
                                            strat_j.date_sty as stratadate,
                                            'IFEC' as logged_by,
                                            'swisstopo' as checked_by,
                                            groundwater_bho as groundwater,

                                            (SELECT
                                                array_to_json(
                                                    array_agg(
                                                        row_to_json(t)
                                                    )
                                                )
                                                FROM (
                                                    SELECT
                                                        id_lay as id,
                                                        id_sty as id_sty,
                                                        depth_from_lay as depth_from,
                                                        depth_to_lay as depth_to,
                                                        CASE
                                                            WHEN elevation_z_bho is NULL 
                                                            THEN 0 - depth_from_lay
                                                            ELSE elevation_z_bho - depth_from_lay
                                                        END AS msm_from,
                                                        CASE
                                                            WHEN elevation_z_bho is NULL 
                                                            THEN 0 - depth_to_lay
                                                            ELSE elevation_z_bho - depth_to_lay
                                                        END AS msm_to,
                                                        cli_lithostra.text_cli_{} as lithostratigraphy,
                                                        cli_lithostra.conf_cli as conf_lithostra,
                                                        cli_lithostra.geolcode as geolcode_lithostra,
                                                        cli_lithology.text_cli_{} as lithology,
                                                        cli_lithology.conf_cli as conf_lithology,
                                                        description_lay as layer_desc,
                                                        geology_lay as geol_desc,
                                                        name_sty as name_st,
                                                        notes_lay as notes
                                                    FROM
                                                        {}.layer
                                                            LEFT JOIN {}.codelist as cli_lithostra
                                                                ON cli_lithostra.id_cli = lithostratigraphy_id_cli
                                                            LEFT JOIN {}.codelist as cli_lithology
                                                                ON cli_lithology.id_cli = lithology_id_cli,
                                                        {}.stratigraphy,
                                                        {}.borehole
                                                    WHERE
                                                        id_sty_fk = id_sty
                                                    AND
                                                        id_sty = $1
                                                    AND
                                                        id_bho_fk = id_bho
                                                    AND
                                                        id_bho = strat_j.id_bho_fk
                                                    ORDER BY depth_from_lay, id_lay

                                            ) AS t
                                        ) AS layers 
                                        FROM 
                                            {}.borehole
                                        LEFT JOIN {}.codelist as cli_kind
                                            ON cli_kind.id_cli = kind_id_cli
                                        LEFT JOIN {}.codelist as cli_srs
                                            ON cli_srs.id_cli = srs_id_cli
                                        LEFT JOIN {}.codelist as cli_hrs
                                            ON cli_hrs.id_cli = hrs_id_cli
                                        LEFT JOIN {}.codelist as cli_restriction
                                            ON cli_restriction.id_cli = restriction_id_cli
                                        LEFT JOIN {}.codelist as cli_purpose
                                            ON cli_purpose.id_cli = purpose_id_cli
                                        LEFT JOIN {}.codelist as cli_method
                                            ON cli_method.id_cli = method_id_cli
                                        LEFT JOIN {}.codelist as cli_landuse
                                            ON cli_landuse.id_cli =landuse_id_cli
                                        LEFT JOIN {}.codelist as cli_status
                                            ON cli_status.id_cli =status_id_cli
                                        LEFT JOIN {}.codelist as cli_cuttings
                                            ON cli_cuttings.id_cli = cuttings_id_cli
                                        LEFT JOIN {}.municipalities as mun_j
                                            ON mun_j.gid = city_bho
                                        LEFT JOIN {}.cantons as cant_j
                                            ON cant_j.gid = canton_bho
                                        LEFT JOIN (
                                            SELECT id_sty, date_sty, name_sty, id_bho_fk 
                                            FROM {}.stratigraphy
                                            --WHERE primary_sty = true
                                        ) as strat_j ON strat_j.id_sty = $2
                                                
                                        WHERE
                                            id_bho = id_bho_fk
                                        AND
                                            strat_j.id_sty = $3
                                    ) AS t2
                                """.format(*((lan,)*11 + (schema,)*18)), sid, sid, sid)

                                a = bdms.bdmsPdf( json.loads(res))
                                a.renderProfilePDF(
                                    arguments['lang'] if 'lang' in arguments else 'en',
                                    int(arguments['scale']) if 'scale' in arguments else 200
                                )
                                a.close()

                                pdfs.append(a.pdf)

                    def append_pdf(input, output):
                        [
                            output.addPage(
                                input.getPage(page_num)
                            ) for page_num in range(input.numPages)
                        ]

                    outputFW = PdfFileWriter()
                    for pdf in pdfs:
                        inpt = PdfFileReader(pdf)
                        if inpt.isEncrypted:
                            inpt.decrypt('')
                        append_pdf(inpt, outputFW)

                    working_file = BytesIO()
                    outputFW.write(working_file)

                    if output_stream is not None:
                        output_stream.writestr(
                            'export-%s.pdf' % now.strftime(
                                    "%Y%m%d%H%M%S"
                            ),
                            working_file.getvalue()
                        )

                    else:
                        self.set_header(
                            "Content-Type",
                            "application/pdf"
                        )
                        self.set_header(
                            "Content-Disposition",
                            "inline; filename=export-%s.pdf" % now.strftime(
                                    "%Y%m%d%H%M%S"
                            )
                        )
                        self.write(working_file.getvalue())

                if 'shape' in arguments['format']:

                    shpA = ExportShapefile(conn)

                    if arguments is None:
                        shp, shx, dbf, prj = await shpA.execute(
                            user=self.user
                        )
                    else:
                        shp, shx, dbf, prj = await shpA.execute(
                            filter=arguments,
                            user=self.user
                        )

                    if output_stream is None:
                        self.set_header(
                            "Content-Type",
                            "application/zip"
                        )
                        self.set_header(
                            "Content-Disposition",
                            "inline; filename=export-%s.zip" % now.strftime(
                                    "%Y%m%d%H%M%S"
                            )
                        )
                        output = BytesIO()
                        output_stream = zipfile.ZipFile(
                            output,
                            mode="w",
                            compression=zipfile.ZIP_DEFLATED
                        )

                    output_stream.writestr(
                        'export-%s.shp' % now.strftime(
                                "%Y%m%d%H%M%S"
                        ),
                        shp.getvalue()
                    )
                    output_stream.writestr(
                        'export-%s.shx' % now.strftime(
                                "%Y%m%d%H%M%S"
                        ),
                        shx.getvalue()
                    )
                    output_stream.writestr(
                        'export-%s.dbf' % now.strftime(
                                "%Y%m%d%H%M%S"
                        ),
                        dbf.getvalue()
                    )
                    output_stream.writestr(
                        'export-%s.prj' % now.strftime(
                                "%Y%m%d%H%M%S"
                        ),
                        prj.getvalue()
                    )

            if output_stream is not None:
                output_stream.close()
                self.write(output.getvalue())

        except BmsException as bex:
            print(traceback.print_exc())
            self.write({
                "success": False,
                "message": str(bex),
                "error": bex.code
            })
        except Exception as ex:
            print(traceback.print_exc())
            self.write({
                "success": False,
                "message": str(ex)
            })
        self.finish()
Ejemplo n.º 4
0
    async def post(self, *args, **kwargs):
        if ('Content-Type' in self.request.headers and 'multipart/form-data'
                in self.request.headers['Content-Type']):
            try:
                self.set_header("Content-Type",
                                "application/json; charset=utf-8")
                if self.user is None:
                    raise AuthenticationException()

                self.authorize()

                # Check file repository
                if options.file_repo == 's3':
                    session = boto3.Session(
                        aws_access_key_id=options.aws_access_key_id,
                        aws_secret_access_key=options.aws_secret_access_key,
                    )
                    s3 = session.resource('s3')

                async with self.pool.acquire() as conn:
                    try:
                        await conn.execute("BEGIN;")

                        action = self.get_body_argument('action')

                        if action == 'IMPORT_INTO_NEW_SUPPLIER':
                            result = await (CreateWorkgroup(conn)).execute(
                                self.get_body_argument('name'), True)
                            workgroup_id = result['id']

                            set_role = SetRole(conn)
                            result = await set_role.execute(
                                self.user['id'], workgroup_id, 'VIEW')
                            result = await set_role.execute(
                                self.user['id'], workgroup_id, 'PUBLIC')

                        elif action in [
                                'IMPORT_INTO_SUPPLIER', 'IMPORT_INTO_WORKGROUP'
                        ]:
                            workgroup_id = int(self.get_body_argument('id'))

                        for files in self.request.files.items():
                            for info in files[1]:
                                archive = BytesIO(info["body"])
                                # archive_name = info["filename"]
                                result = await (Importer(conn)).execute(
                                    self.user, workgroup_id, archive)
                                break
                            break

                        await conn.execute("COMMIT;")

                    except Exception as ex:
                        print(traceback.print_exc())
                        await conn.execute("ROLLBACK;")
                        raise ex

            except BmsException as bex:
                print(traceback.print_exc())
                self.write({
                    "success": False,
                    "message": str(bex),
                    "error": bex.code,
                    "data": bex.data
                })

            except Exception as ex:
                print(traceback.print_exc())
                self.write({"success": False, "message": str(ex)})

            self.finish()

        else:
            await super(ImportAdminHandler, self).post(*args, **kwargs)
Ejemplo n.º 5
0
    async def post(self, *args, **kwargs):

        try:
            if self.user is None:
                raise AuthenticationException()

            self.authorize()
            body = self.request.body.decode('utf-8')

            if body is None or body == '':
                raise ActionEmpty()

            payload = json.loads(body)
            action = payload['action']
            response = await self.execute(payload)

            if action in ['EXPORT', 'EXPORT_BY_ID']:

                now = datetime.datetime.now()

                self.set_header("Content-Type", "application/zip")

                self.set_header(
                    "Content-Disposition",
                    "inline; filename=bdms-export-%s.zip" %
                    now.strftime("%Y%m%d%H%M%S"))

                output = BytesIO()
                output_stream = zipfile.ZipFile(
                    output, mode="w", compression=zipfile.ZIP_DEFLATED)

                if response:
                    output_stream.writestr('swissforages.gpkg',
                                           response.getvalue())

                    # Check file repository
                    if options.file_repo == 's3':
                        session = boto3.Session(
                            aws_access_key_id=options.aws_access_key_id,
                            aws_secret_access_key=options.
                            aws_secret_access_key,
                        )
                        s3 = session.resource('s3')

                    async with self.pool.acquire() as conn:

                        widx = 1
                        where = []
                        arguments = []
                        if 'workgroup' in payload and payload[
                                'workgroup'] != 'all':

                            widx = 1
                            andId = []
                            for id in payload['workgroup']:
                                andId.append(f"""
                                    id_wgp_fk = ${widx}
                                """)
                                arguments.append(id)
                                widx += 1

                            where.append(f"""(
                                {' OR '.join(andId)}
                            )""")
                            # where.append(f"""
                            #     id_wgp_fk = ${widx}
                            # """)
                            # widx += 1
                            # arguments.append(payload['workgroup'])

                        if 'ids' in payload:
                            widx = 1
                            andId = []
                            for id in payload['ids']:
                                andId.append(f"""
                                    borehole.id_bho = ${widx}
                                """)
                                arguments.append(id)
                                widx += 1

                            where.append(f"""(
                                {' OR '.join(andId)}
                            )""")

                        stringWhere = ''

                        if len(where) > 0:
                            stringWhere = f"""
                                WHERE
                                    {' AND '.join(where)}
                            """

                        rows = await conn.fetch(
                            f"""
                            SELECT
                                name_fil,
                                type_fil,
                                conf_fil

                            FROM
                                bdms.files

                            INNER JOIN
                                bdms.borehole_files
                            ON
                                id_fil_fk = id_fil
								
							INNER JOIN
                                bdms.borehole
							ON
								id_bho = id_bho_fk
                            
                            {stringWhere}
                        """, *arguments)

                        not_found = []

                        for file_info in rows:

                            if file_info is None:
                                raise NotFound()

                            conf = json.loads(file_info[2])
                            attachment = BytesIO()

                            try:
                                s3.Bucket(options.aws_bucket).download_fileobj(
                                    conf['key'], attachment)
                                output_stream.writestr(f'files/{conf["key"]}',
                                                       attachment.getvalue())

                            except ClientError as ce:
                                # print(traceback.print_exc())
                                if ce.response['Error']['Code'] == '404':
                                    not_found.append(conf['key'])
                                    continue

                                raise Exception(
                                    "Error while downloading from AWS")

                        if len(not_found) > 0:
                            print(
                                f"\033[91m{len(not_found)} files not found 🤔\033[0m"
                            )

                else:
                    raise Exception("Unknown error")

                output_stream.close()
                self.write(output.getvalue())

            else:
                self.set_header("Content-Type",
                                "application/json; charset=utf-8")

                if response is None:
                    response = {}

                self.write({**{"success": True}, **response})

        except BmsException as bex:
            print(traceback.print_exc())
            self.write({
                "success": False,
                "message": str(bex),
                "error": bex.code,
                "data": bex.data
            })

        except Exception as ex:
            print(traceback.print_exc())
            self.write({"success": False, "message": str(ex)})

        self.finish()
Ejemplo n.º 6
0
    async def post(self, *args, **kwargs):
        if ('Content-Type' in self.request.headers and 'multipart/form-data'
                in self.request.headers['Content-Type']):
            try:
                self.set_header("Content-Type",
                                "application/json; charset=utf-8")

                if self.user is None:
                    raise AuthenticationException()

                self.authorize()

                s3 = None

                # Check file repository
                if options.file_repo == 's3':
                    session = boto3.Session(
                        aws_access_key_id=options.aws_access_key_id,
                        aws_secret_access_key=options.aws_secret_access_key,
                    )
                    s3 = session.resource('s3')

                async with self.pool.acquire() as conn:
                    try:
                        await conn.execute("BEGIN;")

                        borehole_id = int(self.get_body_argument('id'))

                        for files in self.request.files.items():

                            for info in files[1]:
                                file_body = BytesIO(info["body"])
                                file_name = info["filename"]

                                extension = ''
                                if file_name.find('.') > 0:
                                    extension = file_name.rsplit('.',
                                                                 1)[1].lower()

                                file_type = info["content_type"]

                                # Calculating hash
                                m = hashlib.sha256()
                                m.update(info["body"])
                                file_hash = m.hexdigest()

                                # Check for file duplication
                                id_fil = await conn.fetchval(
                                    """
                                        SELECT
                                            id_fil
                                        FROM
                                            bdms.files
                                        WHERE
                                            hash_fil = $1
                                    """, file_hash)

                                conf = {}

                                # upload to file repository if not exists
                                if id_fil is None:

                                    # store file
                                    if options.file_repo == 's3':

                                        # Giving a unig file name
                                        conf['key'] = "{}.{}".format(
                                            str(uuid.uuid1()), extension)

                                        try:
                                            s3.Bucket(options.aws_bucket
                                                      ).upload_fileobj(
                                                          file_body,
                                                          conf['key'],
                                                          ExtraArgs={
                                                              'Metadata': {
                                                                  'filename':
                                                                  file_name
                                                              },
                                                              'ContentType':
                                                              file_type
                                                          })

                                        except ClientError:
                                            print(traceback.print_exc())
                                            raise Exception(
                                                "Error while uploading to AWS")

                                    # Insert info into database
                                    id_fil = await conn.fetchval(
                                        """
                                            INSERT INTO bdms.files (
                                                name_fil, hash_fil,
                                                type_fil, conf_fil,
                                                id_usr_fk
                                            ) VALUES (
                                                $1, $2, $3, $4, $5
                                            )
                                            RETURNING id_fil;
                                        """, file_name, file_hash, file_type,
                                        json.dumps(conf), self.user['id'])

                                # Attach file to borehole
                                await conn.execute(
                                    """
                                        INSERT INTO bdms.borehole_files (
                                            id_bho_fk, id_fil_fk, id_usr_fk
                                        ) VALUES (
                                            $1, $2, $3
                                        );
                                    """, borehole_id, id_fil, self.user['id'])

                        await conn.execute("COMMIT;")

                    except Exception as ex:
                        print(traceback.print_exc())
                        await conn.execute("ROLLBACK;")
                        raise ex

                self.write({"success": True})

            except Exception as ex:
                print(traceback.print_exc())
                self.write({"success": False, "message": str(ex)})

            self.finish()

        else:
            await super(FileHandler, self).post(*args, **kwargs)
Ejemplo n.º 7
0
    async def get(self, *args, **kwargs):
        try:

            if self.user is None:
                raise AuthenticationException()

            self.authorize()

            self.set_header(
                "Expires",
                datetime.datetime.utcnow() + datetime.timedelta(seconds=1))

            self.set_header("Cache-Control", "max-age=" + str(1))

            s3 = None

            # Check file repository
            if options.file_repo == 's3':
                session = boto3.Session(
                    aws_access_key_id=options.aws_access_key_id,
                    aws_secret_access_key=options.aws_secret_access_key,
                )
                s3 = session.resource('s3')

            async with self.pool.acquire() as conn:

                file_id = int(self.get_argument('id', 0))

                if file_id == 0:
                    raise NotFound()

                # Getting attachment info
                file_info = await conn.fetchrow(
                    """
                    SELECT
                        name_fil,
                        type_fil,
                        conf_fil

                    FROM
                        bdms.files

                    WHERE
                        id_fil = $1
                """, file_id)

                if file_info is None:
                    raise NotFound()

                conf = json.loads(file_info[2])

                self.set_header("Content-Type", file_info[1])

                self.set_header("Content-Disposition",
                                f"inline; {file_info[0]}")

                attachment = BytesIO()

                if options.file_repo == 's3':

                    try:
                        s3.Bucket(options.aws_bucket).download_fileobj(
                            conf['key'], attachment)

                    except ClientError:
                        print(traceback.print_exc())
                        raise Exception("Error while downloading from AWS")

                self.write(attachment.getvalue())

        except BmsException as bex:
            print(traceback.print_exc())
            self.write({
                "success": False,
                "message": str(bex),
                "error": bex.code
            })

        except Exception as ex:
            print(traceback.print_exc())
            self.write({"success": False, "message": str(ex)})

        self.finish()
Ejemplo n.º 8
0
    async def get(self, *args, **kwargs):
        try:
            self.set_header(
                "Content-Type",
                "text/csv"
            )

            now = datetime.datetime.now()

            self.set_header(
                "Content-Disposition",
                "inline; filename=export-%s.csv" % now.strftime(
                        "%Y%m%d%H%M%S"
                )
            )

            self.set_header(
                "Expires",
                datetime.datetime.utcnow() +
                datetime.timedelta(seconds=1)
            )

            self.set_header(
                "Cache-Control",
                "max-age=" + str(1)
            )

            if self.user is None:
                raise AuthenticationException()

            self.authorize()

            arguments = {}
            for key in self.request.query_arguments.keys():
                if self.request.query_arguments[key][0] != b'null':
                    if key == 'extent':
                        coords = self.get_argument(key).split(',')
                        for idx in range(0, len(coords)):
                            coords[idx] = float(coords[idx])
                        arguments[key] = coords
                    else:
                        arguments[key] = self.get_argument(key)

            async with self.pool.acquire() as conn:
                action = ExportBorehole(conn)
                if arguments is None:
                    result = await action.execute()
                else:
                    result = await action.execute(filter=arguments)
                data = result['data']
                if len(data) > 0:
                    csvfile = StringIO()
                    cw = csv.writer(
                        csvfile,
                        delimiter=';',
                        quotechar='"'
                    )
                    cols = data[0].keys()
                    cw.writerow(cols)

                    for row in data:
                        r = []
                        for col in cols:
                            r.append(row[col])
                        cw.writerow(r)

                    self.write(csvfile.getvalue())

                else:
                    self.write("no data")

        except BmsException as bex:
            print(traceback.print_exc())
            self.write({
                "success": False,
                "message": str(bex),
                "error": bex.code
            })
        except Exception as ex:
            print(traceback.print_exc())
            self.write({
                "success": False,
                "message": str(ex)
            })
        self.finish()
Ejemplo n.º 9
0
    async def get(self, *args, **kwargs):
        try:

            if self.user is None:
                raise AuthenticationException()

            self.authorize()

            now = datetime.datetime.now()

            self.set_header(
                "Expires",
                datetime.datetime.utcnow() +
                datetime.timedelta(seconds=1)
            )

            self.set_header(
                "Cache-Control",
                "max-age=" + str(1)
            )

            self.set_header(
                "Content-Type",
                "application/zip"
            )

            self.set_header(
                "Content-Disposition",
                "inline; filename=bdms-export-%s.zip" % now.strftime(
                    "%Y%m%d%H%M%S"
                )
            )

            arguments = {}
            for key in self.request.query_arguments.keys():
                if self.request.query_arguments[key][0] != b'null':
                    if key == 'extent':
                        coords = self.get_argument(key).split(',')
                        for idx in range(0, len(coords)):
                            coords[idx] = float(coords[idx])
                        arguments[key] = coords
                    elif key == 'format':
                        arguments[key] = self.get_argument(key).split(',')
                    else:
                        arguments[key] = self.get_argument(key)

            out_format = self.get_argument('format', None)

            if out_format is None:
                raise MissingParameter("format")

            async with self.pool.acquire() as conn:

                output = BytesIO()
                output_stream = zipfile.ZipFile(
                    output, mode="w", compression=zipfile.ZIP_DEFLATED
                )

                if out_format == 'text/csv;type=full':

                    action = ExportCsv(conn)
                    csvfile = await action.execute(
                        arguments,
                        user=self.user
                    )

                    output_stream.writestr(
                        'export-%s.csv' % now.strftime(
                                "%Y%m%d%H%M%S"
                        ),
                        csvfile.getvalue()
                    )

                elif out_format in ['text/csv', 'text/csv;type=simple']:

                    action = ExportSimpleCsv(conn)
                    csvfile = await action.execute(
                        arguments,
                        user=self.user
                    )

                    output_stream.writestr(
                        'export-%s.csv' % now.strftime(
                                "%Y%m%d%H%M%S"
                        ),
                        csvfile.getvalue()
                    )

                elif out_format in ['text/json', 'json']:

                    action = ExportJson(conn)
                    jsonfile = await action.execute(
                        arguments,
                        user=self.user
                    )

                    output_stream.writestr(
                        'export-%s.json' % now.strftime(
                                "%Y%m%d%H%M%S"
                        ),
                        jsonfile.getvalue()
                    )

                elif out_format in ['application/spatialite', 'spatialite']:

                    action = ExportSpatiaLite(conn)
                    spatia_lite_file = await action.execute(
                        arguments,
                        user=self.user
                    )

                    if spatia_lite_file:

                        with open(spatia_lite_file.name) as f:

                            output_stream.writestr(
                                'export-%s.db' % now.strftime(
                                        "%Y%m%d%H%M%S"
                                ),
                                f
                            )

                        # spatia_lite_file.close()

                    # raise Exception("application/spatialite")

            output_stream.close()
            self.write(output.getvalue())

        except BmsException as bex:
            print(traceback.print_exc())
            self.write({
                "success": False,
                "message": str(bex),
                "error": bex.code
            })

        except Exception as ex:
            print(traceback.print_exc())
            self.write({
                "success": False,
                "message": str(ex)
            })

        self.finish()