Subversion Repositories SmartDukaan

Rev

Rev 4996 | Rev 5110 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
353 ashish 1
'''
2
Created on 14-Jul-2010
3
 
4
@author: ashish
5
'''
4691 mandeep.dh 6
from shop2020.config.client.ConfigClient import ConfigClient
353 ashish 7
from shop2020.helpers.impl import DataAccessor
4691 mandeep.dh 8
from shop2020.helpers.impl.Converters import to_t_luser, to_t_useremail, \
4806 varun.gupt 9
    to_t_suser, to_t_ruser, to_t_report, to_t_catalog_user, to_t_quicklink
4691 mandeep.dh 10
from shop2020.helpers.impl.DataAccessor import getSubstitutedMessage, getMessage, \
4806 varun.gupt 11
    updateMessage, addMessage, add_user, delete_user, update_password, share_entities, \
4691 mandeep.dh 12
    save_user_email_for_sending, get_emails_to_be_sent, mark_email_as_sent, \
4806 varun.gupt 13
    initialize, close_session, get_reports, authenticate_dashboard_user, is_alive,\
5055 varun.gupt 14
    save_quick_link, get_quick_links, update_quicklink,\
15
    get_emails_for_notifications_sent
4691 mandeep.dh 16
from shop2020.helpers.impl.DataService import LogisticsUser, StatisticsUser, \
2025 ankur.sing 17
    ReportUser, CatalogDashboardUser
4754 mandeep.dh 18
from shop2020.helpers.impl.model.Agent import Agent, User_Roles, Roles_Permissions
759 chandransh 19
from shop2020.thriftpy.utils.ttypes import HelperServiceException
5055 varun.gupt 20
from shop2020.utils.Utils import to_py_date
353 ashish 21
 
900 rajveer 22
 
353 ashish 23
class HelperServiceHandler():
24
 
25
 
3187 rajveer 26
    def __init__(self, dbname='helper', db_hostname='localhost'):
27
        initialize(dbname, db_hostname)
900 rajveer 28
 
29
        '''
30
        try:
31
            config_client = ConfigClient()
32
            self.customer_care_mail = config_client.get_property('saholic_customer_care_mail')
33
        except:    
34
            self.customer_care_mail = "help@saholic.com"
35
        ''' 
353 ashish 36
 
1395 varun.gupt 37
    def saveUserEmailForSending(self, emailTo, emailFrom, subject, body, source, emailType):
38
        try:
3206 mandeep.dh 39
            return save_user_email_for_sending(emailTo, emailFrom, subject, body, source, emailType)
1395 varun.gupt 40
        finally:
41
            close_session()
42
 
3086 rajveer 43
    def getEmailsToBeSent(self):
1422 varun.gupt 44
        try:
3086 rajveer 45
            return [to_t_useremail(user_email) for user_email in get_emails_to_be_sent()]
1422 varun.gupt 46
        finally:
47
            close_session()
48
 
49
    def markEmailAsSent(self, emailId):
50
        try:
51
            mark_email_as_sent(emailId)
52
        finally:
53
            close_session()
54
 
353 ashish 55
    def sendMail(self, mail):
766 rajveer 56
        try:
57
            DataAccessor.sendMail(mail)
58
        finally:
59
            close_session()
60
 
353 ashish 61
    def sendText(self, message):
62
        """
63
        Parameters:
64
         - message
65
        """
766 rajveer 66
        try:
67
            DataAccessor.sendText(message)
68
        finally:
69
            close_session()
70
 
353 ashish 71
    def addMessage(self, message):
72
        """
73
        Parameters:
74
         - message
75
        """
766 rajveer 76
        try:
77
            addMessage(message)
78
        finally:
79
            close_session()
80
 
353 ashish 81
    def updateMessage(self, id, message):
82
        """
83
        Parameters:
84
         - id
85
         - message
86
        """
766 rajveer 87
        try:
88
            return updateMessage(id, message)
89
        finally:
90
            close_session()
91
 
353 ashish 92
    def getMessage(self, id):
93
        """
94
        Parameters:
95
         - id
96
        """
766 rajveer 97
        try:
98
            return getMessage(id)
99
        finally:
100
            close_session()
101
 
353 ashish 102
    def getSubstitutedMessage(self, id, params):
103
        """
104
        Parameters:
105
         - id
106
         - params
107
        """
766 rajveer 108
        try:
109
            return getSubstitutedMessage(id, params)
110
        finally:
111
            close_session()
112
 
494 rajveer 113
    def addUser(self, username, password, warehouseId):
114
        """
115
        Parameters:
116
         - username
117
         - password
118
         - warehouseId
119
        """
766 rajveer 120
        try:
121
            return add_user(username, password, warehouseId)
122
        finally:
123
            close_session()
494 rajveer 124
 
125
    def deleteUser(self, username):
126
        """
127
        Parameters:
128
         - username
129
        """
766 rajveer 130
        try:
131
            return delete_user(username)
132
        finally:
133
            close_session()
134
 
2445 chandransh 135
    def authenticateDashboardUser(self, username, password):
494 rajveer 136
        """
2445 chandransh 137
        Returns the dashboard user if the supplied username and password match. Raises an exception otherwise.
138
        The loggedOn timestamp for the dashboard user is updated .
139
 
494 rajveer 140
        Parameters:
141
         - username
142
         - password
143
        """
766 rajveer 144
        try:
2445 chandransh 145
            return authenticate_dashboard_user(username, password)
766 rajveer 146
        finally:
147
            close_session()
148
 
494 rajveer 149
 
