def get_trips(request): response = {} user_id = request.POST['user_id'] response["transactions"] = [] user = AuthUserDemographic.get_user_by_id(user_id=user_id) if user.is_ambulance_driver(): trips = Transaction.objects.filter(driver=user, status=END_TRIP) else: trips = Transaction.objects.filter(patient=user, status=END_TRIP) for transaction in list(trips): temp = {} temp["id"] = transaction.id temp["from_lat"] = transaction.final_from_lat temp["from_long"] = transaction.final_from_long temp["to_lat"] = transaction.final_to_lat temp["to_long"] = transaction.final_to_long temp["date"] = str(transaction.created_at.date()) temp["from_area"] = get_area(transaction.final_from_lat, transaction.final_from_long) temp["to_area"] = get_area(transaction.final_to_lat, transaction.final_to_long) temp["charge"] = transaction.final_charge temp["status"] = transaction.status response["transactions"].append(temp) response["status"] = "ok" print(response) return JsonResponse(response, content_type='application/json')
def start_trip(request): if request.method == "POST": try: final_from_lat = request.POST['final_from_lat'] final_from_long = request.POST['final_from_long'] trans_id = request.POST['transaction_id'] user_id = request.POST['user_id'] driver = AuthUserDemographic.get_user_by_id(user_id) import pdb #pdb.set_trace() trans = Transaction.objects.get(id = trans_id) trans.final_from_lat = final_from_lat trans.final_from_long = final_from_long trans.status = "Patient Picked Up" trans.save() send_patient_tripstart_notification(trans.patient,trans) response = json.dumps({'status': 'ok', 'init_to_lat':trans.init_to_lat, 'init_to_long':trans.init_to_long, 'area':get_area(trans.init_to_lat,trans.init_to_long)}) except Exception as ex: response = json.dumps({'status': 'error', 'message': str(ex)}) else: response = json.dumps({'status': 'error', 'message': "Method not allowed"}) return HttpResponse(response, content_type='application/json')
def accept_ambulance_request(request): if request.method == "POST": try: transaction_id = request.POST['transaction_id'] user_id = request.POST['user_id'] registration_number = request.POST['registration_number'] ambulance = Ambulance.get_ambulance_with_reg_num(registration_number) driver = AuthUserDemographic.get_user_by_id(user_id) import pdb #pdb.set_trace() trans = get_object_or_none(Transaction,id = transaction_id) if trans: trans.driver = driver trans.ambulance = ambulance trans.status = "Request Accepted" trans.save() send_patient_accept_notification(trans.patient,trans) response = json.dumps({'status': 'ok', 'message': "Go to Patient Location", "patient_lat":trans.init_from_lat,'patient_long':trans.init_from_long, 'patient_name':trans.patient.first_name,'patient_mobile':trans.patient.mobile}) except Exception as ex: response = json.dumps({'status': 'error', 'message': str(ex)}) else: response = json.dumps({'status': 'error', 'message': "Method not allowed"}) return HttpResponse(response, content_type='application/json')
def find_driver(request): if request.method == "POST": try: init_from_lat = request.POST['from_lat'] init_to_lat = request.POST['to_lat'] init_from_long = request.POST['from_long'] init_to_long = request.POST['to_long'] user_id = request.POST['user_id'] patient = AuthUserDemographic.get_user_by_id(user_id) distance = request.POST['distance'] trans = Transaction(init_from_lat=init_from_lat,init_to_lat=init_to_lat, init_from_long=init_from_long,init_to_long=init_to_long, init_distance=distance,status="Waiting For Driver", patient=patient) trans.save() closest_ambulance = AmbulanceLocation.get_closest_ambulance(lat_a=init_from_lat,long_a=init_from_long) if closest_ambulance: driver = AmbulanceDriverAssignment.get_ambulance_driver(closest_ambulance.ambulance).driver send_driver_request_notification(driver.user,trans) response = json.dumps({'status': 'ok', 'message': "Connecting to Ambulance","transaction_id":trans.id}) else: response = json.dumps({'status': 'error', 'message': "No Ambulance"}) except Exception as ex: response = json.dumps({'status': 'error', 'message': str(ex)}) else: response = json.dumps({'status': 'error', 'message': "Method not allowed"}) return HttpResponse(response, content_type='application/json')
def add_ambulance_driver(request,pk): if request.method == "POST": new_ambulance_driver_form = NewAmbulanceDriverForm(request.POST) if new_ambulance_driver_form.is_valid(): firstname = new_ambulance_driver_form.cleaned_data['first_name'] nationality = new_ambulance_driver_form .cleaned_data['nationality'] surname = new_ambulance_driver_form .cleaned_data['surname'] mobile = new_ambulance_driver_form .cleaned_data['mobile'] email = new_ambulance_driver_form .cleaned_data['email'] address = new_ambulance_driver_form .cleaned_data['address'] dob = new_ambulance_driver_form .cleaned_data['date_of_birth'] sex = new_ambulance_driver_form .cleaned_data['sex'] driver_license_number = new_ambulance_driver_form .cleaned_data['driver_license_number'] password = randomPassword() now = datetime.datetime.now() try: user = User.objects.create_user(username=email, password=password,) user.groups.add(Group.objects.get_or_create(name="Ambulance Driver")[0]) user.save() except Exception as ab: messages.error(request, ab) else: user_info = AuthUserDemographic(user=user, email=email, first_name=firstname, surname=surname, sex=sex, date_of_birth=dob, nationality=nationality, address=address, mobile=mobile ) user_info.save() user_info.unique_id = '{}{}{}{}'.format(now.day, now.month, now.year, add_zeros(5, str(user_info.id))) user_info.save() ambulance_service = AmbulanceService.objects.get(id=pk) absa = AmbulanceDriver(user=user_info,driver_license_number=driver_license_number, ambulance_service= ambulance_service) absa.save() #sendsms(request, mobile, pin) send_pin_register(mobile,password) _sending_enterprise_acc_creation_password(user_info, password) messages.success(request, "Ambulance Driver Added Successfully") else: context = {'new_ambulance_driver_form': new_ambulance_driver_form } return render(request, 'add_ambulance_driver.html', context) new_ambulance_driver_form = NewAmbulanceDriverForm() context = {'new_ambulance_driver_form': new_ambulance_driver_form } return render(request, 'add_ambulance_driver.html', context)
def patient_register(request): if request.method == 'POST': try: email = request.POST['email'] password = request.POST['password'] countrycode = request.POST['country_code'] phone_number = request.POST['phone_number'] try: user = User.objects.create_user( username=email, password=password, ) user.groups.add(Group.objects.get_or_create(name="Patient")[0]) user.save() except Exception as ab: response = json.dumps({ 'status': 'error', 'message': 'Account already exist' }) else: now = datetime.datetime.now() user_info = AuthUserDemographic( user=user, email=email, nationality=Country.objects.get(iso=countrycode).name, mobile=phone_number) user_info.save() user_info.unique_id = 'PA{}{}{}{}'.format( now.day, now.month, now.year, add_zeros(5, str(user_info.id))) user_info.save() response = json.dumps({ 'status': 'ok', 'message': 'Account has been created successfully' }) except Exception as exs: message = exs.args response = json.dumps({'status': 'error', 'message': message}) else: response = json.dumps({ 'status': 'error', 'message': "something went wrong" }) return HttpResponse(response, content_type='application/json')
def end_trip(request): if request.method == "POST": try: final_to_lat = request.POST['final_to_lat'] final_to_long = request.POST['final_to_long'] trans_id = request.POST['transaction_id'] user_id = request.POST['user_id'] user = AuthUserDemographic.get_user_by_id(user_id) import pdb #pdb.set_trace() trans = Transaction.objects.get(id = trans_id) trans.final_to_lat = final_to_lat trans.final_to_long = final_to_long trans.save() if trans.status == START_TRIP: trans.status = END_TRIP distance = calc_dist(eval(str(trans.final_from_lat)), eval(str(trans.final_from_long)), eval(str(trans.final_to_lat)), eval(str(trans.final_to_long))) trans.final_distance = distance cityrate = AmbulanceRate.objects.filter(city="Accra") price = get_price(cityrate[0], str(distance)) trans.final_charge = str(price) response = json.dumps({'status': 'ok','truncated': False, 'charge': str(price)}) else: trans.status = TRIP_TRUNCATED response = json.dumps({'status': 'ok', 'truncated': True}) trans.save() if user.is_ambulance_driver(): send_party_tripend_notification(trans.patient,trans) else: send_party_tripend_notification(trans.driver, trans) except Exception as ex: response = json.dumps({'status': 'error', 'message': str(ex)}) else: response = json.dumps({'status': 'error', 'message': "Method not allowed"}) return HttpResponse(response, content_type='application/json')