def login(request): res = {} if request.session.session_key: res['result'] = 0 res['message'] = 'The user has logon.' elif request.method == 'POST': info = json.loads(request.body) user_name = info['user_name'] password = info['password'] user = UserAccess.getUserByName(user_name) if user: if hashlib.sha1(info['password']).hexdigest() == user['password']: request.session['user_id'] = user['id'] res['result'] = 1 res['data'] = user else: res['result'] = 0 res['message'] = 'The password is wrong.' else: res['result'] = 0 res['message'] = 'The user does not exist.' else: res['result'] = 0 res['message'] = 'Wrong request.' return HttpResponse(json.dumps(res), content_type="application/json")
def deleteUser(request): res = {} if request.method == 'POST': info = json.loads(request.body) if UserAccess.getUserById(info['id']): final = UserAccess.deleteUserById(info['id']) res['result'] = final['result'] res['message'] = final['message'] else: res['result'] = 0 res['message'] = 'The user does not exist!' else: res['result'] = 0 res['message'] = 'Wrong request.' return HttpResponse(json.dumps(res), content_type="application/json")
def assignProject(request): res = {} if request.method == 'POST': info = json.loads(request.body) user = UserAccess.getUserById(info['user_id']) if user: if info['op'] == 1: user['status'] = 1 user['project_id'] = info['project_id'] project_user_record = { "id": uuid.uuid1().hex, "project_id": info['project_id'], "user_id": user["id"], "start_time": datetime.now(), "end_time": "" } ProjectUserRecordAccess.addProjectUserRecord( project_user_record) else: user['status'] = 0 user['project_id'] = '' user_record_list = ProjectUserRecordAccess.getAllProjectUserRecord( ) for item in user_record_list: if item['user_id'] == user['id'] and item['end_time'] == "": user_record = item user_record['end_time'] = datetime.now() ProjectUserRecordAccess.editProjectUserRecord( user_record) break final = UserAccess.editUser(user) res['result'] = final['result'] res['message'] = final['message'] else: res['result'] = 0 res['message'] = 'The user does not exist!' else: res['result'] = 0 res['message'] = 'Wrong request.' return HttpResponse(json.dumps(res), content_type="application/json")
def assignTask(request): res = {} if request.method == 'POST': info = json.loads(request.body) vehicle = VehicleAccess.getVehicleById(info['vehicle_id']) user = UserAccess.getUserById(vehicle['user_id']) if vehicle: path = getPath(info['task']) if path: task = { 'origin' : info['task']['origin'], 'dest' : info['task']['dest'], 'path' : path } record = { 'id' : uuid.uuid1().hex, 'vehicle_id' : vehicle['id'], 'user_id' : user['id'], 'time' : datetime.now(), 'task' : task } TaskRecordAccess.addTaskRecord(record) res['result'] = 1 res['data'] = path vehicle['accept'] = 1 vehicle['task'] = task if user['status'] == 2: user['status'] = 3 vehicle['status'] = 3 UserAccess.editUser(user) VehicleAccess.editVehicle(vehicle) else: res['result'] = 0 res['message'] = 'Can not get the path from baidu map.' else: res['result'] = 0 res['message'] = 'The vehicle does not exist!' else: res['result'] = 0 res['message'] = 'Wrong request.' return HttpResponse(json.dumps(res), content_type="application/json")
def login(request): res = {} if request.session.session_key: res['result'] = 0 res['message'] = 'The user has logon.' elif request.method == 'POST': info = json.loads(request.body) user_name = info['user_name'] password = info['password'] vehicle_id = info['vehicle_id'] user = UserAccess.getUserByName(user_name) vehicle = VehicleAccess.getVehicleById(vehicle_id) if user: if vehicle['user_id'] != user['id']: res['result'] = 0 res['message'] = 'The user login wrong vehicle or no vehicle assigned to the user.' elif hashlib.sha1( info['password']).hexdigest() == user['password']: request.session['user_id'] = user['id'] request.session['vehicle_id'] = vehicle_id res['result'] = 1 res['data'] = {} res['data']['user_id'] = user['id'] res['data']['user_type'] = user['type'] res['data']['name'] = user['name'] if vehicle['task']: user['status'] = 3 vehicle['status'] = 3 else: user['status'] = 2 vehicle['status'] = 2 UserAccess.editUser(user) VehicleAccess.editVehicle(vehicle) else: res['result'] = 0 res['message'] = 'The password is wrong.' else: res['result'] = 0 res['message'] = 'The user does not exist.' else: res['result'] = 0 res['message'] = 'Wrong request.' return HttpResponse(json.dumps(res), content_type="application/json")
def assignProject(request): res = {} if request.method == 'POST': info = json.loads(request.body) user = UserAccess.getUserById(info['user_id']) if user: if info['op'] == 1: user['status'] = 1 user['project_id'] = info['project_id'] project_user_record = { "id" : uuid.uuid1().hex, "project_id" : info['project_id'], "user_id" : user["id"], "start_time" : datetime.now(), "end_time" : "" } ProjectUserRecordAccess.addProjectUserRecord(project_user_record) else: user['status'] = 0 user['project_id'] = '' user_record_list = ProjectUserRecordAccess.getAllProjectUserRecord() for item in user_record_list: if item['user_id'] == user['id'] and item['end_time'] == "": user_record = item user_record['end_time'] = datetime.now() ProjectUserRecordAccess.editProjectUserRecord(user_record) break final = UserAccess.editUser(user) res['result'] = final['result'] res['message'] = final['message'] else: res['result'] = 0 res['message'] = 'The user does not exist!' else: res['result'] = 0 res['message'] = 'Wrong request.' return HttpResponse(json.dumps(res), content_type="application/json")
def login(request): res = {} if request.session.session_key: res['result'] = 0 res['message'] = 'The user has logon.' elif request.method == 'POST': info = json.loads(request.body) user_name = info['user_name'] password = info['password'] vehicle_id = info['vehicle_id'] user = UserAccess.getUserByName(user_name) vehicle = VehicleAccess.getVehicleById(vehicle_id) if user: if vehicle['user_id'] != user['id']: res['result'] = 0 res['message'] = 'The user login wrong vehicle or no vehicle assigned to the user.' elif hashlib.sha1(info['password']).hexdigest() == user['password']: request.session['user_id'] = user['id'] request.session['vehicle_id'] = vehicle_id res['result'] = 1 res['data'] = {} res['data']['user_id'] = user['id'] res['data']['user_type'] = user['type'] res['data']['name'] = user['name'] if vehicle['task']: user['status'] = 3 vehicle['status'] = 3 else: user['status'] = 2 vehicle['status'] = 2 UserAccess.editUser(user) VehicleAccess.editVehicle(vehicle) else: res['result'] = 0 res['message'] = 'The password is wrong.' else: res['result'] = 0 res['message'] = 'The user does not exist.' else: res['result'] = 0 res['message'] = 'Wrong request.' return HttpResponse(json.dumps(res), content_type="application/json")
def logout(request): res = {} if not request.session.session_key: res['result'] = 0 res['message'] = 'The user does not log in' elif request.method == 'GET': request.session.flush() res['result'] = 1 user = UserAccess.getUserById(request.session['user_id']) vehicle = VehicleAccess.getVehicleById(request.session['vehicle_id']) user['status'] = 1 vehicle['status'] = 1 UserAccess.editUser(user) VehicleAccess.editVehicle(vehicle) else: res['result'] = 0 res['message'] = 'Wrong request.' return HttpResponse(json.dumps(res), content_type="application/json")
def addUser(request): res = {} if request.method == 'POST': info = json.loads(request.body) if not UserAccess.getUserByName(info['user_name']): info['password'] = hashlib.sha1(info['password']).hexdigest() info['id'] = uuid.uuid1().hex info['status'] = 0 info['project_id'] = '', info['vehicle_id'] = '', final = UserAccess.addUser(info) res['result'] = final['result'] res['message'] = final['message'] else: res['result'] = 0 res['message'] = 'The userName has exist!' else: res['result'] = 0 res['message'] = 'Wrong request.' return HttpResponse(json.dumps(res), content_type="application/json")
def vehicleData(request): res = {} if request.method == 'POST': info = json.loads(request.body) vehicle_id = info['vehicle_id'] vehicle = VehicleAccess.getVehicleById(vehicle_id) if vehicle: vehicle['data'] = info['data'] vehicle_record = { 'id' : uuid.uuid1().hex, 'vehicle_id' : vehicle_id, 'time' : datetime.now(), 'data' : info['data'] } VehicleRecordAccess.addVehicleRecord(vehicle_record) if vehicle['status'] == 3 \ and info['data']['location']['lat'] == vehicle['task']['dest']['lat'] \ and info['data']['location']['lng'] == vehicle['task']['dest']['lng']: vehicle['task'] = {} vehicle['status'] = 2 user = UserAccess.getUserBuId(vehicle['user_id']) user['status'] =2 UserAccess.editUser(user) res['data'] = {} res['result'] = 1 VehicleAccess.editVehicle(vehicle) else: res['result'] = 0 res['message'] = 'The vehicle does noe exist.' else: res['result'] = 0 res['message'] = 'Wrong request.' return HttpResponse(json.dumps(res), content_type="application/json")
def vehicleData(request): res = {} if request.method == 'POST': info = json.loads(request.body) vehicle_id = info['vehicle_id'] vehicle = VehicleAccess.getVehicleById(vehicle_id) if vehicle: vehicle['data'] = info['data'] vehicle_record = { 'id': uuid.uuid1().hex, 'vehicle_id': vehicle_id, 'time': datetime.now(), 'data': info['data'] } VehicleRecordAccess.addVehicleRecord(vehicle_record) if vehicle['status'] == 3 \ and info['data']['location']['lat'] == vehicle['task']['dest']['lat'] \ and info['data']['location']['lng'] == vehicle['task']['dest']['lng']: vehicle['task'] = {} vehicle['status'] = 2 user = UserAccess.getUserBuId(vehicle['user_id']) user['status'] = 2 UserAccess.editUser(user) res['data'] = {} res['result'] = 1 VehicleAccess.editVehicle(vehicle) else: res['result'] = 0 res['message'] = 'The vehicle does noe exist.' else: res['result'] = 0 res['message'] = 'Wrong request.' return HttpResponse(json.dumps(res), content_type="application/json")
def getCurrentUser(request): res = {} if not request.session.session_key: res['result'] = 0 res['message'] = 'No user online.' elif request.method == 'GET': user_id = request.session['user_id'] res['result'] = 1 res['data'] = UserAccess.getUserById(user_id) else: res['result'] = 0 res['message'] = 'Wrong request.' return HttpResponse(json.dumps(res), content_type="application/json")
def getAllUser(request): res = {} if not request.session.session_key: res['result'] = 0 res['message'] = 'The user does not log in' elif request.method == 'GET': userList = UserAccess.getAllUser() if len(userList) == 0: res['result'] = 0 res['message'] = 'No user.' else: res['result'] = 1 res['data'] = userList else: res['result'] = 0 res['message'] = 'Wrong request.' return HttpResponse(json.dumps(res), content_type="application/json")