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