VirtualMailManager/quotalimit.py
author Pascal Volk <user@localhost.localdomain.org>
Sun, 04 Nov 2012 18:33:11 +0000
branchv0.7.x
changeset 637 ca6621caff2f
parent 568 14abdd04ddf5
child 638 0de0b9e75c9f
permissions -rw-r--r--
VMM: Removed module pycompat.
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 -*-
568
14abdd04ddf5 Updated copyright notices to include the year 2012.
Pascal Volk <user@localhost.localdomain.org>
parents: 450
diff changeset
     2
# Copyright (c) 2011 - 2012, 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
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
        `dbh` : pyPgSQL.PgSQL.Connection || psycopg2._psycopg.connection
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
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
        `bytes` : long
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
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
        for key in kwargs.iterkeys():
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:
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
            assert isinstance(qid, (int, long))
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')
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
            assert all(isinstance(i, (int, long)) for i in (bytes_, msgs))
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
            if bytes_ < 0:
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
                self._bytes = -bytes_
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
            else:
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
                self._bytes = bytes_
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
            if msgs < 0:
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
                self._messages = -msgs
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
            else:
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
                self._messages = msgs
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
            self._load_by_limit()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
    @property
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
    def bytes(self):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
        """Quota limit in bytes."""
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
        return self._bytes
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
    @property
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
    def messages(self):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
        """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
    73
        return self._messages
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
    @property
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
    def qid(self):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
        """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
    78
        return self._qid
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
    def __eq__(self, other):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
        if isinstance(other, self.__class__):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
            return self._qid == other._qid
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
        return NotImplemented
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
    def __ne__(self, other):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
        if isinstance(other, self.__class__):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
            return self._qid != other._qid
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
        return NotImplemented
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
    def _load_by_limit(self):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
        """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
    92
        dbc = self._dbh.cursor()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        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
    94
                    '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
    95
        res = dbc.fetchone()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
        dbc.close()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
        if res:
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
            self._qid = res[0]
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
        else:
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
            self._save()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   101
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   102
    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
   103
        """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
   104
        dbc = self._dbh.cursor()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   105
        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
   106
                    (qid,))
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
        res = dbc.fetchone()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   108
        dbc.close()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   109
        if not res:
450
fd4aa073015f VMM/{maillocation,quotalimit}: Unified object initialization code.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 407
diff changeset
   110
            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
   111
        self._qid = qid
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   112
        self._bytes, self._messages = res
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   113
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   114
    def _save(self):
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   115
        """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
   116
        dbc = self._dbh.cursor()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   117
        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
   118
        self._qid = dbc.fetchone()[0]
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   119
        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
   120
                    '(%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
   121
        self._dbh.commit()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   122
        dbc.close()
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   123
0cae9989395b VMM/quotalimit: Added new module quotalimit to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   124
del _