Subversion Repositories SmartDukaan

Rev

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 MIMEMultipart
from shop2020.thriftpy.utils.ttypes import HelperServiceException, Mail
from email.mime.base import MIMEBase
from email import encoders
import os
import smtplib
from shop2020.utils.Utils import log_entry, to_py_date
from shop2020.helpers.impl.DataService import Message, DashboardUser

from elixir import *

from shop2020.thriftpy.utils.ttypes import Message as Msg
from string import Template
import datetime
from shop2020.helpers.impl import DataService
from email.mime.text import MIMEText

def 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.to
    msg['From'] = "help@saholic.com"
    msg['Subject'] = (mail.subject).
    #msg.attach(mail.data)
    
    #handle attachments in mail
    if 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'] = to
        mail.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):
    pass

def 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.id
    msg.message = message.message
    session.commit()
    
def getMessage(message_id):
    msg = Message.get_by(id=message_id)
    message = Msg()
    message.id = msg.message_id
    message.message = msg.message
    return message

def updateMessage(id, message):
    msg = Message.get_by(message_id=id)
    if msg:
        msg.message = message
        session.commit()
    else:
        raise HelperServiceException(101, "message could not be found with id %d" %(id))

def getSubstitutedMessage(id, params):
    #get the message first
    msg = 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 s
    else:
        return msg.message

def add_user(username, password, warehouseId):
    user = DashboardUser()
    user.username = username
    user.password = password
    user.warehouseId = warehouseId
    user.addedOn = datetime.datetime.now()
    user.status = 0
    try:
        session.commit()
        return True
    except:
        raise HelperServiceException(101, "Some error while adding user")
        return False

def delete_user(username):
    user = DashboardUser.get_by(username=username)
    if user is None:
        return False
    user.delete()
    session.commit()
    return True

def authenticate_user(username, password):
    user = DashboardUser.get_by(username=username)
    if user is None:
        return -1
    if user.password == password:
        user.loggedOn = datetime.datetime.now()
        session.commit()
        return user.warehouseId
    return -1
    
def update_password(username, oldPassword, newPassword):
    user = DashboardUser.get_by(username=username)
    if user is None:
        return False
    if user.password == oldPassword:
        user.password = newPassword
        session.commit()
        return True
    return False

def close_session():
    if session.is_active:
        print "session is active. closing it."
        session.close()