VirtualMailManager/MailLocation.py
author Pascal Volk <neverseen@users.sourceforge.net>
Tue, 02 Sep 2008 01:15:41 +0000
changeset 73 11da3d9298b4
parent 69 0c124160a991
child 76 14c0a092d7d2
permissions -rw-r--r--
'create_optional_types_and_functions.pgsql' - Added function postfix_virtual_alias_map() any type recipient_destination
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
#!/usr/bin/env python
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     2
# -*- coding: UTF-8 -*-
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# Copyright 2008 VEB IT
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
# See COPYING for distribution information.
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
# $Id$
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     6
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
"""Virtual Mail Manager's MailLocation class to manage the mail_location
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
for accounts."""
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
from constants.VERSION import VERSION
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
__author__ = 'Pascal Volk <p.volk@veb-it.de>'
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    13
__version__ = VERSION
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
__revision__ = 'rev '+'$Rev$'.split()[1]
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
__date__ = '$Date$'.split()[1]
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
69
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    17
import re
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    18
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    19
from Exceptions import VMMMailLocationException as MLE
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
import constants.ERROR as ERR
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
69
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    22
RE_MAILLOCATION = """^[\w]{1,20}$"""
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    23
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
class MailLocation:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
    """A wrapper class thats provide access to the maillocation table"""
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
    def __init__(self, dbh, mid=None, maillocation=None):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
        """Creates a new MailLocation instance.
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
        Either mid or maillocation must be specified.
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
        
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
        Keyword arguments:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
        dbh -- a pyPgSQL.PgSQL.connection
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
        mid -- the id of a maillocation (long)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
        maillocation -- the value of the maillocation (str)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
        """
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
        self._dbh = dbh
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
        if mid is None and maillocation is None:
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    38
            raise MLE(_('Either mid or maillocation must be specified.'),
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    39
                ERR.MAILLOCATION_INIT)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
        elif mid is not None:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
            try:
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    42
                self.__id = long(mid)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
            except ValueError:
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    44
                raise MLE(_('mid must be an int/long.'), ERR.MAILLOCATION_INIT)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
            self._loadByID()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
        else:
69
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    47
            re.compile(RE_MAILLOCATION)
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    48
            if re.match(RE_MAILLOCATION, maillocation):
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    49
                self.__maillocation = maillocation
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    50
                self._loadByName()
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    51
            else:
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    52
                raise MLE(
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    53
                    _(u'Invalid folder name »%s«, it may consist only of\n\
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    54
1 - 20 single byte characters (A-Z, a-z, 0-9 and _).') % maillocation,
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    55
                        ERR.MAILLOCATION_INIT)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
    def _loadByID(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        dbc = self._dbh.cursor()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
        dbc.execute('SELECT maillocation FROM maillocation WHERE mid = %s',
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
                self.__id)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
        result = dbc.fetchone()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
        dbc.close()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
        if result is not None:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
            self.__maillocation = result[0]
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
        else:
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    66
            raise MLE(_('Unknown mid specified.'), ERR.UNKNOWN_MAILLOCATION_ID)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
    def _loadByName(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
        dbc = self._dbh.cursor()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
        dbc.execute('SELECT mid FROM maillocation WHERE maillocation = %s',
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
                self.__maillocation)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
        result = dbc.fetchone()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
        dbc.close()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
        if result is not None:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
            self.__id = result[0]
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
        else:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
            self._save()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
    def _save(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
        dbc = self._dbh.cursor()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
        dbc.execute("SELECT nextval('maillocation_id')")
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
        self.__id = dbc.fetchone()[0]
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
        dbc.execute('INSERT INTO maillocation(mid,maillocation) VALUES(%s,%s)',
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
                self.__id, self.__maillocation)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
        self._dbh.commit()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
        dbc.close()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
    def getID(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
        """Returns the unique ID of the maillocation."""
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
        return self.__id
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
    def getMailLocation(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        """Returns the value of maillocation, ex: 'Maildir'"""
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
        return self.__maillocation
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95