DomainModel.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. from modules.Mailcow import Mailcow
  2. from models.BaseModel import BaseModel
  3. class DomainModel(BaseModel):
  4. parser_command = "domain"
  5. required_args = {
  6. "add": [["domain"]],
  7. "delete": [["domain"]],
  8. "get": [["domain"]],
  9. "edit": [["domain"]]
  10. }
  11. def __init__(
  12. self,
  13. domain=None,
  14. active=None,
  15. aliases=None,
  16. backupmx=None,
  17. defquota=None,
  18. description=None,
  19. mailboxes=None,
  20. maxquota=None,
  21. quota=None,
  22. relay_all_recipients=None,
  23. rl_frame=None,
  24. rl_value=None,
  25. restart_sogo=None,
  26. tags=None,
  27. **kwargs
  28. ):
  29. self.mailcow = Mailcow()
  30. self.domain = domain
  31. self.active = active
  32. self.aliases = aliases
  33. self.backupmx = backupmx
  34. self.defquota = defquota
  35. self.description = description
  36. self.mailboxes = mailboxes
  37. self.maxquota = maxquota
  38. self.quota = quota
  39. self.relay_all_recipients = relay_all_recipients
  40. self.rl_frame = rl_frame
  41. self.rl_value = rl_value
  42. self.restart_sogo = restart_sogo
  43. self.tags = tags
  44. @classmethod
  45. def from_dict(cls, data):
  46. return cls(
  47. domain=data.get("domain"),
  48. active=data.get("active", None),
  49. aliases=data.get("aliases", None),
  50. backupmx=data.get("backupmx", None),
  51. defquota=data.get("defquota", None),
  52. description=data.get("description", None),
  53. mailboxes=data.get("mailboxes", None),
  54. maxquota=data.get("maxquota", None),
  55. quota=data.get("quota", None),
  56. relay_all_recipients=data.get("relay_all_recipients", None),
  57. rl_frame=data.get("rl_frame", None),
  58. rl_value=data.get("rl_value", None),
  59. restart_sogo=data.get("restart_sogo", None),
  60. tags=data.get("tags", None)
  61. )
  62. def getAdd(self):
  63. """
  64. Get the domain details as a dictionary for adding, sets default values.
  65. :return: Dictionary containing domain details.
  66. """
  67. domain = {
  68. "domain": self.domain,
  69. "active": self.active if self.active is not None else 1,
  70. "aliases": self.aliases if self.aliases is not None else 400,
  71. "backupmx": self.backupmx if self.backupmx is not None else 0,
  72. "defquota": self.defquota if self.defquota is not None else 3072,
  73. "description": self.description if self.description is not None else "",
  74. "mailboxes": self.mailboxes if self.mailboxes is not None else 10,
  75. "maxquota": self.maxquota if self.maxquota is not None else 10240,
  76. "quota": self.quota if self.quota is not None else 10240,
  77. "relay_all_recipients": self.relay_all_recipients if self.relay_all_recipients is not None else 0,
  78. "rl_frame": self.rl_frame,
  79. "rl_value": self.rl_value,
  80. "restart_sogo": self.restart_sogo if self.restart_sogo is not None else 0,
  81. "tags": self.tags if self.tags is not None else []
  82. }
  83. return {key: value for key, value in domain.items() if value is not None}
  84. def getEdit(self):
  85. """
  86. Get the domain details as a dictionary for editing, sets no default values.
  87. :return: Dictionary containing domain details.
  88. """
  89. domain = {
  90. "domain": self.domain,
  91. "active": self.active,
  92. "aliases": self.aliases,
  93. "backupmx": self.backupmx,
  94. "defquota": self.defquota,
  95. "description": self.description,
  96. "mailboxes": self.mailboxes,
  97. "maxquota": self.maxquota,
  98. "quota": self.quota,
  99. "relay_all_recipients": self.relay_all_recipients,
  100. "rl_frame": self.rl_frame,
  101. "rl_value": self.rl_value,
  102. "restart_sogo": self.restart_sogo,
  103. "tags": self.tags
  104. }
  105. return {key: value for key, value in domain.items() if value is not None}
  106. def get(self):
  107. """
  108. Get the domain details from the mailcow API.
  109. :return: Response from the mailcow API.
  110. """
  111. return self.mailcow.getDomain(self.domain)
  112. def delete(self):
  113. """
  114. Delete the domain from the mailcow API.
  115. :return: Response from the mailcow API.
  116. """
  117. return self.mailcow.deleteDomain(self.domain)
  118. def add(self):
  119. """
  120. Add the domain to the mailcow API.
  121. :return: Response from the mailcow API.
  122. """
  123. return self.mailcow.addDomain(self.getAdd())
  124. def edit(self):
  125. """
  126. Edit the domain in the mailcow API.
  127. :return: Response from the mailcow API.
  128. """
  129. return self.mailcow.editDomain(self.domain, self.getEdit())
  130. @classmethod
  131. def add_parser(cls, subparsers):
  132. parser = subparsers.add_parser(
  133. cls.parser_command,
  134. help="Manage domains (add, delete, get, edit)"
  135. )
  136. parser.add_argument("object", choices=list(cls.required_args.keys()), help="Action to perform: add, delete, get, edit")
  137. parser.add_argument("--domain", required=True, help="Domain name (e.g. domain.tld)")
  138. parser.add_argument("--active", choices=["1", "0"], help="Activate (1) or deactivate (0) the domain")
  139. parser.add_argument("--aliases", help="Number of aliases allowed for the domain")
  140. parser.add_argument("--backupmx", choices=["1", "0"], help="Enable (1) or disable (0) backup MX")
  141. parser.add_argument("--defquota", help="Default quota for mailboxes in MB")
  142. parser.add_argument("--description", help="Description of the domain")
  143. parser.add_argument("--mailboxes", help="Number of mailboxes allowed for the domain")
  144. parser.add_argument("--maxquota", help="Maximum quota for the domain in MB")
  145. parser.add_argument("--quota", help="Quota used by the domain in MB")
  146. parser.add_argument("--relay-all-recipients", choices=["1", "0"], help="Relay all recipients (1 = yes, 0 = no)")
  147. parser.add_argument("--rl-frame", help="Rate limit frame (e.g., s, m, h)")
  148. parser.add_argument("--rl-value", help="Rate limit value")
  149. parser.add_argument("--restart-sogo", help="Restart SOGo after changes (1 = yes, 0 = no)")
  150. parser.add_argument("--tags", nargs="*", help="Tags for the domain")