| 130 |
ashish |
1 |
'''
|
|
|
2 |
Created on 28-Apr-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
|
|
6 |
from shop2020.model.v1.user.impl import Dataservice
|
| 2981 |
rajveer |
7 |
from shop2020.utils.Utils import log_entry, to_py_date
|
|
|
8 |
from shop2020.thriftpy.model.v1.user.ttypes import WidgetType as WType
|
| 1273 |
varun.gupt |
9 |
from shop2020.model.v1.user.impl.Dataservice import User, UserCommunication, IPMap,\
|
| 1845 |
vikas |
10 |
Address, SocialHandle, UserState, InternalInfo, SocialService, Affiliate, Tracker, TrackLog,\
|
| 2981 |
rajveer |
11 |
MasterAffiliate, UserNote, UserWidgetItem
|
| 130 |
ashish |
12 |
from shop2020.thriftpy.model.v1.user.ttypes import UserContextException,\
|
| 557 |
chandransh |
13 |
AuthenticationException, AccountStatus, Sex
|
| 130 |
ashish |
14 |
from elixir import session
|
| 2981 |
rajveer |
15 |
import datetime
|
|
|
16 |
from sqlalchemy import desc
|
| 130 |
ashish |
17 |
|
| 2981 |
rajveer |
18 |
|
| 3187 |
rajveer |
19 |
def initialize(dbname='user', db_hostname="localhost"):
|
| 130 |
ashish |
20 |
log_entry("initialize@DataAccessor", "Initializing data service")
|
| 3187 |
rajveer |
21 |
Dataservice.initialize(dbname, db_hostname)
|
| 557 |
chandransh |
22 |
|
|
|
23 |
def create_anonymous_user(jsession_id, cart):
|
| 576 |
chandransh |
24 |
user=User.get_by(jsession_id=jsession_id)
|
|
|
25 |
if not user is None:
|
|
|
26 |
return user
|
| 557 |
chandransh |
27 |
user = User()
|
|
|
28 |
anonymous_str = "anonymous"
|
| 576 |
chandransh |
29 |
user.email = jsession_id + "@anonymous.com"
|
| 557 |
chandransh |
30 |
user.password = anonymous_str
|
|
|
31 |
user.name = anonymous_str
|
|
|
32 |
user.communication_email = jsession_id + "@anonymous.com"
|
|
|
33 |
user.jsession_id = jsession_id
|
|
|
34 |
user.is_anonymous = True
|
|
|
35 |
user.sex = Sex.WONT_SAY
|
|
|
36 |
user.active_cart_id = cart.id
|
|
|
37 |
#initialize userState
|
|
|
38 |
user_state = UserState()
|
|
|
39 |
user_state.is_email_verified = False
|
|
|
40 |
user_state.is_sms_verified = False
|
|
|
41 |
user_state.account_status = AccountStatus.ACTIVE
|
|
|
42 |
user_state.ip_list = []
|
| 2045 |
chandransh |
43 |
user_state.active_since = datetime.datetime.now()
|
| 557 |
chandransh |
44 |
user_state.user = user
|
| 2747 |
chandransh |
45 |
session.commit()
|
| 557 |
chandransh |
46 |
|
|
|
47 |
cart.user_id = user.id
|
|
|
48 |
session.commit()
|
| 130 |
ashish |
49 |
|
| 557 |
chandransh |
50 |
return user
|
|
|
51 |
|
|
|
52 |
def get_user_by_id(user_id):
|
|
|
53 |
return User.get_by(id=user_id)
|
|
|
54 |
|
| 3032 |
mandeep.dh |
55 |
def get_user_by_mobile_number(mobile_number):
|
|
|
56 |
return User.get_by(mobile_number=mobile_number)
|
|
|
57 |
|
| 1491 |
vikas |
58 |
def get_user_by_email(email):
|
|
|
59 |
return User.get_by(email=email)
|
|
|
60 |
|
| 557 |
chandransh |
61 |
def create_user(user_to_add, cart):
|
|
|
62 |
if user_to_add.userId:
|
| 130 |
ashish |
63 |
raise UserContextException(109, "id is set, user cannot be created")
|
| 557 |
chandransh |
64 |
user = User()
|
|
|
65 |
user.email = user_to_add.email
|
|
|
66 |
user.password = user_to_add.password
|
|
|
67 |
user.name = user_to_add.name
|
|
|
68 |
user.communication_email = user_to_add.communicationEmail
|
|
|
69 |
user.jsession_id = user_to_add.jsessionId
|
|
|
70 |
user.is_anonymous = False
|
|
|
71 |
user.sex = user_to_add.sex
|
| 567 |
rajveer |
72 |
user.date_of_birth = user_to_add.dateOfBirth
|
| 557 |
chandransh |
73 |
user.active_cart_id = cart.id
|
| 567 |
rajveer |
74 |
user.mobile_number = user_to_add.mobileNumber
|
| 2020 |
vikas |
75 |
user.source = user_to_add.source
|
| 2815 |
vikas |
76 |
user.source_start_time = to_py_date(user_to_add.sourceStartTime)
|
| 513 |
rajveer |
77 |
|
| 130 |
ashish |
78 |
#initialize userState
|
| 557 |
chandransh |
79 |
user_state = UserState()
|
| 130 |
ashish |
80 |
user_state.is_email_verified = False
|
|
|
81 |
user_state.is_sms_verified = False
|
|
|
82 |
user_state.account_status = AccountStatus.ACTIVE
|
| 132 |
ashish |
83 |
user_state.ip_list = []
|
| 130 |
ashish |
84 |
user_state.active_since = datetime.datetime.now()
|
| 557 |
chandransh |
85 |
user_state.user = user
|
| 1607 |
vikas |
86 |
session.commit();
|
| 130 |
ashish |
87 |
|
| 557 |
chandransh |
88 |
#Now create the user phones. Mostly, this should only be a mobile number
|
|
|
89 |
|
|
|
90 |
cart.user_id = user.id
|
| 130 |
ashish |
91 |
session.commit()
|
| 404 |
rajveer |
92 |
|
| 557 |
chandransh |
93 |
return user
|
| 130 |
ashish |
94 |
|
|
|
95 |
#===============================================================================
|
|
|
96 |
# Need to provide the update apis here for relevant fields in PrimaryInfo.
|
|
|
97 |
#===============================================================================
|
| 557 |
chandransh |
98 |
def update_user(user_to_update):
|
| 594 |
rajveer |
99 |
if not user_to_update.userId:
|
| 415 |
ashish |
100 |
raise UserContextException(110, "user does not exist")
|
| 594 |
rajveer |
101 |
user = get_user_by_id(user_to_update.userId)
|
|
|
102 |
user.email = user_to_update.email
|
|
|
103 |
user.password = user_to_update.password
|
|
|
104 |
user.name = user_to_update.name
|
|
|
105 |
user.communication_email = user_to_update.communicationEmail
|
|
|
106 |
user.jsession_id = user_to_update.jsessionId
|
|
|
107 |
user.is_anonymous = user_to_update.isAnonymous
|
|
|
108 |
user.sex = user_to_update.sex
|
|
|
109 |
user.date_of_birth = user_to_update.dateOfBirth
|
|
|
110 |
user.active_cart_id = user_to_update.activeCartId
|
|
|
111 |
user.mobile_number = user_to_update.mobileNumber
|
| 415 |
ashish |
112 |
session.commit()
|
| 594 |
rajveer |
113 |
return user
|
| 415 |
ashish |
114 |
|
| 557 |
chandransh |
115 |
def delete_user(user_id):
|
|
|
116 |
user = get_user_by_id(user_id)
|
| 130 |
ashish |
117 |
|
|
|
118 |
if not user:
|
|
|
119 |
raise_user_exception(user_id)
|
| 557 |
chandransh |
120 |
|
|
|
121 |
user.state.account_status = AccountStatus.DELETED
|
|
|
122 |
session.commit()
|
|
|
123 |
return True
|
| 130 |
ashish |
124 |
|
| 557 |
chandransh |
125 |
def get_user_state(user_id):
|
| 766 |
rajveer |
126 |
userstate = UserState.get_by(user_id=user_id)
|
|
|
127 |
return userstate
|
| 130 |
ashish |
128 |
|
| 557 |
chandransh |
129 |
def get_internal_info(user_id):
|
| 766 |
rajveer |
130 |
info = InternalInfo.get_by(user_id=user_id)
|
|
|
131 |
return info
|
| 130 |
ashish |
132 |
|
| 557 |
chandransh |
133 |
def authenticate_user(user_handle, password):
|
|
|
134 |
user = User.get_by(email=user_handle)
|
| 130 |
ashish |
135 |
if not user:
|
| 557 |
chandransh |
136 |
raise AuthenticationException("This email address is not registered.", 102)
|
| 130 |
ashish |
137 |
|
| 557 |
chandransh |
138 |
if user.password == get_db_password(password):
|
|
|
139 |
return user
|
| 130 |
ashish |
140 |
else:
|
|
|
141 |
raise AuthenticationException("Wrong username or password", 102)
|
|
|
142 |
|
|
|
143 |
def user_exists(email):
|
| 413 |
rajveer |
144 |
try:
|
| 557 |
chandransh |
145 |
user = User.get_by(email=email)
|
|
|
146 |
if user:
|
|
|
147 |
return True
|
|
|
148 |
else:
|
|
|
149 |
return False
|
| 413 |
rajveer |
150 |
except:
|
| 130 |
ashish |
151 |
return False
|
|
|
152 |
|
| 567 |
rajveer |
153 |
def add_address_for_user(address, user_id, set_default):
|
| 557 |
chandransh |
154 |
user = get_user_by_id(user_id)
|
| 766 |
rajveer |
155 |
|
| 130 |
ashish |
156 |
if not user:
|
|
|
157 |
raise_user_exception(user_id)
|
|
|
158 |
if not address:
|
|
|
159 |
raise UserContextException(103,"Address cannot be null")
|
|
|
160 |
|
|
|
161 |
address_to_add = Address()
|
|
|
162 |
address_to_add.line_1 = address.line1
|
|
|
163 |
address_to_add.line_2 = address.line2
|
|
|
164 |
address_to_add.landmark = address.landmark
|
|
|
165 |
address_to_add.city = address.city
|
|
|
166 |
address_to_add.country = address.country
|
|
|
167 |
address_to_add.state = address.state
|
|
|
168 |
address_to_add.pin = address.pin
|
|
|
169 |
address_to_add.type = address.type
|
| 414 |
ashish |
170 |
address_to_add.name = address.name
|
|
|
171 |
address_to_add.phone = address.phone
|
| 130 |
ashish |
172 |
address_to_add.added_on = to_py_date(address.addedOn)
|
|
|
173 |
address_to_add.enabled = True
|
| 557 |
chandransh |
174 |
address_to_add.user = user
|
| 130 |
ashish |
175 |
session.commit()
|
| 509 |
rajveer |
176 |
|
| 557 |
chandransh |
177 |
if set_default is True:
|
|
|
178 |
user.default_address_id = address_to_add.id
|
| 567 |
rajveer |
179 |
#set default address if nothing is default
|
|
|
180 |
if user.default_address_id is None:
|
|
|
181 |
user.default_address_id = address_to_add.id
|
|
|
182 |
|
| 557 |
chandransh |
183 |
session.commit()
|
| 513 |
rajveer |
184 |
|
| 567 |
rajveer |
185 |
return address_to_add.id
|
| 513 |
rajveer |
186 |
|
| 130 |
ashish |
187 |
def remove_address_for_user(user_id, address_id):
|
|
|
188 |
address = get_address(address_id)
|
|
|
189 |
|
|
|
190 |
if not address:
|
| 557 |
chandransh |
191 |
raise UserContextException(103, "Address not found")
|
|
|
192 |
if address.user.id != user_id:
|
|
|
193 |
raise UserContextException(104, "This address belongs to some other user")
|
| 130 |
ashish |
194 |
|
|
|
195 |
address.enabled = False
|
|
|
196 |
session.commit()
|
|
|
197 |
return True
|
|
|
198 |
|
|
|
199 |
def set_user_as_logged_in(user_id, time_stamp):
|
| 557 |
chandransh |
200 |
user_state = UserState.get_by(user_id=user_id)
|
|
|
201 |
#user = get_user_by_id(user_id)
|
| 130 |
ashish |
202 |
|
| 557 |
chandransh |
203 |
if not user_state:
|
|
|
204 |
raise_user_exception(user_id)
|
| 130 |
ashish |
205 |
|
|
|
206 |
if not time_stamp:
|
| 557 |
chandransh |
207 |
user_state.last_login = datetime.datetime.now()
|
|
|
208 |
else:
|
|
|
209 |
user_state.last_login = to_py_date(time_stamp)
|
| 130 |
ashish |
210 |
session.commit()
|
|
|
211 |
return True
|
|
|
212 |
|
| 557 |
chandransh |
213 |
def set_user_as_logged_out(user_id, time_stamp):
|
|
|
214 |
user_state = UserState.get_by(user_id=user_id)
|
| 130 |
ashish |
215 |
|
| 557 |
chandransh |
216 |
if not user_state:
|
| 130 |
ashish |
217 |
raise_user_exception(user_id)
|
|
|
218 |
|
|
|
219 |
if not time_stamp:
|
| 557 |
chandransh |
220 |
user_state.last_logout = datetime.datetime.now()
|
|
|
221 |
else:
|
|
|
222 |
user_state.last_logout = to_py_date(time_stamp)
|
| 130 |
ashish |
223 |
session.commit()
|
|
|
224 |
return True
|
|
|
225 |
|
| 557 |
chandransh |
226 |
def set_default_address(user_id, address_id):
|
|
|
227 |
user = get_user_by_id(user_id)
|
|
|
228 |
address = Address.get_by(id=address_id)
|
|
|
229 |
if not user:
|
|
|
230 |
raise_user_exception(user_id)
|
|
|
231 |
if not address:
|
|
|
232 |
raise UserContextException(103, "Address not found")
|
|
|
233 |
if address.user.id != user.id:
|
|
|
234 |
raise UserContextException(104, "This address belongs to some other user")
|
|
|
235 |
|
|
|
236 |
user.default_address_id = address_id
|
|
|
237 |
session.commit()
|
|
|
238 |
|
| 594 |
rajveer |
239 |
def update_password(user_id, old_password, new_password):
|
| 557 |
chandransh |
240 |
user = get_user_by_id(user_id)
|
| 130 |
ashish |
241 |
|
|
|
242 |
if not user:
|
|
|
243 |
raise_user_exception(user_id)
|
|
|
244 |
|
| 594 |
rajveer |
245 |
if user.password != old_password:
|
|
|
246 |
return False
|
|
|
247 |
|
|
|
248 |
if check_for_valid_password(new_password):
|
|
|
249 |
user.password = get_db_password(new_password)
|
| 130 |
ashish |
250 |
session.commit()
|
|
|
251 |
return True
|
|
|
252 |
else:
|
|
|
253 |
return False
|
| 1273 |
varun.gupt |
254 |
|
|
|
255 |
|
|
|
256 |
def create_user_communication(user_id, email, communication_type, order_id, awb, product, subject, message):
|
|
|
257 |
|
|
|
258 |
user_communication = UserCommunication()
|
|
|
259 |
user_communication.user_id = user_id
|
|
|
260 |
user_communication.communication_type = communication_type
|
| 1743 |
varun.gupt |
261 |
|
|
|
262 |
if order_id > 0:
|
|
|
263 |
user_communication.order_id = order_id
|
| 1273 |
varun.gupt |
264 |
user_communication.airwaybill_no = awb
|
|
|
265 |
user_communication.reply_to = email
|
|
|
266 |
user_communication.product_name = product
|
|
|
267 |
user_communication.subject = subject
|
|
|
268 |
user_communication.message = message
|
| 1301 |
varun.gupt |
269 |
user_communication.communication_timestamp = datetime.datetime.now()
|
| 1273 |
varun.gupt |
270 |
session.commit()
|
| 3206 |
mandeep.dh |
271 |
return True
|
|
|
272 |
|
| 1583 |
varun.gupt |
273 |
def get_user_communication_by_id(user_communication_id):
|
|
|
274 |
return UserCommunication.get_by(id = user_communication_id)
|
|
|
275 |
|
|
|
276 |
def get_user_communication_by_user(user_communication_user_id):
|
| 1607 |
vikas |
277 |
return UserCommunication.query.filter_by(user_id = user_communication_user_id).order_by(-UserCommunication.id).all()
|
| 1583 |
varun.gupt |
278 |
|
|
|
279 |
def get_all_user_communications():
|
| 1863 |
vikas |
280 |
return UserCommunication.query.order_by(-UserCommunication.id).all()
|
| 1583 |
varun.gupt |
281 |
|
| 1859 |
vikas |
282 |
def create_master_affiliate(name, added_on):
|
| 1845 |
vikas |
283 |
master_affiliate = MasterAffiliate()
|
|
|
284 |
master_affiliate.name = name
|
| 1859 |
vikas |
285 |
master_affiliate.added_on = to_py_date(added_on)
|
| 1845 |
vikas |
286 |
session.commit()
|
|
|
287 |
return master_affiliate
|
|
|
288 |
|
| 1899 |
vikas |
289 |
def get_all_master_affiliates():
|
|
|
290 |
return MasterAffiliate.query.all()
|
|
|
291 |
|
| 1845 |
vikas |
292 |
def get_master_affiliate_by_id(id):
|
|
|
293 |
return MasterAffiliate.get_by(id = id)
|
|
|
294 |
|
|
|
295 |
def get_master_affiliate_by_name(name):
|
|
|
296 |
return MasterAffiliate.get_by(name = name)
|
|
|
297 |
|
| 1859 |
vikas |
298 |
def create_affiliate(name, url, master_affiliate_id, added_on):
|
| 1845 |
vikas |
299 |
affiliate = Affiliate()
|
|
|
300 |
affiliate.name = name
|
|
|
301 |
if url is not None:
|
|
|
302 |
affiliate.url = url
|
|
|
303 |
affiliate.master_affiliate_id = master_affiliate_id
|
| 1859 |
vikas |
304 |
affiliate.added_on = to_py_date(added_on)
|
| 1845 |
vikas |
305 |
session.commit()
|
|
|
306 |
return affiliate
|
|
|
307 |
|
|
|
308 |
def get_affiliate_by_id(id):
|
|
|
309 |
return Affiliate.get_by(id = id)
|
|
|
310 |
|
|
|
311 |
def get_affiliate_by_name(name):
|
|
|
312 |
return Affiliate.get_by(name = name)
|
|
|
313 |
|
|
|
314 |
def get_affiliates_by_master_affiliate(master_affiliate_id):
|
|
|
315 |
return MasterAffiliate.get_by(id = master_affiliate_id).affiliates
|
|
|
316 |
|
|
|
317 |
def get_tracker_by_id(id):
|
|
|
318 |
return Tracker.get_by(id = id)
|
|
|
319 |
|
| 1996 |
vikas |
320 |
def add_track_log(affiliate_id, user_id, event, url, data, added_on):
|
| 1845 |
vikas |
321 |
track_log = TrackLog()
|
| 1996 |
vikas |
322 |
track_log.affiliate_id = affiliate_id
|
| 1845 |
vikas |
323 |
if user_id:
|
|
|
324 |
track_log.user_id = user_id
|
| 3378 |
vikas |
325 |
track_log.event_id = event
|
| 1845 |
vikas |
326 |
if url:
|
|
|
327 |
track_log.url = url
|
|
|
328 |
if data:
|
| 1859 |
vikas |
329 |
track_log.data = data
|
|
|
330 |
track_log.added_on = to_py_date(added_on)
|
| 1845 |
vikas |
331 |
session.commit()
|
|
|
332 |
return track_log.id
|
|
|
333 |
|
|
|
334 |
def get_track_log_by_id(id):
|
|
|
335 |
return TrackLog.get_by(id = id)
|
|
|
336 |
|
| 3293 |
vikas |
337 |
def get_track_logs_by_affiliate(affiliate_id, start_date, end_date):
|
|
|
338 |
query = TrackLog.query
|
|
|
339 |
|
|
|
340 |
if affiliate_id:
|
|
|
341 |
query = query.filter(TrackLog.affiliate_id == affiliate_id)
|
|
|
342 |
if start_date:
|
|
|
343 |
query = query.filter(TrackLog.added_on >= to_py_date(start_date))
|
|
|
344 |
if end_date:
|
|
|
345 |
query = query.filter(TrackLog.added_on <= to_py_date(end_date))
|
|
|
346 |
return query.all()
|
| 1845 |
vikas |
347 |
|
|
|
348 |
def get_track_logs_by_user(user_id):
|
|
|
349 |
return TrackLog.query.filter(TrackLog.user_id == user_id).all()
|
|
|
350 |
|
| 1996 |
vikas |
351 |
def get_track_logs(affiliate_id, user_id, event, url):
|
| 1845 |
vikas |
352 |
query = TrackLog.query
|
|
|
353 |
|
| 1996 |
vikas |
354 |
if affiliate_id:
|
|
|
355 |
query = query.filter(TrackLog.affiliate_id == affiliate_id)
|
|
|
356 |
if user_id:
|
|
|
357 |
query = query.filter(TrackLog.user_id == user_id)
|
| 1845 |
vikas |
358 |
if event:
|
|
|
359 |
query = query.filter(TrackLog.event == event)
|
|
|
360 |
if url:
|
|
|
361 |
query = query.filter(TrackLog.url == url)
|
|
|
362 |
return query.all()
|
|
|
363 |
|
|
|
364 |
|
| 557 |
chandransh |
365 |
def get_address(address_id):
|
|
|
366 |
address = Address.get_by(id=address_id)
|
|
|
367 |
return address
|
|
|
368 |
|
|
|
369 |
def get_social_service(service_id):
|
|
|
370 |
service = SocialService.get_by(service_id)
|
|
|
371 |
return service
|
|
|
372 |
|
|
|
373 |
def add_ip_address_for_user(ip_address, time_stamp, user_id):
|
|
|
374 |
user = get_user_by_id(user_id)
|
| 130 |
ashish |
375 |
if not user:
|
|
|
376 |
raise_user_exception(user_id)
|
| 557 |
chandransh |
377 |
#user exists create an IP address object
|
|
|
378 |
ip_address = IPMap()
|
|
|
379 |
ip_address.ip = ip_address
|
|
|
380 |
ip_address.timestamp = to_py_date(time_stamp)
|
|
|
381 |
ip_address.user_state = user.state
|
| 130 |
ashish |
382 |
session.commit()
|
|
|
383 |
return True
|
|
|
384 |
|
|
|
385 |
def get_social_service_by_name(service_name):
|
|
|
386 |
service = SocialService.get_by(name=service_name)
|
|
|
387 |
|
|
|
388 |
if not service:
|
|
|
389 |
raise UserContextException(106, "No such social service exists")
|
|
|
390 |
return service
|
|
|
391 |
|
|
|
392 |
def add_social_handle(user_id, social_service, handle):
|
| 557 |
chandransh |
393 |
user = get_user_by_id(user_id)
|
| 130 |
ashish |
394 |
|
|
|
395 |
if not user:
|
|
|
396 |
raise_user_exception(user_id)
|
|
|
397 |
|
|
|
398 |
service = get_social_service_by_name(social_service)
|
|
|
399 |
|
|
|
400 |
#get if use already has this service.
|
| 557 |
chandransh |
401 |
social_handle = SocialHandle.filter(service=service).filter(user=user).one()
|
| 130 |
ashish |
402 |
if not social_handle:
|
|
|
403 |
#create a new handle
|
|
|
404 |
social_handle = SocialHandle()
|
|
|
405 |
social_handle.service = service
|
| 557 |
chandransh |
406 |
social_handle.user = user
|
| 130 |
ashish |
407 |
social_handle.handle = handle
|
|
|
408 |
else:
|
|
|
409 |
social_handle.handle = handle
|
| 557 |
chandransh |
410 |
session.commit()
|
| 130 |
ashish |
411 |
return True
|
| 557 |
chandransh |
412 |
|
| 884 |
rajveer |
413 |
|
|
|
414 |
def forgot_password(email, password):
|
|
|
415 |
try:
|
|
|
416 |
user = User.get_by(email=email)
|
|
|
417 |
if user:
|
|
|
418 |
user.password = password
|
| 900 |
rajveer |
419 |
session.commit()
|
| 884 |
rajveer |
420 |
return True
|
|
|
421 |
else:
|
|
|
422 |
return False
|
|
|
423 |
except:
|
|
|
424 |
return False
|
|
|
425 |
|
|
|
426 |
|
|
|
427 |
'''
|
| 581 |
rajveer |
428 |
def forgot_password(email):
|
|
|
429 |
try:
|
|
|
430 |
user = User.get_by(email=email)
|
|
|
431 |
if user:
|
| 873 |
rajveer |
432 |
new_password = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(8))
|
| 581 |
rajveer |
433 |
mail = Mail()
|
|
|
434 |
mail.to = []
|
|
|
435 |
mail.to.append(email)
|
|
|
436 |
mail.sender = ""
|
|
|
437 |
mail.password = ""
|
|
|
438 |
mail.data = "Your new password is " + user.password
|
|
|
439 |
helper_client = HelperClient()
|
|
|
440 |
helper_client.__start__()
|
|
|
441 |
client = helper_client.get_client()
|
|
|
442 |
client.sendMail(mail);
|
| 873 |
rajveer |
443 |
|
| 581 |
rajveer |
444 |
return True
|
|
|
445 |
else:
|
|
|
446 |
return False
|
|
|
447 |
except:
|
|
|
448 |
return False
|
| 884 |
rajveer |
449 |
'''
|
| 581 |
rajveer |
450 |
|
| 594 |
rajveer |
451 |
def get_all_addresses_for_user(userId):
|
| 2895 |
chandransh |
452 |
return Address.query.filter_by(user_id = userId, enabled=True).all()
|
| 581 |
rajveer |
453 |
|
| 785 |
rajveer |
454 |
def get_default_address_id(userId):
|
| 594 |
rajveer |
455 |
user = get_user_by_id(userId);
|
| 840 |
chandransh |
456 |
if user.default_address_id is None:
|
|
|
457 |
return 0
|
| 594 |
rajveer |
458 |
return user.default_address_id
|
| 581 |
rajveer |
459 |
|
| 785 |
rajveer |
460 |
def get_default_pincode(user_id):
|
|
|
461 |
user = get_user_by_id(user_id)
|
|
|
462 |
if not user:
|
|
|
463 |
raise_user_exception(user_id)
|
|
|
464 |
default_address_id = user.default_address_id
|
|
|
465 |
if default_address_id:
|
|
|
466 |
address = Address.get_by(id=default_address_id)
|
|
|
467 |
default_pincode = address.pin
|
|
|
468 |
else:
|
|
|
469 |
default_pincode = '110001'
|
|
|
470 |
return default_pincode
|
| 1596 |
ankur.sing |
471 |
|
|
|
472 |
def get_user_count(user_type):
|
|
|
473 |
if user_type is None:
|
|
|
474 |
return User.query.count()
|
|
|
475 |
else:
|
|
|
476 |
return User.query.filter_by(is_anonymous = user_type).count()
|
| 785 |
rajveer |
477 |
|
| 1891 |
ankur.sing |
478 |
def get_users(user_type, start_date, end_date):
|
|
|
479 |
query = session.query(User).join(UserState)
|
|
|
480 |
if start_date != -1:
|
|
|
481 |
query = query.filter(UserState.active_since >= to_py_date(start_date))
|
|
|
482 |
if end_date != -1:
|
|
|
483 |
query = query.filter(UserState.active_since <= to_py_date(end_date))
|
|
|
484 |
if user_type is not None:
|
|
|
485 |
query = query.filter(User.is_anonymous == user_type)
|
|
|
486 |
return query.all()
|
| 1673 |
ankur.sing |
487 |
|
| 130 |
ashish |
488 |
#=============================================================================
|
|
|
489 |
# Helper functions
|
|
|
490 |
#=============================================================================
|
|
|
491 |
|
|
|
492 |
'''
|
|
|
493 |
This function returns the password as stored in the db
|
|
|
494 |
'''
|
|
|
495 |
def get_db_password(password):
|
|
|
496 |
return password
|
|
|
497 |
|
|
|
498 |
def check_for_valid_password(password):
|
|
|
499 |
if not password:
|
|
|
500 |
raise UserContextException(105,"password cannot be null")
|
|
|
501 |
return True
|
|
|
502 |
#------------------------------------------------------------------------------
|
|
|
503 |
|
|
|
504 |
#===============================================================================
|
|
|
505 |
# raises the UserContextException
|
|
|
506 |
#===============================================================================
|
|
|
507 |
def raise_user_exception(user_id):
|
| 766 |
rajveer |
508 |
raise UserContextException(101, "no such user in system %d" %(user_id))
|
|
|
509 |
|
| 2641 |
varun.gupt |
510 |
'''
|
|
|
511 |
My Notes methods
|
|
|
512 |
'''
|
|
|
513 |
def get_user_notes(user_id, entity_id):
|
|
|
514 |
return UserNote.query.filter_by(user_id = user_id).filter_by(entity_id = entity_id).all()
|
|
|
515 |
|
| 2717 |
varun.gupt |
516 |
def put_user_note(user_id, entity_id, slide, note):
|
|
|
517 |
user_notes = UserNote.query.filter_by(user_id = user_id).filter_by(entity_id = entity_id).filter_by(slide = slide).all()
|
| 2641 |
varun.gupt |
518 |
|
|
|
519 |
if user_notes is None or len(user_notes) == 0:
|
|
|
520 |
user_note = UserNote()
|
|
|
521 |
user_note.user_id = user_id
|
|
|
522 |
user_note.entity_id = entity_id
|
| 2717 |
varun.gupt |
523 |
user_note.slide = slide
|
| 2641 |
varun.gupt |
524 |
user_note.note = note
|
|
|
525 |
else:
|
|
|
526 |
user_note = user_notes.pop()
|
|
|
527 |
user_note.note = note
|
|
|
528 |
|
|
|
529 |
session.commit()
|
|
|
530 |
|
| 2981 |
rajveer |
531 |
|
|
|
532 |
def get_my_research_items(userId):
|
|
|
533 |
query = UserWidgetItem.query.filter_by(userId=userId)
|
|
|
534 |
query = query.filter_by(widgetId=WType.MY_RESEARCH)
|
|
|
535 |
query = query.order_by(desc(UserWidgetItem.addedOn))
|
|
|
536 |
widgetItems = query.all()
|
|
|
537 |
return [widgetItem.itemId for widgetItem in widgetItems]
|
|
|
538 |
|
|
|
539 |
def get_browse_history_items(userId):
|
|
|
540 |
query = UserWidgetItem.query.filter_by(userId=userId)
|
|
|
541 |
query = query.filter_by(widgetId=WType.BROWSE_HISTORY)
|
|
|
542 |
query = query.order_by(desc(UserWidgetItem.addedOn)).limit(10)
|
|
|
543 |
widgetItems = query.all()
|
|
|
544 |
return [widgetItem.itemId for widgetItem in widgetItems]
|
|
|
545 |
|
|
|
546 |
def update_my_research(userId, itemId):
|
|
|
547 |
isNew = False
|
|
|
548 |
query = UserWidgetItem.query.filter_by(userId=userId)
|
|
|
549 |
query = query.filter_by(widgetId=WType.MY_RESEARCH)
|
|
|
550 |
query = query.filter_by(itemId=itemId)
|
|
|
551 |
widgetItem = query.first()
|
|
|
552 |
if not widgetItem:
|
|
|
553 |
isNew = True
|
|
|
554 |
widgetItem = UserWidgetItem()
|
|
|
555 |
widgetItem.userId = userId
|
|
|
556 |
widgetItem.widgetId = WType.MY_RESEARCH
|
|
|
557 |
widgetItem.itemId = itemId
|
|
|
558 |
widgetItem.addedOn = datetime.datetime.now()
|
|
|
559 |
session.commit()
|
|
|
560 |
return isNew
|
|
|
561 |
|
|
|
562 |
def update_browse_history(userId, itemId):
|
|
|
563 |
query = UserWidgetItem.query.filter_by(userId=userId)
|
|
|
564 |
query = query.filter_by(widgetId=WType.BROWSE_HISTORY)
|
|
|
565 |
query = query.filter_by(itemId=itemId)
|
|
|
566 |
widgetItem = query.first()
|
|
|
567 |
if not widgetItem:
|
|
|
568 |
widgetItem = UserWidgetItem()
|
|
|
569 |
widgetItem.userId = userId
|
|
|
570 |
widgetItem.widgetId = WType.BROWSE_HISTORY
|
|
|
571 |
widgetItem.itemId = itemId
|
|
|
572 |
widgetItem.addedOn = datetime.datetime.now()
|
|
|
573 |
session.commit()
|
|
|
574 |
|
|
|
575 |
def delete_item_from_my_research(userId, itemId):
|
|
|
576 |
query = UserWidgetItem.query.filter_by(userId=userId)
|
|
|
577 |
query = query.filter_by(widgetId=WType.MY_RESEARCH)
|
|
|
578 |
query = query.filter_by(itemId=itemId)
|
|
|
579 |
widgetItem = query.first()
|
|
|
580 |
if widgetItem:
|
|
|
581 |
widgetItem.delete()
|
|
|
582 |
session.commit()
|
|
|
583 |
else:
|
|
|
584 |
print "Not item in my research to delete with itemId" + itemId
|
|
|
585 |
|
| 766 |
rajveer |
586 |
def close_session():
|
|
|
587 |
if session.is_active:
|
|
|
588 |
print "session is active. closing it."
|
| 3376 |
rajveer |
589 |
session.close()
|
|
|
590 |
|
|
|
591 |
def is_alive():
|
|
|
592 |
try:
|
|
|
593 |
session.query(User.id).limit(1).one()
|
|
|
594 |
return True
|
|
|
595 |
except:
|
|
|
596 |
return False
|