Subversion Repositories SmartDukaan

Rev

Rev 581 | Rev 766 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
557 chandransh 8
from shop2020.model.v1.user.impl.Dataservice import User, IPMap,\
9
    Address, SocialHandle, UserState, InternalInfo, SocialService
130 ashish 10
from shop2020.thriftpy.model.v1.user.ttypes import UserContextException,\
557 chandransh 11
    AuthenticationException, AccountStatus, Sex
581 rajveer 12
from shop2020.clients.HelperClient import HelperClient
130 ashish 13
from elixir import session
14
import datetime
581 rajveer 15
from shop2020.thriftpy.utils.ttypes import Mail
130 ashish 16
 
17
def initialize():
18
    log_entry("initialize@DataAccessor", "Initializing data service")
19
    Dataservice.initialize()
557 chandransh 20
 
21
def create_anonymous_user(jsession_id, cart):
576 chandransh 22
    user=User.get_by(jsession_id=jsession_id)
23
    if not user is None:
24
        return user
557 chandransh 25
    user = User()
26
    anonymous_str = "anonymous"
576 chandransh 27
    user.email = jsession_id + "@anonymous.com"
557 chandransh 28
    user.password = anonymous_str
29
    user.name = anonymous_str
30
    user.communication_email = jsession_id + "@anonymous.com"
31
    user.jsession_id = jsession_id
32
    user.is_anonymous = True
33
    user.sex = Sex.WONT_SAY
34
    user.active_cart_id = cart.id
35
    #initialize userState
36
    user_state = UserState()
37
    user_state.is_email_verified = False
38
    user_state.is_sms_verified = False
39
    user_state.account_status = AccountStatus.ACTIVE
40
    user_state.ip_list = []
41
    user_state.active_since = datetime.datetime.now()    
42
    user_state.user = user
43
    session.commit()
44
 
45
    cart.user_id = user.id    
46
    session.commit()
130 ashish 47
 
557 chandransh 48
    return user
49
 
50
def get_user_by_id(user_id):
51
    return User.get_by(id=user_id)
52
 
53
def create_user(user_to_add, cart):
54
    if user_to_add.userId:
130 ashish 55
        raise UserContextException(109, "id is set, user cannot be created")
557 chandransh 56
    user = User()
57
    user.email = user_to_add.email
58
    user.password = user_to_add.password
59
    user.name = user_to_add.name
60
    user.communication_email = user_to_add.communicationEmail
61
    user.jsession_id = user_to_add.jsessionId
62
    user.is_anonymous = False
63
    user.sex = user_to_add.sex
567 rajveer 64
    user.date_of_birth = user_to_add.dateOfBirth
557 chandransh 65
    user.active_cart_id = cart.id
567 rajveer 66
    user.mobile_number = user_to_add.mobileNumber
513 rajveer 67
 
130 ashish 68
    #initialize userState
557 chandransh 69
    user_state = UserState()
130 ashish 70
    user_state.is_email_verified = False
71
    user_state.is_sms_verified = False
72
    user_state.account_status = AccountStatus.ACTIVE
132 ashish 73
    user_state.ip_list = []
130 ashish 74
    user_state.active_since = datetime.datetime.now()
557 chandransh 75
    user_state.user = user
76
    session.commit()
130 ashish 77
 
557 chandransh 78
    #Now create the user phones. Mostly, this should only be a mobile number
79
 
80
    cart.user_id = user.id    
130 ashish 81
    session.commit()
404 rajveer 82
 
557 chandransh 83
    return user
130 ashish 84
 
85
#===============================================================================
86
# Need to provide the update apis here for relevant fields in PrimaryInfo.
87
#===============================================================================
557 chandransh 88
def update_user(user_to_update):
594 rajveer 89
    if not user_to_update.userId:
415 ashish 90
        raise UserContextException(110, "user does not exist")
594 rajveer 91
    user = get_user_by_id(user_to_update.userId)
92
    user.email = user_to_update.email
93
    user.password = user_to_update.password
94
    user.name = user_to_update.name
95
    user.communication_email = user_to_update.communicationEmail
96
    user.jsession_id = user_to_update.jsessionId
97
    user.is_anonymous = user_to_update.isAnonymous
98
    user.sex = user_to_update.sex
99
    user.date_of_birth = user_to_update.dateOfBirth
