def auth(request): if request.method == 'GET': email = request.GET.get('email', '') if not email or not validate_email(email): raise BadRequest("Invalid Email Address") otp = otpgen() gmail = Gmail(settings.GMAIL_USER, settings.GMAIL_PASSWORD) gmail.send_message( email, 'Vaccine Alert One Time Password', f'Please use OTP - {otp} to register for vaccine alert') encrypted_key = encrypt(otp).decode() data = {'encrypted_key': encrypted_key} return OK(data) if request.method == 'POST': body_unicode = request.body.decode('utf-8') request_data = json.loads(body_unicode) authorize_user(request_data['encrypted_key'], request_data['otp']) validate_email(request_data['email']) user_details = UserDetails.objects.fetch_user_details( request_data['email']) data = {"data": {"userDetails": user_details}} return OK(data)
def receive_referrals(request, provider_id): referral = list( Referrals.objects.raw({ 'referredToId': provider_id }).values()) response = {'data': referral} return OK(response)
def show_appointments(request, provider_id): appointment = list( Appointments.objects.raw({ 'providerId': provider_id }).values()) response = {'data': appointment} return OK(response)
def manage_videos(request): """View to manage videos request Args: request: A Django HttpRequest Returns: response: videos list """ query_params = request.GET limit = query_params.get("limit", 10) reference_id = query_params.get("reference_id", "") path_info = request.META.get("PATH_INFO") fetch_videos() video_list = Video.objects.fetch_videos(reference_id, limit) pagination_url = None if len(video_list) == int(limit): reference_id = video_list[-1].get("_id") pagination_url = create_pagination_url( url=path_info, limit=limit, query_params={"reference_id": reference_id}) response = { "metadata": { "count": len(video_list), "nextUrl": pagination_url }, "data": video_list, } return OK(response)
def manage_videos_search(request): """View to manage videos request Args: request: A Django HttpRequest Returns: response: videos list """ if request._body: request_data = json.loads(request._body) else: raise BadRequest("No query in POST body") # set of allowed search keys search_keys = {"title", "description", "channelTitle", "videoURL"} query = {} for term, value in request_data.items(): if term in search_keys: query[term] = value # searching videos with required query video_list = Video.objects.search_videos(query) response = {"metadata": {"count": len(video_list)}, "data": video_list} return OK(response)
def station(request): result = Station.objects.get_all(projection={'_id':0}) response = { 'data': result } return OK(response)
def manage_districts(request, state_code): """View to manage states request Args: request: A Django HttpRequest Returns: response: districts list """ districts = fetch_districts(state_code) return OK(districts)
def manage_states(request): """View to manage states request Args: request: A Django HttpRequest Returns: response: states list """ states = fetch_states() return OK(states)
def book_rickshaw(request): data = json.loads(request.body.decode('utf8')) x = data.get('lat',0) y = data.get('lng',0) destination = data.get('stationCode',None) if not destination or not x or not y: raise BadRequest result = CTRickshaw.objects.get_one(queries={ 'location': { '$near' : { '$geometry': { 'type': 'Point', 'coordinates': [y,x] }, '$maxDistance': 200000000 } }, 'capacity' : {'$lt':4}, 'destination': destination }) resul = CTRickshaw.objects.update_one(queries={ 'location': { '$near' : { '$geometry': { 'type': 'Point', 'coordinates': [y,x] }, '$maxDistance': 200000000 } }, 'capacity' : {'$lt':4}, 'destination': destination }, data = { '$inc' : {'capacity':1} }) print(result) if result: result['capacity'] += 1 return OK({'data':result}) else: return NoContent()
def manage_patient_referrals(request, patient_id): auth_token = request.META.get('HTTP_AUTHORIZATION') request_id = request.request_id # create patient referral if request.method == 'POST': user_id = request.user_data['user_id'] request_data = request._json_body data = { 'urgency': request_data.get('urgency', ''), 'speciality': request_data.get('speciality', ''), 'insuranceDetails': request_data.get('insuranceDetails', ''), 'referralReason': request_data.get('referralReason', ''), 'createdBy': user_id, 'referringProvider': request_data.get('referringProvider', ''), 'referredProvider': request_data.get('referredProvider', ''), 'attachments': request_data.get('attachments', ''), 'patientId': request_data.get('patientId', '') } response = create_referral(auth_token, data) return OK(response.get('message', '')) # get list of patient referrals if request.method == 'GET': response = fetch_referrals(auth_token, patient_id) return OK(response) # "speciality":"Cardiologist", # "urgency":"urgent", # "referralReason":"provider is not available", # "referringProviderId":"1234", # "referredProviderId":"2345", # "patientId":"PA1290", # "attachments": {"fileName":"firstfile","link":"/a/b"}, # "payer":"Atena", # "insurancePlan":"gold" # }
def search_providers(request): query = request._json_body # print(type(query)) filters = [] for k, v in query.items(): filters.append({k: v}) query = {'$and': filters} # print(query) resources = list(Provider.objects.raw(query).values()) res = {'data': resources} # print(res) return OK(res)
def calendar_pin(request): """View to manage states request Args: request: A Django HttpRequest Returns: response: calendar by pin """ url_params = { "pincode": request.GET.get('pincode', ''), "date": request.GET.get('date', '') } calendar_pin = fetch_calender_by_pin(url_params) return OK(calendar_pin)
def calendar_district(request): """View to manage states request Args: request: A Django HttpRequest Returns: response: calendar by districts """ url_params = { "district_id": request.GET.get('district_id', ''), "date": request.GET.get('date', '') } calendar_district = fetch_calender_by_district(url_params) return OK(calendar_district)
def search_provider(request, patient_zipcode): if request.method == 'GET': user_id = request.user_data['user_id'] auth_token = request.META.get('HTTP_AUTHORIZATION') # view_name = request.url_info['view_name'] request_id = request.request_id speciality = request.GET.get('speciality') patient_zipcode = request.GET.get('patient_zipcode') # is_name = request.GET.get('is_name') # referring_provider_name = request.GET.get('name') response = inref_search_provider(auth_token, speciality, patient_zipcode) return OK(response)
def delete(self, request, grid_id): '''DELETE method for each Gride Args: request: Request object grid_id: String(UUID) Return: JSONResponse Raise: BadRequest ''' #delete each grid err, res = self.handler_obj.delete_grid(grid_id) if err: raise BadRequest(400, res) return OK({'message': res, 'statusCode': 200})
def register_user(request): body_unicode = request.body.decode('utf-8') request_data = json.loads(body_unicode) authorize_user(request_data['encrypted_key'], request_data['otp']) if request.method == 'POST': email = request_data['email'] pincode = request_data.get("pincode") if not email or not validate_email(email): raise BadRequest("Invalid Email Address") user_details = UserDetails.objects.fetch_user_details(email) if user_details: raise BadRequest("User Alerady registered") if pincode and not validate_pincode(pincode): raise BadRequest("Invalid Pincode") try: user_details = { "email": request_data['email'], "district": request_data.get("district"), "pincode": request_data.get("pincode"), "age": request_data["age"], } user_details = UserDetails.objects.insert_user_details( user_details) except Exception: raise BadRequest("Email, district, pincode, age are required") return OK({'data': user_details}) if request.method == 'PATCH': email = request_data['email'] user_details = UserDetails.objects.fetch_user_details(email) if (user_details['active'] == request_data['active'] and user_details.get("pincode", '') == request_data.get( "pincode", '') and user_details['age'] == request_data['age'] and user_details.get("district", '') == request_data.get( "district", '')): raise BadRequest("Nothing to Update") if not email or not validate_email(email) or not user_details: raise BadRequest("Invalid Email Address") if request_data.get("district", '') and validate_pincode( request_data.get("district", '')): raise BadRequest("Invalid Pincode") try: user_details = { "district": request_data.get("district"), "pincode": request_data.get("pincode"), "age": request_data["age"], "active": request_data["active"] } user_details = UserDetails.objects.update_user_details( email, user_details) except Exception: raise BadRequest("Email, district, pincode, age are required") return OK(user_details)
def best_stations(request): data = json.loads(request.body.decode('utf8')) x = data.get('lat',0) y = data.get('lng',0) result = Station.objects.get_all(queries={ 'location': { '$near' : { '$geometry': { 'type': 'Point', 'coordinates': [y,x] }, '$maxDistance': 200000000 } } }) result = list(result[:3]) d = datetime.now() # print(d) # print(result['stationCode']) for r in result: week_before = weekbefore(d, st_cd1[r['stationCode']] ,arr) month_before = monthbefore(d, st_cd1[r['stationCode']] ,arr) year_before = yearbefore(d, st_cd1[r['stationCode']] ,arr) two_year_before = twoyearbefore(d, st_cd1[r['stationCode']] ,arr) directions_result = gmaps.directions({'lat':r['location'][1],'lng':r['location'][0]}, {'lat':x,'lng':y}, mode="driving", departure_time=d ) r['eta'] = directions_result[0]['legs'][0]['duration']['text'] total = 0 cnt = 0 if week_before: total += week_before*8 cnt += 8 if month_before: total += month_before*4 cnt += 4 if year_before: total += year_before*2 cnt += 2 if two_year_before: total+= two_year_before*1 cnt += 1 if cnt!=0: r['population'] = int(total/cnt) else: r['population'] = 0 response = { 'data' : sorted(result, key=itemgetter('population')) } return OK(response)