| 130 |
ashish |
1 |
'''
|
|
|
2 |
Created on 28-Apr-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
| 12696 |
amit.gupta |
6 |
from elixir import session
|
| 130 |
ashish |
7 |
from shop2020.model.v1.user.impl import Dataservice
|
| 12696 |
amit.gupta |
8 |
from shop2020.model.v1.user.impl.CartDataAccessors import create_cart
|
|
|
9 |
from shop2020.model.v1.user.impl.Dataservice import User, UserCommunication, \
|
|
|
10 |
Address, Affiliate, Tracker, TrackLog, MasterAffiliate, UserWidgetItem, \
|
|
|
11 |
FacebookUser, UserSource, PrivateDealUser, Counter
|
|
|
12 |
from shop2020.thriftpy.model.v1.user.ttypes import UserContextException, \
|
|
|
13 |
AuthenticationException, Sex, WidgetType as WType
|
| 2981 |
rajveer |
14 |
from shop2020.utils.Utils import log_entry, to_py_date
|
| 12696 |
amit.gupta |
15 |
from sqlalchemy import desc, select
|
|
|
16 |
from sqlalchemy.sql import and_
|
| 12722 |
amit.gupta |
17 |
from sqlalchemy.sql.expression import or_
|
| 2981 |
rajveer |
18 |
import datetime
|
| 130 |
ashish |
19 |
|
| 12696 |
amit.gupta |
20 |
CounterStateMap = {"Telangana" : "TS", "Andhra Pradesh" : "AP"}
|
|
|
21 |
|
| 3187 |
rajveer |
22 |
def initialize(dbname='user', db_hostname="localhost"):
|
| 130 |
ashish |
23 |
log_entry("initialize@DataAccessor", "Initializing data service")
|
| 3187 |
rajveer |
24 |
Dataservice.initialize(dbname, db_hostname)
|
| 557 |
chandransh |
25 |
|
| 5326 |
rajveer |
26 |
def create_anonymous_user(jsession_id):
|
| 576 |
chandransh |
27 |
user=User.get_by(jsession_id=jsession_id)
|
| 5036 |
rajveer |
28 |
#user=User.query.with_lockmode("update").filter_by(jsession_id=jsession_id)
|
| 576 |
chandransh |
29 |
if not user is None:
|
|
|
30 |
return user
|
| 5326 |
rajveer |
31 |
cart = create_cart()
|
| 557 |
chandransh |
32 |
user = User()
|
|
|
33 |
anonymous_str = "anonymous"
|
| 576 |
chandransh |
34 |
user.email = jsession_id + "@anonymous.com"
|
| 557 |
chandransh |
35 |
user.password = anonymous_str
|
|
|
36 |
user.name = anonymous_str
|
|
|
37 |
user.communication_email = jsession_id + "@anonymous.com"
|
|
|
38 |
user.jsession_id = jsession_id
|
|
|
39 |
user.is_anonymous = True
|
|
|
40 |
user.sex = Sex.WONT_SAY
|
| 5326 |
rajveer |
41 |
user.active_cart = cart
|
| 3499 |
mandeep.dh |
42 |
user.trust_level = 0
|
| 5326 |
rajveer |
43 |
user.active_since = datetime.datetime.now()
|
| 2747 |
chandransh |
44 |
session.commit()
|
| 557 |
chandransh |
45 |
|
|
|
46 |
return user
|
|
|
47 |
|
|
|
48 |
def get_user_by_id(user_id):
|
|
|
49 |
return User.get_by(id=user_id)
|
|
|
50 |
|
| 5326 |
rajveer |
51 |
def get_user_by_cart_id(cart_id):
|
|
|
52 |
return User.get_by(active_cart_id=cart_id)
|
|
|
53 |
|
| 3032 |
mandeep.dh |
54 |
def get_user_by_mobile_number(mobile_number):
|
|
|
55 |
return User.get_by(mobile_number=mobile_number)
|
|
|
56 |
|
| 1491 |
vikas |
57 |
def get_user_by_email(email):
|
|
|
58 |
return User.get_by(email=email)
|
|
|
59 |
|
| 5326 |
rajveer |
60 |
def create_user(user_to_add):
|
| 7825 |
amar.kumar |
61 |
user = User.get_by(email=user_to_add.email)
|
|
|
62 |
if user:
|
| 13661 |
amit.gupta |
63 |
if user.password == user_to_add.password or user_to_add.password == "":
|
|
|
64 |
if user_to_add.password == "":
|
|
|
65 |
add_private_deal_user(user.id)
|
| 7825 |
amar.kumar |
66 |
return user
|
|
|
67 |
else:
|
|
|
68 |
raise UserContextException(109, "User already exists with this email id.")
|
| 5326 |
rajveer |
69 |
|
|
|
70 |
cart = create_cart()
|
|
|
71 |
|
| 557 |
chandransh |
72 |
user = User()
|
|
|
73 |
user.email = user_to_add.email
|
|
|
74 |
user.password = user_to_add.password
|
|
|
75 |
user.name = user_to_add.name
|
|
|
76 |
user.communication_email = user_to_add.communicationEmail
|
|
|
77 |
user.jsession_id = user_to_add.jsessionId
|
|
|
78 |
user.is_anonymous = False
|
|
|
79 |
user.sex = user_to_add.sex
|
| 567 |
rajveer |
80 |
user.date_of_birth = user_to_add.dateOfBirth
|
| 5326 |
rajveer |
81 |
user.active_cart = cart
|
| 567 |
rajveer |
82 |
user.mobile_number = user_to_add.mobileNumber
|
| 2020 |
vikas |
83 |
user.source = user_to_add.source
|
| 2815 |
vikas |
84 |
user.source_start_time = to_py_date(user_to_add.sourceStartTime)
|
| 3499 |
mandeep.dh |
85 |
user.trust_level = 0
|
| 5326 |
rajveer |
86 |
user.active_since = datetime.datetime.now()
|
| 12696 |
amit.gupta |
87 |
addresses = []
|
|
|
88 |
if user_to_add.addresses:
|
|
|
89 |
address = user_to_add.addresses[0]
|
|
|
90 |
address_to_add = Address()
|
|
|
91 |
address_to_add.line_1 = address.line1
|
|
|
92 |
address_to_add.line_2 = address.line2
|
|
|
93 |
address_to_add.landmark = address.landmark
|
|
|
94 |
address_to_add.city = address.city
|
|
|
95 |
address_to_add.country = address.country
|
|
|
96 |
address_to_add.state = address.state
|
|
|
97 |
address_to_add.pin = address.pin
|
|
|
98 |
address_to_add.type = address.type
|
|
|
99 |
address_to_add.name = address.name
|
|
|
100 |
address_to_add.phone = address.phone
|
|
|
101 |
address_to_add.added_on = to_py_date(address.addedOn)
|
|
|
102 |
address_to_add.enabled = True
|
|
|
103 |
address_to_add.user = user
|
|
|
104 |
address_to_add.type = 1
|
|
|
105 |
address_to_add.added_on = datetime.datetime.now()
|
|
|
106 |
addresses.append(address_to_add)
|
|
|
107 |
user.addresses = addresses
|
| 8201 |
rajveer |
108 |
us = UserSource()
|
|
|
109 |
us.user = user
|
|
|
110 |
us.source_id = 1
|
|
|
111 |
if user_to_add.sourceId:
|
|
|
112 |
us.source_id = user_to_add.sourceId
|
| 7883 |
rajveer |
113 |
|
|
|
114 |
if user_to_add.isFacebookUser:
|
|
|
115 |
fuser = FacebookUser()
|
|
|
116 |
fuser.facebook_access_token = user_to_add.facebookAccessToken
|
|
|
117 |
fuser.facebook_id = user_to_add.facebookId
|
|
|
118 |
fuser.user = user
|
| 130 |
ashish |
119 |
session.commit()
|
| 12696 |
amit.gupta |
120 |
if user_to_add.addresses:
|
|
|
121 |
user.default_address_id = user.addresses[0].id
|
|
|
122 |
session.commit()
|
| 14819 |
amit.gupta |
123 |
|
|
|
124 |
if user_to_add.password == "":
|
|
|
125 |
add_private_deal_user(user.id)
|
| 557 |
chandransh |
126 |
return user
|
| 130 |
ashish |
127 |
|
|
|
128 |
#===============================================================================
|
|
|
129 |
# Need to provide the update apis here for relevant fields in PrimaryInfo.
|
|
|
130 |
#===============================================================================
|
| 557 |
chandransh |
131 |
def update_user(user_to_update):
|
| 594 |
rajveer |
132 |
if not user_to_update.userId:
|
| 415 |
ashish |
133 |
raise UserContextException(110, "user does not exist")
|
| 594 |
rajveer |
134 |
user = get_user_by_id(user_to_update.userId)
|
|
|
135 |
user.email = user_to_update.email
|
|
|
136 |
user.password = user_to_update.password
|
|
|
137 |
user.name = user_to_update.name
|
|
|
138 |
user.communication_email = user_to_update.communicationEmail
|
|
|
139 |
user.jsession_id = user_to_update.jsessionId
|
|
|
140 |
user.is_anonymous = user_to_update.isAnonymous
|
|
|
141 |
user.sex = user_to_update.sex
|
|
|
142 |
user.date_of_birth = user_to_update.dateOfBirth
|
|
|
143 |
user.mobile_number = user_to_update.mobileNumber
|
| 7883 |
rajveer |
144 |
if user_to_update.isFacebookUser:
|
| 7884 |
rajveer |
145 |
if user.fbusers and user.fbusers[0]:
|
|
|
146 |
fuser = user.fbusers[0]
|
|
|
147 |
else:
|
|
|
148 |
fuser = FacebookUser()
|
| 7883 |
rajveer |
149 |
if user_to_update.facebookAccessToken is not None:
|
|
|
150 |
fuser.facebook_access_token = user_to_update.facebookAccessToken
|
|
|
151 |
if user_to_update.facebookId is not None:
|
|
|
152 |
fuser.facebook_id = user_to_update.facebookId
|
|
|
153 |
fuser.user = user
|
| 415 |
ashish |
154 |
session.commit()
|
| 594 |
rajveer |
155 |
return user
|
| 415 |
ashish |
156 |
|
| 557 |
chandransh |
157 |
def authenticate_user(user_handle, password):
|
|
|
158 |
user = User.get_by(email=user_handle)
|
| 130 |
ashish |
159 |
if not user:
|
| 557 |
chandransh |
160 |
raise AuthenticationException("This email address is not registered.", 102)
|
| 130 |
ashish |
161 |
|
| 557 |
chandransh |
162 |
if user.password == get_db_password(password):
|
|
|
163 |
return user
|
| 130 |
ashish |
164 |
else:
|
|
|
165 |
raise AuthenticationException("Wrong username or password", 102)
|
|
|
166 |
|
|
|
167 |
def user_exists(email):
|
| 413 |
rajveer |
168 |
try:
|
| 557 |
chandransh |
169 |
user = User.get_by(email=email)
|
|
|
170 |
if user:
|
|
|
171 |
return True
|
|
|
172 |
else:
|
|
|
173 |
return False
|
| 413 |
rajveer |
174 |
except:
|
| 130 |
ashish |
175 |
return False
|
|
|
176 |
|
| 567 |
rajveer |
177 |
def add_address_for_user(address, user_id, set_default):
|
| 557 |
chandransh |
178 |
user = get_user_by_id(user_id)
|
| 766 |
rajveer |
179 |
|
| 130 |
ashish |
180 |
if not user:
|
|
|
181 |
raise_user_exception(user_id)
|
|
|
182 |
if not address:
|
|
|
183 |
raise UserContextException(103,"Address cannot be null")
|
|
|
184 |
|
|
|
185 |
address_to_add = Address()
|
|
|
186 |
address_to_add.line_1 = address.line1
|
|
|
187 |
address_to_add.line_2 = address.line2
|
|
|
188 |
address_to_add.landmark = address.landmark
|
|
|
189 |
address_to_add.city = address.city
|
|
|
190 |
address_to_add.country = address.country
|
|
|
191 |
address_to_add.state = address.state
|
|
|
192 |
address_to_add.pin = address.pin
|
|
|
193 |
address_to_add.type = address.type
|
| 414 |
ashish |
194 |
address_to_add.name = address.name
|
|
|
195 |
address_to_add.phone = address.phone
|
| 130 |
ashish |
196 |
address_to_add.added_on = to_py_date(address.addedOn)
|
|
|
197 |
address_to_add.enabled = True
|
| 557 |
chandransh |
198 |
address_to_add.user = user
|
| 130 |
ashish |
199 |
session.commit()
|
| 509 |
rajveer |
200 |
|
| 557 |
chandransh |
201 |
if set_default is True:
|
|
|
202 |
user.default_address_id = address_to_add.id
|
| 567 |
rajveer |
203 |
#set default address if nothing is default
|
|
|
204 |
if user.default_address_id is None:
|
|
|
205 |
user.default_address_id = address_to_add.id
|
|
|
206 |
|
| 557 |
chandransh |
207 |
session.commit()
|
| 513 |
rajveer |
208 |
|
| 567 |
rajveer |
209 |
return address_to_add.id
|
| 513 |
rajveer |
210 |
|
| 130 |
ashish |
211 |
def remove_address_for_user(user_id, address_id):
|
|
|
212 |
address = get_address(address_id)
|
|
|
213 |
|
|
|
214 |
if not address:
|
| 557 |
chandransh |
215 |
raise UserContextException(103, "Address not found")
|
|
|
216 |
if address.user.id != user_id:
|
|
|
217 |
raise UserContextException(104, "This address belongs to some other user")
|
| 130 |
ashish |
218 |
address.enabled = False
|
| 10714 |
amit.gupta |
219 |
user = User.get_by(id=user_id)
|
|
|
220 |
if user.default_address_id == address_id:
|
|
|
221 |
firstValidAddress = Address.query.filter_by(user_id = user_id, enabled=True).first()
|
|
|
222 |
if firstValidAddress is None:
|
|
|
223 |
user.default_address_id = None
|
|
|
224 |
else:
|
|
|
225 |
user.default_address_id = firstValidAddress.id
|
| 130 |
ashish |
226 |
session.commit()
|
|
|
227 |
return True
|
|
|
228 |
|
|
|
229 |
def set_user_as_logged_in(user_id, time_stamp):
|
| 5326 |
rajveer |
230 |
user = User.get_by(id=user_id)
|
| 130 |
ashish |
231 |
|
| 5326 |
rajveer |
232 |
if not user:
|
| 557 |
chandransh |
233 |
raise_user_exception(user_id)
|
| 130 |
ashish |
234 |
|
|
|
235 |
if not time_stamp:
|
| 5326 |
rajveer |
236 |
user.last_login = datetime.datetime.now()
|
| 557 |
chandransh |
237 |
else:
|
| 5326 |
rajveer |
238 |
user.last_login = to_py_date(time_stamp)
|
| 130 |
ashish |
239 |
session.commit()
|
|
|
240 |
return True
|
|
|
241 |
|
| 557 |
chandransh |
242 |
def set_user_as_logged_out(user_id, time_stamp):
|
| 5326 |
rajveer |
243 |
user = User.get_by(id=user_id)
|
| 130 |
ashish |
244 |
|
| 5326 |
rajveer |
245 |
if not user:
|
| 130 |
ashish |
246 |
raise_user_exception(user_id)
|
|
|
247 |
|
|
|
248 |
if not time_stamp:
|
| 5326 |
rajveer |
249 |
user.last_logout = datetime.datetime.now()
|
| 557 |
chandransh |
250 |
else:
|
| 5326 |
rajveer |
251 |
user.last_logout = to_py_date(time_stamp)
|
| 130 |
ashish |
252 |
session.commit()
|
|
|
253 |
return True
|
|
|
254 |
|
| 557 |
chandransh |
255 |
def set_default_address(user_id, address_id):
|
|
|
256 |
user = get_user_by_id(user_id)
|
|
|
257 |
address = Address.get_by(id=address_id)
|
|
|
258 |
if not user:
|
|
|
259 |
raise_user_exception(user_id)
|
|
|
260 |
if not address:
|
|
|
261 |
raise UserContextException(103, "Address not found")
|
|
|
262 |
if address.user.id != user.id:
|
|
|
263 |
raise UserContextException(104, "This address belongs to some other user")
|
|
|
264 |
|
|
|
265 |
user.default_address_id = address_id
|
|
|
266 |
session.commit()
|
|
|
267 |
|
| 594 |
rajveer |
268 |
def update_password(user_id, old_password, new_password):
|
| 557 |
chandransh |
269 |
user = get_user_by_id(user_id)
|
| 130 |
ashish |
270 |
|
|
|
271 |
if not user:
|
|
|
272 |
raise_user_exception(user_id)
|
|
|
273 |
|
| 594 |
rajveer |
274 |
if user.password != old_password:
|
|
|
275 |
return False
|
|
|
276 |
|
|
|
277 |
if check_for_valid_password(new_password):
|
|
|
278 |
user.password = get_db_password(new_password)
|
| 130 |
ashish |
279 |
session.commit()
|
|
|
280 |
return True
|
|
|
281 |
else:
|
|
|
282 |
return False
|
| 1273 |
varun.gupt |
283 |
|
|
|
284 |
|
|
|
285 |
def create_user_communication(user_id, email, communication_type, order_id, awb, product, subject, message):
|
|
|
286 |
|
|
|
287 |
user_communication = UserCommunication()
|
|
|
288 |
user_communication.user_id = user_id
|
|
|
289 |
user_communication.communication_type = communication_type
|
| 1743 |
varun.gupt |
290 |
|
|
|
291 |
if order_id > 0:
|
|
|
292 |
user_communication.order_id = order_id
|
| 1273 |
varun.gupt |
293 |
user_communication.airwaybill_no = awb
|
|
|
294 |
user_communication.reply_to = email
|
|
|
295 |
user_communication.product_name = product
|
|
|
296 |
user_communication.subject = subject
|
|
|
297 |
user_communication.message = message
|
| 1301 |
varun.gupt |
298 |
user_communication.communication_timestamp = datetime.datetime.now()
|
| 1273 |
varun.gupt |
299 |
session.commit()
|
| 3206 |
mandeep.dh |
300 |
return True
|
|
|
301 |
|
| 1583 |
varun.gupt |
302 |
def get_user_communication_by_id(user_communication_id):
|
|
|
303 |
return UserCommunication.get_by(id = user_communication_id)
|
|
|
304 |
|
|
|
305 |
def get_user_communication_by_user(user_communication_user_id):
|
| 1607 |
vikas |
306 |
return UserCommunication.query.filter_by(user_id = user_communication_user_id).order_by(-UserCommunication.id).all()
|
| 1583 |
varun.gupt |
307 |
|
|
|
308 |
def get_all_user_communications():
|
| 1863 |
vikas |
309 |
return UserCommunication.query.order_by(-UserCommunication.id).all()
|
| 1583 |
varun.gupt |
310 |
|
| 5407 |
amar.kumar |
311 |
def remove_user_communication(commId):
|
|
|
312 |
UserCommunication.query.filter_by(id=commId).delete()
|
|
|
313 |
session.commit()
|
|
|
314 |
|
| 1859 |
vikas |
315 |
def create_master_affiliate(name, added_on):
|
| 1845 |
vikas |
316 |
master_affiliate = MasterAffiliate()
|
|
|
317 |
master_affiliate.name = name
|
| 1859 |
vikas |
318 |
master_affiliate.added_on = to_py_date(added_on)
|
| 1845 |
vikas |
319 |
session.commit()
|
|
|
320 |
return master_affiliate
|
|
|
321 |
|
| 1899 |
vikas |
322 |
def get_all_master_affiliates():
|
|
|
323 |
return MasterAffiliate.query.all()
|
|
|
324 |
|
| 1845 |
vikas |
325 |
def get_master_affiliate_by_id(id):
|
|
|
326 |
return MasterAffiliate.get_by(id = id)
|
|
|
327 |
|
|
|
328 |
def get_master_affiliate_by_name(name):
|
|
|
329 |
return MasterAffiliate.get_by(name = name)
|
|
|
330 |
|
| 1859 |
vikas |
331 |
def create_affiliate(name, url, master_affiliate_id, added_on):
|
| 1845 |
vikas |
332 |
affiliate = Affiliate()
|
|
|
333 |
affiliate.name = name
|
|
|
334 |
if url is not None:
|
|
|
335 |
affiliate.url = url
|
|
|
336 |
affiliate.master_affiliate_id = master_affiliate_id
|
| 1859 |
vikas |
337 |
affiliate.added_on = to_py_date(added_on)
|
| 1845 |
vikas |
338 |
session.commit()
|
|
|
339 |
return affiliate
|
|
|
340 |
|
|
|
341 |
def get_affiliate_by_id(id):
|
|
|
342 |
return Affiliate.get_by(id = id)
|
|
|
343 |
|
|
|
344 |
def get_affiliate_by_name(name):
|
|
|
345 |
return Affiliate.get_by(name = name)
|
|
|
346 |
|
|
|
347 |
def get_affiliates_by_master_affiliate(master_affiliate_id):
|
|
|
348 |
return MasterAffiliate.get_by(id = master_affiliate_id).affiliates
|
|
|
349 |
|
|
|
350 |
def get_tracker_by_id(id):
|
|
|
351 |
return Tracker.get_by(id = id)
|
|
|
352 |
|
| 1996 |
vikas |
353 |
def add_track_log(affiliate_id, user_id, event, url, data, added_on):
|
| 1845 |
vikas |
354 |
track_log = TrackLog()
|
| 1996 |
vikas |
355 |
track_log.affiliate_id = affiliate_id
|
| 1845 |
vikas |
356 |
if user_id:
|
|
|
357 |
track_log.user_id = user_id
|
| 3378 |
vikas |
358 |
track_log.event_id = event
|
| 1845 |
vikas |
359 |
if url:
|
|
|
360 |
track_log.url = url
|
|
|
361 |
if data:
|
| 1859 |
vikas |
362 |
track_log.data = data
|
|
|
363 |
track_log.added_on = to_py_date(added_on)
|
| 1845 |
vikas |
364 |
session.commit()
|
|
|
365 |
return track_log.id
|
|
|
366 |
|
|
|
367 |
def get_track_log_by_id(id):
|
|
|
368 |
return TrackLog.get_by(id = id)
|
|
|
369 |
|
| 3293 |
vikas |
370 |
def get_track_logs_by_affiliate(affiliate_id, start_date, end_date):
|
|
|
371 |
query = TrackLog.query
|
|
|
372 |
|
|
|
373 |
if affiliate_id:
|
|
|
374 |
query = query.filter(TrackLog.affiliate_id == affiliate_id)
|
|
|
375 |
if start_date:
|
|
|
376 |
query = query.filter(TrackLog.added_on >= to_py_date(start_date))
|
|
|
377 |
if end_date:
|
|
|
378 |
query = query.filter(TrackLog.added_on <= to_py_date(end_date))
|
|
|
379 |
return query.all()
|
| 1845 |
vikas |
380 |
|
|
|
381 |
def get_track_logs_by_user(user_id):
|
|
|
382 |
return TrackLog.query.filter(TrackLog.user_id == user_id).all()
|
|
|
383 |
|
| 1996 |
vikas |
384 |
def get_track_logs(affiliate_id, user_id, event, url):
|
| 1845 |
vikas |
385 |
query = TrackLog.query
|
|
|
386 |
|
| 1996 |
vikas |
387 |
if affiliate_id:
|
|
|
388 |
query = query.filter(TrackLog.affiliate_id == affiliate_id)
|
|
|
389 |
if user_id:
|
|
|
390 |
query = query.filter(TrackLog.user_id == user_id)
|
| 1845 |
vikas |
391 |
if event:
|
|
|
392 |
query = query.filter(TrackLog.event == event)
|
|
|
393 |
if url:
|
|
|
394 |
query = query.filter(TrackLog.url == url)
|
|
|
395 |
return query.all()
|
|
|
396 |
|
|
|
397 |
|
| 557 |
chandransh |
398 |
def get_address(address_id):
|
|
|
399 |
address = Address.get_by(id=address_id)
|
|
|
400 |
return address
|
|
|
401 |
|
| 884 |
rajveer |
402 |
def forgot_password(email, password):
|
|
|
403 |
try:
|
|
|
404 |
user = User.get_by(email=email)
|
|
|
405 |
if user:
|
|
|
406 |
user.password = password
|
| 900 |
rajveer |
407 |
session.commit()
|
| 884 |
rajveer |
408 |
return True
|
|
|
409 |
else:
|
|
|
410 |
return False
|
|
|
411 |
except:
|
|
|
412 |
return False
|
|
|
413 |
|
| 594 |
rajveer |
414 |
def get_all_addresses_for_user(userId):
|
| 2895 |
chandransh |
415 |
return Address.query.filter_by(user_id = userId, enabled=True).all()
|
| 581 |
rajveer |
416 |
|
| 785 |
rajveer |
417 |
def get_default_address_id(userId):
|
| 594 |
rajveer |
418 |
user = get_user_by_id(userId);
|
| 840 |
chandransh |
419 |
if user.default_address_id is None:
|
|
|
420 |
return 0
|
| 594 |
rajveer |
421 |
return user.default_address_id
|
| 581 |
rajveer |
422 |
|
| 785 |
rajveer |
423 |
def get_default_pincode(user_id):
|
|
|
424 |
user = get_user_by_id(user_id)
|
|
|
425 |
if not user:
|
|
|
426 |
raise_user_exception(user_id)
|
|
|
427 |
default_address_id = user.default_address_id
|
|
|
428 |
if default_address_id:
|
|
|
429 |
address = Address.get_by(id=default_address_id)
|
|
|
430 |
default_pincode = address.pin
|
|
|
431 |
else:
|
|
|
432 |
default_pincode = '110001'
|
|
|
433 |
return default_pincode
|
| 1596 |
ankur.sing |
434 |
|
|
|
435 |
def get_user_count(user_type):
|
|
|
436 |
if user_type is None:
|
|
|
437 |
return User.query.count()
|
|
|
438 |
else:
|
|
|
439 |
return User.query.filter_by(is_anonymous = user_type).count()
|
| 785 |
rajveer |
440 |
|
| 1891 |
ankur.sing |
441 |
def get_users(user_type, start_date, end_date):
|
| 5326 |
rajveer |
442 |
query = session.query(User)
|
| 1891 |
ankur.sing |
443 |
if start_date != -1:
|
| 5326 |
rajveer |
444 |
query = query.filter(User.active_since >= to_py_date(start_date))
|
| 1891 |
ankur.sing |
445 |
if end_date != -1:
|
| 5326 |
rajveer |
446 |
query = query.filter(User.active_since <= to_py_date(end_date))
|
| 1891 |
ankur.sing |
447 |
if user_type is not None:
|
|
|
448 |
query = query.filter(User.is_anonymous == user_type)
|
|
|
449 |
return query.all()
|
| 1673 |
ankur.sing |
450 |
|
| 130 |
ashish |
451 |
#=============================================================================
|
|
|
452 |
# Helper functions
|
|
|
453 |
#=============================================================================
|
|
|
454 |
|
|
|
455 |
'''
|
|
|
456 |
This function returns the password as stored in the db
|
|
|
457 |
'''
|
|
|
458 |
def get_db_password(password):
|
|
|
459 |
return password
|
|
|
460 |
|
|
|
461 |
def check_for_valid_password(password):
|
|
|
462 |
if not password:
|
|
|
463 |
raise UserContextException(105,"password cannot be null")
|
|
|
464 |
return True
|
|
|
465 |
#------------------------------------------------------------------------------
|
|
|
466 |
|
|
|
467 |
#===============================================================================
|
|
|
468 |
# raises the UserContextException
|
|
|
469 |
#===============================================================================
|
|
|
470 |
def raise_user_exception(user_id):
|
| 766 |
rajveer |
471 |
raise UserContextException(101, "no such user in system %d" %(user_id))
|
|
|
472 |
|
| 2981 |
rajveer |
473 |
def get_my_research_items(userId):
|
|
|
474 |
query = UserWidgetItem.query.filter_by(userId=userId)
|
|
|
475 |
query = query.filter_by(widgetId=WType.MY_RESEARCH)
|
|
|
476 |
query = query.order_by(desc(UserWidgetItem.addedOn))
|
|
|
477 |
widgetItems = query.all()
|
|
|
478 |
return [widgetItem.itemId for widgetItem in widgetItems]
|
|
|
479 |
|
|
|
480 |
def get_browse_history_items(userId):
|
|
|
481 |
query = UserWidgetItem.query.filter_by(userId=userId)
|
|
|
482 |
query = query.filter_by(widgetId=WType.BROWSE_HISTORY)
|
|
|
483 |
query = query.order_by(desc(UserWidgetItem.addedOn)).limit(10)
|
|
|
484 |
widgetItems = query.all()
|
|
|
485 |
return [widgetItem.itemId for widgetItem in widgetItems]
|
|
|
486 |
|
|
|
487 |
def update_my_research(userId, itemId):
|
|
|
488 |
isNew = False
|
|
|
489 |
query = UserWidgetItem.query.filter_by(userId=userId)
|
|
|
490 |
query = query.filter_by(widgetId=WType.MY_RESEARCH)
|
|
|
491 |
query = query.filter_by(itemId=itemId)
|
|
|
492 |
widgetItem = query.first()
|
|
|
493 |
if not widgetItem:
|
|
|
494 |
isNew = True
|
|
|
495 |
widgetItem = UserWidgetItem()
|
|
|
496 |
widgetItem.userId = userId
|
|
|
497 |
widgetItem.widgetId = WType.MY_RESEARCH
|
|
|
498 |
widgetItem.itemId = itemId
|
|
|
499 |
widgetItem.addedOn = datetime.datetime.now()
|
|
|
500 |
session.commit()
|
|
|
501 |
return isNew
|
|
|
502 |
|
|
|
503 |
def update_browse_history(userId, itemId):
|
|
|
504 |
query = UserWidgetItem.query.filter_by(userId=userId)
|
|
|
505 |
query = query.filter_by(widgetId=WType.BROWSE_HISTORY)
|
|
|
506 |
query = query.filter_by(itemId=itemId)
|
|
|
507 |
widgetItem = query.first()
|
|
|
508 |
if not widgetItem:
|
|
|
509 |
widgetItem = UserWidgetItem()
|
|
|
510 |
widgetItem.userId = userId
|
|
|
511 |
widgetItem.widgetId = WType.BROWSE_HISTORY
|
|
|
512 |
widgetItem.itemId = itemId
|
|
|
513 |
widgetItem.addedOn = datetime.datetime.now()
|
|
|
514 |
session.commit()
|
|
|
515 |
|
|
|
516 |
def delete_item_from_my_research(userId, itemId):
|
|
|
517 |
query = UserWidgetItem.query.filter_by(userId=userId)
|
|
|
518 |
query = query.filter_by(widgetId=WType.MY_RESEARCH)
|
|
|
519 |
query = query.filter_by(itemId=itemId)
|
|
|
520 |
widgetItem = query.first()
|
|
|
521 |
if widgetItem:
|
|
|
522 |
widgetItem.delete()
|
|
|
523 |
session.commit()
|
|
|
524 |
else:
|
| 11499 |
amit.gupta |
525 |
print "Not item in my research to delete with itemId " + str(itemId)
|
| 2981 |
rajveer |
526 |
|
| 3499 |
mandeep.dh |
527 |
def increase_trust_level(userId, trustLevelDelta):
|
|
|
528 |
user = User.query.filter_by(id = userId).with_lockmode('update').first()
|
|
|
529 |
user.trust_level += trustLevelDelta
|
|
|
530 |
session.commit()
|
|
|
531 |
|
| 5407 |
amar.kumar |
532 |
def get_trust_level(userId):
|
|
|
533 |
user = User.query.filter_by(id = userId).first()
|
|
|
534 |
return user.trust_level
|
| 5623 |
anupam.sin |
535 |
|
|
|
536 |
def get_user_emails(start_date, end_date):
|
|
|
537 |
emails = select([User.table.c.communication_email], and_(User.table.c.active_since >= start_date, User.table.c.active_since <= end_date, User.table.c.is_anonymous==0)).execute()
|
|
|
538 |
email_addresses = []
|
| 5407 |
amar.kumar |
539 |
|
| 5623 |
anupam.sin |
540 |
for email in emails:
|
|
|
541 |
email_addresses.append(str(email[0]))
|
|
|
542 |
|
|
|
543 |
return email_addresses
|
| 7825 |
amar.kumar |
544 |
|
| 11679 |
vikram.rag |
545 |
def is_private_deal_user(userId):
|
|
|
546 |
try:
|
|
|
547 |
user = PrivateDealUser.get_by(id=userId)
|
|
|
548 |
if user is None:
|
|
|
549 |
return False
|
|
|
550 |
elif not user.isActive:
|
|
|
551 |
return False
|
|
|
552 |
else:
|
|
|
553 |
return True
|
|
|
554 |
except:
|
|
|
555 |
print("Error : Unable to get details for User Id : " + str(userId))
|
|
|
556 |
return False
|
|
|
557 |
finally:
|
|
|
558 |
close_session()
|
|
|
559 |
|
| 11890 |
kshitij.so |
560 |
def add_private_deal_user(userId):
|
|
|
561 |
user = PrivateDealUser.get_by(id=userId)
|
|
|
562 |
if user is None:
|
|
|
563 |
try:
|
|
|
564 |
pd = PrivateDealUser()
|
|
|
565 |
pd.id = userId
|
|
|
566 |
pd.created_on = datetime.datetime.now()
|
|
|
567 |
pd.isActive = True
|
| 13401 |
manish.sha |
568 |
pd.bulkShipmentAmountLimit=50000
|
| 11890 |
kshitij.so |
569 |
session.commit()
|
|
|
570 |
return True
|
|
|
571 |
except Exception as e:
|
|
|
572 |
print "Unable to add user to private deals",e
|
|
|
573 |
return False
|
|
|
574 |
else:
|
| 13661 |
amit.gupta |
575 |
return True
|
| 11679 |
vikram.rag |
576 |
|
| 11890 |
kshitij.so |
577 |
def change_private_deal_user_status(userId,isActive):
|
|
|
578 |
user = PrivateDealUser.get_by(id=userId)
|
|
|
579 |
if user is None:
|
|
|
580 |
return False
|
|
|
581 |
else:
|
|
|
582 |
user.isActive = isActive
|
|
|
583 |
session.commit()
|
|
|
584 |
return True
|
|
|
585 |
|
|
|
586 |
def get_private_deal_user(userId):
|
|
|
587 |
return PrivateDealUser.get_by(id=userId)
|
|
|
588 |
|
| 12696 |
amit.gupta |
589 |
def register_counter(tCounter, userId):
|
|
|
590 |
firstValidAddress = Address.query.filter_by(user_id = userId, enabled=True).first()
|
|
|
591 |
counter = Counter()
|
|
|
592 |
_setCounter(counter, tCounter, firstValidAddress)
|
|
|
593 |
pDealUser = PrivateDealUser()
|
|
|
594 |
pDealUser.counter = counter
|
|
|
595 |
pDealUser.tin = tCounter.tin
|
|
|
596 |
pDealUser.id = userId
|
|
|
597 |
pDealUser.created_on = datetime.datetime.now()
|
|
|
598 |
pDealUser.isActive = True
|
| 13401 |
manish.sha |
599 |
pDealUser.bulkShipmentAmountLimit=50000
|
| 12696 |
amit.gupta |
600 |
session.commit()
|
|
|
601 |
m = {}
|
|
|
602 |
m['counter_code'] = counter.code
|
|
|
603 |
return m
|
|
|
604 |
|
|
|
605 |
def _setCounter(counter,tCounter,address):
|
|
|
606 |
#counter = Counter()
|
|
|
607 |
counter.name = tCounter.name
|
|
|
608 |
counter.ownerName = tCounter.ownerName
|
|
|
609 |
counter.createdOn = datetime.datetime.now()
|
|
|
610 |
counter.email = tCounter.email
|
|
|
611 |
counter.mobile = tCounter.mobile
|
|
|
612 |
counter.addressId = address.id
|
| 12790 |
amit.gupta |
613 |
counter.dob = tCounter.dob
|
| 12696 |
amit.gupta |
614 |
counter.tin = tCounter.tin
|
|
|
615 |
query = Counter.query.filter(Counter.code.like(CounterStateMap[address.state] + '%')).order_by(Counter.id.desc())
|
|
|
616 |
lastStateCounter = query.first()
|
|
|
617 |
if lastStateCounter:
|
| 12698 |
amit.gupta |
618 |
counter.code = CounterStateMap[address.state] + str(int(lastStateCounter.code[2:])+1).zfill(6)
|
| 12696 |
amit.gupta |
619 |
else:
|
| 12698 |
amit.gupta |
620 |
counter.code = CounterStateMap[address.state] + "000001"
|
| 12696 |
amit.gupta |
621 |
counter.striker = tCounter.striker
|
|
|
622 |
if tCounter.alternateMobile:
|
|
|
623 |
counter.alternateMobile = tCounter.alternateMobile
|
|
|
624 |
if tCounter.spCounterSize:
|
|
|
625 |
counter.spCounterSize = tCounter.spCounterSize
|
|
|
626 |
if tCounter.fpCounterSize:
|
|
|
627 |
counter.fpCounterSize = tCounter.fpCounterSize
|
| 12722 |
amit.gupta |
628 |
|
|
|
629 |
def search_counter(type1, searchString):
|
|
|
630 |
if type1=="counter_code":
|
|
|
631 |
return Counter.query.filter(Counter.code==searchString).all()
|
|
|
632 |
elif type1=="mobile":
|
|
|
633 |
return Counter.query.filter(or_(Counter.alternateMobile==searchString, Counter.mobile==searchString)).all()
|
|
|
634 |
elif type1=="email":
|
|
|
635 |
return Counter.query.filter(Counter.email==searchString).all()
|
|
|
636 |
elif type1=="counter_name":
|
|
|
637 |
return Counter.query.filter(Counter.name.like("%" + searchString + "%")).all()
|
|
|
638 |
else:
|
|
|
639 |
return []
|
|
|
640 |
|
|
|
641 |
def get_all_users_by_counter(counterid):
|
|
|
642 |
dealers = PrivateDealUser.query.filter(PrivateDealUser.counter_id == counterid).all()
|
|
|
643 |
dealerids = [dealer.id for dealer in dealers]
|
|
|
644 |
print dealerids
|
|
|
645 |
if len(dealerids) > 0:
|
|
|
646 |
return User.query.filter(User.id.in_(dealerids)).all()
|
|
|
647 |
return []
|
|
|
648 |
|
|
|
649 |
|
| 12696 |
amit.gupta |
650 |
|
| 766 |
rajveer |
651 |
def close_session():
|
|
|
652 |
if session.is_active:
|
|
|
653 |
print "session is active. closing it."
|
| 3376 |
rajveer |
654 |
session.close()
|
|
|
655 |
|
|
|
656 |
def is_alive():
|
|
|
657 |
try:
|
|
|
658 |
session.query(User.id).limit(1).one()
|
|
|
659 |
return True
|
|
|
660 |
except:
|
|
|
661 |
return False
|