Coverage for django_napse/core/models/bots/config.py: 97%

36 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-12 13:49 +0000

1import uuid 

2 

3from django.db import models 

4 

5from django_napse.core.models.bots.managers.bot_config import BotConfigManager 

6from django_napse.core.models.bots.strategy import Strategy 

7from django_napse.utils.findable_class import FindableClass 

8 

9 

10class BotConfig(models.Model, FindableClass): 

11 uuid = models.UUIDField(unique=True, editable=False, default=uuid.uuid4) 

12 space = models.ForeignKey("NapseSpace", on_delete=models.CASCADE, related_name="bot_configs") 

13 immutable = models.BooleanField(default=False) 

14 

15 objects = BotConfigManager() 

16 

17 def __str__(self) -> str: # pragma: no cover 

18 return f"BOT CONFIG: {self.pk}" 

19 

20 def info(self, verbose=True, beacon=""): 

21 string = "" 

22 string += f"{beacon}BotConfig: ({self.pk=})\n" 

23 string += f"{beacon}Args:\n" 

24 string += f"{beacon}\t{self.space=}\n" 

25 string += f"{beacon}Settings:\n" 

26 for setting, value in self.settings.items(): 

27 string += f"{beacon}\t" + setting + f"={value}\n" 

28 if verbose: # pragma: no cover 

29 print(string) 

30 return string 

31 

32 @property 

33 def settings(self): 

34 settings = {} 

35 for setting in self._meta.get_fields(): 

36 if setting.name.startswith("setting_"): 

37 settings[setting.name[8:]] = getattr(self, setting.name) 

38 return settings 

39 

40 @property 

41 def strategy(self): 

42 return Strategy.objects.get(config=self).find() 

43 

44 def duplicate_immutable(self): 

45 return self.__class__.objects.create( 

46 space=self.space, 

47 immutable=True, 

48 settings=self.settings, 

49 ) 

50 

51 def duplicate_other_space(self, space): 

52 if space == self.space: 

53 error_msg = "The space of the new bot config must be different from the space of the original bot config." 

54 raise ValueError(error_msg) 

55 return self.__class__.objects.create( 

56 space=space, 

57 immutable=True, 

58 settings=self.settings, 

59 )