init learning cats
This commit is contained in:
93
storage/thread_model.py
Normal file
93
storage/thread_model.py
Normal file
@@ -0,0 +1,93 @@
|
||||
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
|
||||
|
||||
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)
|
||||
islabeled = Column(Boolean)
|
||||
opened = Column(Boolean)
|
||||
body = Column(Text)
|
||||
__schema__=FullThreadSchema
|
||||
__jsonid__='thread'
|
||||
__whiteattrs__= ["body"]
|
||||
__jsonattrs__=None
|
||||
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 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():
|
||||
m.compile_envelope()
|
||||
a=a + " ".join(yaml.load(m.subject))+"\n"
|
||||
|
||||
return a
|
||||
|
||||
def text(self):
|
||||
a=u""
|
||||
for m in self.mails():
|
||||
m.compile_text()
|
||||
t=yaml.load(m.text)
|
||||
if type(t) is unicode:
|
||||
txt=t
|
||||
else:
|
||||
# print "withintm:"+str(type(t))
|
||||
t=t.decode("ISO-8859-1")
|
||||
txt=t
|
||||
a=a+txt+"\n\n"
|
||||
|
||||
return a
|
||||
Reference in New Issue
Block a user