100
    user.active_cart_id = user_to_update.activeCartId
101
    user.mobile_number = user_to_update.mobileNumber
415 ashish 102
    session.commit()
594 rajveer 103
    return user
415 ashish 104
 
557 chandransh 105
def delete_user(user_id):
106
    user = get_user_by_id(user_id)
130 ashish 107
 
108
    if not user:
109
        raise_user_exception(user_id)
557 chandransh 110
 
111
    user.state.account_status = AccountStatus.DELETED
112
    session.commit()
113
    return True
130 ashish 114
 
557 chandransh 115
def get_user_state(user_id):
116
    return UserState.get_by(user_id=user_id)
130 ashish 117
 
557 chandransh 118
def get_internal_info(user_id):
119
    return InternalInfo.get_by(user_id=user_id)
130 ashish 120
 
557 chandransh 121
def authenticate_user(user_handle, password):
122
    user = User.get_by(email=user_handle)
130 ashish 123
    if not user:
557 chandransh 124
        raise AuthenticationException("This email address is not registered.", 102)
130 ashish 125
 
557 chandransh 126
    if user.password == get_db_password(password):
127
        return user
130 ashish 128
    else:
129
        raise AuthenticationException("Wrong username or password", 102)
130
 
131
def user_exists(email):
413 rajveer 132
    try:
557 chandransh 133
        user = User.get_by(email=email)
134
        if user:
135
            return True
136
        else:
137
            return False
413 rajveer 138
    except:
130 ashish 139
        return False
140
 
567 rajveer 141
def add_address_for_user(address, user_id, set_default):
557 chandransh 142
    user = get_user_by_id(user_id)
130 ashish 143
    if not user:
144
        raise_user_exception(user_id)
145
    if not address:
146
        raise UserContextException(103,"Address cannot be null")
147
 
148
    address_to_add = Address()
149
    address_to_add.line_1 = address.line1
150
    address_to_add.line_2 = address.line2
151
    address_to_add.landmark = address.landmark
152
    address_to_add.city = address.city
153
    address_to_add.country = address.country
154
    address_to_add.state = address.state
155
    address_to_add.pin = address.pin
156
    address_to_add.type = address.type
414 ashish 157
    address_to_add.name = address.name
158
    address_to_add.phone = address.phone
130 ashish 159
    address_to_add.added_on = to_py_date(address.addedOn)
160
    address_to_add.enabled = True
557 chandransh 161
    address_to_add.user = user
130 ashish 162
    session.commit()
509 rajveer 163
 
557 chandransh 164
    if set_default is True:
165
        user.default_address_id = address_to_add.id
567 rajveer 166
    #set default address if nothing is default    
167
    if user.default_address_id is None:
168
        user.default_address_id = address_to_add.id
169
 
557 chandransh 170
    session.commit()
513 rajveer 171
 
567 rajveer 172
    return address_to_add.id
513 rajveer 173
 
130 ashish 174
def remove_address_for_user(user_id, address_id):
175
    address = get_address(address_id) 
176
 
177
    if not address:
557 chandransh 178
        raise UserContextException(103, "Address not found")
179
    if address.user.id != user_id:
180
        raise UserContextException(104, "This address belongs to some other user")
130 ashish 181
 
182
    address.enabled = False
183
    session.commit()
184
    return True
185
 
186
def set_user_as_logged_in(user_id, time_stamp):
557 chandransh 187
    user_state = UserState.get_by(user_id=user_id)
188
    #user = get_user_by_id(user_id)
130 ashish 189
 
557 chandransh 190
    if not user_state:
191
        raise_user_exception(user_id)
130 ashish 192
 
193
    if not time_stamp:
557 chandransh 194
        user_state.last_login = datetime.datetime.now()
195
    else:
196
        user_state.last_login = to_py_date(time_stamp)
130 ashish 197
    session.commit()
198
    return True
199
 
557 chandransh 200
def set_user_as_logged_out(user_id, time_stamp):
201
    user_state = UserState.get_by(user_id=user_id)
130 ashish 202
 
557 chandransh 203
    if not user_state:
130 ashish 204
        raise_user_exception(user_id)
205
 
206
    if not time_stamp:
557 chandransh 207
        user_state.last_logout = datetime.datetime.now()
208
    else:
209
        user_state.last_logout = to_py_date(time_stamp)
