Rev 873 | Rev 900 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 14-Jul-2010@author: ashish'''from email.mime.multipart import MIMEMultipartfrom shop2020.thriftpy.utils.ttypes import HelperServiceException, Mailfrom email.mime.base import MIMEBasefrom email import encodersimport osimport smtplibfrom shop2020.utils.Utils import log_entry, to_py_datefrom shop2020.helpers.impl.DataService import Message, DashboardUserfrom elixir import *from shop2020.thriftpy.utils.ttypes import Message as Msgfrom string import Templateimport datetimefrom shop2020.helpers.impl import DataServicefrom email.mime.text import MIMETextdef initialize():log_entry("initialize@DataAccessor", "Initializing data service")DataService.initialize()def sendMail(mail):if not mail:raise HelperServiceException(101, "mail not present")#msg = MIMEMultipart()#mail = Mail()msg = MIMEText(mail.data)msg['To'] = mail.tomsg['From'] = "help@saholic.com"msg['Subject'] = (mail.subject).#msg.attach(mail.data)#handle attachments in mailif mail.attachments:for attach in mail.attachments:part = MIMEBase('application', 'octet-stream')part.set_payload(open(attach, 'rb').read())encoders.encode_base64(part)part.add_header('Content-Disposition','attachment; filename="%s"' % os.path.basename(attach))msg.attach(part)for to in mail.to:msg['To'] = tomail.sender = "help@shop2020.in"mail.password = "5h0p2o2o"mailServer = smtplib.SMTP("smtp.gmail.com", 587)mailServer.ehlo()mailServer.starttls()mailServer.ehlo()mailServer.login(mail.sender, mail.password)mailServer.sendmail(mail.password, to, msg.as_string())# Should be mailServer.quit(), but that crashes...mailServer.close()def sendText(text):passdef addMessage(message):msg = Message.get_by(message_id=message.id)if msg:raise HelperServiceException(101, "Message is already present. Please try updation api instead")msg = Message();msg.message_id = message.idmsg.message = message.messagesession.commit()def getMessage(message_id):msg = Message.get_by(id=message_id)message = Msg()message.id = msg.message_idmessage.message = msg.messagereturn messagedef updateMessage(id, message):msg = Message.get_by(message_id=id)if msg:msg.message = messagesession.commit()else:raise HelperServiceException(101, "message could not be found with id %d" %(id))def getSubstitutedMessage(id, params):#get the message firstmsg = Message.get_by(message_id=id)if not msg:raise HelperServiceException(101, "message could not be found with id %d" %(id))if params:s = Template(msg.message)s = s.safe_substitute(params)return selse:return msg.messagedef add_user(username, password, warehouseId):user = DashboardUser()user.username = usernameuser.password = passworduser.warehouseId = warehouseIduser.addedOn = datetime.datetime.now()user.status = 0try:session.commit()return Trueexcept:raise HelperServiceException(101, "Some error while adding user")return Falsedef delete_user(username):user = DashboardUser.get_by(username=username)if user is None:return Falseuser.delete()session.commit()return Truedef authenticate_user(username, password):user = DashboardUser.get_by(username=username)if user is None:return -1if user.password == password:user.loggedOn = datetime.datetime.now()session.commit()return user.warehouseIdreturn -1def update_password(username, oldPassword, newPassword):user = DashboardUser.get_by(username=username)if user is None:return Falseif user.password == oldPassword:user.password = newPasswordsession.commit()return Truereturn Falsedef close_session():if session.is_active:print "session is active. closing it."session.close()