Subversion Repositories SmartDukaan

Rev

Rev 3206 | Rev 4691 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 14-Jul-2010

@author: ashish
'''
from shop2020.helpers.impl import DataAccessor
from shop2020.helpers.impl.DataAccessor import getSubstitutedMessage, getMessage,\
    updateMessage, addMessage, add_user, delete_user, update_password,\
    save_user_email_for_sending, get_emails_to_be_sent, mark_email_as_sent , initialize, close_session,\
    get_reports, authenticate_dashboard_user, is_alive
from shop2020.helpers.impl.DataService import LogisticsUser, StatisticsUser,\
    ReportUser, CatalogDashboardUser
from shop2020.thriftpy.utils.ttypes import HelperServiceException
from shop2020.helpers.impl.Converters import to_t_luser, to_t_useremail,\
    to_t_suser, to_t_ruser, to_t_report, to_t_catalog_user
from shop2020.config.client.ConfigClient import ConfigClient


class HelperServiceHandler():
    

    def __init__(self, dbname='helper', db_hostname='localhost'):
        initialize(dbname, db_hostname)
        
        '''
        try:
            config_client = ConfigClient()
            self.customer_care_mail = config_client.get_property('saholic_customer_care_mail')
        except:    
            self.customer_care_mail = "help@saholic.com"
        ''' 

    def saveUserEmailForSending(self, emailTo, emailFrom, subject, body, source, emailType):
        try:
            return save_user_email_for_sending(emailTo, emailFrom, subject, body, source, emailType)
        finally:
            close_session()
    
    def getEmailsToBeSent(self):
        try:
            return [to_t_useremail(user_email) for user_email in get_emails_to_be_sent()]
        finally:
            close_session()
    
    def markEmailAsSent(self, emailId):
        try:
            mark_email_as_sent(emailId)
        finally:
            close_session()
    
    def sendMail(self, mail):
        try:
            DataAccessor.sendMail(mail)
        finally:
            close_session()
            
    def sendText(self, message):
        """
        Parameters:
         - message
        """
        try:
            DataAccessor.sendText(message)
        finally:
            close_session()
            
    def addMessage(self, message):
        """
        Parameters:
         - message
        """
        try:
            addMessage(message)
        finally:
            close_session()
            
    def updateMessage(self, id, message):
        """
        Parameters:
         - id
         - message
        """
        try:
            return updateMessage(id, message)
        finally:
            close_session()
            
    def getMessage(self, id):
        """
        Parameters:
         - id
        """
        try:
            return getMessage(id)
        finally:
            close_session()
            
    def getSubstitutedMessage(self, id, params):
        """
        Parameters:
         - id
         - params
        """
        try:
            return getSubstitutedMessage(id, params)
        finally:
            close_session()
            
    def addUser(self, username, password, warehouseId):
        """
        Parameters:
         - username
         - password
         - warehouseId
        """
        try:
            return add_user(username, password, warehouseId)
        finally:
            close_session()
    
    def deleteUser(self, username):
        """
        Parameters:
         - username
        """
        try:
            return delete_user(username)
        finally:
            close_session()
                
    def authenticateDashboardUser(self, username, password):
        """
        Returns the dashboard user if the supplied username and password match. Raises an exception otherwise.
        The loggedOn timestamp for the dashboard user is updated .
        
        Parameters:
         - username
         - password
        """
        try:
            return authenticate_dashboard_user(username, password)
        finally:
            close_session()
            
    
    def updatePassword(self, username, oldPassword, newPassword):
        """
        Parameters:
         - username
         - oldPassword
         - newPassword
        """
        try:
            update_password(username, oldPassword, newPassword)
        finally:
            close_session()

    def authenticateLogisticsUser(self, username, password):
        """
        Returns the LogisticsUser struct associated with the given username and password if they match.
        Throws an exception otherwise.
        
        Parameters:
         - username
         - password
        """
        try:
            luser = LogisticsUser.get_by(username=username, password=password)
            return to_t_luser(luser)
        except:
            raise HelperServiceException(101, "No such user")
        finally:
            close_session()
            
    def authenticateStatisticsUser(self, username, password):
        """
        Returns the StatisticsUser struct associated with the given username and password if they match.
        Throws an exception otherwise.
        
        Parameters:
         - username
         - password
        """
        try:
            suser = StatisticsUser.get_by(username=username, password=password)
            return to_t_suser(suser)
        except:
            raise HelperServiceException(101, "No such user")
        finally:
            close_session()

    def authenticateReportUser(self, username, password):
        """
        Returns the ReportUser struct associated with the given username and password if they match.
        Throws an exception otherwise.
        
        Parameters:
         - username
         - password
        """
        try:
            ruser = ReportUser.get_by(username=username, password=password)
            return to_t_ruser(ruser)
        except:
            raise HelperServiceException(101, "No such user")
        finally:
            close_session()

    def getReports(self, role):
        """
        Returns list of reports which are configured for the given role.
        
        Parameters:
         - role
        """
        try:
            reports = get_reports(role) 
            return [to_t_report(report) for report in reports]
        finally:
            close_session()
            
    def authenticateCatalogUser(self, username, password, role):
        """
        Returns the CatalogDashboardUser struct associated with the given username and password if they match.
        Throws an exception otherwise.
        
        Parameters:
         - username
         - password
        """
        try:
            catalog_user = CatalogDashboardUser.get_by(username=username, password=password, role=role)
            return to_t_catalog_user(catalog_user)
        except:
            raise HelperServiceException(101, "No such catalog user")
        finally:
            close_session()         
    
    def closeSession(self, ):
        close_session()
        
    def isAlive(self, ):
        """
        For checking weather service is active alive or not. It also checks connectivity with database
        """
        try:
            return is_alive()
        finally:
            close_session()