130 ashish 210
    session.commit()
211
    return True
212
 
557 chandransh 213
def set_default_address(user_id, address_id):
214
    user = get_user_by_id(user_id)
215
    address = Address.get_by(id=address_id)
216
    if not user:
217
        raise_user_exception(user_id)
218
    if not address:
219
        raise UserContextException(103, "Address not found")
220
    if address.user.id != user.id:
221
        raise UserContextException(104, "This address belongs to some other user")
222
 
223
    user.default_address_id = address_id 
224
    session.commit()
225
 
594 rajveer 226
def update_password(user_id, old_password, new_password):
557 chandransh 227
    user = get_user_by_id(user_id)
130 ashish 228
 
229
    if not user:
230
        raise_user_exception(user_id)
231
 
594 rajveer 232
    if user.password != old_password:
233
        return False
234
 
235
    if check_for_valid_password(new_password):
236
        user.password = get_db_password(new_password)
130 ashish 237
        session.commit()
238
        return True
239
    else:
240
        return False
241
 
557 chandransh 242
def get_address(address_id):
243
    address = Address.get_by(id=address_id)
244
    return address
245
 
246
def get_social_service(service_id):
247
    service = SocialService.get_by(service_id)
248
    return service
249
 
250
def add_ip_address_for_user(ip_address, time_stamp, user_id):
251
    user = get_user_by_id(user_id)
130 ashish 252
    if not user:
253
        raise_user_exception(user_id)
557 chandransh 254
    #user exists create an IP address object
255
    ip_address = IPMap()
256
    ip_address.ip = ip_address
257
    ip_address.timestamp = to_py_date(time_stamp)
258
    ip_address.user_state = user.state
130 ashish 259
    session.commit()
260
    return True
261
 
262
def get_social_service_by_name(service_name):
263
    service = SocialService.get_by(name=service_name)
264
 
265
    if not service:
266
        raise UserContextException(106, "No such social service exists")
267
    return service
268
 
269
def add_social_handle(user_id, social_service, handle):
557 chandransh 270
    user = get_user_by_id(user_id)
130 ashish 271
 
272
    if not user:
273
        raise_user_exception(user_id)
274
 
275
    service = get_social_service_by_name(social_service)
276
 
277
    #get if use already has this service.
557 chandransh 278
    social_handle = SocialHandle.filter(service=service).filter(user=user).one()
130 ashish 279
    if not social_handle:
280
        #create a new handle
281
        social_handle = SocialHandle()
282
        social_handle.service = service
557 chandransh 283
        social_handle.user = user
130 ashish 284
        social_handle.handle = handle
285
    else:
286
        social_handle.handle = handle
557 chandransh 287
    session.commit()
130 ashish 288
    return True
557 chandransh 289
 
581 rajveer 290
def forgot_password(email):
291
    try:
292
        user = User.get_by(email=email)
293
        if user:
294
            mail = Mail()
295
            mail.to = []
296
            mail.to.append(email)
297
            mail.sender = ""
298
            mail.password = ""
299
            mail.data = "Your new password is " + user.password
300
            helper_client = HelperClient()
301
            helper_client.__start__()
302
            client = helper_client.get_client()
303
            client.sendMail(mail);
304
            return True
305
        else:
306
            return False
307
    except:
308
        return False
309
 
310
 
594 rajveer 311
def get_all_addresses_for_user(userId):
312
    query = Address.query.filter(Address.user.has(id = userId))
313
    query = query.filter(Address.enabled == True)
314
    return query.all()
581 rajveer 315
 
594 rajveer 316
def get_default_addredd_id(userId):
317
    user = get_user_by_id(userId);
318
    return user.default_address_id
581 rajveer 319
 
130 ashish 320
#=============================================================================
321
# Helper functions 
322
#=============================================================================
323
 
324
'''
325
This function returns the password as stored in the db
326
'''    
327
def get_db_password(password):
328
    return password
329
 
330
def check_for_valid_password(password):
331
    if not password:
332
        raise UserContextException(105,"password cannot be null")
333
    return True
334
#------------------------------------------------------------------------------ 
335
 
336
#===============================================================================
337
# raises the UserContextException
338
#===============================================================================
339
def raise_user_exception(user_id):
340
    raise UserContextException(101, "no such user in system %d" %(user_id))