|
17 | 17 | ExtractDatasetType, |
18 | 18 | ExtractDependency, |
19 | 19 | ExtractProcess, |
| 20 | + ExtractSource, |
| 21 | + ExtractSourceObject, |
20 | 22 | ExtractStatus, |
21 | 23 | ) |
| 24 | +from process_tracker.models.source import ( |
| 25 | + Source, |
| 26 | + SourceLocation, |
| 27 | + SourceObjectAttribute, |
| 28 | + SourceObjectLocation, |
| 29 | +) |
22 | 30 | from process_tracker.models.source import DatasetType |
23 | 31 |
|
24 | 32 |
|
@@ -104,6 +112,7 @@ def __init__( |
104 | 112 | self.full_filename = self.get_full_filename() |
105 | 113 | self.dataset_types = self.get_dataset_types() |
106 | 114 | self.extract_process = self.retrieve_extract_process() |
| 115 | + self.sources = self.extract.extract_sources |
107 | 116 |
|
108 | 117 | else: |
109 | 118 | if filename is None: |
@@ -192,6 +201,28 @@ def __init__( |
192 | 201 |
|
193 | 202 | self.extract_process = self.retrieve_extract_process() |
194 | 203 |
|
| 204 | + if self.process_run.source_objects is not None: |
| 205 | + self.logger.info( |
| 206 | + "Associating source system(s) object(s) with extract and location." |
| 207 | + ) |
| 208 | + self.source_objects = self.register_extract_sources( |
| 209 | + source_objects=self.process_run.source_objects |
| 210 | + ) |
| 211 | + self.sources = self.source_objects |
| 212 | + |
| 213 | + elif self.process_run.process.sources is not None: |
| 214 | + self.logger.info( |
| 215 | + "Associating source system(s) with extract and location." |
| 216 | + ) |
| 217 | + |
| 218 | + self.sources = self.register_extract_sources( |
| 219 | + sources=self.process_run.sources |
| 220 | + ) |
| 221 | + |
| 222 | + else: |
| 223 | + self.logger.info("No source system(s) to associate to.") |
| 224 | + self.sources = None |
| 225 | + |
195 | 226 | if status is not None: |
196 | 227 | self.logger.info("Status was provided by user.") |
197 | 228 | self.change_extract_status(new_status=status) |
@@ -422,6 +453,60 @@ def register_extract_dataset_types(self, dataset_types): |
422 | 453 |
|
423 | 454 | return dataset_types |
424 | 455 |
|
| 456 | + def register_extract_sources(self, sources=None, source_objects=None): |
| 457 | + """ |
| 458 | + For the provided sources from process_run instance, associate with given Extract instance. |
| 459 | + :param sources: List of sources from process_run record. |
| 460 | + :param source_objects: List of sources and their objects from process_run record. |
| 461 | + :return: |
| 462 | + """ |
| 463 | + source_list = list() |
| 464 | + |
| 465 | + if source_objects is not None: |
| 466 | + |
| 467 | + for object in source_objects: |
| 468 | + self.logger.debug( |
| 469 | + "Associating extract %s to source %s." |
| 470 | + % (self.extract.extract_id, object.source_object_id) |
| 471 | + ) |
| 472 | + |
| 473 | + source_object = self.data_store.get_or_create_item( |
| 474 | + model=ExtractSourceObject, |
| 475 | + extract_id=self.extract.extract_id, |
| 476 | + source_object_id=object.source_object_id, |
| 477 | + ) |
| 478 | + source_list.append(source_object) |
| 479 | + |
| 480 | + self.data_store.get_or_create_item( |
| 481 | + SourceObjectLocation, |
| 482 | + source_object_id=object.source_object_id, |
| 483 | + location_id=self.extract.extract_location_id, |
| 484 | + ) |
| 485 | + |
| 486 | + elif sources is not None: |
| 487 | + |
| 488 | + for source in sources: |
| 489 | + |
| 490 | + self.logger.debug( |
| 491 | + "Associating extract %s to source %s." |
| 492 | + % (self.extract.extract_id, source.source_id) |
| 493 | + ) |
| 494 | + extract_source = self.data_store.get_or_create_item( |
| 495 | + model=ExtractSource, |
| 496 | + extract_id=self.extract.extract_id, |
| 497 | + source_id=source.source_id, |
| 498 | + ) |
| 499 | + source_list.append(extract_source) |
| 500 | + self.logger.debug("Extract source record created. %s" % extract_source) |
| 501 | + |
| 502 | + self.data_store.get_or_create_item( |
| 503 | + model=SourceLocation, |
| 504 | + source_id=source.source_id, |
| 505 | + location_id=self.extract.extract_location_id, |
| 506 | + ) |
| 507 | + |
| 508 | + return source_list |
| 509 | + |
425 | 510 | def retrieve_extract_process(self): |
426 | 511 | """ |
427 | 512 | Create and initialize or retrieve the process/extract relationship. |
|
0 commit comments