iro/model/message.py
author Sandro Knauß <knauss@netzguerilla.net>
Thu, 29 Mar 2012 16:27:40 +0200
branchdevel
changeset 258 0a5eb5aac0be
parent 244 48fba7b4afe9
child 294 0e75bd39767d
permissions -rw-r--r--
iro.model: adding docstring

# -*- coding: utf-8 -*-
"""All available message typs to send send.
"""
from email.mime.text import MIMEText
from email.header import Header
from email.Utils import formatdate

class Message:
    """ Baseclass for all different message typs."""
    def __init__(self,content, typ="Message"):
        """Constructor of Message class.
        
        :param content: content of the message
        :param string typ: typ of the message

        .. automethod:: __eq__
        .. automethod:: __neq__
        """
        self.content=content
        self.typ = typ


    def getContent(self):
        """returns the content of the message"""
        return self.content

    def __eq__(self,other):
        """return ``True`` if **other** has the same content."""
        return self.content == other.content

    def __neq__(self,other):
        """return ``False`` if **other** has the same content."""
        return not self.__eq__(other)

class SMS(Message):
    """ A representation of one SMS"""
    def __init__(self, cont, from_ = None):
        """Constructor of SMS class.

        :param string cont: SMS content
        :param string from_: the telnumber from the SMS should be sended.
        """
        Message.__init__(self, cont.encode("utf-8"), typ="sms")
        self.from_ = from_

class Fax(Message):
    """A representation of one fax."""
    def __init__(self,header,cont,attachments=[]):
        """Constructor of one fax.
        
        :param string header: Headline of fax
        :param string cont: fax content
        :param list attachments: attachments of fax
        """
        Message.__init__(self,cont.encode("utf-8"),typ="fax")
        self.header=header
        self.attachments=attachments

    def getAttachment(self,i):
        """returns a attachment
        :param integer i:  the i-th attachment
        """
        return self.attachments[i]

    def __eq__(self,other):
        if not  Message.__eq__(self,other):
            return False

        if self.header != other.header:
            return False

        if len(self.attachments) != len(other.attachments):
            return False

        for a in self.attachments:
            if a not in other.attachments:
                return False
        return True



class Mail(Message):
    """A representation of one Mail"""
    def __init__(self, subject, body, frm):
        """Constructor of one mail.

        :param string subject: subject of the mail
        :param string body: body of the mail
        :param string frm: mailaddress to send mail from

        .. automethod:: __repr__
        """
        con = MIMEText(body.encode("utf-8"), _charset='utf-8')
        sub = Header(subject.encode('utf-8'), 'utf-8')
        con['Subject'] = sub
        con['Date'] = formatdate(localtime=True)
        self.subject = subject
        self.body = body
        self.frm=frm
        Message.__init__(self, con, typ='mail')

    def as_string(self):
        """returns created mail"""
        return self.content.as_string()

    def getFrom(self):
        """returns the from mailaddress"""
        return self.frm
    
    def __eq__(self,other):
        if self.as_string() != other.as_string():
            return False

        if self.frm != other.frm:
            return False
        
        return True

    def __repr__(self):
        """string representation of the class.

        :returns: ``<Mail(subject, body, frm)>``
        """
        return "<Mail(%s, %s, %s)>"%(self.subject,self.body,self.frm)

__all__=["Message", "SMS", "Fax", "Mail"]