Subversion Repositories SmartDukaan

Rev

Rev 576 | Rev 594 | 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):
89
    if not user_to_update.id:
415 ashish 90
        raise UserContextException(110, "user does not exist")
557 chandransh 91
    session.add(user_to_update)    
415 ashish 92
    session.commit()
557 chandransh 93
    return user_to_update
415 ashish 94
 
557 chandransh 95
def delete_user(user_id):
96
    user = get_user_by_id(user_id)
130 ashish 97
 
98
    if not user:
99
        raise_user_exception(user_id)
557 chandransh 100
 
101
    user.state.account_status = AccountStatus.DELETED
102
    session.commit()
103
    return True
130 ashish 104
 
557 chandransh 105
def get_user_state(user_id):
106
    return UserState.get_by(user_id=user_id)
130 ashish 107
 
557 chandransh 108
def get_internal_info(user_id):
109
    return InternalInfo.get_by(user_id=user_id)
130 ashish 110
 
557 chandransh 111
def authenticate_user(user_handle, password):
112
    user = User.get_by(email=user_handle)
130 ashish 113
    if not user:
557 chandransh 114
        raise AuthenticationException("This email address is not registered.", 102)
130 ashish 115
 
557 chandransh 116
    if user.password == get_db_password(password):
117
        return user
130 ashish 118
    else:
119
        raise AuthenticationException("Wrong username or password", 102)
120
 
121
def user_exists(email):
413 rajveer 122
    try:
557 chandransh 123
        user = User.get_by(email=email)
124
        if user:
125
            return True
126
        else:
127
            return False
413 rajveer 128
    except:
130 ashish 129
        return False
130
 
567 rajveer 131
def add_address_for_user(address, user_id, set_default):
557 chandransh 132
    user = get_user_by_id(user_id)
130 ashish 133
    if not user:
134
        raise_user_exception(user_id)
135
    if not address:
136
        raise UserContextException(103,"Address cannot be null")
137
 
138
    address_to_add = Address()
139
    address_to_add.line_1 = address.line1
140
    address_to_add.line_2 = address.line2
141
    address_to_add.landmark = address.landmark
142
    address_to_add.city = address.city
143
    address_to_add.country = address.country
144
    address_to_add.state = address.state
145
    address_to_add.pin = address.pin
146
    address_to_add.type = address.type
414 ashish 147
    address_to_add.name = address.name
148
    address_to_add.phone = address.phone
130 ashish 149
    address_to_add.added_on = to_py_date(address.addedOn)
150
    address_to_add.enabled = True
557 chandransh 151
    address_to_add.user = user
130 ashish 152
    session.commit()
509 rajveer 153
 
557 chandransh 154
    if set_default is True:
155
        user.default_address_id = address_to_add.id
567 rajveer 156
    #set default address if nothing is default    
157
    if user.default_address_id is None:
158
        user.default_address_id = address_to_add.id
159
 
557 chandransh 160
    session.commit()
513 rajveer 161
 
567 rajveer 162
    return address_to_add.id
513 rajveer 163
 
130 ashish 164
def remove_address_for_user(user_id, address_id):
165
    address = get_address(address_id) 
166
 
167
    if not address:
557 chandransh 168
        raise UserContextException(103, "Address not found")
169
    if address.user.id != user_id:
170
        raise UserContextException(104, "This address belongs to some other user")
130 ashish 171
 
172
    address.enabled = False
173
    session.commit()
174
    return True
175
 
176
def set_user_as_logged_in(user_id, time_stamp):
557 chandransh 177
    user_state = UserState.get_by(user_id=user_id)
178
    #user = get_user_by_id(user_id)
130 ashish 179
 
557 chandransh 180
    if not user_state:
181
        raise_user_exception(user_id)
130 ashish 182
 
183
    if not time_stamp:
557 chandransh 184
        user_state.last_login = datetime.datetime.now()
185
    else:
186
        user_state.last_login = to_py_date(time_stamp)
130 ashish 187
    session.commit()
188
    return True
189
 
557 chandransh 190
def set_user_as_logged_out(user_id, time_stamp):
191
    user_state = UserState.get_by(user_id=user_id)
