Skip to content

Commit

Permalink
chore: removing pdf files
Browse files Browse the repository at this point in the history
  • Loading branch information
pbastia committed Nov 26, 2024
1 parent 93cda80 commit a9aa7be
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,6 @@ helm/**/charts
# Vite(st) temporary config files
vite.config.**s.timestamp-*
vitest.config.**s.timestamp-*

# Local file attachments
bc_obps/test_media/*
2 changes: 1 addition & 1 deletion bc_obps/reporting/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
from .report_activity import save_report_activity_data, load_report_activity_data
from .report_facilities import get_report_facility_list_by_version_id
from .report_verification import get_report_verification_by_version_id, save_report_verification
from .attachments import save_report_attachments
from .report_attachments import load_report_attachments, download_report_attachment_file
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from ninja import File, Form, UploadedFile
from registration.decorators import handle_http_errors
from reporting.constants import EMISSIONS_REPORT_TAGS
from reporting.models import report_version
from reporting.models.report_attachment import ReportAttachment
from reporting.schema.generic import Message
from reporting.schema.report_attachment import ReportAttachmentOut
Expand All @@ -22,22 +21,21 @@
auth=authorize("approved_industry_user"),
)
@handle_http_errors()
@transaction.atomic()
def save_report_attachments(
request: HttpRequest,
report_version_id: int,
file_types: Form[List[str]],
files: List[UploadedFile] = File(...),
) -> Literal[200]:

print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
for f in files:
print(f)
print("~~~")
for n in file_types:
print(n)

for index, file_type in enumerate(file_types):
ReportAttachment.objects.filter(report_version_id=report_version_id, attachment_type=file_type).delete()

report_attachment = ReportAttachment.objects.get(report_version_id=report_version_id, attachment_type=file_type)

# Delete file from storage then from db
report_attachment.attachment.delete()
report_attachment.delete()

attachment = ReportAttachment(
report_version_id=report_version_id,
Expand All @@ -50,15 +48,23 @@ def save_report_attachments(
return 200


@router.get("report-version/{report_version_id}/attachments")
@router.get(
"report-version/{report_version_id}/attachments",
response={200: List[ReportAttachmentOut], custom_codes_4xx: Message},
tags=EMISSIONS_REPORT_TAGS,
description="""Returns the list of file attachments for a report version.""",
auth=authorize("approved_industry_user"),
)
def load_report_attachments(
request: HttpRequest,
report_version_id: int,
) -> Tuple[Literal[200], List[ReportAttachmentOut]]:
return ReportAttachment.objects.filter(report_version_id=report_version_id)


@router.get("report-version/{report_version_id}/attachments/{file_id}")
@router.get(
"report-version/{report_version_id}/attachments/{file_id}",
)
def download_report_attachment_file(request: HttpRequest, report_version_id: int, file_id: int):

file = ReportAttachment.objects.get(id=file_id, report_version_id=report_version_id)
Expand Down
2 changes: 1 addition & 1 deletion bc_obps/reporting/models/report_attachment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models
from django.db.models import Q, CharField, FileField, ForeignKey
from django.db.models import CharField, FileField, ForeignKey
from registration.models.time_stamped_model import TimeStampedModel
from reporting.models.report_version import ReportVersion

Expand Down
13 changes: 10 additions & 3 deletions bc_obps/reporting/schema/report_attachment.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from ninja import Schema
import os
from ninja import ModelSchema
from reporting.models.report_attachment import ReportAttachment


class ReportAttachmentOut(Schema):
class ReportAttachmentOut(ModelSchema):

file_name: str

@staticmethod
def resolve_file_name(obj):
return os.path.basename(obj.attachment.name)

class Meta:
model = ReportAttachment
fields = ['id', 'attachment', 'attachment_type']
fields = ['id', 'attachment_type']
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Attachments from "@reporting/src/app/components/attachments/Attachments";
import AttachmentsPage from "@reporting/src/app/components/attachments/AttachmentsPage";
import defaultPageFactory from "@reporting/src/app/utils/defaultPageFactory";

export default defaultPageFactory(Attachments);
export default defaultPageFactory(AttachmentsPage);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Attachments from "@reporting/src/app/components/attachments/Attachments";
import AttachmentsPage from "@reporting/src/app/components/attachments/AttachmentsPage";
import defaultPageFactory from "@reporting/src/app/utils/defaultPageFactory";

export default defaultPageFactory(Attachments);
export default defaultPageFactory(AttachmentsPage);
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { ChangeEvent, MutableRefObject, useRef, useState } from "react";
import { UploadedAttachment } from "./types";

interface Props {
value?: string;
uploadedAttachment?: UploadedAttachment;
accept?: string;
title: string;
onFileChange: (file: File | undefined) => void;
}

const AttachmentElement: React.FC<Props> = ({
value,
uploadedAttachment,
accept = "application/pdf",
title,
onFileChange,
}) => {
const hiddenFileInput = useRef() as MutableRefObject<HTMLInputElement>;
const [currentValue, setCurrentValue] = useState(value);
const [currentValue, setCurrentValue] = useState(
uploadedAttachment?.file_name,
);
const [currentFile, setCurrentFile] = useState<File>();

const handleClick = () => {
Expand All @@ -31,7 +34,6 @@ const AttachmentElement: React.FC<Props> = ({

if (evt.target.files.length > 0) {
const file = evt.target.files[0];

setCurrentFile(file);
setCurrentValue(file.name);
onFileChange(file);
Expand Down Expand Up @@ -63,9 +65,10 @@ const AttachmentElement: React.FC<Props> = ({
{currentFile || currentValue ? (
<ul className="m-0 py-0 flex flex-col justify-start">
<li>
<a download={`preview`} href={"#"} className="file-download">
<a download={currentValue} href={"#"} className="file-download">
{currentValue}
</a>
{currentFile && <span className="ml-3">- will upload on save</span>}
</li>
</ul>
) : (
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ import postAttachments from "@bciers/actions/api/postAttachments";
import MultiStepWrapperWithTaskList from "./MultiStepWrapperWithTaskList";
import AttachmentElement from "./AttachmentElement";
import { useState } from "react";
import { UploadedAttachment } from "./types";

interface Props extends HasReportVersion {}
interface Props extends HasReportVersion {
uploaded_attachments: {
[attachment_type: string]: UploadedAttachment;
};
}

const AttachmentsForm: React.FC<Props> = ({ version_id }) => {
const AttachmentsForm: React.FC<Props> = ({
version_id,
uploaded_attachments,
}) => {
const taskListElements = getSignOffAndSubmitSteps(version_id);
const [files, setFiles] = useState<{ [filename: string]: File }>({});

Expand Down Expand Up @@ -51,10 +59,12 @@ const AttachmentsForm: React.FC<Props> = ({ version_id }) => {
<AttachmentElement
title="Verification statement"
onFileChange={(file) => handleChange("verification_statement", file)}
uploadedAttachment={uploaded_attachments.verification_statement}
/>
<AttachmentElement
title="WCI.352 and WCI.362"
onFileChange={(file) => handleChange("wci_352_362", file)}
uploadedAttachment={uploaded_attachments.wci_352_362}
/>
</MultiStepWrapperWithTaskList>
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { HasReportVersion } from "../../utils/defaultPageFactory";
import AttachmentsForm from "./AttachmentsForm";
import getAttachments from "@bciers/actions/api/getAttachments";
import { UploadedAttachment } from "./types";

const AttachmentsPage: React.FC<HasReportVersion> = async ({ version_id }) => {
const uploaded_attachments: UploadedAttachment[] =
await getAttachments(version_id);
console.log("!~!!!!@@~~~~~~~~~~~~~~~~~~~~~~~~```", uploaded_attachments);
const uploaded_attachments_dict = Object.fromEntries(
uploaded_attachments.map((a) => [a.attachment_type, a]),
);
return (
<AttachmentsForm
version_id={version_id}
uploaded_attachments={uploaded_attachments_dict}
/>
);
};

export default AttachmentsPage;
5 changes: 5 additions & 0 deletions bciers/apps/reporting/src/app/components/attachments/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface UploadedAttachment {
id: number;
file_name: string;
attachment_type: string;
}
7 changes: 7 additions & 0 deletions bciers/libs/actions/src/api/getAttachments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { actionHandler } from "@bciers/actions";

// 🛠️ Function to fetch a contact by id
export default async function getAttachments(report_version_id: number) {
const endpoint = `reporting/report-version/${report_version_id}/attachments`;
return actionHandler(endpoint, "GET", "");
}
25 changes: 25 additions & 0 deletions erd_diagrams/erd_reporting.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ ReportVersion {
DateTimeField archived_at
ForeignKey report
BooleanField is_latest_submitted
<<<<<<< HEAD
CharField report_type
=======
>>>>>>> 49d11b96b (chore: removing pdf files)
CharField status
}
ReportPersonResponsible {
Expand Down Expand Up @@ -312,6 +315,21 @@ ReportProduct {
FloatField quantity_sold_during_period
FloatField quantity_throughput_during_period
}
<<<<<<< HEAD
=======
ReportAttachment {
BigAutoField id
ForeignKey created_by
DateTimeField created_at
ForeignKey updated_by
DateTimeField updated_at
ForeignKey archived_by
DateTimeField archived_at
ForeignKey report_version
FileField attachment
CharField attachment_type
}
>>>>>>> 49d11b96b (chore: removing pdf files)
Report }|--|| User : created_by
Report }|--|| User : updated_by
Report }|--|| User : archived_by
Expand Down Expand Up @@ -424,3 +442,10 @@ ReportProduct }|--|| User : archived_by
ReportProduct }|--|| ReportVersion : report_version
ReportProduct }|--|| FacilityReport : facility_report
ReportProduct }|--|| RegulatedProduct : product
<<<<<<< HEAD
=======
ReportAttachment }|--|| User : created_by
ReportAttachment }|--|| User : updated_by
ReportAttachment }|--|| User : archived_by
ReportAttachment }|--|| ReportVersion : report_version
>>>>>>> 49d11b96b (chore: removing pdf files)

0 comments on commit a9aa7be

Please sign in to comment.