| 353 |
ashish |
1 |
'''
|
|
|
2 |
Created on 14-Jul-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
| 5110 |
mandeep.dh |
6 |
from elixir import *
|
|
|
7 |
from email import encoders
|
|
|
8 |
from email.mime.base import MIMEBase
|
| 353 |
ashish |
9 |
from email.mime.multipart import MIMEMultipart
|
| 5110 |
mandeep.dh |
10 |
from email.mime.text import MIMEText
|
|
|
11 |
from shop2020.helpers.impl import DataService
|
| 4806 |
varun.gupt |
12 |
from shop2020.helpers.impl.DataService import Message, UserEmail, EntitiesShared, \
|
| 7410 |
amar.kumar |
13 |
Report, ReportRoleAuthority, CatalogDashboardUser, UserEmailArchive, QuickLink, \
|
|
|
14 |
AgentWarehouseMapping
|
| 5110 |
mandeep.dh |
15 |
from shop2020.helpers.impl.model.Agent import Agent
|
|
|
16 |
from shop2020.helpers.impl.model.DashboardUser import DashboardUser
|
|
|
17 |
from shop2020.thriftpy.utils.ttypes import HelperServiceException, Mail, \
|
|
|
18 |
Message as Msg
|
|
|
19 |
from shop2020.utils.Utils import log_entry, to_py_date
|
|
|
20 |
from sqlalchemy.orm import query
|
| 353 |
ashish |
21 |
from string import Template
|
| 494 |
rajveer |
22 |
import datetime
|
| 5110 |
mandeep.dh |
23 |
import os
|
|
|
24 |
import smtplib
|
| 5483 |
varun.gupt |
25 |
from shop2020.clients.CatalogClient import CatalogClient
|
| 353 |
ashish |
26 |
|
| 3187 |
rajveer |
27 |
def initialize(dbname='helper', db_hostname="localhost"):
|
| 494 |
rajveer |
28 |
log_entry("initialize@DataAccessor", "Initializing data service")
|
| 3187 |
rajveer |
29 |
DataService.initialize(dbname, db_hostname)
|
| 494 |
rajveer |
30 |
|
| 5864 |
rajveer |
31 |
def save_user_email_for_sending(email_to, email_from, subject, body, source, email_type, cc, bcc):
|
| 1395 |
varun.gupt |
32 |
user_email = UserEmail()
|
| 5864 |
rajveer |
33 |
user_email.emailTo = ';'.join(email_to)
|
| 1395 |
varun.gupt |
34 |
user_email.emailFrom = email_from
|
|
|
35 |
user_email.subject = subject
|
|
|
36 |
user_email.body = body
|
|
|
37 |
user_email.source = source
|
|
|
38 |
user_email.emailType = email_type
|
|
|
39 |
user_email.status = False
|
|
|
40 |
user_email.timestamp = datetime.datetime.now()
|
| 5864 |
rajveer |
41 |
if cc:
|
|
|
42 |
user_email.cc = ';'.join(cc)
|
|
|
43 |
if bcc:
|
|
|
44 |
user_email.bcc = ';'.join(bcc)
|
| 1395 |
varun.gupt |
45 |
session.commit()
|
| 3206 |
mandeep.dh |
46 |
return user_email.id
|
| 1422 |
varun.gupt |
47 |
|
| 3086 |
rajveer |
48 |
def get_emails_to_be_sent():
|
| 1422 |
varun.gupt |
49 |
print "get_emails_to_be_sent"
|
| 3005 |
chandransh |
50 |
return UserEmail.query.all()
|
| 1422 |
varun.gupt |
51 |
|
|
|
52 |
def mark_email_as_sent(email_id):
|
| 3005 |
chandransh |
53 |
query = 'INSERT INTO ' + str(UserEmailArchive.table) + ' (select * from ' + str(UserEmail.table) + ' where id = ' + str(email_id) + ')'
|
|
|
54 |
session.execute(query, mapper=UserEmailArchive)
|
| 1422 |
varun.gupt |
55 |
email = UserEmail.get_by(id = email_id)
|
| 3005 |
chandransh |
56 |
if email:
|
|
|
57 |
email.delete()
|
| 1422 |
varun.gupt |
58 |
session.commit()
|
|
|
59 |
|
| 353 |
ashish |
60 |
def sendMail(mail):
|
|
|
61 |
if not mail:
|
|
|
62 |
raise HelperServiceException(101, "mail not present")
|
| 581 |
rajveer |
63 |
#msg = MIMEMultipart()
|
|
|
64 |
#mail = Mail()
|
| 928 |
rajveer |
65 |
if mail.sender:
|
|
|
66 |
mail.data = "This mail is sent by " + mail.sender + "\n" + mail.data
|
|
|
67 |
|
| 581 |
rajveer |
68 |
msg = MIMEText(mail.data)
|
| 900 |
rajveer |
69 |
msg['To'] = ', '.join( mail.to )
|
| 928 |
rajveer |
70 |
if mail.sender:
|
|
|
71 |
msg['From'] = mail.sender
|
|
|
72 |
else:
|
|
|
73 |
msg['From'] = "help@saholic.com"
|
| 900 |
rajveer |
74 |
msg['Subject'] = mail.subject
|
| 581 |
rajveer |
75 |
#msg.attach(mail.data)
|
| 353 |
ashish |
76 |
|
|
|
77 |
#handle attachments in mail
|
|
|
78 |
if mail.attachments:
|
|
|
79 |
for attach in mail.attachments:
|
|
|
80 |
|
|
|
81 |
part = MIMEBase('application', 'octet-stream')
|
|
|
82 |
part.set_payload(open(attach, 'rb').read())
|
|
|
83 |
encoders.encode_base64(part)
|
|
|
84 |
part.add_header('Content-Disposition',
|
|
|
85 |
'attachment; filename="%s"' % os.path.basename(attach))
|
|
|
86 |
msg.attach(part)
|
| 900 |
rajveer |
87 |
|
| 353 |
ashish |
88 |
for to in mail.to:
|
| 873 |
rajveer |
89 |
mail.sender = "help@shop2020.in"
|
|
|
90 |
mail.password = "5h0p2o2o"
|
| 353 |
ashish |
91 |
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
|
|
|
92 |
mailServer.ehlo()
|
|
|
93 |
mailServer.starttls()
|
|
|
94 |
mailServer.ehlo()
|
|
|
95 |
mailServer.login(mail.sender, mail.password)
|
|
|
96 |
mailServer.sendmail(mail.password, to, msg.as_string())
|
|
|
97 |
# Should be mailServer.quit(), but that crashes...
|
|
|
98 |
mailServer.close()
|
|
|
99 |
|
|
|
100 |
def sendText(text):
|
|
|
101 |
pass
|
|
|
102 |
|
|
|
103 |
def addMessage(message):
|
|
|
104 |
msg = Message.get_by(message_id=message.id)
|
|
|
105 |
if msg:
|
|
|
106 |
raise HelperServiceException(101, "Message is already present. Please try updation api instead")
|
|
|
107 |
msg = Message();
|
|
|
108 |
msg.message_id = message.id
|
|
|
109 |
msg.message = message.message
|
|
|
110 |
session.commit()
|
|
|
111 |
|
|
|
112 |
def getMessage(message_id):
|
|
|
113 |
msg = Message.get_by(id=message_id)
|
|
|
114 |
message = Msg()
|
|
|
115 |
message.id = msg.message_id
|
|
|
116 |
message.message = msg.message
|
|
|
117 |
return message
|
|
|
118 |
|
|
|
119 |
def updateMessage(id, message):
|
|
|
120 |
msg = Message.get_by(message_id=id)
|
|
|
121 |
if msg:
|
|
|
122 |
msg.message = message
|
|
|
123 |
session.commit()
|
|
|
124 |
else:
|
|
|
125 |
raise HelperServiceException(101, "message could not be found with id %d" %(id))
|
|
|
126 |
|
|
|
127 |
def getSubstitutedMessage(id, params):
|
|
|
128 |
#get the message first
|
|
|
129 |
msg = Message.get_by(message_id=id)
|
|
|
130 |
if not msg:
|
|
|
131 |
raise HelperServiceException(101, "message could not be found with id %d" %(id))
|
|
|
132 |
if params:
|
|
|
133 |
s = Template(msg.message)
|
|
|
134 |
s = s.safe_substitute(params)
|
|
|
135 |
return s
|
|
|
136 |
else:
|
|
|
137 |
return msg.message
|
| 494 |
rajveer |
138 |
|
|
|
139 |
def add_user(username, password, warehouseId):
|
|
|
140 |
user = DashboardUser()
|
|
|
141 |
user.username = username
|
|
|
142 |
user.password = password
|
|
|
143 |
user.warehouseId = warehouseId
|
|
|
144 |
user.addedOn = datetime.datetime.now()
|
|
|
145 |
user.status = 0
|
|
|
146 |
try:
|
|
|
147 |
session.commit()
|
|
|
148 |
return True
|
|
|
149 |
except:
|
|
|
150 |
raise HelperServiceException(101, "Some error while adding user")
|
|
|
151 |
return False
|
|
|
152 |
|
|
|
153 |
def delete_user(username):
|
|
|
154 |
user = DashboardUser.get_by(username=username)
|
|
|
155 |
if user is None:
|
|
|
156 |
return False
|
|
|
157 |
user.delete()
|
| 766 |
rajveer |
158 |
session.commit()
|
| 494 |
rajveer |
159 |
return True
|
|
|
160 |
|
| 2445 |
chandransh |
161 |
def authenticate_dashboard_user(username, password):
|
|
|
162 |
user = DashboardUser.get_by(username=username, password=password)
|
| 494 |
rajveer |
163 |
if user is None:
|
| 2445 |
chandransh |
164 |
raise HelperServiceException(101, "No dashboard user found")
|
| 494 |
rajveer |
165 |
|
| 2445 |
chandransh |
166 |
user.loggedOn = datetime.datetime.now()
|
|
|
167 |
session.commit()
|
|
|
168 |
return user.to_thrift_object()
|
|
|
169 |
|
| 494 |
rajveer |
170 |
def update_password(username, oldPassword, newPassword):
|
|
|
171 |
user = DashboardUser.get_by(username=username)
|
|
|
172 |
if user is None:
|
|
|
173 |
return False
|
|
|
174 |
if user.password == oldPassword:
|
|
|
175 |
user.password = newPassword
|
| 766 |
rajveer |
176 |
session.commit()
|
| 494 |
rajveer |
177 |
return True
|
| 766 |
rajveer |
178 |
return False
|
|
|
179 |
|
| 1891 |
ankur.sing |
180 |
def get_reports(role):
|
|
|
181 |
query = session.query(Report).join(ReportRoleAuthority)
|
|
|
182 |
query = query.filter(ReportRoleAuthority.role == role)
|
|
|
183 |
reports = query.all()
|
|
|
184 |
return reports
|
|
|
185 |
|
| 4806 |
varun.gupt |
186 |
def share_entities(entityIds, email):
|
|
|
187 |
if entityIds:
|
|
|
188 |
entityIdsStr = ''
|
| 5483 |
varun.gupt |
189 |
email_body = get_email_body_for_product_sharing(entityIds)
|
|
|
190 |
save_user_email_for_sending(email, '', 'Check out Saholic.com', email_body, '', 'MOBILE_SHARE')
|
| 4806 |
varun.gupt |
191 |
|
|
|
192 |
for entityId in entityIds:
|
|
|
193 |
entityIdsStr += str(entityId)
|
|
|
194 |
|
|
|
195 |
entities_to_be_shared = EntitiesShared()
|
|
|
196 |
entities_to_be_shared.entityIds = entityIdsStr
|
|
|
197 |
entities_to_be_shared.email = email
|
|
|
198 |
entities_to_be_shared.isEmailed = False
|
|
|
199 |
session.commit()
|
|
|
200 |
|
| 5483 |
varun.gupt |
201 |
def get_email_body_for_product_sharing(entityIds):
|
|
|
202 |
catalog_client = CatalogClient().get_client()
|
|
|
203 |
|
|
|
204 |
emailBody = '<html><body><p>Check out following products on Saholic:</p><ul>\n'
|
|
|
205 |
for entityId in entityIds:
|
|
|
206 |
list_items = catalog_client.getItemsByCatalogId(entityId)
|
|
|
207 |
url = 'http://www.saholic.com/entity/%s' % (entityId)
|
|
|
208 |
item = list_items[0]
|
|
|
209 |
name = item.brand + ' '
|
|
|
210 |
name += item.modelName + ' ' if item.modelName is not None else ''
|
|
|
211 |
name += item.modelNumber if item.modelNumber is not None else ''
|
|
|
212 |
emailBody += '<li><a href="%s">%s</a></li>' % (url, name)
|
|
|
213 |
emailBody += '</ul></body></html>'
|
|
|
214 |
return emailBody
|
|
|
215 |
|
| 4806 |
varun.gupt |
216 |
def save_quick_link(url, text):
|
|
|
217 |
quickLink = QuickLink()
|
|
|
218 |
quickLink.url = url
|
|
|
219 |
quickLink.text = text
|
|
|
220 |
session.commit()
|
|
|
221 |
|
|
|
222 |
def get_quick_links():
|
|
|
223 |
return QuickLink.query.all()
|
|
|
224 |
|
| 4996 |
varun.gupt |
225 |
def update_quicklink(id, url, text):
|
|
|
226 |
quicklink = QuickLink.get_by(id = id)
|
|
|
227 |
quicklink.url = url
|
|
|
228 |
quicklink.text = text
|
|
|
229 |
session.commit()
|
|
|
230 |
|
| 5110 |
mandeep.dh |
231 |
def update_password_for_agent(agentEmailId, password):
|
|
|
232 |
agent = Agent.get_by(emailId = agentEmailId)
|
|
|
233 |
agent.password = password
|
|
|
234 |
session.commit()
|
|
|
235 |
|
| 5055 |
varun.gupt |
236 |
def get_emails_for_notifications_sent(start_datetime, end_datetime):
|
|
|
237 |
query = UserEmailArchive.query.filter(UserEmailArchive.emailType == 'ProductNotification')
|
|
|
238 |
query = query.filter(UserEmailArchive.timestamp >= start_datetime)
|
|
|
239 |
query = query.filter(UserEmailArchive.timestamp <= end_datetime)
|
|
|
240 |
return query.all()
|
| 6322 |
amar.kumar |
241 |
def get_order_confirmation_mail(order_id):
|
|
|
242 |
email = UserEmailArchive.get_by(emailType = 'TransactionInfo', source = order_id)
|
| 7643 |
manish.sha |
243 |
#Start:- Added by Manish Sharma for resolving Exception for getting order delivery and confirmation mail on 27-Jun-2013
|
|
|
244 |
if email:
|
|
|
245 |
return email.body
|
|
|
246 |
else:
|
|
|
247 |
return ''
|
|
|
248 |
#End:- Added by Manish Sharma for resolving Exception for getting order delivery and confirmation mail on 27-Jun-2013
|
| 6322 |
amar.kumar |
249 |
|
| 7221 |
kshitij.so |
250 |
def get_order_delivery_mail(order_id):
|
|
|
251 |
email = UserEmailArchive.get_by(emailType = 'DeliverySuccess', source = order_id)
|
| 7643 |
manish.sha |
252 |
#Start:- Added by Manish Sharma for resolving Exception for getting order delivery and confirmation mail on 27-Jun-2013
|
|
|
253 |
if email:
|
|
|
254 |
return email.body
|
|
|
255 |
else:
|
|
|
256 |
return ''
|
|
|
257 |
#End:- Added by Manish Sharma for resolving Exception for getting order delivery and confirmation mail on 27-Jun-2013
|
| 7221 |
kshitij.so |
258 |
|
| 7410 |
amar.kumar |
259 |
def get_warehouseIds_for_agent(agent_emailId):
|
|
|
260 |
agent = Agent.get_by(emailId = agent_emailId)
|
|
|
261 |
agent_warehouse_mappings = AgentWarehouseMapping.query.filter(AgentWarehouseMapping.agentId == agent.id).all()
|
|
|
262 |
warehouseIds = []
|
|
|
263 |
for mapping in agent_warehouse_mappings:
|
|
|
264 |
try:
|
|
|
265 |
warehouseIds.append(mapping.warehouseId)
|
|
|
266 |
except:
|
|
|
267 |
raise HelperServiceException(108, "Exception while getting warehouseIds for Agent")
|
|
|
268 |
return warehouseIds
|
|
|
269 |
|
| 766 |
rajveer |
270 |
def close_session():
|
|
|
271 |
if session.is_active:
|
|
|
272 |
print "session is active. closing it."
|
| 3376 |
rajveer |
273 |
session.close()
|
|
|
274 |
|
|
|
275 |
def is_alive():
|
|
|
276 |
try:
|
|
|
277 |
return True
|
|
|
278 |
except:
|
|
|
279 |
return False
|