def delete_alert(id): """ Delete an alert. """ return 'Not Implemented', 501 # We do not support a UI for this if current_user.role == 'advocate': alert = Alert.get_user_alert(current_user, id) else: alert = Alert.get(id) if not alert: return api_error('No alert was found.', 404) alert.delete() return '', 200
def send_out_alert(alert_form): """ Send out an alert to providers. """ alert = Alert( description=alert_form.description.data, gender=alert_form.gender.data, age=alert_form.age.data, user=current_user, ) alert.save() need_ids = alert_form.needs.data for service in Service.get_by_ids(need_ids): need = Need(alert=alert, service=service) need.save() providers = User.providers_with_services(need_ids) for provider in providers: needs_provided = [ need_ for need_ in provider.services if need_.id in need_ids ] gender = alert.get_gender() needs = ", ".join( [need_provided.name for need_provided in needs_provided]) body = ('New 15th night alert!\n' '%d y/o%s\n' 'Needs: %s\n' 'Desc: %s\n' 'Respond at %sr/%s') % ( alert.age, gender, needs, alert_form.description.data, url_for('index', _external=True), str(alert.id) ) provider_notified = ProviderNotified( provider=provider, alert=alert, needs=Need.get_by_ids([need_id.id for need_id in needs_provided]) ) # TODO: test provider_notified.save() queue_send_message.apply_async( kwargs=dict( email=provider.email, number=provider.phone_number, subject='15th Night Alert', body=body ) )
def get_alerts(): """ Gets list of a alerts. Admin gets a list of all results. Provider gets a list of their outstanding alerts. Advocate gets a list of their sent alerts. """ # TODO: pagination if current_user.role == 'advocate': alerts = Alert.get_advocate_alerts(current_user) elif current_user.role == 'provider': scope = request.args.get('scope') if scope == 'all': alerts = Alert.get_provider_alerts(current_user) elif scope == 'responded': alerts = Alert.get_responded_alerts_for_provider(current_user) else: alerts = Alert.get_active_alerts_for_provider(current_user) else: alerts = Alert.get_alerts() return jsonify(alerts)
def get_alerts(): """ Gets list of a alerts. Admin gets a list of all results. Provider gets a list of their outstanding alerts. Advocate gets a list of their sent alerts. """ # TODO: pagination if current_user.role == 'advocate': alerts = Alert.get_advocate_alerts(current_user) elif current_user.role == 'provider': scope = request.args.get('scope') if scope == 'all': alerts = Alert.get_provider_alerts(current_user) elif scope == 'responded': alerts = Alert.get_responded_alerts_for_provider(current_user) else: alerts = Alert.get_active_alerts_for_provider(current_user) else: alerts = Alert.get_admin_alerts() return jsonify(alerts)
def delete_response(uuid): """ Delete a response to an alert. """ return 'Not Implemented', 501 # We currently don't support a UI for this alert = Alert.get(uuid) if not alert: return api_error('Alert not found.', 404) if current_user.role == 'advocate' and alert.user.id != current_user.id: return api_error('Forbidden.', 403) alert.delete() return '', 202
def get_alert(alert_id): alert = Alert.get(alert_id) if not alert: return api_error('Alert not found') if current_user.role == 'provider': if not alert.provider_has_permission(current_user): return api_error('Permission denied') data = alert.to_provider_json(current_user) elif current_user.role == 'advocate': if alert.user.id != current_user.id: return api_error('Permission denied') data = alert.to_advocate_json() else: # is an admin data = alert.to_json() return jsonify(data)
def get_alert(alert_id): alert = Alert.get(alert_id) if not alert: return api_error('Alert not found') if current_user.role == 'provider': if not alert.provider_has_permission(current_user): return api_error('Permission denied') data = alert.to_provider_json(current_user) elif current_user.role == 'advocate': if alert.user.id != current_user.id: return api_error('Permission denied') data = alert.to_advocate_json() else: # is an admin data = alert.to_advocate_json() return jsonify(data)
def send_out_alert(alert_form): """ Send out an alert to providers. """ alert = Alert( description=alert_form.description.data, gender=alert_form.gender.data, age=alert_form.age.data, user=current_user, ) alert.save() need_ids = alert_form.needs.data for service in Service.get_by_ids(need_ids): need = Need(alert=alert, service=service) need.save() providers = User.providers_with_services(need_ids) for provider in providers: needs_provided = [need for need in provider.services if need.id in need_ids] gender = " " + alert.gender if alert.gender != "unspecified" else "" needs = ", ".join([need.name for need in needs_provided]) body = ("New 15th night alert!\n" "%d y/o%s\n" "Needs: %s\n" "Desc: %s\n" "Respond at %s/r/%s") % ( alert.age, gender, needs, alert_form.description.data, HOST_NAME, str(alert.id), ) provider_notified = ProviderNotified( provider=provider, alert=alert, needs=Need.get_by_ids([need.id for need in needs_provided]) ) # TODO: test provider_notified.save() queue_send_message.apply_async( kwargs=dict(email=provider.email, number=provider.phone_number, subject="15th Night Alert", body=body) )
def resolve_all_alert_needs(alert_id): alert = Alert.get(alert_id) if not alert: return api_error('Alert not found') for need in alert.needs: need.resolved = True need.resolved_at = datetime.utcnow() need.save(False) alert.save() if ('notifyProvidersAllResolved' in request.json and request.json['notifyProvidersAllResolved']): send_out_alert_closed(alert) return '', 200
def create_response(): """ Create a response to an alert. POST params: - alert_id: alert identifier - message: response message """ if 'alert_id' not in request.json or 'needs_provided' not in request.json: return api_error('Invalid form') alert = Alert.get(int(request.json['alert_id'])) if not alert: return api_error('Alert not found.', 404) respond_to_alert(current_user, request.json['needs_provided'], alert) return '', 201