--- a/VirtualMailManager/account.py Fri Apr 13 23:24:12 2012 +0200
+++ b/VirtualMailManager/account.py Sat Apr 14 15:11:27 2012 +0000
@@ -8,8 +8,7 @@
Virtual Mail Manager's Account class to manage e-mail accounts.
"""
-from VirtualMailManager.common import version_str, \
- format_domain_default
+from VirtualMailManager.common import version_str
from VirtualMailManager.constants import \
ACCOUNT_EXISTS, ACCOUNT_MISSING_PASSWORD, ALIAS_PRESENT, \
INVALID_ARGUMENT, INVALID_MAIL_LOCATION, NO_SUCH_ACCOUNT, \
@@ -60,9 +59,9 @@
self._addr.domainname, NO_SUCH_DOMAIN)
self._uid = 0
self._mail = None
- self._qlimit = None
- self._services = None
- self._transport = None
+ self._qlimit = self._domain.quotalimit
+ self._services = self._domain.serviceset
+ self._transport = self._domain.transport
self._passwd = None
self._new = True
self._load()
@@ -82,19 +81,12 @@
dbc.close()
if result:
self._uid, _mid, _qid, _ssid, _tid = result
-
- def load_helper(ctor, own, field, dbresult):
- cur = None if own is None else getattr(own, field)
- if cur != dbresult:
- kwargs = { field : dbresult }
- return None if dbresult is None \
- else ctor(self._dbh, **kwargs)
-
- self._qlimit = load_helper(QuotaLimit, self._qlimit, 'qid', _qid)
- self._services = load_helper(ServiceSet, self._services, 'ssid',
- _ssid)
- self._transport = load_helper(Transport, self._transport, 'tid',
- _tid)
+ if _qid != self._qlimit.qid:
+ self._qlimit = QuotaLimit(self._dbh, qid=_qid)
+ if _ssid != self._services.ssid:
+ self._services = ServiceSet(self._dbh, ssid=_ssid)
+ if _tid != self._transport.tid:
+ self._transport = Transport(self._dbh, tid=_tid)
self._mail = MailLocation(self._dbh, mid=_mid)
self._new = False
@@ -231,10 +223,8 @@
'qid, ssid, tid) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)',
(self._addr.localpart,
pwhash(self._passwd, user=self._addr), self._uid,
- self._domain.gid, self._mail.mid,
- self._qlimit.qid if self._qlimit else None,
- self._services.ssid if self._services else None,
- self._transport.tid if self._transport else None))
+ self._domain.gid, self._mail.mid, self._qlimit.qid,
+ self._services.ssid, self._transport.tid))
self._dbh.commit()
dbc.close()
self._new = False
@@ -277,13 +267,11 @@
raise VMMError(_(u'PostgreSQL-based dictionary quota requires '
u'Dovecot >= v1.1.2.'), VMM_ERROR)
self._chk_state()
+ assert isinstance(quotalimit, QuotaLimit)
if quotalimit == self._qlimit:
return
+ self._update_tables('qid', quotalimit.qid)
self._qlimit = quotalimit
- if quotalimit is not None:
- assert isinstance(quotalimit, QuotaLimit)
- quotalimit = quotalimit.qid
- self._update_tables('qid', quotalimit)
def update_serviceset(self, serviceset):
"""Assign a different set of services to the Account.
@@ -294,13 +282,11 @@
the new service set.
"""
self._chk_state()
+ assert isinstance(serviceset, ServiceSet)
if serviceset == self._services:
return
+ self._update_tables('ssid', serviceset.ssid)
self._services = serviceset
- if serviceset is not None:
- assert isinstance(serviceset, ServiceSet)
- serviceset = serviceset.ssid
- self._update_tables('ssid', serviceset)
def update_transport(self, transport):
"""Sets a new transport for the Account.
@@ -311,47 +297,24 @@
the new transport
"""
self._chk_state()
+ assert isinstance(transport, Transport)
if transport == self._transport:
return
+ if transport.transport.lower() in ('virtual', 'virtual:') and \
+ not self._mail.postfix:
+ raise AErr(_(u"Invalid transport '%(transport)s' for mailbox "
+ u"format '%(mbfmt)s'.") %
+ {'transport': transport, 'mbfmt': self._mail.mbformat},
+ INVALID_MAIL_LOCATION)
+ self._update_tables('tid', transport.tid)
self._transport = transport
- if transport is not None:
- assert isinstance(transport, Transport)
- if transport.transport.lower() in ('virtual', 'virtual:') and \
- not self._mail.postfix:
- raise AErr(_(u"Invalid transport '%(transport)s' for mailbox "
- u"format '%(mbfmt)s'.") %
- {'transport': transport, 'mbfmt': self._mail.mbformat},
- INVALID_MAIL_LOCATION)
- transport = transport.tid
- self._update_tables('tid', transport)
-
- def _get_info_transport(self):
- if self._transport:
- return self._transport.transport
- return format_domain_default(self._domain.transport.transport)
-
- def _get_info_serviceset(self):
- if self._services:
- services = self._services.services
- fmt = lambda s: s
- else:
- services = self._domain.serviceset.services
- fmt = format_domain_default
-
- ret = {}
- for service, state in services.iteritems():
- # TP: A service (e.g. pop3 or imap) may be enabled/usable or
- # disabled/unusable for a user.
- ret[service] = fmt((_('disabled'), _('enabled'))[state])
- return ret
def get_info(self):
"""Returns a dict with some information about the Account.
The keys of the dict are: 'address', 'gid', 'home', 'imap'
'mail_location', 'name', 'pop3', 'sieve', 'smtp', transport', 'uid',
- 'uq_bytes', 'uq_messages', 'ql_bytes', 'ql_messages', and
- 'ql_domaindefault'.
+ 'uq_bytes', 'uq_messages', 'ql_bytes', and 'ql_messages'.
"""
self._chk_state()
dbc = self._dbh.cursor()
@@ -363,20 +326,17 @@
dbc.close()
if info:
info = dict(zip(('name', 'uq_bytes', 'uq_messages'), info))
- info.update(self._get_info_serviceset())
+ for service, state in self._services.services.iteritems():
+ # TP: A service (e.g. pop3 or imap) may be enabled/usable or
+ # disabled/unusable for a user.
+ info[service] = (_('disabled'), _('enabled'))[state]
info['address'] = self._addr
info['gid'] = self._domain.gid
info['home'] = '%s/%s' % (self._domain.directory, self._uid)
info['mail_location'] = self._mail.mail_location
- if self._qlimit:
- info['ql_bytes'] = self._qlimit.bytes
- info['ql_messages'] = self._qlimit.messages
- info['ql_domaindefault'] = False
- else:
- info['ql_bytes'] = self._domain.quotalimit.bytes
- info['ql_messages'] = self._domain.quotalimit.messages
- info['ql_domaindefault'] = True
- info['transport'] = self._get_info_transport()
+ info['ql_bytes'] = self._qlimit.bytes
+ info['ql_messages'] = self._qlimit.messages
+ info['transport'] = self._transport.transport
info['uid'] = self._uid
return info
# nearly impossibleā½
--- a/VirtualMailManager/cli/subcommands.py Fri Apr 13 23:24:12 2012 +0200
+++ b/VirtualMailManager/cli/subcommands.py Sat Apr 14 15:11:27 2012 +0000
@@ -16,8 +16,7 @@
from VirtualMailManager import ENCODING
from VirtualMailManager.cli import get_winsize, prog, w_err, w_std
-from VirtualMailManager.common import human_size, size_in_bytes, \
- version_str, format_domain_default
+from VirtualMailManager.common import human_size, size_in_bytes, version_str
from VirtualMailManager.constants import __copyright__, __date__, \
__version__, ACCOUNT_EXISTS, ALIAS_EXISTS, ALIASDOMAIN_ISDOMAIN, \
DOMAIN_ALIAS_EXISTS, INVALID_ARGUMENT, EX_MISSING_ARGS, RELOCATED_EXISTS
@@ -573,17 +572,15 @@
else:
if details in (None, 'du'):
info['quota storage'] = _format_quota_usage(info['ql_bytes'],
- info['uq_bytes'], True, info['ql_domaindefault'])
+ info['uq_bytes'], True)
info['quota messages'] = _format_quota_usage(info['ql_messages'],
- info['uq_messages'], domaindefault=info['ql_domaindefault'])
+ info['uq_messages'])
_print_info(ctx, info, _(u'Account'))
else:
info[0]['quota storage'] = _format_quota_usage(info[0]['ql_bytes'],
- info[0]['uq_bytes'], True, info[0]['ql_domaindefault'])
- info[0]['quota messages'] = \
- _format_quota_usage(info[0]['ql_messages'],
- info[0]['uq_messages'],
- domaindefault=info[0]['ql_domaindefault'])
+ info[0]['uq_bytes'], True)
+ info[0]['quota messages'] = _format_quota_usage(
+ info[0]['ql_messages'], info[0]['uq_messages'])
_print_info(ctx, info[0], _(u'Account'))
_print_list(info[1], _(u'alias addresses'))
@@ -833,7 +830,7 @@
return order
-def _format_quota_usage(limit, used, human=False, domaindefault=False):
+def _format_quota_usage(limit, used, human=False):
"""Put quota's limit / usage / percentage in a formatted string."""
if human:
q_usage = {
@@ -849,8 +846,7 @@
q_usage['percent'] = locale.format('%6.2f', 100. / limit * used, True)
else:
q_usage['percent'] = locale.format('%6.2f', 0, True)
- fmt = format_domain_default if domaindefault else lambda s: s
- return fmt(_(u'[%(percent)s%%] %(used)s/%(limit)s') % q_usage)
+ return _(u'[%(percent)s%%] %(used)s/%(limit)s') % q_usage
def _print_info(ctx, info, title):
--- a/VirtualMailManager/common.py Fri Apr 13 23:24:12 2012 +0200
+++ b/VirtualMailManager/common.py Sat Apr 14 15:11:27 2012 +0000
@@ -186,7 +186,4 @@
_version_cache[version] = version_string
return version_string
-def format_domain_default(domaindata):
- return _(u'%s [domain default]') % domaindata
-
del _
--- a/pgsql/create_tables-dovecot-1.2.x.pgsql Fri Apr 13 23:24:12 2012 +0200
+++ b/pgsql/create_tables-dovecot-1.2.x.pgsql Sat Apr 14 15:11:27 2012 +0000
@@ -129,9 +129,9 @@
uid bigint NOT NULL DEFAULT nextval('users_uid'),
gid bigint NOT NULL,
mid bigint NOT NULL DEFAULT 1,
- qid bigint NULL DEFAULT NULL,
- ssid bigint NULL DEFAULT NULL,
- tid bigint NULL DEFAULT NULL,
+ qid bigint NOT NULL DEFAULT 1,
+ ssid bigint NOT NULL DEFAULT 1,
+ tid bigint NOT NULL DEFAULT 1,
CONSTRAINT pkey_users PRIMARY KEY (local_part, gid),
CONSTRAINT ukey_users_uid UNIQUE (uid),
CONSTRAINT fkey_users_gid_domain_data FOREIGN KEY (gid)
@@ -369,19 +369,12 @@
BEGIN
FOR record IN
SELECT userid, passwd, smtp, pop3, imap, sieve
- FROM users, service_set, domain_data
- WHERE users.gid = (SELECT gid
- FROM domain_name
- WHERE domainname = the_domain)
+ FROM users, service_set
+ WHERE gid = (SELECT gid
+ FROM domain_name
+ WHERE domainname = the_domain)
AND local_part = localpart
- AND users.gid = domain_data.gid
- AND CASE WHEN
- users.ssid IS NOT NULL
- THEN
- service_set.ssid = users.ssid
- ELSE
- service_set.ssid = domain_data.ssid
- END
+ AND service_set.ssid = users.ssid
LOOP
RETURN NEXT record;
END LOOP;
@@ -418,13 +411,7 @@
AND maillocation.mid = users.mid
AND mailboxformat.fid = maillocation.fid
AND domain_data.gid = did
- AND CASE WHEN
- users.qid IS NOT NULL
- THEN
- quotalimit.qid = users.qid
- ELSE
- quotalimit.qid = domain_data.qid
- END
+ AND quotalimit.qid = users.qid
LOOP
RETURN NEXT record;
END LOOP;
@@ -568,20 +555,16 @@
DECLARE
record recipient_transport;
recipient varchar(320) := localpart || '@' || the_domain;
- did bigint := (SELECT gid FROM domain_name WHERE domainname = the_domain);
- transport_id bigint := (SELECT tid FROM users
- WHERE gid = did AND local_part = localpart);
BEGIN
- IF transport_id IS NULL THEN
- SELECT tid INTO STRICT transport_id
- FROM domain_data
- WHERE gid = did;
- END IF;
-
FOR record IN
SELECT recipient, transport
FROM transport
- WHERE tid = transport_id
+ WHERE tid = (SELECT tid
+ FROM users
+ WHERE gid = (SELECT gid
+ FROM domain_name
+ WHERE domainname = the_domain)
+ AND local_part = localpart)
LOOP
RETURN NEXT record;
END LOOP;
--- a/pgsql/create_tables.pgsql Fri Apr 13 23:24:12 2012 +0200
+++ b/pgsql/create_tables.pgsql Sat Apr 14 15:11:27 2012 +0000
@@ -129,9 +129,9 @@
uid bigint NOT NULL DEFAULT nextval('users_uid'),
gid bigint NOT NULL,
mid bigint NOT NULL DEFAULT 1,
- qid bigint NULL DEFAULT NULL,
- ssid bigint NULL DEFAULT NULL,
- tid bigint NULL DEFAULT NULL,
+ qid bigint NOT NULL DEFAULT 1,
+ ssid bigint NOT NULL DEFAULT 1,
+ tid bigint NOT NULL DEFAULT 1,
CONSTRAINT pkey_users PRIMARY KEY (local_part, gid),
CONSTRAINT ukey_users_uid UNIQUE (uid),
CONSTRAINT fkey_users_gid_domain_data FOREIGN KEY (gid)
@@ -346,20 +346,12 @@
BEGIN
FOR record IN
SELECT userid, passwd, smtp, pop3, imap, managesieve
- FROM users, service_set, domain_data
- WHERE users.gid = (SELECT gid
- FROM domain_name
- WHERE domainname = the_domain)
+ FROM users, service_set
+ WHERE gid = (SELECT gid
+ FROM domain_name
+ WHERE domainname = the_domain)
AND local_part = localpart
AND service_set.ssid = users.ssid
- AND users.gid = domain_data.gid
- AND CASE WHEN
- users.ssid IS NOT NULL
- THEN
- service_set.ssid = users.ssid
- ELSE
- service_set.ssid = domain_data.ssid
- END
LOOP
RETURN NEXT record;
END LOOP;
@@ -396,13 +388,7 @@
AND maillocation.mid = users.mid
AND mailboxformat.fid = maillocation.fid
AND domain_data.gid = did
- AND CASE WHEN
- users.qid IS NOT NULL
- THEN
- quotalimit.qid = users.qid
- ELSE
- quotalimit.qid = domain_data.qid
- END
+ AND quotalimit.qid = users.qid
LOOP
RETURN NEXT record;
END LOOP;
@@ -546,20 +532,16 @@
DECLARE
record recipient_transport;
recipient varchar(320) := localpart || '@' || the_domain;
- did bigint := (SELECT gid FROM domain_name WHERE domainname = the_domain);
- transport_id bigint := (SELECT tid FROM users
- WHERE gid = did AND local_part = localpart);
BEGIN
- IF transport_id IS NULL THEN
- SELECT tid INTO STRICT transport_id
- FROM domain_data
- WHERE gid = did;
- END IF;
-
FOR record IN
SELECT recipient, transport
FROM transport
- WHERE tid = transport_id
+ WHERE tid = (SELECT tid
+ FROM users
+ WHERE gid = (SELECT gid
+ FROM domain_name
+ WHERE domainname = the_domain)
+ AND local_part = localpart)
LOOP
RETURN NEXT record;
END LOOP;
--- a/pgsql/update_tables_0.5.x-0.6-dovecot-1.2.x.pgsql Fri Apr 13 23:24:12 2012 +0200
+++ b/pgsql/update_tables_0.5.x-0.6-dovecot-1.2.x.pgsql Sat Apr 14 15:11:27 2012 +0000
@@ -91,7 +91,7 @@
ALTER TABLE domain_data ADD CONSTRAINT fkey_domain_data_qid_quotalimit
FOREIGN KEY (qid) REFERENCES quotalimit (qid);
-ALTER TABLE users ADD COLUMN qid bigint NULL DEFAULT NULL;
+ALTER TABLE users ADD COLUMN qid bigint NOT NULL DEFAULT 1;
ALTER TABLE users ADD CONSTRAINT fkey_users_qid_quotalimit
FOREIGN KEY (qid) REFERENCES quotalimit (qid);
@@ -147,7 +147,7 @@
ALTER TABLE domain_data ADD CONSTRAINT fkey_domain_data_ssid_service_set
FOREIGN KEY (ssid) REFERENCES service_set (ssid);
-ALTER TABLE users ADD COLUMN ssid bigint NULL DEFAULT NULL;
+ALTER TABLE users ADD COLUMN ssid bigint NOT NULL DEFAULT 1;
-- save current service sets
UPDATE users u
SET ssid = ss.ssid
@@ -177,24 +177,6 @@
);
-- ---
--- Quota/Service/Transport inheritance
--- ---
-ALTER TABLE users ALTER COLUMN tid DROP NULL;
-ALTER TABLE users ALTER COLUMN tid SET DEFAULT NULL;
--- The qid and ssid columns have already been defined accordingly above.
--- The rest of the logic will take place in the functions.
-
--- While qid and ssid are new and it's perfectly okay for existing users to
--- get NULL values (i.e. inherit from the domain's default), tid existed in
--- vmm 0.5.x. A sensible way forward seems thus to NULL all user records' tid
--- fields where the tid duplicates the value stored in the domain's record.
-UPDATE users
- SET tid = NULL
- WHERE tid = (SELECT tid
- FROM domain_data
- WHERE domain_data.gid = users.gid);
-
--- ---
-- Restore view
-- ---
CREATE VIEW vmm_domain_info AS
@@ -317,19 +299,12 @@
BEGIN
FOR record IN
SELECT userid, passwd, smtp, pop3, imap, sieve
- FROM users, service_set, domain_data
- WHERE users.gid = (SELECT gid
- FROM domain_name
- WHERE domainname = the_domain)
+ FROM users, service_set
+ WHERE gid = (SELECT gid
+ FROM domain_name
+ WHERE domainname = the_domain)
AND local_part = localpart
- AND users.gid = domain_data.gid
- AND CASE WHEN
- users.ssid IS NOT NULL
- THEN
- service_set.ssid = users.ssid
- ELSE
- service_set.ssid = domain_data.ssid
- END
+ AND service_set.ssid = users.ssid
LOOP
RETURN NEXT record;
END LOOP;
@@ -360,13 +335,7 @@
AND maillocation.mid = users.mid
AND mailboxformat.fid = maillocation.fid
AND domain_data.gid = did
- AND CASE WHEN
- users.qid IS NOT NULL
- THEN
- quotalimit.qid = users.qid
- ELSE
- quotalimit.qid = domain_data.qid
- END
+ AND quotalimit.qid = users.qid
LOOP
RETURN NEXT record;
END LOOP;
@@ -488,20 +457,16 @@
DECLARE
record recipient_transport;
recipient varchar(320) := localpart || '@' || the_domain;
- did bigint := (SELECT gid FROM domain_name WHERE domainname = the_domain);
- transport_id bigint := (SELECT tid FROM users
- WHERE gid = did AND local_part = localpart);
BEGIN
- IF transport_id IS NULL THEN
- SELECT tid INTO STRICT transport_id
- FROM domain_data
- WHERE gid = did;
- END IF;
-
FOR record IN
SELECT recipient, transport
FROM transport
- WHERE tid = transport_id
+ WHERE tid = (SELECT tid
+ FROM users
+ WHERE gid = (SELECT gid
+ FROM domain_name
+ WHERE domainname = the_domain)
+ AND local_part = localpart)
LOOP
RETURN NEXT record;
END LOOP;
--- a/pgsql/update_tables_0.5.x-0.6.pgsql Fri Apr 13 23:24:12 2012 +0200
+++ b/pgsql/update_tables_0.5.x-0.6.pgsql Sat Apr 14 15:11:27 2012 +0000
@@ -91,7 +91,7 @@
ALTER TABLE domain_data ADD CONSTRAINT fkey_domain_data_qid_quotalimit
FOREIGN KEY (qid) REFERENCES quotalimit (qid);
-ALTER TABLE users ADD COLUMN qid bigint NULL DEFAULT NULL;
+ALTER TABLE users ADD COLUMN qid bigint NOT NULL DEFAULT 1;
ALTER TABLE users ADD CONSTRAINT fkey_users_qid_quotalimit
FOREIGN KEY (qid) REFERENCES quotalimit (qid);
@@ -125,7 +125,7 @@
ALTER TABLE domain_data ADD CONSTRAINT fkey_domain_data_ssid_service_set
FOREIGN KEY (ssid) REFERENCES service_set (ssid);
-ALTER TABLE users ADD COLUMN ssid bigint NULL DEFAULT NULL;
+ALTER TABLE users ADD COLUMN ssid bigint NOT NULL DEFAULT 1;
-- save current service sets
UPDATE users u
SET ssid = ss.ssid
@@ -145,24 +145,6 @@
-- ---
-- Catchall
-- ---
--- Quota/Service/Transport inheritance
--- ---
-ALTER TABLE users ALTER COLUMN tid DROP NULL;
-ALTER TABLE users ALTER COLUMN tid SET DEFAULT NULL;
--- The qid and ssid columns have already been defined accordingly above.
--- The rest of the logic will take place in the functions.
-
--- While qid and ssid are new and it's perfectly okay for existing users to
--- get NULL values (i.e. inherit from the domain's default), tid existed in
--- vmm 0.5.x. A sensible way forward seems thus to NULL all user records' tid
--- fields where the tid duplicates the value stored in the domain's record.
-UPDATE users
- SET tid = NULL
- WHERE tid = (SELECT tid
- FROM domain_data
- WHERE domain_data.gid = users.gid);
-
--- ---
CREATE TABLE catchall (
gid bigint NOT NULL,
@@ -295,20 +277,12 @@
BEGIN
FOR record IN
SELECT userid, passwd, smtp, pop3, imap, managesieve
- FROM users, service_set, domain_data
- WHERE users.gid = (SELECT gid
- FROM domain_name
- WHERE domainname = the_domain)
+ FROM users, service_set
+ WHERE gid = (SELECT gid
+ FROM domain_name
+ WHERE domainname = the_domain)
AND local_part = localpart
AND service_set.ssid = users.ssid
- AND users.gid = domain_data.gid
- AND CASE WHEN
- users.ssid IS NOT NULL
- THEN
- service_set.ssid = users.ssid
- ELSE
- service_set.ssid = domain_data.ssid
- END
LOOP
RETURN NEXT record;
END LOOP;
@@ -339,13 +313,7 @@
AND maillocation.mid = users.mid
AND mailboxformat.fid = maillocation.fid
AND domain_data.gid = did
- AND CASE WHEN
- users.qid IS NOT NULL
- THEN
- quotalimit.qid = users.qid
- ELSE
- quotalimit.qid = domain_data.qid
- END
+ AND quotalimit.qid = users.qid
LOOP
RETURN NEXT record;
END LOOP;
@@ -467,20 +435,16 @@
DECLARE
record recipient_transport;
recipient varchar(320) := localpart || '@' || the_domain;
- did bigint := (SELECT gid FROM domain_name WHERE domainname = the_domain);
- transport_id bigint := (SELECT tid FROM users
- WHERE gid = did AND local_part = localpart);
BEGIN
- IF transport_id IS NULL THEN
- SELECT tid INTO STRICT transport_id
- FROM domain_data
- WHERE gid = did;
- END IF;
-
FOR record IN
SELECT recipient, transport
FROM transport
- WHERE tid = transport_id
+ WHERE tid = (SELECT tid
+ FROM users
+ WHERE gid = (SELECT gid
+ FROM domain_name
+ WHERE domainname = the_domain)
+ AND local_part = localpart)
LOOP
RETURN NEXT record;
END LOOP;