(self, app=None, **kwargs)
| 64 | _connection = None |
| 65 | |
| 66 | def __init__(self, app=None, **kwargs): |
| 67 | self.options = {} |
| 68 | |
| 69 | super().__init__(app, **kwargs) |
| 70 | |
| 71 | if not pymongo: |
| 72 | raise ImproperlyConfigured( |
| 73 | 'You need to install the pymongo library to use the ' |
| 74 | 'MongoDB backend.') |
| 75 | |
| 76 | # Set option defaults |
| 77 | for key, value in self._prepare_client_options().items(): |
| 78 | self.options.setdefault(key, value) |
| 79 | |
| 80 | # update conf with mongo uri data, only if uri was given |
| 81 | if self.url: |
| 82 | self.url = self._ensure_mongodb_uri_compliance(self.url) |
| 83 | |
| 84 | uri_data = uri_parser.parse_uri(self.url) |
| 85 | # build the hosts list to create a mongo connection |
| 86 | hostslist = [ |
| 87 | f'{x[0]}:{x[1]}' for x in uri_data['nodelist'] |
| 88 | ] |
| 89 | self.user = uri_data['username'] |
| 90 | self.password = uri_data['password'] |
| 91 | self.mongo_host = hostslist |
| 92 | if uri_data['database']: |
| 93 | # if no database is provided in the uri, use default |
| 94 | self.database_name = uri_data['database'] |
| 95 | |
| 96 | self.options.update(uri_data['options']) |
| 97 | |
| 98 | # update conf with specific settings |
| 99 | config = self.app.conf.get('mongodb_backend_settings') |
| 100 | if config is not None: |
| 101 | if not isinstance(config, dict): |
| 102 | raise ImproperlyConfigured( |
| 103 | 'MongoDB backend settings should be grouped in a dict') |
| 104 | config = dict(config) # don't modify original |
| 105 | |
| 106 | if 'host' in config or 'port' in config: |
| 107 | # these should take over uri conf |
| 108 | self.mongo_host = None |
| 109 | |
| 110 | self.host = config.pop('host', self.host) |
| 111 | self.port = config.pop('port', self.port) |
| 112 | self.mongo_host = config.pop('mongo_host', self.mongo_host) |
| 113 | self.user = config.pop('user', self.user) |
| 114 | self.password = config.pop('password', self.password) |
| 115 | self.database_name = config.pop('database', self.database_name) |
| 116 | self.taskmeta_collection = config.pop( |
| 117 | 'taskmeta_collection', self.taskmeta_collection, |
| 118 | ) |
| 119 | self.groupmeta_collection = config.pop( |
| 120 | 'groupmeta_collection', self.groupmeta_collection, |
| 121 | ) |
| 122 | |
| 123 | self.options.update(config.pop('options', {})) |
nothing calls this directly
no test coverage detected