150
    def updatePassword(self, username, oldPassword, newPassword):
151
        """
152
        Parameters:
153
         - username
154
         - oldPassword
155
         - newPassword
156
        """
766 rajveer 157
        try:
158
            update_password(username, oldPassword, newPassword)
159
        finally:
160
            close_session()
759 chandransh 161
 
162
    def authenticateLogisticsUser(self, username, password):
163
        """
164
        Returns the LogisticsUser struct associated with the given username and password if they match.
165
        Throws an exception otherwise.
166
 
167
        Parameters:
168
         - username
169
         - password
170
        """
171
        try:
172
            luser = LogisticsUser.get_by(username=username, password=password)
173
            return to_t_luser(luser)
174
        except:
766 rajveer 175
            raise HelperServiceException(101, "No such user")
176
        finally:
177
            close_session()
178
 
1610 ankur.sing 179
    def authenticateStatisticsUser(self, username, password):
180
        """
181
        Returns the StatisticsUser struct associated with the given username and password if they match.
182
        Throws an exception otherwise.
183
 
184
        Parameters:
185
         - username
186
         - password
187
        """
188
        try:
189
            suser = StatisticsUser.get_by(username=username, password=password)
190
            return to_t_suser(suser)
191
        except:
192
            raise HelperServiceException(101, "No such user")
193
        finally:
194
            close_session()
195
 
1891 ankur.sing 196
    def authenticateReportUser(self, username, password):
197
        """
198
        Returns the ReportUser struct associated with the given username and password if they match.
199
        Throws an exception otherwise.
200
 
201
        Parameters:
202
         - username
203
         - password
204
        """
205
        try:
206
            ruser = ReportUser.get_by(username=username, password=password)
207
            return to_t_ruser(ruser)
208
        except:
209
            raise HelperServiceException(101, "No such user")
210
        finally:
211
            close_session()
212
 
213
    def getReports(self, role):
214
        """
215
        Returns list of reports which are configured for the given role.
216
 
217
        Parameters:
218
         - role
219
        """
220
        try:
221
            reports = get_reports(role) 
222
            return [to_t_report(report) for report in reports]
223
        finally:
224
            close_session()
2025 ankur.sing 225
 
2358 ankur.sing 226
    def authenticateCatalogUser(self, username, password, role):
2025 ankur.sing 227
        """
228
        Returns the CatalogDashboardUser struct associated with the given username and password if they match.
229
        Throws an exception otherwise.
230
 
231
        Parameters:
232
         - username
233
         - password
234
        """
235
        try:
2358 ankur.sing 236
            catalog_user = CatalogDashboardUser.get_by(username=username, password=password, role=role)
2025 ankur.sing 237
            return to_t_catalog_user(catalog_user)
238
        except:
239
            raise HelperServiceException(101, "No such catalog user")
240
        finally:
4806 varun.gupt 241
            close_session()
242
 
243
    def shareEntities(self, entityIds, email):
244
        '''
245
        Shares the entities with en email
246
 
247
        Parameters:
248
         - entityIds
249
         - email
250
        '''
251
        try:
252
            share_entities(entityIds, email)
253
        finally:
254
            close_session()
4691 mandeep.dh 255
 
256
    def getAgents(self, ):
257
        return [Agent.to_thrift_object(t) for t in Agent.query.all()]
258
 
259
    def validateLogIn(self, emailId, password):
260
        """
261
        Parameters:
262
         - emailId
263
         - password
264
        """
265
        pass
266
 
267
    def updatePasswordForAgent(self, agentEmailId, password):
268
        """
269
        Parameters:
270
         - agentEmailId
271
         - password
272
        """
273
        pass
274
 
275
    def getRoleNamesForAgent(self, agentEmailId):
276
        """
277
        Parameters:
278
         - agentEmailId
279
        """
4754 mandeep.dh 280
        try:
281
            agent = Agent.get_by(emailId = agentEmailId)
282
            return [a.role_name for a in User_Roles.query.filter_by(agentId = agent.id).all()]
283
        finally:
284
            close_session()
4691 mandeep.dh 285
 
286
    def getPermissionsForRoleName(self, roleName):
287
        """
288
        Parameters:
289
         - roleName
290
        """
4754 mandeep.dh 291
        try:
292
            return [a.permission for a in Roles_Permissions.query.filter_by(role_name = roleName).all()]
293
        finally:
294
            close_session()
1610 ankur.sing 295
 
4806 varun.gupt 296
    def saveQuickLink(self, url, text):
297
        try:
298
            save_quick_link(url, text)
299
        finally:
300
            close_session()
301
 
302
    def getQuickLinks(self):
303
        try:
304
            return [to_t_quicklink(quicklink) for quicklink in get_quick_links()]
305
        finally:
306
            close_session()
307
 
4996 varun.gupt 308
    def updateQuickLink(self, id, url, text):
309
        try:
310
            update_quicklink(id, url, text)
311
        finally:
312
            close_session()
5055 varun.gupt 313
 
314
    def getEmailsForNotificationsSent(self, startDatetime, endDatetime):
315
        '''
316
        '''
317
        try:
318
            return [notification.emailTo for notification in get_emails_for_notifications_sent(to_py_date(startDatetime), to_py_date(endDatetime))]
319
        finally:
320
            close_session()
321
 
766 rajveer 322
    def closeSession(self, ):
3376 rajveer 323
        close_session()
324
 
325
    def isAlive(self, ):
326
        """
327
        For checking weather service is active alive or not. It also checks connectivity with database
328
        """
329
        try:
330
            return is_alive()
331
        finally:
332
            close_session()