130 ashish 192
 
557 chandransh 193
    if not user_state:
130 ashish 194
        raise_user_exception(user_id)
195
 
196
    if not time_stamp:
557 chandransh 197
        user_state.last_logout = datetime.datetime.now()
198
    else:
199
        user_state.last_logout = to_py_date(time_stamp)
130 ashish 200
    session.commit()
201
    return True
202
 
557 chandransh 203
def set_default_address(user_id, address_id):
204
    user = get_user_by_id(user_id)
205
    address = Address.get_by(id=address_id)
206
    if not user:
207
        raise_user_exception(user_id)
208
    if not address:
209
        raise UserContextException(103, "Address not found")
210
    if address.user.id != user.id:
211
        raise UserContextException(104, "This address belongs to some other user")
212
 
213
    user.default_address_id = address_id 
214
    session.commit()
215
 
130 ashish 216
def update_password(user_id, password):
557 chandransh 217
    user = get_user_by_id(user_id)
130 ashish 218
 
219
    if not user:
220
        raise_user_exception(user_id)
221
 
222
    if check_for_valid_password(password):
557 chandransh 223
        user.password = get_db_password(password)
130 ashish 224
        session.commit()
225
        return True
226
    else:
227
        return False
228
 
557 chandransh 229
def get_address(address_id):
230
    address = Address.get_by(id=address_id)
231
    return address
232
 
233
def get_social_service(service_id):
234
    service = SocialService.get_by(service_id)
235
    return service
236
 
237
def add_ip_address_for_user(ip_address, time_stamp, user_id):
238
    user = get_user_by_id(user_id)
130 ashish 239
    if not user:
240
        raise_user_exception(user_id)
557 chandransh 241
    #user exists create an IP address object
242
    ip_address = IPMap()
243
    ip_address.ip = ip_address
244
    ip_address.timestamp = to_py_date(time_stamp)
245
    ip_address.user_state = user.state
130 ashish 246
    session.commit()
247
    return True
248
 
249
def get_social_service_by_name(service_name):
250
    service = SocialService.get_by(name=service_name)
251
 
252
    if not service:
253
        raise UserContextException(106, "No such social service exists")
254
    return service
255
 
256
def add_social_handle(user_id, social_service, handle):
557 chandransh 257
    user = get_user_by_id(user_id)
130 ashish 258
 
259
    if not user:
260
        raise_user_exception(user_id)
261
 
262
    service = get_social_service_by_name(social_service)
263
 
264
    #get if use already has this service.
557 chandransh 265
    social_handle = SocialHandle.filter(service=service).filter(user=user).one()
130 ashish 266
    if not social_handle:
267
        #create a new handle
268
        social_handle = SocialHandle()
269
        social_handle.service = service
557 chandransh 270
        social_handle.user = user
130 ashish 271
        social_handle.handle = handle
272
    else:
273
        social_handle.handle = handle
557 chandransh 274
    session.commit()
130 ashish 275
    return True
557 chandransh 276
 
581 rajveer 277
def forgot_password(email):
278
    try:
279
        user = User.get_by(email=email)
280
        if user:
281
            mail = Mail()
282
            mail.to = []
283
            mail.to.append(email)
284
            mail.sender = ""
285
            mail.password = ""
286
            mail.data = "Your new password is " + user.password
287
            helper_client = HelperClient()
288
            helper_client.__start__()
289
            client = helper_client.get_client()
290
            client.sendMail(mail);
291
            return True
292
        else:
293
            return False
294
    except:
295
        return False
296
 
297
 
298
 
299
 
130 ashish 300
#=============================================================================
301
# Helper functions 
302
#=============================================================================
303
 
304
'''
305
This function returns the password as stored in the db
306
'''    
307
def get_db_password(password):
308
    return password
309
 
310
def check_for_valid_password(password):
311
    if not password:
312
        raise UserContextException(105,"password cannot be null")
313
    return True
314
#------------------------------------------------------------------------------ 
315
 
316
#===============================================================================
317
# raises the UserContextException
318
#===============================================================================
319
def raise_user_exception(user_id):
320
    raise UserContextException(101, "no such user in system %d" %(user_id))