Skip to content

Commit 2314990

Browse files
authored
Merge pull request #52 from openagri-eu/main
Sync Fork Repo
2 parents 6e1c886 + 8ccf2cb commit 2314990

File tree

5 files changed

+215
-63
lines changed

5 files changed

+215
-63
lines changed

app/api/api_v1/endpoints/report.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ def retrieve_generated_pdf(
4444

4545
if not os.path.exists(file_path):
4646
raise HTTPException(
47-
status_code=404,
48-
detail=f"File with uuid {report_id} for logged user not found.",
47+
status_code=202,
48+
detail=f"PDF uuid {report_id} is being generated. Please be patient and try again in couple of seconds.",
4949
)
5050

5151
return FileResponse(

app/utils/animals_report.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ def create_pdf_from_animals(
9393
pdf.set_font("FreeSerif", "", 10)
9494
pdf.multi_cell(0, 8, identifier, ln=True, fill=True)
9595

96-
9796
pdf.set_font("FreeSerif", "B", 10)
9897
pdf.cell(
9998
40,
10099
8,
101100
"Farm information:",
102101
)
103102
pdf.set_font("FreeSerif", "", 10)
104-
pdf.multi_cell(0, 8, farm, ln=True, fill=True)
103+
farm_local = f"Name: {farm.name} | Municipality: {farm.municipality}"
104+
pdf.multi_cell(0, 8, farm_local, ln=True, fill=True)
105105

106106
pdf.set_font("FreeSerif", "B", 10)
107107
pdf.cell(

app/utils/farm_calendar_report.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from utils.json_handler import make_get_request
2121
from geopy.geocoders import Nominatim
2222

23-
geolocator = Nominatim(user_agent="reporting_app")
23+
geolocator = Nominatim(user_agent="reporting_app", timeout=2)
2424

2525
logging.basicConfig(level=logging.INFO)
2626
logger = logging.getLogger(__name__)
@@ -105,7 +105,6 @@ def create_farm_calendar_pdf(
105105
params={"format": "json"})
106106
parcel_id = agr_resp.get("hasAgriParcel", {}).get("@id", None) if agr_mach_id else None
107107

108-
109108
address, farm = "", ""
110109
if parcel_id:
111110
address, farm = get_parcel_info(
@@ -124,7 +123,8 @@ def create_farm_calendar_pdf(
124123
"Farm information:",
125124
)
126125
pdf.set_font("FreeSerif", "", 10)
127-
pdf.multi_cell(0, 8, farm, ln=True, fill=True)
126+
farm_local = f"Name: {farm.name} | Municipality: {farm.municipality}"
127+
pdf.multi_cell(0, 8, farm_local, ln=True, fill=True)
128128

129129
cp_id = (
130130
operation.isOperatedOn.get("@id", "N/A:N/A").split(":")[-1]
@@ -253,7 +253,8 @@ def create_farm_calendar_pdf(
253253
address, farm = get_parcel_info(parcel_id, token, geolocator)
254254
row.cell(f"{machinery_ids}")
255255
row.cell(address)
256-
row.cell(farm)
256+
farm_local = f"Name: {farm.name} | Municipality: {farm.municipality}"
257+
row.cell(farm_local)
257258
operation = calendar_data.operations[0]
258259
cp = (
259260
operation.isOperatedOn.get("@id").split(":")[3]

app/utils/irrigation_report.py

Lines changed: 159 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def parse_irrigation_operations(data: dict) -> Optional[List[IrrigationOperation
3131

3232
def create_pdf_from_operations(
3333
operations: List[IrrigationOperation], token: dict[str, str] = None,
34-
data_used: bool = False,
34+
data_used: bool = False, parcel_id: str = None,
35+
from_date: datetime.date = None,
36+
to_date: datetime.date = None,
3537
):
3638
"""
3739
Create PDF report from irrigation operations
@@ -43,13 +45,14 @@ def create_pdf_from_operations(
4345

4446
EX.ln(pdf)
4547

48+
today = datetime.now().strftime('%d/%m/%Y')
4649
pdf.set_font("FreeSerif", "B", 14)
4750
pdf.cell(0, 10, f"Irrigation Operation Report", ln=True, align="C")
4851
pdf.set_font("FreeSerif", style="", size=9)
4952
pdf.cell(
5053
0,
5154
7,
52-
f"Data Generated - {datetime.now().strftime('%d/%m/%Y')}",
55+
f"Data Generated - {today}",
5356
ln=True,
5457
align="C",
5558
)
@@ -63,18 +66,93 @@ def create_pdf_from_operations(
6366
pdf.line(pdf.l_margin, y_position, line_end_x, y_position)
6467
pdf.ln(5)
6568

69+
address, farm, parcel_defined = None, None, None
70+
from_date_local, to_date_local = today, None
71+
if from_date:
72+
from_date_local = from_date.strftime("%Y-%m-%d")
73+
74+
if to_date:
75+
to_date_local = to_date.strftime("%Y-%m-%d")
76+
else:
77+
to_date_local = ''
78+
79+
pdf.set_font("FreeSerif", "B", 10)
80+
pdf.cell(40, 8, "Reporting Period")
81+
pdf.set_font("FreeSerif", "", 10)
82+
pdf.multi_cell(0, 8, f"{from_date_local} / {to_date_local}", ln=True, fill=True)
83+
84+
if parcel_id:
85+
address, farm, identifier = get_parcel_info(
86+
parcel_id, token, geolocator, identifier_flag=True
87+
)
88+
pdf.set_font("FreeSerif", "B", 10)
89+
pdf.cell(40, 8, "Parcel Location:")
90+
pdf.set_font("FreeSerif", "", 10)
91+
pdf.multi_cell(0, 8, address, ln=True, fill=True)
92+
93+
pdf.set_font("FreeSerif", "B", 10)
94+
pdf.cell(40, 8, "Parcel Identifier:")
95+
pdf.set_font("FreeSerif", "", 10)
96+
pdf.multi_cell(0, 8, identifier, ln=True, fill=True)
97+
98+
pdf.set_font("FreeSerif", "B", 10)
99+
pdf.cell(
100+
40,
101+
8,
102+
"Farm Location:",
103+
)
104+
pdf.set_font("FreeSerif", "", 10)
105+
farm_local = f"Name: {farm.name} | Municipality: {farm.municipality}"
106+
pdf.multi_cell(0, 8, farm_local, ln=True, fill=True)
107+
108+
pdf.set_font("FreeSerif", "B", 10)
109+
pdf.cell(
110+
40,
111+
8,
112+
"Administrator:",
113+
)
114+
pdf.set_font("FreeSerif", "", 10)
115+
pdf.multi_cell(0, 8, farm.administrator, ln=True, fill=True)
116+
117+
pdf.set_font("FreeSerif", "B", 10)
118+
pdf.cell(
119+
40,
120+
8,
121+
"Contact Person:",
122+
)
123+
pdf.set_font("FreeSerif", "", 10)
124+
pdf.multi_cell(0, 8, farm.contactPerson, ln=True, fill=True)
125+
126+
pdf.set_font("FreeSerif", "B", 10)
127+
pdf.cell(
128+
40,
129+
8,
130+
"Farm vat:",
131+
)
132+
pdf.set_font("FreeSerif", "", 10)
133+
pdf.multi_cell(0, 8, farm.vatID, ln=True, fill=True)
134+
135+
pdf.set_font("FreeSerif", "B", 10)
136+
pdf.cell(
137+
40,
138+
8,
139+
"Farm Description:",
140+
)
141+
pdf.set_font("FreeSerif", "", 10)
142+
pdf.multi_cell(0, 8, farm.description, fill=True)
143+
parcel_defined = True
144+
66145
if len(operations) == 1:
67146
op = operations[0]
68-
parcel_id = op.operatedOn.get("@id") if op.operatedOn else None
69-
address = ""
70-
farm = ""
71-
identifier = ""
72-
if parcel_id:
73-
parcel = parcel_id.split(":")[3] if op.operatedOn else None
74-
if parcel:
75-
address, farm, identifier = get_parcel_info(
76-
parcel_id.split(":")[-1], token, geolocator, identifier_flag=True
77-
)
147+
if not parcel_defined:
148+
parcel_id = op.operatedOn.get("@id") if op.operatedOn else None
149+
identifier = ""
150+
if parcel_id:
151+
parcel = parcel_id.split(":")[3] if op.operatedOn else None
152+
if parcel:
153+
address, farm, identifier = get_parcel_info(
154+
parcel_id.split(":")[-1], token, geolocator, identifier_flag=True
155+
)
78156
start_time = (
79157
op.hasStartDatetime.strftime("%d/%m/%Y") if op.hasStartDatetime else ""
80158
)
@@ -98,10 +176,47 @@ def create_pdf_from_operations(
98176
pdf.cell(
99177
40,
100178
8,
101-
"Farm information:",
179+
"Farm Location:",
180+
)
181+
pdf.set_font("FreeSerif", "", 10)
182+
farm_local = f"Name: {farm.name} | Municipality: {farm.municipality}"
183+
pdf.multi_cell(0, 8, farm_local, ln=True, fill=True)
184+
185+
pdf.set_font("FreeSerif", "B", 10)
186+
pdf.cell(
187+
40,
188+
8,
189+
"Administrator:",
190+
)
191+
pdf.set_font("FreeSerif", "", 10)
192+
pdf.multi_cell(0, 8, farm.administrator, ln=True, fill=True)
193+
194+
pdf.set_font("FreeSerif", "B", 10)
195+
pdf.cell(
196+
40,
197+
8,
198+
"Contact Person:",
199+
)
200+
pdf.set_font("FreeSerif", "", 10)
201+
pdf.multi_cell(0, 8, farm.contactPerson, ln=True, fill=True)
202+
203+
pdf.set_font("FreeSerif", "B", 10)
204+
pdf.cell(
205+
40,
206+
8,
207+
"Farm vat:",
102208
)
103209
pdf.set_font("FreeSerif", "", 10)
104-
pdf.multi_cell(0, 8, farm, ln=True, fill=True)
210+
pdf.multi_cell(0, 8, farm.vatID, ln=True, fill=True)
211+
212+
pdf.set_font("FreeSerif", "B", 10)
213+
pdf.cell(
214+
40,
215+
8,
216+
"Farm Description:",
217+
)
218+
pdf.set_font("FreeSerif", "", 10)
219+
pdf.multi_cell(0, 8, farm.description, ln=True, fill=True)
105220

106221
pdf.set_font("FreeSerif", "B", 10)
107222
pdf.cell(
@@ -114,23 +229,23 @@ def create_pdf_from_operations(
114229
0,
115230
8,
116231
f"{op.hasAppliedAmount.numericValue} ({op.hasAppliedAmount.unit})",
117-
ln=True,
118232
fill=True,
119233
)
120234

121235
if len(operations) > 1:
122236
if not data_used:
123237
operations.sort(key=lambda x: x.hasStartDatetime)
124238
pdf.set_fill_color(0, 255, 255)
125-
with pdf.table(text_align="CENTER", padding=0.5) as table:
239+
with pdf.table(text_align="CENTER") as table:
126240
row = table.row()
127241
pdf.set_font("FreeSerif", "B", 10)
128242
row.cell("Start - End")
129-
row.cell("Details")
130-
row.cell("Parcel")
131-
row.cell("Parcel Identifier")
132-
row.cell("Farm")
133-
row.cell("Value info")
243+
if not parcel_defined:
244+
row.cell("Parcel")
245+
row.cell("Parcel Identifier")
246+
row.cell("Farm")
247+
row.cell("Dose")
248+
row.cell("Unit")
134249
row.cell("Irrigation System")
135250
pdf.set_font("FreeSerif", "", 9)
136251
pdf.set_fill_color(255, 255, 240)
@@ -146,32 +261,36 @@ def create_pdf_from_operations(
146261
op.hasEndDatetime.strftime("%d/%m/%Y") if op.hasEndDatetime else ""
147262
)
148263
row.cell(f"{start_time} - {end_time}")
149-
row.cell(op.details)
150-
151-
parcel_id = op.operatedOn.get("@id") if op.operatedOn else None
152-
address = ""
153-
farm = ""
154-
identifier = ""
155-
if parcel_id:
156-
parcel = parcel_id.split(":")[3] if op.operatedOn else None
157-
if parcel:
158-
address, farm, identifier= get_parcel_info(
159-
parcel_id.split(":")[-1], token, geolocator, identifier_flag=True
160-
)
161-
row.cell(address)
162-
row.cell(identifier)
163-
row.cell(farm)
164264

265+
if not parcel_defined:
266+
parcel_id = op.operatedOn.get("@id") if op.operatedOn else None
267+
address = ""
268+
farm = ""
269+
identifier = ""
270+
if parcel_id:
271+
parcel = parcel_id.split(":")[3] if op.operatedOn else None
272+
if parcel:
273+
address, farm, identifier= get_parcel_info(
274+
parcel_id.split(":")[-1], token, geolocator, identifier_flag=True
275+
)
276+
row.cell(address)
277+
row.cell(identifier)
278+
farm_local = f"Name: {farm.name} | Municipality: {farm.municipality}"
279+
row.cell(farm_local)
280+
281+
row.cell(
282+
f"{op.hasAppliedAmount.numericValue}",
283+
)
165284
row.cell(
166-
f"{op.hasAppliedAmount.numericValue} ({op.hasAppliedAmount.unit})",
285+
f"{op.hasAppliedAmount.unit}",
167286
)
168287

169288
if isinstance(op.usesIrrigationSystem, dict):
170289
local_sys = op.usesIrrigationSystem.get("name")
171290
else:
172291
local_sys = op.usesIrrigationSystem
173292
row.cell(local_sys)
174-
pdf.ln(10)
293+
pdf.ln(2)
175294

176295
return pdf
177296

@@ -223,7 +342,8 @@ def process_irrigation_data(
223342
operations = []
224343

225344
try:
226-
pdf = create_pdf_from_operations(operations, token, data_used)
345+
pdf = create_pdf_from_operations(operations, token, data_used, parcel_id=parcel_id, from_date=from_date,
346+
to_date=to_date)
227347
except Exception:
228348
raise HTTPException(
229349
status_code=400, detail="PDF generation of irrigation report failed."

0 commit comments

Comments
 (0)