150 lines
4.3 KiB
Python
150 lines
4.3 KiB
Python
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Text, ForeignKey, Unicode
|
|
from sqlalchemy.orm import relationship
|
|
from datetime import datetime
|
|
from database import Base
|
|
from database import db_session
|
|
from email.header import decode_header
|
|
from marshmallow import Schema, fields, post_load
|
|
import yaml
|
|
import email
|
|
from mail_model import Mail
|
|
#from fetch_mail import fetch_mail
|
|
import re
|
|
|
|
|
|
class FullThreadSchema(Schema):
|
|
id=fields.Integer()
|
|
text=fields.String()
|
|
body=fields.String()
|
|
envelope=fields.String()
|
|
|
|
|
|
class MailThread(Base):
|
|
__tablename__ = 'threads'
|
|
id = Column(Integer, primary_key=True)
|
|
firstmail = Column(Integer)
|
|
date = Column(DateTime)
|
|
islabeled = Column(Boolean)
|
|
opened = Column(Boolean)
|
|
body = Column(Text)
|
|
maintopic=Column(String)
|
|
__schema__=FullThreadSchema
|
|
__jsonid__='thread'
|
|
__whiteattrs__= ["body"]
|
|
__jsonattrs__=None
|
|
answered=False
|
|
# maintopic="information"
|
|
lang=""
|
|
def bdy(self):
|
|
return yaml.load(self.body)
|
|
|
|
def to_text(self):
|
|
mmm=self.mails()
|
|
txt=""
|
|
for m in mmm:
|
|
# m.compile_envelope()
|
|
txt=txt+"mail: \n"
|
|
for f in yaml.load(m.from_):
|
|
txt=txt+f["mail"]+"@"+f["host"]
|
|
txt=txt+" --- "
|
|
txt=txt+" ".join(yaml.load(m.subject))
|
|
txt=txt+"\n"
|
|
return txt
|
|
|
|
def tstr(self):
|
|
fr=yaml.load(self.mails()[0].from_)
|
|
return "(" + str(self.opened)+ ", "+ str(self.maintopic)+ ", "+ str(self.lang) + ") " + str(self.firstmail)+": "+str(fr[0]["mail"])+"@"+str(fr[0]["host"]) + " | ".join(yaml.load(self.mails()[0].subject))
|
|
|
|
def mails(self):
|
|
a=[]
|
|
# print self.bdy()
|
|
for m in self.bdy():
|
|
mail=db_session.query(Mail).filter(Mail.id==int(m)).first()
|
|
if mail is None:
|
|
mail=Mail.fetch_mail(int(m))
|
|
a.append(mail)
|
|
return a
|
|
|
|
def mail_dicts(self):
|
|
a=[]
|
|
# print "maildicts: "+ str(self.mails())
|
|
for m in self.mails():
|
|
m.compile_envelope()
|
|
a.append(m.dict_envelope())
|
|
return a
|
|
|
|
def mail_flat_dict(self):
|
|
a=[]
|
|
d={}
|
|
dc=self.mail_dicts()
|
|
# print dc
|
|
for i in range(0,len(dc)):
|
|
for k, v in dc[i].iteritems():
|
|
d["mail_"+str(i)+"_"+k]=v
|
|
return d
|
|
|
|
def subject(self):
|
|
a=""
|
|
for m in self.mails():
|
|
a=a + " ".join(yaml.load(m.subject))+"\n"
|
|
|
|
return a
|
|
|
|
def compile(self):
|
|
for m in self.mails():
|
|
m.compile_envelope()
|
|
m.compile_text()
|
|
db_session.add(m)
|
|
db_session.commit()
|
|
self.date=self.mails()[0].date
|
|
|
|
def print_text(self,filter="all"):
|
|
a=u""
|
|
def mail_txt(m):
|
|
#txt ="Gesendet von: "+ str(m.from_mailbox)+"@"+str(m.from_host) +"\n"
|
|
txt=""
|
|
fr=yaml.load(m.from_)
|
|
txt= txt+ "Gesendet von: "+str(fr[0]["mail"])+"@"+str(fr[0]["host"])+" am "+ str(m.date) + "\n"
|
|
t=yaml.load(m.text)
|
|
if type(t) is unicode:
|
|
#txt=txt
|
|
txt=txt+t
|
|
else:
|
|
t=t.decode("ISO-8859-1")
|
|
txt=txt+t
|
|
return txt
|
|
|
|
if filter=="all":
|
|
mm=self.mails()
|
|
for m in mm:
|
|
a=a+mail_txt(m)+"\n****........................................***\n"
|
|
elif filter=="first":
|
|
a=mail_txt(m[0])
|
|
a=re.sub(r'\n\s*\n',r'\n',a)
|
|
a=re.sub(r'<!--.*-->',r'',a,flags=re.MULTILINE|re.DOTALL)
|
|
a=re.sub(r'\s*>+ .*\n',r'',a)
|
|
return a
|
|
def text(self,filter="all"):
|
|
a=u""
|
|
def mail_txt(m):
|
|
t=yaml.load(m.text)
|
|
if type(t) is unicode:
|
|
txt=t
|
|
else:
|
|
t=t.decode("ISO-8859-1")
|
|
txt=t
|
|
return txt
|
|
mm=self.mails()
|
|
if filter=="all":
|
|
for m in mm:
|
|
a=a+mail_txt(m)+"\n****........................................***\n"
|
|
elif filter=="first":
|
|
a=mail_txt(m[0])
|
|
a=re.sub(r'\n\s*\n',r'\n',a)
|
|
a=re.sub(r'<!--.*-->',r'',a,flags=re.MULTILINE|re.DOTALL)
|
|
a=re.sub(r'\s*>+ .*\n',r'',a)
|
|
|
|
|
|
|
|
return a
|