def change_current_site(request): context = get_context(request) provider = user_is_provider(request.user) staffer = user_is_office_staff(request.user) if (provider): user = provider if (staffer): user = staffer sites = user.sites.all() if (request.method == "POST"): current = request.POST.get('id') form = CurrentSiteForm(sites, request.POST) context['site_form'] = form if (form.is_valid()): new_site_id = int(context['site_form'].cleaned_data['current']) if (new_site_id == 0): new_site = None else: site_ids = [site.id for site in sites] if (not new_site_id in site_ids): raise Exception(_("User tried to select site id ") + str(new_site_id) + _(", but isn't affiliated with that site.")) new_site = Site.objects.filter(id=new_site_id).all() if (new_site.count() != 1): raise Exception(_("Incorrect number of sites found for id ") + str(new_site_id)) new_site = new_site[0] user.current_site = new_site user.save() return HttpResponse(json.dumps({'status': 'ok'})) else: err_obj = { 'errors': form.errors, } return HttpResponseBadRequest(json.dumps(err_obj), mimetype='application/json')
def sendSMS_Twilio(request, message, msg_body): """Sends the given message via Twilio. Note that if the message is longer than 160 characters, it will be appropriately fragmented, up to a limit of 99 fragments. :param request: The HTTP Request object :param message: The MHLogin.DoctorCom.models.Message object to send. :returns: None :raises: Passes on Exceptions from msg_fragmenter() and twilioSMS_sender(). """ logger.debug('%s: into sendMSM_Twilio with message from %s %s with body %s' % (request.session.session_key, message.sender.first_name, message.sender.last_name, msg_body)) # Prepend the DoctorCom notification msg_body = _('DoctorCom msg from %(first_name)s %(last_name)s: %(msg_body)s') \ % { 'first_name': message.sender.first_name, 'last_name': message.sender.last_name, 'msg_body': msg_body } sms_msgs = msg_fragmenter(msg_body) # pass Exceptions on to the parent sender_provider = user_is_provider(message.sender) recipient_pks = list(message.recipients.values_list('pk', flat=True)) recipients = MHLUser.objects.in_bulk(recipient_pks) for recipient_key in recipients.keys(): recipient = recipients[recipient_key] #if recipient is Office Staff, check for cell phone #any office staff cand send but only those with cell phone can receive #skip those without cell, they only get dashboard msq manager_info = OfficeStaff.objects.filter(user=recipient) if (manager_info.count() > 0 and not manager_info[0].user.mobile_phone): continue # sender's phone number callerID = None current_site = None if (sender_provider and sender_provider.mdcom_phone): callerID = sender_provider.mdcom_phone logger.debug('sender %s is provider. Current site is %s' % (sender_provider, sender_provider.current_site)) current_site = sender_provider.current_site else: logger.debug('sender %s pk %i is not provider. No Current site.' % (message.sender, message.sender.pk)) if (not callerID): # TODO: get caller ID from number pool! callerID = alternativeCallerID(message.sender, recipient) for msg_part in sms_msgs: logger.debug('%s: sending message to %s %s with body %s' % (request.session.session_key, recipient.first_name, recipient.last_name, msg_part)) twilioSMS_sender(request, recipient, msg_part, callerID, current_site)
def site_admin(request): context = get_context(request) context['sites'] = Site.objects.all() if (request.POST): if (request.POST['searchRadio'] == 'ST'): if ('state' in request.POST): state = request.POST['state'] context['sites'] = Site.objects.filter(state__iexact=state) elif (request.POST['searchRadio'] == 'PR'): if ('proximity' in request.POST): user = user_is_provider(request.user) # use MHLUser's lat, longit instead of Provider's office_lat, office_longit mylat = user.user.lat mylongit = user.user.longit miles = float(request.POST['proximity']) latmin, latmax, longitmin, longitmax = miles2latlong_range( mylat, mylongit, miles) nearbySites = Site.objects.filter( lat__range=(latmin, latmax)).filter(longit__range=(longitmin, longitmax)) context['sites'] = nearbySites elif (request.POST['searchRadio'] == 'CI'): if ('city' in request.POST): city = request.POST['city'] context['sites'] = Site.objects.filter(city__istartswith=city) elif (request.POST['searchRadio'] == 'ZI'): if ('zip' in request.POST): zip = request.POST['zip'] context['sites'] = Site.objects.filter(zip__istartswith=zip) elif (request.POST['searchRadio'] == 'NA'): if ('name' in request.POST): name = request.POST['name'] context['sites'] = Site.objects.filter(name__istartswith=name) #raise Exception(request.POST) else: context['sites'] = Site.objects.all() context['searchby'] = SiteSearchForm() return render_to_response('Sites/sites_view.html', context)
def change_current_site(request): context = get_context(request) provider = user_is_provider(request.user) staffer = user_is_office_staff(request.user) if (provider): user = provider if (staffer): user = staffer sites = user.sites.all() if (request.method == "POST"): current = request.POST.get('id') form = CurrentSiteForm(sites, request.POST) context['site_form'] = form if (form.is_valid()): new_site_id = int(context['site_form'].cleaned_data['current']) if (new_site_id == 0): new_site = None else: site_ids = [site.id for site in sites] if (not new_site_id in site_ids): raise Exception( _("User tried to select site id ") + str(new_site_id) + _(", but isn't affiliated with that site.")) new_site = Site.objects.filter(id=new_site_id).all() if (new_site.count() != 1): raise Exception( _("Incorrect number of sites found for id ") + str(new_site_id)) new_site = new_site[0] user.current_site = new_site user.save() return HttpResponse(json.dumps({'status': 'ok'})) else: err_obj = { 'errors': form.errors, } return HttpResponseBadRequest(json.dumps(err_obj), mimetype='application/json')
def site_admin(request): context = get_context(request) context['sites'] = Site.objects.all() if (request.POST): if (request.POST['searchRadio'] == 'ST'): if ('state' in request.POST): state = request.POST['state'] context['sites'] = Site.objects.filter(state__iexact=state) elif (request.POST['searchRadio'] == 'PR'): if ('proximity' in request.POST): user = user_is_provider(request.user) # use MHLUser's lat, longit instead of Provider's office_lat, office_longit mylat = user.user.lat mylongit = user.user.longit miles = float(request.POST['proximity']) latmin, latmax, longitmin, longitmax = miles2latlong_range(mylat, mylongit, miles) nearbySites = Site.objects.filter(lat__range=(latmin, latmax)).filter(longit__range=(longitmin, longitmax)) context['sites'] = nearbySites elif (request.POST['searchRadio'] == 'CI'): if ('city' in request.POST): city = request.POST['city'] context['sites'] = Site.objects.filter(city__istartswith=city) elif (request.POST['searchRadio'] == 'ZI'): if ('zip' in request.POST): zip = request.POST['zip'] context['sites'] = Site.objects.filter(zip__istartswith=zip) elif (request.POST['searchRadio'] == 'NA'): if ('name' in request.POST): name = request.POST['name'] context['sites'] = Site.objects.filter(name__istartswith=name) #raise Exception(request.POST) else: context['sites'] = Site.objects.all() context['searchby'] = SiteSearchForm() return render_to_response('Sites/sites_view.html', context)
def twiMLCall_callback(request): form = TwiMLCallbackForm(request.POST) r = twilio.Response() if (not form.is_valid()): r.append( tts( _("A system error has occurred. " "Please contact the administrator or try it later."))) return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE) callSid = form.cleaned_data["CallSid"] caller_user_id = form.cleaned_data["caller_user_id"] called_number = form.cleaned_data["called_number"] called_user_id = form.cleaned_data["called_user_id"] called_practice_id = form.cleaned_data["called_practice_id"] # decide called number or called user. called_user = None if called_user_id: called_users = MHLUser.objects.filter(pk=called_user_id) if (not called_users): r.append(tts(_("The person you called doesn't exist."))) return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE) called_user = called_users[0] called_number = called_user.mobile_phone elif called_practice_id: called_practice = PracticeLocation.objects.filter( id=called_practice_id) if not called_practice: r.append(tts(_("The practice you called doesn't exist."))) return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE) called_number = called_practice[0].practice_phone # decide which number is caller_number. caller_mhluser = MHLUser.objects.get(pk=caller_user_id) caller_provider = user_is_provider(caller_mhluser) caller_manager = None caller_mgrs = Office_Manager.objects.filter(user__user=caller_mhluser) if caller_mgrs: caller_manager = caller_mgrs[0] caller_number = caller_mhluser.mobile_phone if not caller_provider and caller_manager: staffs = OfficeStaff.objects.filter(user=caller_mhluser) if (staffs.count() != 1): pass # staff = staffs[0] if staff.caller_anssvc == 'MO': caller_number = staff.user.mobile_phone elif staff.caller_anssvc == 'OF': caller_number = staff.office_phone elif staff.caller_anssvc == 'OT': caller_number = staff.user.phone else: pass log = Click2Call_Log(caller=caller_mhluser, caller_number=caller_number, callid=callSid, called_number=called_number, called_user=called_user, source='APP') log.save() # decide which number is caller_id. caller_id = settings.TWILIO_CALLER_ID if (caller_provider and caller_provider.mdcom_phone): caller_id = caller_provider.mdcom_phone else: if caller_manager: if caller_manager and caller_manager.user.current_practice and \ caller_manager.user.current_practice.mdcom_phone: caller_id = caller_manager.user.current_practice.mdcom_phone # check called person/number, decide call process if called_user: called_provider = user_is_provider(called_user) if (not called_provider): #office managers can get phone calls too, but they must have mobile phone manager_info = OfficeStaff.objects.filter(user=called_user) if (manager_info.count() > 0 and manager_info[0].user.mobile_phone): called_manager = manager_info[0] if (called_provider): # Send the call through the IVR ProviderIVR_OutsideInit(request, log.caller_number, called_provider, log) request.session['Caller'] = caller_id return ProviderIVR_ForwardCall(request) elif called_manager: # Send the call through the IVR from MHLogin.DoctorCom.IVR.views_practice import \ PracticeIVR_ForwardCall, PracticeIVR_OutsideInit PracticeIVR_OutsideInit(request, log.caller_number, called_manager, log) request.session['click2call'] = True request.session['Caller'] = caller_id return PracticeIVR_ForwardCall(request) dial = twilio.Dial(called_number, callerId=caller_id, timeout=120) r.append(dial) return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)
def manage_sites(request): provider = user_is_provider(request.user) staffer = user_is_office_staff(request.user) if (not (provider or staffer)): return HttpResponseRedirect('/') try: if (provider): user = provider lat = float(user.lat) longit = float(user.longit) elif (staffer): user = staffer if user.current_practice: lat = float(user.current_practice.practice_lat) longit = float(user.current_practice.practice_longit) else: geocode_response = geocode2(user.office_address1, \ user.office_city, user.office_state, user.office_zip) lat = geocode_response['lat'] longit = geocode_response['lng'] except: lat, longit = (0, 0) context = get_context(request) # get the latitutde, longitude range to search for nearby sites if not lat and not longit: latmin, latmax, longitmin, longitmax = (0, 0, 0, 0) else: latmin, latmax, longitmin, longitmax = miles2latlong_range(lat, longit) sites = user.sites.all() if (request.method == "POST"): searchby = '' searchbyvalue = '' """ #raise Exception(request.POST) if ('submit_search_criteria' and 'searchby' and 'searchbyvalue' in request.POST): raise Exception(request.POST) searchby = request.POST['searchby'] searchbyvalue = request.POST['searchbyvalue'] #raise Exception(request.POST) if (searchby == 'proximity'): miles = float(searchbyvalue) del_lat, del_longit = miles2latlong(miles, lat, longit) latmin = lat - del_lat latmax = lat + del_lat longitmin = longit - del_longit longitmax = longit + del_longit #raise Exception(miles, latmin, latmax, longitmin, longitmax) """ context['site_form'] = CurrentSiteForm(sites, request.POST) site_list_ids = [site.id for site in sites] context['affiliation_form'] = SiteManagementForm( searchby, searchbyvalue, latmin, latmax, longitmin, longitmax, site_list_ids, request.POST) # context['affiliation_form'] = SiteManagementForm(request.POST) if (context['site_form'].is_valid()): new_site_id = int(context['site_form'].cleaned_data['current']) if (new_site_id == 0): new_site = None else: site_ids = [site.id for site in sites] if (not new_site_id in site_ids): raise Exception( _("User tried to select site id ") + str(new_site_id) + _(", but isn't affiliated with that site.")) # Check to see if the site exists new_site = Site.objects.filter(id=new_site_id).all() if (new_site.count() != 1): raise Exception( _("Incorrect number of sites found for id ") + str(new_site_id)) new_site = new_site[0] user.current_site = new_site user.save() return HttpResponseRedirect( reverse('MHLogin.MHLUsers.views.profile_view')) elif (context['affiliation_form'].is_valid()): new_sites = context['affiliation_form'].cleaned_data['site_list'] new_site_ids = [site.id for site in new_sites] # This ensures that providers can't remove their current site. The outer check # is just to make sure that an error doesn't occur when the provider doesn't have # a current site. site_match = True if (user.current_site != None): site_match = False for site in new_site_ids: if (user.current_site.id == site): site_match = True if (site_match): user.sites = new_sites user.save() return HttpResponseRedirect( reverse('MHLogin.MHLUsers.views.profile_view')) else: context['affiliation_form'].error_current_site_removal() else: if (user.sites): site_list_ids = [site.id for site in user.sites.all()] site_list = { 'site_list': site_list_ids, 'record_key': '1IFSWP3s8gjRpqzJyvhM', } context['affiliation_form'] = SiteManagementForm('proximity', 'none', latmin, latmax, longitmin, longitmax, site_list_ids, initial=site_list) # context['affiliation_form'] = SiteManagementForm(initial=site_list) else: site_list = dict() site_list = { 'record_key': '1IFSWP3s8gjRpqzJyvhM', } context['affiliation_form'] = SiteManagementForm('proximity', 'none', latmin, latmax, longitmin, longitmax, [], initial=site_list) # context['affiliation_form'] = SiteManagementForm(initial=site_list) if (user.current_site): current_site = { 'current': user.current_site.id, } context['site_form'] = CurrentSiteForm(sites, initial=current_site) else: context['site_form'] = CurrentSiteForm(sites) #context['new_affiliation_form'] = NewSiteManagementForm(initial=site_list) return render_to_response("Sites/sites_edit.html", context)
def manage_sites(request): provider = user_is_provider(request.user) staffer = user_is_office_staff(request.user) if (not (provider or staffer)): return HttpResponseRedirect('/') try: if (provider): user = provider lat = float(user.lat) longit = float(user.longit) elif (staffer): user = staffer if user.current_practice: lat = float(user.current_practice.practice_lat) longit = float(user.current_practice.practice_longit) else: geocode_response = geocode2(user.office_address1, \ user.office_city, user.office_state, user.office_zip) lat = geocode_response['lat'] longit = geocode_response['lng'] except: lat, longit = (0, 0) context = get_context(request) # get the latitutde, longitude range to search for nearby sites if not lat and not longit: latmin, latmax, longitmin, longitmax = (0, 0, 0, 0) else: latmin, latmax, longitmin, longitmax = miles2latlong_range(lat, longit) sites = user.sites.all() if (request.method == "POST"): searchby = '' searchbyvalue = '' """ #raise Exception(request.POST) if ('submit_search_criteria' and 'searchby' and 'searchbyvalue' in request.POST): raise Exception(request.POST) searchby = request.POST['searchby'] searchbyvalue = request.POST['searchbyvalue'] #raise Exception(request.POST) if (searchby == 'proximity'): miles = float(searchbyvalue) del_lat, del_longit = miles2latlong(miles, lat, longit) latmin = lat - del_lat latmax = lat + del_lat longitmin = longit - del_longit longitmax = longit + del_longit #raise Exception(miles, latmin, latmax, longitmin, longitmax) """ context['site_form'] = CurrentSiteForm(sites, request.POST) site_list_ids = [site.id for site in sites] context['affiliation_form'] = SiteManagementForm(searchby, searchbyvalue, latmin, latmax, longitmin, longitmax, site_list_ids, request.POST) # context['affiliation_form'] = SiteManagementForm(request.POST) if (context['site_form'].is_valid()): new_site_id = int(context['site_form'].cleaned_data['current']) if (new_site_id == 0): new_site = None else: site_ids = [site.id for site in sites] if (not new_site_id in site_ids): raise Exception(_("User tried to select site id ") + str(new_site_id) + _(", but isn't affiliated with that site.")) # Check to see if the site exists new_site = Site.objects.filter(id=new_site_id).all() if (new_site.count() != 1): raise Exception(_("Incorrect number of sites found for id ") + str(new_site_id)) new_site = new_site[0] user.current_site = new_site user.save() return HttpResponseRedirect(reverse('MHLogin.MHLUsers.views.profile_view')) elif (context['affiliation_form'].is_valid()): new_sites = context['affiliation_form'].cleaned_data['site_list'] new_site_ids = [site.id for site in new_sites] # This ensures that providers can't remove their current site. The outer check # is just to make sure that an error doesn't occur when the provider doesn't have # a current site. site_match = True if (user.current_site != None): site_match = False for site in new_site_ids: if (user.current_site.id == site): site_match = True if (site_match): user.sites = new_sites user.save() return HttpResponseRedirect(reverse('MHLogin.MHLUsers.views.profile_view')) else: context['affiliation_form'].error_current_site_removal() else: if (user.sites): site_list_ids = [site.id for site in user.sites.all()] site_list = { 'site_list': site_list_ids, 'record_key': '1IFSWP3s8gjRpqzJyvhM', } context['affiliation_form'] = SiteManagementForm('proximity', 'none', latmin, latmax, longitmin, longitmax, site_list_ids, initial=site_list) # context['affiliation_form'] = SiteManagementForm(initial=site_list) else: site_list = dict() site_list = { 'record_key': '1IFSWP3s8gjRpqzJyvhM', } context['affiliation_form'] = SiteManagementForm('proximity', 'none', latmin, latmax, longitmin, longitmax, [], initial=site_list) # context['affiliation_form'] = SiteManagementForm(initial=site_list) if (user.current_site): current_site = { 'current': user.current_site.id, } context['site_form'] = CurrentSiteForm(sites, initial=current_site) else: context['site_form'] = CurrentSiteForm(sites) #context['new_affiliation_form'] = NewSiteManagementForm(initial=site_list) return render_to_response("Sites/sites_edit.html", context)
def twiMLCall_callback(request): form = TwiMLCallbackForm(request.POST) r = twilio.Response() if (not form.is_valid()): r.append(tts(_("A system error has occurred. " "Please contact the administrator or try it later."))) return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE) callSid = form.cleaned_data["CallSid"] caller_user_id = form.cleaned_data["caller_user_id"] called_number = form.cleaned_data["called_number"] called_user_id = form.cleaned_data["called_user_id"] called_practice_id = form.cleaned_data["called_practice_id"] # decide called number or called user. called_user = None if called_user_id: called_users = MHLUser.objects.filter(pk=called_user_id) if (not called_users): r.append(tts(_("The person you called doesn't exist."))) return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE) called_user = called_users[0] called_number = called_user.mobile_phone elif called_practice_id: called_practice = PracticeLocation.objects.filter(id=called_practice_id) if not called_practice: r.append(tts(_("The practice you called doesn't exist."))) return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE) called_number = called_practice[0].practice_phone # decide which number is caller_number. caller_mhluser = MHLUser.objects.get(pk=caller_user_id) caller_provider = user_is_provider(caller_mhluser) caller_manager = None caller_mgrs = Office_Manager.objects.filter(user__user=caller_mhluser) if caller_mgrs: caller_manager = caller_mgrs[0] caller_number = caller_mhluser.mobile_phone if not caller_provider and caller_manager: staffs = OfficeStaff.objects.filter(user=caller_mhluser) if (staffs.count() != 1): pass # staff = staffs[0] if staff.caller_anssvc == 'MO': caller_number = staff.user.mobile_phone elif staff.caller_anssvc == 'OF': caller_number = staff.office_phone elif staff.caller_anssvc == 'OT': caller_number = staff.user.phone else: pass log = Click2Call_Log( caller=caller_mhluser, caller_number=caller_number, callid=callSid, called_number=called_number, called_user=called_user, source='APP' ) log.save() # decide which number is caller_id. caller_id = settings.TWILIO_CALLER_ID if (caller_provider and caller_provider.mdcom_phone): caller_id = caller_provider.mdcom_phone else: if caller_manager: if caller_manager and caller_manager.user.current_practice and \ caller_manager.user.current_practice.mdcom_phone: caller_id = caller_manager.user.current_practice.mdcom_phone # check called person/number, decide call process if called_user: called_provider = user_is_provider(called_user) if (not called_provider): #office managers can get phone calls too, but they must have mobile phone manager_info = OfficeStaff.objects.filter(user=called_user) if (manager_info.count() > 0 and manager_info[0].user.mobile_phone): called_manager = manager_info[0] if(called_provider): # Send the call through the IVR ProviderIVR_OutsideInit(request, log.caller_number, called_provider, log) request.session['Caller'] = caller_id return ProviderIVR_ForwardCall(request) elif called_manager: # Send the call through the IVR from MHLogin.DoctorCom.IVR.views_practice import \ PracticeIVR_ForwardCall, PracticeIVR_OutsideInit PracticeIVR_OutsideInit(request, log.caller_number, called_manager, log) request.session['click2call'] = True request.session['Caller'] = caller_id return PracticeIVR_ForwardCall(request) dial = twilio.Dial(called_number, callerId=caller_id, timeout=120) r.append(dial) return HttpResponse(str(r), mimetype=settings.TWILIO_RESPONSE_MIMETYPE)