Subversion Repositories SmartDukaan

Rev

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