Subversion Repositories SmartDukaan

Rev

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