我有一个 python 脚本,它从单个数据库表中提取信息并向用户发送每周更新的电子邮件。我想找到一种方法来修改我的代码以允许规范化结构并将所有调查员信息(前缀、fname、lname 和电子邮件)放入它自己的表中。但是,我不确定如何在 elixir 模型的结构和我的 python 代码中执行此操作。
这是我当前的长生不老药模型文件:
from elixir import *
import auth as auth
au = auth.UserAuth()
metadata.bind = 'mysql://' + au.user + ':' + au.password + '@localhost/' + au.database
metadata.bind.echo = True
class Protocol(Entity):
id = Field(Integer, primary_key = True)
irb_no = Field(Text)
title = Field(Text)
prefix = Field(Text)
fname = Field(Text)
lname = Field(Text)
email = Field(Text)
action_date = Field(Date)
action = Field(Text)
approved = Field(Integer)
using_options(tablename = 'protocols')
def __repr__ (self):
return '%d' %(self.id)
这是我的python脚本:
import smtplib
import auth as auth
import ProtocolModel as PM
from elixir import *
from datetime import datetime
au = auth.UserAuth()
setup_all()
create_all()
table = PM.Protocol
records = table.query.all()
if len(records) == 0:
print 'No records in Table'
else:
for record in records:
setup_all()
if record.approved == 1:
print 'Completed'
else:
FROMADDR = au.google_user
LOGIN = FROMADDR
PASSWORD = au.google_password
TOADDRS = record.email
SUBJECT = "Weekly Research Update for " + record.irb_no + " - " + record.title
date1 = datetime.strptime(str(record.action_date), '%Y-%m-%d')
date2 = date1.strftime('%B %d, %Y')
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += 'Dear' + ' ' + record.prefix + ' ' + record.fname + ' ' + record.lname + ',' + '\n' + '\n'
msg += 'Our records indicate that the most recent action on your research protocol titled ' + record.title + ' was taken on ' + str(date2) +'.' +'\n'
msg += 'This action was: ' + record.action
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()
这似乎应该是一个相对简单的改变,但我只是不确定如何去做。非常感谢任何帮助,谢谢!