VirtualMailManager/MailLocation.py
changeset 8 7e3ce56f49e6
child 9 e3d3dbeb5b84
equal deleted inserted replaced
7:96761c442dcf 8:7e3ce56f49e6
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: UTF-8 -*-
       
     3 # Copyright 2008 VEB IT
       
     4 # See COPYING for distribution information.
       
     5 # $Id$
       
     6 
       
     7 """Virtual Mail Manager's MailLocation class to manage the mail_location
       
     8 for accounts."""
       
     9 
       
    10 from constants.VERSION import VERSION
       
    11 
       
    12 __author__ = 'Pascal Volk <p.volk@veb-it.de>'
       
    13 __version__ = VERSION
       
    14 __revision__ = 'rev '+'$Rev$'.split()[1]
       
    15 __date__ = '$Date$'.split()[1]
       
    16 
       
    17 
       
    18 from Exceptions import VMMMailLocationException as MLE
       
    19 import constants.ERROR as ERR
       
    20 
       
    21 class MailLocation:
       
    22     """A wrapper class thats provide access to the maillocation table"""
       
    23     def __init__(self, dbh, mid=None, maillocation=None):
       
    24         """Creates a new MailLocation instance.
       
    25 
       
    26         Either mid or maillocation must be specified.
       
    27         
       
    28         Keyword arguments:
       
    29         dbh -- a pyPgSQL.PgSQL.connection
       
    30         mid -- the id of a maillocation (long)
       
    31         maillocation -- the value of the maillocation (str)
       
    32         """
       
    33         self._dbh = dbh
       
    34         if mid is None and maillocation is None:
       
    35             raise MLE(('Either mid or maillocation must be specified.',
       
    36                 ERR.MAILLOCATION_INIT))
       
    37         elif mid is not None:
       
    38             try:
       
    39                 self.__id = long(tid)
       
    40             except ValueError:
       
    41                 raise MLE(('mid must be an int/long.', ERR.MAILLOCATION_INIT))
       
    42             self._loadByID()
       
    43         else:
       
    44             self.__maillocation = maillocation
       
    45             self._loadByName()
       
    46 
       
    47     def _loadByID(self):
       
    48         dbc = self._dbh.cursor()
       
    49         dbc.execute('SELECT maillocation FROM maillocation WHERE mid = %s',
       
    50                 self.__id)
       
    51         result = dbc.fetchone()
       
    52         dbc.close()
       
    53         if result is not None:
       
    54             self.__maillocation = result[0]
       
    55         else:
       
    56             raise MLE(('Unknown mid specified.', ERR.UNKNOWN_MAILLOCATION_ID))
       
    57 
       
    58     def _loadByName(self):
       
    59         dbc = self._dbh.cursor()
       
    60         dbc.execute('SELECT mid FROM maillocation WHERE maillocation = %s',
       
    61                 self.__maillocation)
       
    62         result = dbc.fetchone()
       
    63         dbc.close()
       
    64         if result is not None:
       
    65             self.__id = result[0]
       
    66         else:
       
    67             self._save()
       
    68 
       
    69     def _save(self):
       
    70         dbc = self._dbh.cursor()
       
    71         dbc.execute("SELECT nextval('maillocation_id')")
       
    72         self.__id = dbc.fetchone()[0]
       
    73         dbc.execute('INSERT INTO maillocation(mid,maillocation) VALUES(%s,%s)',
       
    74                 self.__id, self.__maillocation)
       
    75         self._dbh.commit()
       
    76         dbc.close()
       
    77 
       
    78     def getID(self):
       
    79         """Returns the unique ID of the maillocation."""
       
    80         return self.__id
       
    81 
       
    82     def getMailLocation(self):
       
    83         """Returns the value of maillocation, ex: 'Maildir'"""
       
    84         return self.__maillocation
       
    85