def create_response(self, file_context, file_type):
        if file_type == DownloadFileView.FILETYPES.CSV:
            body = csv_generator.iter_csv(self.generate_csv_rows(file_context),
                                          quoting=csv.QUOTE_ALL)

            mimetype = 'text/csv; header=present'

        elif file_type == DownloadFileView.FILETYPES.ODS:
            buffer = BytesIO()

            ods = self.create_blank_ods_with_styles()
            self.populate_styled_ods_with_data(ods, file_context)
            ods.save(buffer)

            body = buffer.getvalue()

            mimetype = 'application/vnd.oasis.opendocument.spreadsheet'

        else:
            abort(400)

        content_disposition = 'attachment;filename={}.{}'.format(
            file_context['filename'], file_type.name.lower())

        return Response(body,
                        mimetype=mimetype,
                        headers={
                            "Content-Disposition": content_disposition,
                            "Content-Type": mimetype
                        }), 200
Beispiel #2
0
def generate_user_csv(users):
    header_row = ("email address", "name")
    user_attributes = ("emailAddress", "name")

    def rows_iter():
        """Iterator yielding header then rows."""
        yield header_row
        for user in sorted(users, key=lambda user: user["name"]):
            yield (user.get(field_name, "") for field_name in user_attributes)

    return csv_generator.iter_csv(rows_iter())
Beispiel #3
0
    def test_it_creates_csv_lines_from_list_of_rows(self):
        rows = [
            ['a', 'b', 'c', 'd'],
            ['e', u'\u00a3', 'g', 'h']
        ]

        result = iter_csv(rows)
        lines = [line for line in result]

        assert lines[0] == b'a,b,c,d\r\n'
        # NOTE this assertion relies on our encoding being utf8
        assert lines[1] == b'e,\xc2\xa3,g,h\r\n'
Beispiel #4
0
def download_direct_award_outcomes():
    download_filename = "direct-award-outcomes-{}.csv".format(
        datetime.utcnow().strftime('%Y-%m-%d-at-%H-%M-%S'))
    projects = data_api_client.find_direct_award_projects(having_outcome=True,
                                                          with_users=True).get(
                                                              'projects', [])
    headers = [
        'ID',
        'Name',
        'Submitted at',
        'Result',
        'Award service ID',
        'Award service name',
        'Award supplier id',
        'Award supplier name',
        'Award value',
        'Awarding organisation name',
        'Award start date',
        'Award end date',
        'User id',
        'User name',
        'User email',
    ]

    formatted_rows = []
    formatted_rows.append(headers)  # add header row
    for project in projects:
        if project['outcome']['result'] != 'awarded':
            continue

        awardDetails = project['outcome']['award']
        resultOfDirectAward = project['outcome']['resultOfDirectAward']
        service = data_api_client.get_archived_service(
            archived_service_id=resultOfDirectAward['archivedService']
            ['id'])['services']

        user = project['users'][0]

        row = [
            project['id'],  # id
            project['name'],  # name
            project['outcome']['completedAt'],  # 'Submitted at',
            project['outcome']['result'],  # 'result',
            resultOfDirectAward['archivedService']['service']
            ['id'],  # 'Award service',
            service['serviceName'],  # 'Award service name',
            service['supplierId'],  # 'Award supplier id',
            service['supplierName'],  # 'Award supplier name',
            awardDetails['awardValue'],  # 'awardValue',
            awardDetails[
                'awardingOrganisationName'],  # 'awardingOrganisationName',
            awardDetails['startDate'],  # 'awardStartDate',
            awardDetails['endDate'],  # 'awardEndDate',
            user['id'],  # 'User id',
            user['name'],  # 'User name',
            user['emailAddress'],  # 'User email',
        ]
        formatted_rows.append(row)

    return Response(csv_generator.iter_csv(formatted_rows),
                    mimetype='text/csv',
                    headers={
                        "Content-Disposition":
                        "attachment;filename={}".format(download_filename),
                        "Content-Type":
                        "text/csv; header=present"
                    })