"Fossies" - the Fresh Open Source Software Archive

Member "mailman-3.3.7/src/mailman/model/address.py" (10 Nov 2022, 3152 Bytes) of package /linux/misc/mailman-3.3.7.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "address.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.3.5_vs_3.3.6.

    1 # Copyright (C) 2006-2022 by the Free Software Foundation, Inc.
    2 #
    3 # This file is part of GNU Mailman.
    4 #
    5 # GNU Mailman is free software: you can redistribute it and/or modify it under
    6 # the terms of the GNU General Public License as published by the Free
    7 # Software Foundation, either version 3 of the License, or (at your option)
    8 # any later version.
    9 #
   10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
   11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   12 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   13 # more details.
   14 #
   15 # You should have received a copy of the GNU General Public License along with
   16 # GNU Mailman.  If not, see <https://www.gnu.org/licenses/>.
   17 
   18 """Model for addresses."""
   19 
   20 from email.utils import formataddr
   21 from mailman.database.model import Model
   22 from mailman.database.types import SAUnicode, SAUnicode4Byte
   23 from mailman.interfaces.address import (
   24     AddressVerificationEvent,
   25     IAddress,
   26     IEmailValidator,
   27 )
   28 from mailman.utilities.datetime import now
   29 from public import public
   30 from sqlalchemy import Column, DateTime, ForeignKey, Integer
   31 from sqlalchemy.orm import backref, relationship
   32 from zope.component import getUtility
   33 from zope.event import notify
   34 from zope.interface import implementer
   35 
   36 
   37 @public
   38 @implementer(IAddress)
   39 class Address(Model):
   40     """See `IAddress`."""
   41 
   42     __tablename__ = 'address'
   43 
   44     id = Column(Integer, primary_key=True)
   45     email = Column(SAUnicode, index=True, unique=True)
   46     _original = Column(SAUnicode)
   47     display_name = Column(SAUnicode4Byte)
   48     _verified_on = Column('verified_on', DateTime)
   49     registered_on = Column(DateTime)
   50 
   51     user_id = Column(Integer, ForeignKey('user.id'), index=True)
   52 
   53     preferences_id = Column(Integer, ForeignKey('preferences.id'), index=True)
   54     preferences = relationship(
   55         'Preferences', backref=backref('address', uselist=False))
   56 
   57     def __init__(self, email, display_name):
   58         super().__init__()
   59         getUtility(IEmailValidator).validate(email)
   60         lower_case = email.lower()
   61         self.email = lower_case
   62         self.display_name = display_name
   63         self._original = (None if lower_case == email else email)
   64         self.registered_on = now()
   65 
   66     def __str__(self):
   67         addr = (self.email if self._original is None else self._original)
   68         return formataddr((self.display_name, addr))
   69 
   70     def __repr__(self):
   71         verified = ('verified' if self.verified_on else 'not verified')
   72         address_str = str(self)
   73         if self._original is None:
   74             return '<Address: {} [{}] at {:#x}>'.format(
   75                 address_str, verified, id(self))
   76         else:
   77             return '<Address: {} [{}] key: {} at {:#x}>'.format(
   78                 address_str, verified, self.email, id(self))
   79 
   80     @property
   81     def verified_on(self):
   82         return self._verified_on
   83 
   84     @verified_on.setter
   85     def verified_on(self, timestamp):
   86         self._verified_on = timestamp
   87         notify(AddressVerificationEvent(self))
   88 
   89     @property
   90     def original_email(self):
   91         return (self.email if self._original is None else self._original)