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