Subversion Repositories SmartDukaan

Rev

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