VirtualMailManager/quotalimit.py
author Pascal Volk <user@localhost.localdomain.org>
Sun, 09 Feb 2014 14:37:51 +0000
branchv0.7.x
changeset 716 915c14b21db3
parent 711 2a75058fc064
permissions -rw-r--r--
VMM/config: Updated Dovecot version check. Renamed function check_version_format() -> check_dovecot_version(). Now this function checks also if misc.dovecot_version >= MIN_DOVECOT_VERSION.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
385
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
703
58815c004a61 Updated copyright notices to include the year 2014.
Pascal Volk <user@localhost.localdomain.org>
parents: 675
diff changeset
     2
# Copyright (c) 2011 - 2014, Pascal Volk
385
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
"""
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
    VirtualMailManager.quotalimit
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
    Virtual Mail Manager's QuotaLimit class to manage quota limits
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
    for domains and accounts.
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
"""
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
_ = lambda msg: msg
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    13
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
class QuotaLimit(object):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
    """Class to handle quota limit specific data."""
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
    __slots__ = ('_dbh', '_qid', '_bytes', '_messages')
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
    _kwargs = ('qid', 'bytes', 'messages')
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    19
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
    def __init__(self, dbh, **kwargs):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
        """Create a new QuotaLimit instance.
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
        Either the `qid` keyword or the `bytes` and `messages` keywords
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
        must be specified.
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
        Arguments:
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
694
b1bfd4d1d9c0 Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
    28
        `dbh` : psycopg2._psycopg.connection
385
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
          A database connection for the database access.
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
        Keyword arguments:
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
        `qid` : int
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
          The id of a quota limit
670
f374ef062c94 VMM/*: Post-2to3 fix. Updated some error messages/comments.
Pascal Volk <user@localhost.localdomain.org>
parents: 643
diff changeset
    35
        `bytes` : int
385
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
          The quota limit in bytes.
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
        `messages` : int
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
          The quota limit in number of messages
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
        """
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
        self._dbh = dbh
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
        self._qid = 0
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    42
        self._bytes = 0
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
        self._messages = 0
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 638
diff changeset
    45
        for key in kwargs.keys():
385
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
            if key not in self.__class__._kwargs:
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
                raise ValueError('unrecognized keyword: %r' % key)
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
        qid = kwargs.get('qid')
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
        if qid is not None:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 638
diff changeset
    50
            assert isinstance(qid, int)
385
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    51
            self._load_by_qid(qid)
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
        else:
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
            bytes_, msgs = kwargs.get('bytes'), kwargs.get('messages')
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 638
diff changeset
    54
            assert all(isinstance(i, int) for i in (bytes_, msgs))
638
0de0b9e75c9f VMM: Partial PEP-308-ification.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
    55
            self._bytes = -bytes_ if bytes_ < 0 else bytes_
0de0b9e75c9f VMM: Partial PEP-308-ification.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
    56
            self._messages = -msgs if msgs < 0 else msgs
385
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
            self._load_by_limit()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
    @property
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
    def bytes(self):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
        """Quota limit in bytes."""
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
        return self._bytes
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
    @property
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
    def messages(self):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
        """Quota limit in number of messages."""
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
        return self._messages
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
    @property
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
    def qid(self):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
        """The quota limit's unique ID."""
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
        return self._qid
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
    def __eq__(self, other):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
        if isinstance(other, self.__class__):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
            return self._qid == other._qid
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
        return NotImplemented
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
    def __ne__(self, other):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
        if isinstance(other, self.__class__):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
            return self._qid != other._qid
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
        return NotImplemented
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
    def _load_by_limit(self):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
        """Load the quota limit by limit values from the database."""
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
        dbc = self._dbh.cursor()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
        dbc.execute('SELECT qid FROM quotalimit WHERE bytes = %s AND '
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
                    'messages = %s', (self._bytes, self._messages))
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
        res = dbc.fetchone()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
        dbc.close()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
        if res:
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
            self._qid = res[0]
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        else:
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
            self._save()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
    def _load_by_qid(self, qid):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
        """Load the quota limit by its unique ID from the database."""
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
        dbc = self._dbh.cursor()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
        dbc.execute('SELECT bytes, messages FROM quotalimit WHERE qid = %s',
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
                    (qid,))
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   101
        res = dbc.fetchone()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   102
        dbc.close()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   103
        if not res:
450
fd4aa073015f VMM/{maillocation,quotalimit}: Unified object initialization code.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 407
diff changeset
   104
            raise ValueError('Unknown quota limit id specified: %r' % qid)
385
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   105
        self._qid = qid
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   106
        self._bytes, self._messages = res
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   108
    def _save(self):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   109
        """Store a new quota limit in the database."""
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   110
        dbc = self._dbh.cursor()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   111
        dbc.execute("SELECT nextval('quotalimit_id')")
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   112
        self._qid = dbc.fetchone()[0]
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   113
        dbc.execute('INSERT INTO quotalimit (qid, bytes, messages) VALUES '
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   114
                    '(%s, %s, %s)', (self._qid, self._bytes, self._messages))
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   115
        self._dbh.commit()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   116
        dbc.close()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   117
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   118
del _