VirtualMailManager/MailLocation.py
changeset 571 a4aead244f75
parent 465 c0e1fb1b0145
parent 570 28230a8230bf
child 572 3238c58d01ae
equal deleted inserted replaced
465:c0e1fb1b0145 571:a4aead244f75
     1 # -*- coding: UTF-8 -*-
       
     2 # Copyright (c) 2008 - 2010, Pascal Volk
       
     3 # See COPYING for distribution information.
       
     4 
       
     5 """Virtual Mail Manager's MailLocation class to manage the mail_location
       
     6 for accounts."""
       
     7 
       
     8 from __main__ import re, ERR
       
     9 from Exceptions import VMMMailLocationException as MLE
       
    10 
       
    11 RE_MAILLOCATION = """^\w{1,20}$"""
       
    12 
       
    13 class MailLocation(object):
       
    14     """A wrapper class thats provide access to the maillocation table"""
       
    15     __slots__ = ('__id', '__maillocation', '_dbh')
       
    16     def __init__(self, dbh, mid=None, maillocation=None):
       
    17         """Creates a new MailLocation instance.
       
    18 
       
    19         Either mid or maillocation must be specified.
       
    20 
       
    21         Keyword arguments:
       
    22         dbh -- a pyPgSQL.PgSQL.connection
       
    23         mid -- the id of a maillocation (long)
       
    24         maillocation -- the value of the maillocation (str)
       
    25         """
       
    26         self._dbh = dbh
       
    27         if mid is None and maillocation is None:
       
    28             raise MLE(_('Either mid or maillocation must be specified.'),
       
    29                 ERR.MAILLOCATION_INIT)
       
    30         elif mid is not None:
       
    31             try:
       
    32                 self.__id = long(mid)
       
    33             except ValueError:
       
    34                 raise MLE(_('mid must be an int/long.'), ERR.MAILLOCATION_INIT)
       
    35             self._loadByID()
       
    36         else:
       
    37             if re.match(RE_MAILLOCATION, maillocation):
       
    38                 self.__maillocation = maillocation
       
    39                 self._loadByName()
       
    40             else:
       
    41                 raise MLE(
       
    42                     _(u'Invalid folder name ā€œ%sā€, it may consist only of\n\
       
    43 1 - 20 single byte characters (A-Z, a-z, 0-9 and _).') % maillocation,
       
    44                         ERR.MAILLOCATION_INIT)
       
    45 
       
    46     def _loadByID(self):
       
    47         dbc = self._dbh.cursor()
       
    48         dbc.execute('SELECT maillocation FROM maillocation WHERE mid = %s',
       
    49                 self.__id)
       
    50         result = dbc.fetchone()
       
    51         dbc.close()
       
    52         if result is not None:
       
    53             self.__maillocation = result[0]
       
    54         else:
       
    55             raise MLE(_('Unknown mid specified.'), ERR.UNKNOWN_MAILLOCATION_ID)
       
    56 
       
    57     def _loadByName(self):
       
    58         dbc = self._dbh.cursor()
       
    59         dbc.execute('SELECT mid FROM maillocation WHERE maillocation = %s',
       
    60                 self.__maillocation)
       
    61         result = dbc.fetchone()
       
    62         dbc.close()
       
    63         if result is not None:
       
    64             self.__id = result[0]
       
    65         else:
       
    66             self._save()
       
    67 
       
    68     def _save(self):
       
    69         dbc = self._dbh.cursor()
       
    70         dbc.execute("SELECT nextval('maillocation_id')")
       
    71         self.__id = dbc.fetchone()[0]
       
    72         dbc.execute('INSERT INTO maillocation(mid,maillocation) VALUES(%s,%s)',
       
    73                 self.__id, self.__maillocation)
       
    74         self._dbh.commit()
       
    75         dbc.close()
       
    76 
       
    77     def getID(self):
       
    78         """Returns the unique ID of the maillocation."""
       
    79         return self.__id
       
    80 
       
    81     def getMailLocation(self):
       
    82         """Returns the value of maillocation, ex: 'Maildir'"""
       
    83         return self.__maillocation
       
    84