(self, input)
| 528 | self.data[doc['_id']] = deepcopy(doc) |
| 529 | |
| 530 | def insert(self, input): |
| 531 | oldData = deepcopy(self.data) |
| 532 | try: |
| 533 | if isinstance(input, OrderedDict): |
| 534 | self._insert(sort_id_field(input)) |
| 535 | elif isinstance(input, list): |
| 536 | all_ids = set() |
| 537 | input = map(sort_id_field, input) |
| 538 | for i in input: |
| 539 | if '_id' in i: |
| 540 | if not self.options.object_field_order_matters and isinstance(i['_id'], HashableOrderedDict): |
| 541 | i['_id'] = HashableOrderedDict(sorted(i['_id'].items(), key=lambda (key, value): key)) |
| 542 | if i['_id'] in all_ids: |
| 543 | # print i['_id'] |
| 544 | raise MongoModelException("Duplicated value not allowed by unique index", code=11000) |
| 545 | all_ids.add(i['_id']) |
| 546 | # All '_id's are good |
| 547 | buffer = [] |
| 548 | # keep in mind when inserting many docs at once, the operation needs to be atomic, i.e. all or nothing. |
| 549 | for doc in input: |
| 550 | if '_id' not in doc: |
| 551 | doc['_id'] = gen.random_object_id() |
| 552 | if doc['_id'] in self.data: |
| 553 | raise MongoModelException("Duplicated value not allowed by unique index", code=11000) |
| 554 | buffer.append(doc) |
| 555 | tmp = self.data.values() + buffer |
| 556 | for index in self.indexes: |
| 557 | if not index.inError: |
| 558 | index.validate_and_build_entry(tmp) |
| 559 | # Ready to insert them all. |
| 560 | for doc in buffer: |
| 561 | self.data[doc['_id']] = deepcopy(doc) |
| 562 | else: |
| 563 | raise MongoModelException("Tried to insert an unordered document.") |
| 564 | except MongoModelException as e: |
| 565 | self.data = oldData |
| 566 | raise e |
| 567 | |
| 568 | def insert_one(self, dict): |
| 569 | self.insert(dict) |
no test coverage detected