Skip to content

Commit

Permalink
add plain text template type
Browse files Browse the repository at this point in the history
  • Loading branch information
sadnub committed Oct 5, 2023
1 parent 4bbe22b commit 52e7fd6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.5 on 2023-10-05 16:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('reporting', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='reporttemplate',
name='type',
field=models.CharField(choices=[('markdown', 'Markdown'), ('html', 'Html'), ('plaintext', 'Plain Text')], default='markdown', max_length=15),
),
]
1 change: 1 addition & 0 deletions api/tacticalrmm/ee/reporting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class ReportFormatType(models.TextChoices):
MARKDOWN = "markdown", "Markdown"
HTML = "html", "Html"
PLAIN_TEXT = "plaintext", "Plain Text"


class ReportTemplate(models.Model):
Expand Down
2 changes: 1 addition & 1 deletion api/tacticalrmm/ee/reporting/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def add_custom_fields(
return data


def normalize_asset_url(text: str, type: Literal["pdf", "html"]) -> str:
def normalize_asset_url(text: str, type: Literal["pdf", "html", "plaintext"]) -> str:
RE_ASSET_URL = re.compile(
r"(asset://([0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}))"
)
Expand Down
12 changes: 6 additions & 6 deletions api/tacticalrmm/ee/reporting/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def post(self, request: Request, pk: int) -> Union[FileResponse, Response]:

format = request.data["format"]

if format not in ["pdf", "html"]:
if format not in ["pdf", "html", "plaintext"]:
return notify_error("Report format is incorrect.")

try:
Expand All @@ -133,7 +133,7 @@ def post(self, request: Request, pk: int) -> Union[FileResponse, Response]:

html_report = normalize_asset_url(html_report, format)

if format == "html":
if format != "pdf":
return Response(html_report)
else:
pdf_bytes = generate_pdf(html=html_report)
Expand Down Expand Up @@ -161,7 +161,7 @@ class InputRequest:
template_html: int
template_variables: Dict[str, Any]
dependencies: Dict[str, Any]
format: Literal["html", "pdf"]
format: Literal["html", "pdf", "plaintext"]
debug: bool

class InputSerializer(Serializer[InputRequest]):
Expand All @@ -171,7 +171,7 @@ class InputSerializer(Serializer[InputRequest]):
template_html = IntegerField(allow_null=True, required=False)
template_variables = JSONField()
dependencies = JSONField()
format = ChoiceField(choices=["html", "pdf"])
format = ChoiceField(choices=["html", "pdf", "plaintext"])
debug = BooleanField(default=False)

def post(self, request: Request) -> Union[FileResponse, Response]:
Expand Down Expand Up @@ -220,11 +220,11 @@ def _process_debug_response(
return Response({"template": html_report, "variables": variables})

def _generate_response_based_on_format(
self, html_report: str, format: Literal["html", "pdf"]
self, html_report: str, format: Literal["html", "pdf", "plaintext"]
) -> Union[Response, FileResponse]:
html_report = normalize_asset_url(html_report, format)

if format == "html":
if format != "pdf":
return Response(html_report)
else:
pdf_bytes = generate_pdf(html=html_report)
Expand Down

0 comments on commit 52e7fd6

Please sign in to comment.