Metadata-Version: 2.4
Name: djangorestreport
Version: 0.0.4
Summary: A versatile Django REST Report (DRR) toolkit for generating reports from various backends
Author: Chinmay Roy
Project-URL: Issues, https://gist.github.com/chinmayroy/05487df46a5dc51b768716b4b650c7d7
Keywords: django,rest-framework,reporting,jasperreports,rest-api,pdf-generator,excel-export,drr,djangorestreports
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: requests>=2.25.0
Requires-Dist: django>=3.2
Dynamic: license-file

# Django REST Report
A versatile Django toolkit for managing reports.

## JasperReports
Easily generate JasperReports from your Django views using the JasperReports Server REST API. This package handles authentication, URL construction, and maps Django responses to the correct file types automatically.

### 🛠 Prerequisites
Before using this package, you must have a JasperReports Server instance running.

You can host it yourself or use a cloud instance.

Ensure the REST API is accessible (typically at port 8080).

### 🚀 Installation
```bash
pip install djangorestreport
```

### ⚙️ Setup
Add your JasperServer credentials to your Django `settings.py`.
```python
# settings.py
JASPER_SERVER_URL = "http://server-ip:8080"
JASPER_USER = "username"
JASPER_PASSWORD = "password"
```

You can also load these values from environment variables if that matches your project setup.

### 📄 Supported Formats
Pass these strings into the `output_format` parameter to receive the corresponding file type:

Documents: `pdf`, `docx`, `rtf`, `odt`, `html`, `xml`

Spreadsheets: `xlsx`, `xls`, `csv`, `ods`

Presentations: `pptx`

### 📖 Usage
#### Basic Example (DRF View)
In your `views.py`, you can extract query parameters from the URL and pass them directly to your Jasper report.

```python
from rest_report import JasperReport
from rest_framework.views import APIView
from rest_framework.response import Response

class ReportView(APIView):
    def get(self, request):
        report = JasperReport()

        # 1. Path to the report unit in your JasperServer repository
        report_path = "/organization/reports/NameofReport"

        # 2. Get all query params (e.g., ?id=5&status=active)
        params = request.query_params.dict()

        # 3. Extract the format (default to 'pdf')
        output_format = params.pop("output_format", "pdf")

        # 4. Generate and return the report as a file download
        try:
            return report.generate(report_path, output_format=output_format, params=params)
        except Exception as e:
            return Response({"error": str(e)}, status=400)
```

#### Extended Example (DRF View)
In your `views.py`, you can set a custom filename and message, then pass them directly to your Jasper report.

```python
from rest_report import JasperReport
from rest_framework.views import APIView
from rest_framework.response import Response

class ReportView(APIView):
    def get(self, request):
        report = JasperReport()
        report_path = "/organization/reports/SalesOverview"
        
        # Extract format and parameters
        params = request.query_params.dict()
        output_format = params.pop("output_format", "pdf")

        try:
            return report.generate(
                report_path,
                output_format=output_format,
                params=params,
                filename="Monthly_Sales_Report",  # 1. Custom Filename
                message="Report generated successfully for User ID 101"  # 2. Custom Message
            )
        except Exception as e:
            return Response({"error": str(e)}, status=400)
```

#### Multiple Report/Report Path Handle (DRF View)
In your `views.py`, you can manage more reports/report path, filename then pass them directly to your Jasper report.

```python
params = request.query_params.dict()
output_format = params.pop("output_format", "pdf")

if params.get("id"):
    report_path = "/organization/reports/NameofSingleReport"
    filename = "Name_of_Single_Report"
else:
    report_path = "/organization/reports/NameofDetailsReport"
    filename = "Name_of_Details_Report"

return report.generate(
    report_path,
    output_format=output_format,
    params=params,
    filename=filename,
)
```

### 💡 Working with Parameters
The `params` dictionary keys must exactly match the parameter names defined in your `.jrxml` file. For example, if you pass `{"id": 10}`, your Jasper report should have a parameter defined as `$P{id}`.

### ⚠️ Error Handling
- Missing settings raise `ImproperlyConfigured` when `JasperReport` is initialized.
- Invalid formats raise `ValueError`.
- JasperServer request failures raise `RuntimeError` with the underlying request error.
