ethosinnovations 0.0.4
Reusable utilities for Django projects
Requires Python: >=3.6

# Ethos Innovations Internal Utilities

A collection of reusable utilities, validators, permissions, and mixins for Django projects.

## Installation
```bash
pip install ethosinnovations
```


## Using the package's settings
This is the recommended way to use the package's settings. This will automatically inject the settings into the Django settings. 

`settings.py`
```python
INSTALLED_APPS = [
    ....
    ethosinnovations,
    ....
]
```

`.env`
```.env
AWS_ACCESS_KEY_ID=your-access-key-id
AWS_SECRET_ACCESS_KEY=your-secret-access-key
AWS_SESSION_TOKEN=your-session-token


AWS_S3_STATIC_BUCKET=my-static-bucket
AWS_S3_STATIC_CUSTOM_DOMAIN=my-static-custom-domain

AWS_S3_MEDIA_BUCKET=my-media-bucket
AWS_S3_MEDIA_CUSTOM_DOMAIN=my-media-custom-domain
```

## Package's Middlewares

### Health Check Middleware
This middleware is used to check if the server is healthy. It will return a 200 status code if the server is healthy.

`settings.py`
```python
from ethosinnovations.middlewares.healthcheck import HealthCheckMiddleware

MIDDLEWARE = [
    ...
    "ethosinnovations.middlewares.healthcheck.HealthCheckMiddleware",
    ...
]
```

### S3 Client
This module provides an S3 client for interacting with AWS S3 storage. It allows users to upload files, generate presigned URLs for secure access, delete files, and move files between S3 buckets. The S3Client class is initialized with AWS credentials, which can be provided directly or assumed from an IAM role if running in an AWS environment. The upload_to_s3 method handles both file uploads and file-like object uploads, ensuring flexibility in how files are managed. This module is essential for applications that require cloud storage capabilities, particularly for media and static files in Django projects.


`views.py`
```python
from ethosinnovations.aws.s3 import S3Client

s3_client = S3Client()  # Initialize the S3 client

# Upload a file to S3
file_path = "path/to/your/file.txt"
object_name = "your_object_name"
s3_client.upload_file(file_path, object_name)

# Generate a presigned URL for a file
object_name = "your_object_name"
presigned_url = s3_client.generate_presigned_url(object_name)

# Delete a file from S3
object_name = "your_object_name"
s3_client.delete_file(object_name)

# Move a file from one S3 bucket to another
source_object_name = "source_object_name"
destination_object_name = "destination_object_name"
s3_client.move_file(source_object_name, destination_object_name)
```

### CSV Generator
This module provides functionality to generate CSV files by utilizing Django's field mapping capabilities. It allows for easy export of model data into CSV format, making it convenient for data analysis and reporting. By defining the fields and their corresponding headers, users can customize the output to fit their needs, ensuring that the generated CSV files are structured and meaningful.


```python
from ethosinnovations.utils.csv import CSVGenerator

csv_fields = [
    {"field_name": "first_name", "header_name": "First Name"},
    {"field_name": "last_name", "header_name": "Last Name"},
    {
        "header_name": "Position",
        "getter": lambda obj: (
            obj.position.parent.name if obj.position.parent else obj.position.name
        ),
    },

]

@admin.action(description="Export selected as CSV")
def export_as_csv(modeladmin, request, queryset):
    response = StreamingHttpResponse(
        csv_generator(stream_csv_data(queryset, csv_fields)), content_type="text/csv"
    )
    time_stamp = datetime.now().strftime("%Y-%m-%d")
    file_name = f"{settings.APP_NAME}_members_{time_stamp}.csv"
    response["Content-Disposition"] = f'attachment; filename="{file_name}"'
    return response

```








## Contributing

Contributions are welcome! Please open an issue or submit a pull request.