def set_quota(self, value=None, override_rules=False): """Set or update quota value for this mailbox. A value equal to 0 means the mailbox won't have any quota. The following cases allow people to define such behaviour: * The domain has no quota * :keyword:`override_rules` is True :param integer value: the quota's value :param bool override_rules: allow to override defined quota rules """ old_quota = self.quota if value is None: if self.use_domain_quota: self.quota = self.domain.default_mailbox_quota else: self.quota = 0 else: self.quota = value if self.quota == 0: if self.domain.quota and not override_rules: raise lib_exceptions.BadRequest(_("A quota is required")) elif self.domain.quota: quota_usage = self.domain.allocated_quota if old_quota: quota_usage -= old_quota if quota_usage + self.quota > self.domain.quota: raise lib_exceptions.BadRequest(_("Domain quota exceeded"))
def import_account_mailbox(sender, user, account, row, **kwargs): """Handle extra fields when an account is imported. Expected fields: email address; quota; [domain; ...] :param User user: user importing the account :param User account: account being imported :param list rom: list of fields (strings) """ account.email = row[0].strip().lower() if account.email: mailbox, domname = split_mailbox(account.email) domain = models.Domain.objects.filter(name=domname).first() if not domain: raise exceptions.BadRequest( _("Account import failed (%s): domain does not exist") % account.username) if not user.can_access(domain): raise exceptions.PermDeniedException core_signals.can_create_object.send(sender="import", context=user, klass=models.Mailbox) core_signals.can_create_object.send(sender="import", context=domain, object_type="mailboxes") account.save() qset = models.Mailbox.objects.filter(address=mailbox, domain=domain) if qset.exists(): raise exceptions.Conflict( _("Mailbox {} already exists").format(account.email)) if len(row) == 1: quota = None else: try: quota = int(row[1].strip()) except ValueError: raise exceptions.BadRequest( _("Account import failed (%s): wrong quota value") % account.username) use_domain_quota = True if not quota else False mb = models.Mailbox(address=mailbox, domain=domain, user=account, use_domain_quota=use_domain_quota) mb.set_quota(quota, override_rules=user.has_perm("admin.change_domain")) mb.save(creator=user) if account.role == "DomainAdmins": for domname in row[2:]: try: dom = models.Domain.objects.get(name=domname.strip()) except models.Domain.DoesNotExist: continue dom.add_admin(account)
def set_quota(self, value=None, override_rules=False): """Set or update quota's value for this mailbox. A value equal to 0 means the mailbox won't have any quota. The following cases allow people to define such behaviour: * The domain has no quota * :keyword:`override_rules` is True :param integer value: the quota's value :param bool override_rules: allow to override defined quota rules """ if value is None: if self.use_domain_quota: self.quota = self.domain.quota else: self.quota = 0 elif int(value) > self.domain.quota and not override_rules: raise lib_exceptions.BadRequest( _("Quota is greater than the allowed domain's limit (%dM)") % self.domain.quota) else: self.quota = value if not self.quota and self.domain.quota and not override_rules: raise lib_exceptions.BadRequest(_("A quota is required"))