Skip to content

Commit 4ec0e8d

Browse files
authored
Merge pull request #5 from openagri-eu/46-issue
2 parents 1cfeb90 + 26ac500 commit 4ec0e8d

6 files changed

Lines changed: 71 additions & 33 deletions

File tree

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ docker compose up
5252

5353
The application will be served on http://127.0.0.1:8009 (I.E. typing localhost/docs in your browser will load the swagger documentation)
5454

55-
Full list of APIs available you can check [here](https://editor-next.swagger.io/?url=https://gist.githubusercontent.com/JoleVLF/c0d5998cda18b2d0d3a1da3a98e379aa/raw/0f159195275da4156a593d0a50e4e5b08563a02b/v23.json)
55+
Full list of APIs available you can check [here](https://editor-next.swagger.io/?url=https://gist.githubusercontent.com/JoleVLF/c29adf44808a683149426912383c75eb/raw/7b57a845ab37954424c0ef2108962fd248c6966f/api_v4.json)
5656
# Documentation
5757
<h3>GET</h3>
5858

@@ -94,6 +94,10 @@ Response is generated PDF file.
9494
- ### to_date
9595
- **Type**: `date`
9696
- **Description**: AOptional date filter (until which data is filtered)
97+
98+
- ### parcel_id
99+
- **Type**: `str`
100+
- **Description**: Optional parcel filter.
97101
-
98102
## Response
99103

@@ -121,7 +125,12 @@ Response is uuid of generated PDF file.
121125

122126
- ### to_date
123127
- **Type**: `date`
124-
- **Description**: AOptional date filter (until which data is filtered). If operation_id provided not used.
128+
- **Description**: Optional date filter (until which data is filtered). If operation_id provided not used.
129+
130+
- ### parcel_id
131+
- **Type**: `str`
132+
- **Description**: Optional parcel filter (When operation id not used).
133+
125134

126135
### data
127136
- **Type**: `UploadFile`
@@ -174,6 +183,10 @@ When service is run without Gatekeeper data must be provided in .json file forma
174183
- **Type**: `date`
175184
- **Description**: AOptional date filter (until which data is filtered)
176185

186+
- ### parcel_id
187+
- **Type**: `str`
188+
- **Description**: Optional parcel filter.
189+
177190
### data
178191
- **Type**: `UploadFile`
179192
- **Description**: API processes the data directly to generate the report if data passed. This parameter is not required and when it is, must be provided as an `UploadFile`.

app/api/api_v1/endpoints/report.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ async def generate_irrigation_report(
6161
data: UploadFile = None,
6262
from_date: datetime.date = None,
6363
to_date: datetime.date = None,
64+
parcel_id: str = None
6465
):
6566
"""
6667
Generates Irrigation Report PDF file
@@ -88,6 +89,7 @@ async def generate_irrigation_report(
8889
from_date=from_date,
8990
to_date=to_date,
9091
irrigation_id=irrigation_id,
92+
parcel_id=parcel_id
9193
)
9294

9395
return PDF(uuid=uuid_v4)
@@ -102,6 +104,7 @@ async def generate_generic_observation_report(
102104
operation_id: str = None,
103105
from_date: datetime.date = None,
104106
to_date: datetime.date = None,
107+
parcel_id: str = None
105108
):
106109
"""
107110
Generates Observation Report PDF file
@@ -126,6 +129,7 @@ async def generate_generic_observation_report(
126129
operation_id=operation_id,
127130
from_date=from_date,
128131
to_date=to_date,
132+
parcel_id=parcel_id
129133
)
130134

131135
return PDF(uuid=uuid_v4)
@@ -143,6 +147,7 @@ async def generate_animal_report(
143147
data: UploadFile = None,
144148
from_date: datetime.date = None,
145149
to_date: datetime.date = None,
150+
parcel_id: str = None
146151
):
147152
"""
148153
Generates Animal Report PDF file
@@ -171,6 +176,8 @@ async def generate_animal_report(
171176
params["parcel"] = str(parcel)
172177
if status is not None:
173178
params["status"] = status
179+
if parcel_id:
180+
params['parcel'] = parcel_id
174181

175182
background_tasks.add_task(
176183
process_animal_data,

app/schemas/compost.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,24 @@ class CropObservation(BaseModel):
4747
isMeasuredIn: Optional[str] = None
4848
observedProperty: Optional[str] = None
4949

50-
5150
class Operation(BaseModel):
5251
"""Model for farm operations"""
5352

5453
type: str = Field(alias="@type")
5554
id: str = Field(alias="@id")
56-
activityType: dict
55+
activityType: Optional[dict] = {}
5756
title: str = ""
5857
details: str = ""
5958
hasStartDatetime: Optional[datetime] = None
6059
hasEndDatetime: Optional[datetime] = None
6160
responsibleAgent: Optional[str] = ""
6261
usesAgriculturalMachinery: List[dict] = []
63-
isOperatedOn: dict = None
64-
operatedOn: dict = None
62+
isOperatedOn: Optional[dict] = None
63+
operatedOn: Optional[dict] = {}
6564
hasMeasurement: list[dict] = None
6665
hasNestedOperation: list[dict] = None
6766
usesIrrigationSystem: Optional[str] = None
67+
hasAgriParcel: Optional[dict] = None
6868

6969

7070
class AddRawMaterialOperation(Operation):

app/utils/animals_report.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,10 @@ def process_animal_data(
247247
token=token,
248248
params={"format": "json"},
249249
)
250-
251-
animals = parse_animal_data(json_data)
250+
if json_data:
251+
animals = parse_animal_data(json_data)
252+
else:
253+
animals = []
252254
try:
253255
anima_pdf = create_pdf_from_animals(animals, token)
254256
except Exception:

app/utils/farm_calendar_report.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,33 +91,39 @@ def create_farm_calendar_pdf(
9191
if operation.usesAgriculturalMachinery
9292
else None
9393
)
94-
if agr_mach_id:
94+
95+
agr_resp = None
96+
parcel_id = None
97+
if operation.hasAgriParcel:
98+
parcel_id = operation.hasAgriParcel.get("@id", None)
99+
100+
elif agr_mach_id:
95101
agr_resp = make_get_request(
96-
url=f'{settings.REPORTING_FARMCALENDAR_BASE_URL}{settings.REPORTING_FARMCALENDAR_URLS["machines"]}{agr_mach_id}/',
97-
token=token,
98-
params={"format": "json"},
102+
url=f'{settings.REPORTING_FARMCALENDAR_BASE_URL}{settings.REPORTING_FARMCALENDAR_URLS["machines"]}{agr_mach_id}/',
103+
token=token,
104+
params={"format": "json"})
105+
parcel_id = agr_resp.get("hasAgriParcel", {}).get("@id", None) if agr_mach_id else None
106+
107+
108+
address, farm = "", ""
109+
if parcel_id:
110+
address, farm = get_parcel_info(
111+
parcel_id.split(":")[-1], token, geolocator
99112
)
100-
if agr_resp:
101-
parcel_id = agr_resp.get("hasAgriParcel", {}).get("@id", None)
102-
address, farm = "", ""
103-
if parcel_id:
104-
address, farm = get_parcel_info(
105-
parcel_id.split(":")[-1], token, geolocator
106-
)
107113

108-
pdf.set_font("FreeSerif", "B", 10)
109-
pdf.cell(40, 8, "Parcel Location:")
110-
pdf.set_font("FreeSerif", "", 10)
111-
pdf.multi_cell(0, 8, address, ln=True, fill=True)
114+
pdf.set_font("FreeSerif", "B", 10)
115+
pdf.cell(40, 8, "Parcel Location:")
116+
pdf.set_font("FreeSerif", "", 10)
117+
pdf.multi_cell(0, 8, address, ln=True, fill=True)
112118

113-
pdf.set_font("FreeSerif", "B", 10)
114-
pdf.cell(
115-
40,
116-
8,
117-
"Farm information:",
118-
)
119-
pdf.set_font("FreeSerif", "", 10)
120-
pdf.multi_cell(0, 8, farm, ln=True, fill=True)
119+
pdf.set_font("FreeSerif", "B", 10)
120+
pdf.cell(
121+
40,
122+
8,
123+
"Farm information:",
124+
)
125+
pdf.set_font("FreeSerif", "", 10)
126+
pdf.multi_cell(0, 8, farm, ln=True, fill=True)
121127

122128
cp_id = (
123129
operation.isOperatedOn.get("@id", "N/A:N/A").split(":")[-1]
@@ -385,6 +391,7 @@ def process_farm_calendar_data(
385391
operation_id: str = None,
386392
from_date: datetime.date = None,
387393
to_date: datetime.date = None,
394+
parcel_id: str = None
388395
) -> None:
389396
"""
390397
Process farm calendar data and generate PDF report
@@ -428,6 +435,8 @@ def process_farm_calendar_data(
428435
params=params,
429436
)
430437

438+
if parcel_id:
439+
params['parcel'] = parcel_id
431440
operations = make_get_request(
432441
url=operation_url,
433442
token=token,
@@ -448,7 +457,7 @@ def process_farm_calendar_data(
448457
else:
449458
operation_url = f"{operation_url}{operation_id}/"
450459
del params["activity_type"]
451-
460+
logger.info("Delete act from params")
452461
operation_params = params.copy()
453462
operations = make_get_request(
454463
url=operation_url,

app/utils/irrigation_report.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def process_irrigation_data(
177177
from_date: datetime.date = None,
178178
to_date: datetime.date = None,
179179
irrigation_id: str = None,
180+
parcel_id: str = None,
180181
) -> None:
181182
"""
182183
Process irrigation data and generate PDF report
@@ -194,6 +195,9 @@ def process_irrigation_data(
194195
else:
195196
if not data:
196197
params = {"format": "json"}
198+
if parcel_id:
199+
params['parcel'] = parcel_id
200+
197201
decode_dates_filters(params, from_date, to_date)
198202
json_data = make_get_request(
199203
url=f'{settings.REPORTING_FARMCALENDAR_BASE_URL}{settings.REPORTING_FARMCALENDAR_URLS["irrigations"]}',
@@ -204,7 +208,10 @@ def process_irrigation_data(
204208
else:
205209
json_data = json.load(data.file)
206210

207-
operations = parse_irrigation_operations(json_data)
211+
if json_data:
212+
operations = parse_irrigation_operations(json_data)
213+
else:
214+
operations = []
208215

209216
try:
210217
pdf = create_pdf_from_operations(operations, token)

0 commit comments

Comments
 (0)