45 'domainname = %s', (self._name,)) |
45 'domainname = %s', (self._name,)) |
46 result = dbc.fetchone() |
46 result = dbc.fetchone() |
47 dbc.close() |
47 dbc.close() |
48 if result: |
48 if result: |
49 if result[1]: |
49 if result[1]: |
50 raise ADErr(_(u"The domain '%s' is a primary domain.") % |
50 raise ADErr(_("The domain '%s' is a primary domain.") % |
51 self._name, ALIASDOMAIN_ISDOMAIN) |
51 self._name, ALIASDOMAIN_ISDOMAIN) |
52 self._gid = result[0] |
52 self._gid = result[0] |
53 |
53 |
54 def set_destination(self, dest_domain): |
54 def set_destination(self, dest_domain): |
55 """Set the destination of a new AliasDomain or updates the |
55 """Set the destination of a new AliasDomain or updates the |
64 self._domain = dest_domain |
64 self._domain = dest_domain |
65 |
65 |
66 def save(self): |
66 def save(self): |
67 """Stores information about the new AliasDomain in the database.""" |
67 """Stores information about the new AliasDomain in the database.""" |
68 if self._gid > 0: |
68 if self._gid > 0: |
69 raise ADErr(_(u"The alias domain '%s' already exists.") % |
69 raise ADErr(_("The alias domain '%s' already exists.") % |
70 self._name, ALIASDOMAIN_EXISTS) |
70 self._name, ALIASDOMAIN_EXISTS) |
71 if not self._domain: |
71 if not self._domain: |
72 raise ADErr(_(u'No destination domain set for the alias domain.'), |
72 raise ADErr(_('No destination domain set for the alias domain.'), |
73 ALIASDOMAIN_NO_DOMDEST) |
73 ALIASDOMAIN_NO_DOMDEST) |
74 if self._domain.gid < 1: |
74 if self._domain.gid < 1: |
75 raise ADErr(_(u"The target domain '%s' does not exist.") % |
75 raise ADErr(_("The target domain '%s' does not exist.") % |
76 self._domain.name, NO_SUCH_DOMAIN) |
76 self._domain.name, NO_SUCH_DOMAIN) |
77 dbc = self._dbh.cursor() |
77 dbc = self._dbh.cursor() |
78 dbc.execute('INSERT INTO domain_name (domainname, gid, is_primary) ' |
78 dbc.execute('INSERT INTO domain_name (domainname, gid, is_primary) ' |
79 'VALUES (%s, %s, FALSE)', (self._name, self._domain.gid)) |
79 'VALUES (%s, %s, FALSE)', (self._name, self._domain.gid)) |
80 self._dbh.commit() |
80 self._dbh.commit() |
83 |
83 |
84 def info(self): |
84 def info(self): |
85 """Returns a dict (keys: "alias" and "domain") with the names of the |
85 """Returns a dict (keys: "alias" and "domain") with the names of the |
86 AliasDomain and its primary domain.""" |
86 AliasDomain and its primary domain.""" |
87 if self._gid < 1: |
87 if self._gid < 1: |
88 raise ADErr(_(u"The alias domain '%s' does not exist.") % |
88 raise ADErr(_("The alias domain '%s' does not exist.") % |
89 self._name, NO_SUCH_ALIASDOMAIN) |
89 self._name, NO_SUCH_ALIASDOMAIN) |
90 dbc = self._dbh.cursor() |
90 dbc = self._dbh.cursor() |
91 dbc.execute('SELECT domainname FROM domain_name WHERE gid = %s AND ' |
91 dbc.execute('SELECT domainname FROM domain_name WHERE gid = %s AND ' |
92 'is_primary', (self._gid,)) |
92 'is_primary', (self._gid,)) |
93 domain = dbc.fetchone() |
93 domain = dbc.fetchone() |
94 dbc.close() |
94 dbc.close() |
95 if domain: |
95 if domain: |
96 return {'alias': self._name, 'domain': domain[0]} |
96 return {'alias': self._name, 'domain': domain[0]} |
97 else: # an almost unlikely case, isn't it? |
97 else: # an almost unlikely case, isn't it? |
98 raise ADErr(_(u'There is no primary domain for the alias domain ' |
98 raise ADErr(_('There is no primary domain for the alias domain ' |
99 u"'%s'.") % self._name, NO_SUCH_DOMAIN) |
99 "'%s'.") % self._name, NO_SUCH_DOMAIN) |
100 |
100 |
101 def switch(self): |
101 def switch(self): |
102 """Switch the destination of the AliasDomain to the new destination, |
102 """Switch the destination of the AliasDomain to the new destination, |
103 set with the method `set_destination()`. |
103 set with the method `set_destination()`. |
104 """ |
104 """ |
105 if not self._domain: |
105 if not self._domain: |
106 raise ADErr(_(u'No destination domain set for the alias domain.'), |
106 raise ADErr(_('No destination domain set for the alias domain.'), |
107 ALIASDOMAIN_NO_DOMDEST) |
107 ALIASDOMAIN_NO_DOMDEST) |
108 if self._domain.gid < 1: |
108 if self._domain.gid < 1: |
109 raise ADErr(_(u"The target domain '%s' does not exist.") % |
109 raise ADErr(_("The target domain '%s' does not exist.") % |
110 self._domain.name, NO_SUCH_DOMAIN) |
110 self._domain.name, NO_SUCH_DOMAIN) |
111 if self._gid < 1: |
111 if self._gid < 1: |
112 raise ADErr(_(u"The alias domain '%s' does not exist.") % |
112 raise ADErr(_("The alias domain '%s' does not exist.") % |
113 self._name, NO_SUCH_ALIASDOMAIN) |
113 self._name, NO_SUCH_ALIASDOMAIN) |
114 if self._gid == self._domain.gid: |
114 if self._gid == self._domain.gid: |
115 raise ADErr(_(u"The alias domain '%(alias)s' is already assigned " |
115 raise ADErr(_("The alias domain '%(alias)s' is already assigned " |
116 u"to the domain '%(domain)s'.") % |
116 "to the domain '%(domain)s'.") % |
117 {'alias': self._name, 'domain': self._domain.name}, |
117 {'alias': self._name, 'domain': self._domain.name}, |
118 ALIASDOMAIN_EXISTS) |
118 ALIASDOMAIN_EXISTS) |
119 dbc = self._dbh.cursor() |
119 dbc = self._dbh.cursor() |
120 dbc.execute('UPDATE domain_name SET gid = %s WHERE gid = %s AND ' |
120 dbc.execute('UPDATE domain_name SET gid = %s WHERE gid = %s AND ' |
121 'domainname = %s AND NOT is_primary', (self._domain.gid, |
121 'domainname = %s AND NOT is_primary', (self._domain.gid, |
128 """Delete the AliasDomain's record form the database. |
128 """Delete the AliasDomain's record form the database. |
129 |
129 |
130 Raises an AliasDomainError if the AliasDomain doesn't exist. |
130 Raises an AliasDomainError if the AliasDomain doesn't exist. |
131 """ |
131 """ |
132 if self._gid < 1: |
132 if self._gid < 1: |
133 raise ADErr(_(u"The alias domain '%s' does not exist.") % |
133 raise ADErr(_("The alias domain '%s' does not exist.") % |
134 self._name, NO_SUCH_ALIASDOMAIN) |
134 self._name, NO_SUCH_ALIASDOMAIN) |
135 dbc = self._dbh.cursor() |
135 dbc = self._dbh.cursor() |
136 dbc.execute('DELETE FROM domain_name WHERE domainname = %s AND NOT ' |
136 dbc.execute('DELETE FROM domain_name WHERE domainname = %s AND NOT ' |
137 'is_primary', (self._name,)) |
137 'is_primary', (self._name,)) |
138 if dbc.rowcount > 0: |
138 if dbc.rowcount > 0: |