Coverage for django_napse/core/models/bots/managers/bot_config.py: 100%
22 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 13:49 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 13:49 +0000
1from django.db import models
3from django_napse.utils.errors import BotConfigError
6class BotConfigManager(models.Manager):
7 def create(
8 self,
9 space,
10 immutable: bool = False,
11 settings=None,
12 ):
13 settings = settings or {}
14 for setting in self.model._meta.get_fields():
15 if setting.name.startswith("setting_"):
16 try:
17 settings[setting.name[8:]]
18 except KeyError as e:
19 error_msg = f"Missing setting: {setting.name[8:]}"
20 raise BotConfigError.MissingSettingError(error_msg) from e
21 if not immutable:
22 try:
23 self.model.objects.get(
24 space=space,
25 immutable=immutable,
26 **{f"setting_{setting_name}": setting_value for setting_name, setting_value in settings.items()},
27 )
28 except self.model.DoesNotExist:
29 pass
30 else:
31 error_msg = "This BotConfig already exists in this space."
32 raise BotConfigError.DuplicateBotConfig(error_msg)
33 config = self.model(
34 space=space,
35 immutable=immutable,
36 **{f"setting_{setting_name}": setting_value for setting_name, setting_value in settings.items()},
37 )
38 config.save()
39 return config