-
-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathmapping_service.py
More file actions
596 lines (543 loc) · 22.2 KB
/
mapping_service.py
File metadata and controls
596 lines (543 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import datetime
import xml.etree.ElementTree as ET
from databases import Database
from fastapi import BackgroundTasks
from geoalchemy2 import WKBElement
from geoalchemy2.shape import to_shape
from loguru import logger
from backend.exceptions import NotFound
from backend.models.dtos.mapping_dto import (
ExtendLockTimeDTO,
LockTaskDTO,
MappedTaskDTO,
StopMappingTaskDTO,
TaskCommentDTO,
TaskDTO,
)
from backend.models.postgis.statuses import MappingNotAllowed
from backend.models.postgis.task import Task, TaskAction, TaskHistory, TaskStatus
from backend.models.postgis.utils import UserLicenseError
from backend.services.messaging.message_service import MessageService
from backend.services.project_service import ProjectService
from backend.services.stats_service import StatsService
class MappingServiceError(Exception):
"""Custom Exception to notify callers an error occurred when handling mapping"""
def __init__(self, message):
logger.debug(message)
class MappingService:
@staticmethod
async def get_task(task_id: int, project_id: int, db: Database) -> Task:
"""
Get task from DB
:raises: NotFound
"""
task = await Task.get(task_id, project_id, db)
if not task:
raise NotFound(
sub_code="TASK_NOT_FOUND", project_id=project_id, task_id=task_id
)
return task
@staticmethod
async def get_task_as_dto(
task_id: int,
project_id: int,
db,
preferred_local: str = "en",
) -> TaskDTO:
"""Get task as DTO for transmission over API"""
task = await Task.exists(task_id, project_id, db)
if not task:
raise NotFound(
sub_code="TASK_NOT_FOUND", project_id=project_id, task_id=task_id
)
task_dto = await Task.as_dto_with_instructions(
task_id, project_id, db, preferred_local
)
return task_dto
@staticmethod
async def _is_task_undoable(
logged_in_user_id: int, task: dict, db: Database
) -> bool:
"""Determines if the current task status can be undone by the logged in user"""
if logged_in_user_id and TaskStatus(task.task_status) not in [
TaskStatus.LOCKED_FOR_MAPPING,
TaskStatus.LOCKED_FOR_VALIDATION,
TaskStatus.READY,
]:
last_action = await TaskHistory.get_last_action(
task.project_id, task.id, db
)
# User requesting task made the last change, so they are allowed to undo it.
is_user_permitted, _ = await ProjectService.is_user_permitted_to_validate(
task.project_id, logged_in_user_id, db
)
if last_action.user_id == logged_in_user_id or is_user_permitted:
return True
return False
@staticmethod
async def lock_task_for_mapping(
lock_task_dto: LockTaskDTO, db: Database
) -> TaskDTO:
"""
Sets the task_locked status to locked so no other user can work on it
:param lock_task_dto: DTO with data needed to lock the task
:raises TaskServiceError
:return: Updated task, or None if not found
"""
task = await MappingService.get_task(
lock_task_dto.task_id, lock_task_dto.project_id, db
)
if task.locked_by != lock_task_dto.user_id:
if not Task.is_mappable(task):
raise MappingServiceError(
"InvalidTaskState- Task in invalid state for mapping"
)
user_can_map, error_reason = await ProjectService.is_user_permitted_to_map(
lock_task_dto.project_id, lock_task_dto.user_id, db
)
if not user_can_map:
if error_reason == MappingNotAllowed.USER_NOT_ACCEPTED_LICENSE:
raise UserLicenseError("User must accept license to map this task")
elif error_reason == MappingNotAllowed.USER_NOT_ON_ALLOWED_LIST:
raise MappingServiceError(
"UserNotAllowed- User not on allowed list"
)
elif error_reason == MappingNotAllowed.PROJECT_NOT_PUBLISHED:
raise MappingServiceError(
"ProjectNotPublished- Project is not published"
)
elif error_reason == MappingNotAllowed.USER_ALREADY_HAS_TASK_LOCKED:
raise MappingServiceError(
"UserAlreadyHasTaskLocked- User already has task locked"
)
else:
raise MappingServiceError(
f"{error_reason}- Mapping not allowed because: {error_reason}"
)
await Task.lock_task_for_mapping(
lock_task_dto.task_id, lock_task_dto.project_id, lock_task_dto.user_id, db
)
return await Task.as_dto_with_instructions(
lock_task_dto.task_id,
lock_task_dto.project_id,
db,
lock_task_dto.preferred_locale,
)
@staticmethod
async def unlock_task_after_mapping(
mapped_task: MappedTaskDTO,
db: Database,
background_tasks: BackgroundTasks,
) -> TaskDTO:
"""Unlocks the task and sets the task history appropriately"""
# Fetch the task locked by the user
task = await MappingService.get_task_locked_by_user(
mapped_task.project_id, mapped_task.task_id, mapped_task.user_id, db
)
# Validate the new state
new_state = TaskStatus[mapped_task.status.upper()]
if new_state not in [
TaskStatus.MAPPED,
TaskStatus.BADIMAGERY,
TaskStatus.READY,
]:
raise MappingServiceError(
"InvalidUnlockState - Can only set status to MAPPED, BADIMAGERY, READY after mapping"
)
last_state = await TaskHistory.get_last_status(
mapped_task.project_id, mapped_task.task_id, db
)
await StatsService.update_stats_after_task_state_change(
mapped_task.project_id, mapped_task.user_id, last_state, new_state, db
)
if mapped_task.comment:
await MessageService.send_message_after_comment(
mapped_task.user_id,
mapped_task.comment,
task.id,
mapped_task.project_id,
db,
)
# Unlock the task and change its state
await Task.unlock_task(
task_id=mapped_task.task_id,
project_id=mapped_task.project_id,
user_id=mapped_task.user_id,
new_state=new_state,
db=db,
comment=mapped_task.comment,
)
# Send email on project progress
background_tasks.add_task(
ProjectService.send_email_on_project_progress, mapped_task.project_id
)
return await Task.as_dto_with_instructions(
task_id=mapped_task.task_id,
project_id=mapped_task.project_id,
db=db,
preferred_locale=mapped_task.preferred_locale,
)
@staticmethod
async def stop_mapping_task(stop_task: StopMappingTaskDTO, db: Database) -> TaskDTO:
"""Unlocks the task and revert the task status to the last one"""
task = await MappingService.get_task_locked_by_user(
stop_task.project_id, stop_task.task_id, stop_task.user_id, db
)
if stop_task.comment:
# Parses comment to see if any users have been @'d
await MessageService.send_message_after_comment(
stop_task.user_id, stop_task.comment, task.id, stop_task.project_id, db
)
await Task.reset_lock(
task.id,
stop_task.project_id,
task.task_status,
stop_task.user_id,
stop_task.comment,
db,
)
return await Task.as_dto_with_instructions(
task.id, stop_task.project_id, db, stop_task.preferred_locale
)
@staticmethod
async def get_task_locked_by_user(
project_id: int, task_id: int, user_id: int, db: Database
):
"""Returns task specified by project id and task id if found and locked for mapping by user"""
query = """
SELECT * FROM tasks
WHERE id = :task_id AND project_id = :project_id
"""
task = await db.fetch_one(
query, values={"task_id": task_id, "project_id": project_id}
)
if task is None:
raise NotFound(
status_code=404,
sub_code="TASK_NOT_FOUND",
project_id=project_id,
task_id=task_id,
)
if task.task_status != TaskStatus.LOCKED_FOR_MAPPING.value:
raise MappingServiceError(
"LockBeforeUnlocking- Status must be LOCKED_FOR_MAPPING to unlock"
)
if task.locked_by != user_id:
raise MappingServiceError(
"TaskNotOwned- Attempting to unlock a task owned by another user"
)
return task
@staticmethod
async def add_task_comment(task_comment: TaskCommentDTO, db: Database) -> TaskDTO:
"""Adds the comment to the task history"""
# Check if project exists
await ProjectService.exists(task_comment.project_id, db)
task = await Task.get(task_comment.task_id, task_comment.project_id, db)
if task is None:
raise NotFound(
sub_code="TASK_NOT_FOUND",
project_id=task_comment.project_id,
task_id=task_comment.task_id,
)
await Task.set_task_history(
task_id=task_comment.task_id,
project_id=task_comment.project_id,
user_id=task_comment.user_id,
action=TaskAction.COMMENT,
db=db,
comment=task_comment.comment,
)
# Parse comment to see if any users have been @'d
await MessageService.send_message_after_comment(
task_comment.user_id,
task_comment.comment,
task.id,
task_comment.project_id,
db,
)
return await Task.as_dto_with_instructions(
task_comment.task_id,
task_comment.project_id,
db,
task_comment.preferred_locale,
)
@staticmethod
async def generate_gpx(
project_id: int, task_ids_str: str, db: Database, timestamp=None
):
"""
Creates a GPX file for supplied tasks. Timestamp is for unit testing only.
You can use the following URL to test locally:
http://www.openstreetmap.org/edit?editor=id&#map=11/31.50362930069913/34.628906243797054&comment=CHANGSET_COMMENT&gpx=http://localhost:5000/api/v2/projects/{project_id}/tasks/queries/gpx%3Ftasks=2
"""
if timestamp is None:
timestamp = datetime.datetime.utcnow()
root = ET.Element(
"gpx",
attrib=dict(
version="1.1",
creator="HOT Tasking Manager",
xmlns="http://www.topografix.com/GPX/1/1",
),
)
# Create GPX Metadata element
metadata = ET.Element("metadata")
link = ET.SubElement(
metadata,
"link",
attrib=dict(href="https://github.com/hotosm/tasking-manager"),
)
ET.SubElement(link, "text").text = "HOT Tasking Manager"
ET.SubElement(metadata, "time").text = timestamp.isoformat()
root.append(metadata)
# Create trk element
trk = ET.Element("trk")
root.append(trk)
ET.SubElement(trk, "name").text = (
f"Task for project {project_id}. Do not edit outside of this area!"
)
# Construct trkseg elements
if task_ids_str is not None:
task_ids = list(map(int, task_ids_str.split(",")))
tasks = await Task.get_tasks(project_id, task_ids, db)
if not tasks or len(tasks) == 0:
raise NotFound(
sub_code="TASKS_NOT_FOUND", project_id=project_id, task_ids=task_ids
)
else:
tasks = await Task.get_all_tasks(project_id, db)
if not tasks or len(tasks) == 0:
raise NotFound(sub_code="TASKS_NOT_FOUND", project_id=project_id)
for task in tasks:
# task_geom = shape.to_shape(task.geometry)
if isinstance(task["geometry"], (bytes, str)):
task_geom = to_shape(WKBElement(task["geometry"], srid=4326))
else:
raise ValueError("Invalid geometry format")
for poly in task_geom.geoms:
trkseg = ET.SubElement(trk, "trkseg")
for point in poly.exterior.coords:
ET.SubElement(
trkseg,
"trkpt",
attrib=dict(lon=str(point[0]), lat=str(point[1])),
)
wpt = ET.Element(
"wpt", attrib=dict(lon=str(point[0]), lat=str(point[1]))
)
root.append(wpt)
xml_gpx = ET.tostring(root, encoding="utf8")
return xml_gpx
@staticmethod
async def generate_osm_xml(project_id: int, task_ids_str: str, db: Database) -> str:
"""Generate xml response suitable for loading into JOSM. A sample output file is in
/backend/helpers/testfiles/osm-sample.xml"""
# Note XML created with upload No to ensure it will be rejected by OSM if uploaded by mistake
root = ET.Element(
"osm",
attrib=dict(version="0.6", upload="never", creator="HOT Tasking Manager"),
)
if task_ids_str:
task_ids = list(map(int, task_ids_str.split(",")))
tasks = await Task.get_tasks(project_id, task_ids, db)
if not tasks or len(tasks) == 0:
raise NotFound(
sub_code="TASKS_NOT_FOUND", project_id=project_id, task_ids=task_ids
)
else:
tasks = await Task.get_all_tasks(project_id, db)
if not tasks or len(tasks) == 0:
raise NotFound(sub_code="TASKS_NOT_FOUND", project_id=project_id)
fake_id = -1 # We use fake-ids to ensure XML will not be validated by OSM
for task in tasks:
if isinstance(task["geometry"], (bytes, str)):
task_geom = to_shape(WKBElement(task["geometry"], srid=4326))
else:
raise ValueError("Invalid geometry format")
way = ET.SubElement(
root,
"way",
attrib=dict(id=str((task.id * -1)), action="modify", visible="true"),
)
for poly in task_geom.geoms:
for point in poly.exterior.coords:
ET.SubElement(
root,
"node",
attrib=dict(
action="modify",
visible="true",
id=str(fake_id),
lon=str(point[0]),
lat=str(point[1]),
),
)
ET.SubElement(way, "nd", attrib=dict(ref=str(fake_id)))
fake_id -= 1
xml_gpx = ET.tostring(root, encoding="utf8")
return xml_gpx
@staticmethod
async def undo_mapping(
project_id: int,
task_id: int,
user_id: int,
db: Database,
preferred_locale: str = "en",
) -> TaskDTO:
"""Allows a user to Undo the task state they updated"""
task = await MappingService.get_task(task_id, project_id, db)
if not await MappingService._is_task_undoable(user_id, task, db):
raise MappingServiceError(
"UndoPermissionError- Undo not allowed for this user"
)
current_state = TaskStatus(task.task_status)
# Set the state to the previous state in the workflow
if current_state == TaskStatus.VALIDATED:
undo_state = TaskStatus.MAPPED
elif current_state == TaskStatus.BADIMAGERY:
undo_state = TaskStatus.READY
elif current_state == TaskStatus.MAPPED:
undo_state = TaskStatus.READY
else:
undo_state = await TaskHistory.get_last_status(
project_id, task_id, db, True
)
# Refer to last action for user of it.
last_action = await TaskHistory.get_last_action(project_id, task_id, db)
await StatsService.update_stats_after_task_state_change(
project_id, last_action.user_id, current_state, undo_state, db, "undo"
)
await Task.unlock_task(
task_id=task_id,
project_id=project_id,
user_id=user_id,
new_state=undo_state,
db=db,
comment=f"Undo state from {current_state.name} to {undo_state.name}",
undo=True,
)
return await Task.as_dto_with_instructions(
task_id, project_id, db, preferred_locale
)
@staticmethod
async def map_all_tasks(project_id: int, user_id: int, db: Database):
"""Marks all tasks on a project as mapped using raw SQL queries"""
query = """
SELECT id, task_status
FROM tasks
WHERE project_id = :project_id
AND task_status NOT IN (:bad_imagery, :mapped, :validated)
"""
tasks_to_map = await db.fetch_all(
query=query,
values={
"project_id": project_id,
"bad_imagery": TaskStatus.BADIMAGERY.value,
"mapped": TaskStatus.MAPPED.value,
"validated": TaskStatus.VALIDATED.value,
},
)
for task in tasks_to_map:
task_id = task["id"]
current_status = TaskStatus(task["task_status"])
# Lock the task for mapping if it's not already locked
if current_status not in [
TaskStatus.LOCKED_FOR_MAPPING,
TaskStatus.LOCKED_FOR_VALIDATION,
]:
await Task.lock_task_for_mapping(task_id, project_id, user_id, db)
# Unlock the task and set its status to MAPPED
await Task.unlock_task(
task_id=task_id,
project_id=project_id,
user_id=user_id,
new_state=TaskStatus.MAPPED,
db=db,
)
project_update_query = """
UPDATE projects
SET tasks_mapped = (total_tasks - tasks_bad_imagery - tasks_validated)
WHERE id = :project_id
"""
await db.execute(query=project_update_query, values={"project_id": project_id})
@staticmethod
async def reset_all_badimagery(project_id: int, user_id: int, db: Database):
"""Marks all bad imagery tasks as ready for mapping and resets the bad imagery counter"""
# Fetch all tasks with status BADIMAGERY for the given project
badimagery_query = """
SELECT id FROM tasks
WHERE task_status = :task_status AND project_id = :project_id
"""
badimagery_tasks = await db.fetch_all(
query=badimagery_query,
values={
"task_status": TaskStatus.BADIMAGERY.value,
"project_id": project_id,
},
)
for task in badimagery_tasks:
task_id = task["id"]
await Task.lock_task_for_mapping(task_id, project_id, user_id, db)
await Task.unlock_task(task_id, project_id, user_id, TaskStatus.READY, db)
# Reset bad imagery counter in the project
reset_query = """
UPDATE projects
SET tasks_bad_imagery = 0
WHERE id = :project_id
"""
await db.execute(query=reset_query, values={"project_id": project_id})
@staticmethod
async def lock_time_can_be_extended(
project_id: int, task_id: int, user_id: int, db: Database
):
task = await Task.get(task_id, project_id, db)
if task is None:
raise NotFound(
sub_code="TASK_NOT_FOUND", project_id=project_id, task_id=task_id
)
if TaskStatus(task.task_status) not in [
TaskStatus.LOCKED_FOR_MAPPING,
TaskStatus.LOCKED_FOR_VALIDATION,
]:
raise MappingServiceError(
f"TaskStatusNotLocked- Task {task_id} status is not LOCKED_FOR_MAPPING or LOCKED_FOR_VALIDATION."
)
if task.locked_by != user_id:
raise MappingServiceError(
"LockedByAnotherUser- Task is locked by another user."
)
@staticmethod
async def extend_task_lock_time(extend_dto: ExtendLockTimeDTO, db: Database):
"""
Extends expiry time of locked tasks.
:raises ValidatorServiceError
"""
# Validate each task before extending lock time
for task_id in extend_dto.task_ids:
await MappingService.lock_time_can_be_extended(
extend_dto.project_id, task_id, extend_dto.user_id, db
)
# Extend lock time for validated tasks
for task_id in extend_dto.task_ids:
task = await Task.get(task_id, extend_dto.project_id, db)
action = (
TaskAction.EXTENDED_FOR_MAPPING
if task["task_status"] == TaskStatus.LOCKED_FOR_MAPPING
else TaskAction.EXTENDED_FOR_VALIDATION
)
# Update the duration of the lock/extension before creating new history
last_history = await TaskHistory.get_last_action(
extend_dto.project_id, task_id
)
# To reset a lock the last action must have been either lock or extension
last_action = TaskAction[last_history["result"][0]["action"]]
await TaskHistory.update_task_locked_with_duration(
task_id,
extend_dto.project_id,
last_action,
extend_dto.user_id,
db,
)
await Task.set_task_history(
task_id, extend_dto.project_id, extend_dto.user_id, action, db
)