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