Example #1
0
def save_csv_files_datawarehouse():
    """Create CSV files for Datawarehouse and save them on the storage backend.

    :returns:
    """
    # Creating all CSV files.
    csv_files = list()
    with tempfile.TemporaryDirectory() as tmp_dir:
        reset_queries()
        csv_files.append(_create_signals_csv(tmp_dir))
        csv_files.append(_create_locations_csv(tmp_dir))
        csv_files.append(_create_reporters_csv(tmp_dir))
        csv_files.append(_create_category_assignments_csv(tmp_dir))
        csv_files.append(_create_statuses_csv(tmp_dir))

        # KTO feedback if running on acceptance or production
        try:
            csv_files.append(_create_kto_feedback_csv(tmp_dir))
        except EnvironmentError:
            pass

        pprint(connection.queries)
        # Getting the storage backend and save all CSV files.
        storage = _get_storage_backend(get_swift_parameters())
        for csv_file_path in csv_files:
            with open(csv_file_path, 'rb') as opened_csv_file:
                file_name = os.path.basename(opened_csv_file.name)
                storage.save(name=file_name, content=opened_csv_file)
Example #2
0
# TODO: consider moving this to a central location (settings?)


def get_swift_parameters():
    """Get Swift parameters for 'Horeca data levering'"""
    return {
        'api_auth_url': settings.HORECA_SWIFT_AUTH_URL,
        'api_username': settings.HORECA_SWIFT_USERNAME,
        'api_key': settings.HORECA_SWIFT_PASSWORD,
        'tenant_name': settings.HORECA_SWIFT_TENANT_NAME,
        'tenant_id': settings.HORECA_SWIFT_TENANT_ID,
        'region_name': settings.HORECA_SWIFT_REGION_NAME,
        'container_name': settings.HORECA_SWIFT_CONTAINER_NAME,
        'auto_overwrite': True,
    }


storage = _get_storage_backend(get_swift_parameters())


class HorecaCSVExport(models.Model):
    class Meta:
        ordering = ('-isoyear', '-isoweek', '-created_at')

    created_by = models.EmailField(blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)

    isoweek = models.IntegerField()
    isoyear = models.IntegerField()
    uploaded_file = models.FileField(upload_to='exports/%Y', storage=storage)