VirtualMailManager/Domain.py
changeset 48 0d5f58f8b8f5
parent 47 191d5a5adc4a
child 50 927b0705d31a
equal deleted inserted replaced
47:191d5a5adc4a 48:0d5f58f8b8f5
    68         result = dbc.fetchone()
    68         result = dbc.fetchone()
    69         dbc.close()
    69         dbc.close()
    70         if result is None:
    70         if result is None:
    71             return False
    71             return False
    72         elif result[1]:
    72         elif result[1]:
    73             raise VMMDomainException((_('Domain already exists.'),
    73             raise VMMDomainException(_('Domain already exists.'),
    74                 ERR.DOMAIN_EXISTS))
    74                 ERR.DOMAIN_EXISTS)
    75         else:
    75         else:
    76             raise VMMDomainException((_('Domain alias already exists.'),
    76             raise VMMDomainException(_('Domain alias already exists.'),
    77                 ERR.DOMAIN_ALIAS_EXISTS))
    77                 ERR.DOMAIN_ALIAS_EXISTS)
    78 
    78 
    79     def _setID(self):
    79     def _setID(self):
    80         """Sets the ID of the domain."""
    80         """Sets the ID of the domain."""
    81         dbc = self._dbh.cursor()
    81         dbc = self._dbh.cursor()
    82         dbc.execute("SELECT nextval('domain_gid')")
    82         dbc.execute("SELECT nextval('domain_gid')")
   125         if not delAlias:
   125         if not delAlias:
   126             hasAlias = self._has('alias')
   126             hasAlias = self._has('alias')
   127         else:
   127         else:
   128             hasAlias = False
   128             hasAlias = False
   129         if hasUser and hasAlias:
   129         if hasUser and hasAlias:
   130             raise VMMDomainException((_('There are accounts and aliases.'),
   130             raise VMMDomainException(_('There are accounts and aliases.'),
   131                 ERR.ACCOUNT_AND_ALIAS_PRESENT))
   131                 ERR.ACCOUNT_AND_ALIAS_PRESENT)
   132         elif hasUser:
   132         elif hasUser:
   133             raise VMMDomainException((_('There are accounts.'),
   133             raise VMMDomainException(_('There are accounts.'),
   134                 ERR.ACCOUNT_PRESENT))
   134                 ERR.ACCOUNT_PRESENT)
   135         elif hasAlias:
   135         elif hasAlias:
   136             raise VMMDomainException((_('There are aliases.'),
   136             raise VMMDomainException(_('There are aliases.'),
   137                 ERR.ALIAS_PRESENT))
   137                 ERR.ALIAS_PRESENT)
   138 
   138 
   139     def save(self):
   139     def save(self):
   140         """Stores the new domain in the database."""
   140         """Stores the new domain in the database."""
   141         if self._id < 1:
   141         if self._id < 1:
   142             self._prepare()
   142             self._prepare()
   146             dbc.execute("INSERT INTO domain_name (domainname, gid, is_primary)\
   146             dbc.execute("INSERT INTO domain_name (domainname, gid, is_primary)\
   147  VALUES (%s, %s, %s)", self._name, self._id, True)
   147  VALUES (%s, %s, %s)", self._name, self._id, True)
   148             self._dbh.commit()
   148             self._dbh.commit()
   149             dbc.close()
   149             dbc.close()
   150         else:
   150         else:
   151             raise VMMDomainException((_('Domain already exists.'),
   151             raise VMMDomainException(_('Domain already exists.'),
   152                 ERR.DOMAIN_EXISTS))
   152                 ERR.DOMAIN_EXISTS)
   153 
   153 
   154     def delete(self, delUser=False, delAlias=False):
   154     def delete(self, delUser=False, delAlias=False):
   155         """Deletes the domain.
   155         """Deletes the domain.
   156 
   156 
   157         Keyword arguments:
   157         Keyword arguments:
   164             for t in ('alias','users','relocated','domain_name','domain_data'):
   164             for t in ('alias','users','relocated','domain_name','domain_data'):
   165                 dbc.execute("DELETE FROM %s WHERE gid = %d" % (t, self._id))
   165                 dbc.execute("DELETE FROM %s WHERE gid = %d" % (t, self._id))
   166             self._dbh.commit()
   166             self._dbh.commit()
   167             dbc.close()
   167             dbc.close()
   168         else:
   168         else:
   169             raise VMMDomainException((_("Domain doesn't exist yet."),
   169             raise VMMDomainException(_("Domain doesn't exist yet."),
   170                 ERR.NO_SUCH_DOMAIN))
   170                 ERR.NO_SUCH_DOMAIN)
   171 
   171 
   172     def updateTransport(self, transport, force = False):
   172     def updateTransport(self, transport, force = False):
   173         """Sets a new transport for the domain.
   173         """Sets a new transport for the domain.
   174 
   174 
   175         Keyword arguments:
   175         Keyword arguments:
   188                         trsp.getID(), self._id)
   188                         trsp.getID(), self._id)
   189                 if dbc.rowcount > 0:
   189                 if dbc.rowcount > 0:
   190                     self._dbh.commit()
   190                     self._dbh.commit()
   191             dbc.close()
   191             dbc.close()
   192         else:
   192         else:
   193             raise VMMDomainException((_("Domain doesn't exist yet."),
   193             raise VMMDomainException(_("Domain doesn't exist yet."),
   194                 ERR.NO_SUCH_DOMAIN))
   194                 ERR.NO_SUCH_DOMAIN)
   195 
   195 
   196     def saveAlias(self, aliasname):
   196     def saveAlias(self, aliasname):
   197         """Stores the alias name for the domain in the database.
   197         """Stores the alias name for the domain in the database.
   198 
   198 
   199         Keyword arguments:
   199         Keyword arguments:
   205                     aliasname, self._id, False)
   205                     aliasname, self._id, False)
   206             if dbc.rowcount == 1:
   206             if dbc.rowcount == 1:
   207                 self._dbh.commit()
   207                 self._dbh.commit()
   208             dbc.close()
   208             dbc.close()
   209         else:
   209         else:
   210             raise VMMDomainException((_("Domain doesn't exist yet."),
   210             raise VMMDomainException(_("Domain doesn't exist yet."),
   211                 ERR.NO_SUCH_DOMAIN))
   211                 ERR.NO_SUCH_DOMAIN)
   212 
   212 
   213     def getID(self):
   213     def getID(self):
   214         """Returns the ID of the domain."""
   214         """Returns the ID of the domain."""
   215         return self._id
   215         return self._id
   216 
   216 
   235         dbc = self._dbh.cursor()
   235         dbc = self._dbh.cursor()
   236         dbc.execute(sql)
   236         dbc.execute(sql)
   237         info = dbc.fetchone()
   237         info = dbc.fetchone()
   238         dbc.close()
   238         dbc.close()
   239         if info is None:
   239         if info is None:
   240             raise VMMDomainException((_("Domain doesn't exist yet."),
   240             raise VMMDomainException(_("Domain doesn't exist yet."),
   241                 ERR.NO_SUCH_DOMAIN))
   241                 ERR.NO_SUCH_DOMAIN)
   242         else:
   242         else:
   243             keys = ['gid', 'domainname', 'transport', 'domaindir',
   243             keys = ['gid', 'domainname', 'transport', 'domaindir',
   244                     'aliasdomains', 'accounts', 'aliases']
   244                     'aliasdomains', 'accounts', 'aliases']
   245             return dict(zip(keys, info))
   245             return dict(zip(keys, info))
   246 
   246