Ejemplo n.º 1
0
    def get(self, request):
        user = request.user
        all_exps = Exp.objects.all().order_by('name')
        open_alerts = Alert.objects.filter(dismissed=False).order_by('when')
        classified = {'RUN_OK': [], 'RUN_ERR': [],
                      'RUN_ABO': [], 'END_OK': [],
                      'END_ABO': []}
        total_aborted, total_errors = 0, 0
        for exp in all_exps:
            try:
                status, error, aborted = exp.parse_exp_overview()
                info = exp.parse_exp_info()
                total_aborted += aborted
                total_errors += error
                classified[status].append([exp, info])
            except:
                #what to do if the exp doesn't exist?
                # ALERT! And this alert never ever goes away
                alert = Alert.objects.filter(exp=exp).filter(message='NOT FOUND').filter(dismissed=False)
                if not alert:
                    alert = Alert(exp=exp, message='NOT FOUND')
                    alert.save()
            

        return render(request, self.template_name, 
            {'user':user, 'alerts': open_alerts, 'classified':classified,
             'total_errors': total_errors, 'total_aborted': total_aborted})
Ejemplo n.º 2
0
def createdb():
    print "Creating schema..."
    Subscriber.drop_table(fail_silently=True)
    Subscriber.create_table(fail_silently=True)
    Alert.drop_table(fail_silently=True)
    Alert.create_table(fail_silently=True)
    Caller.drop_table(fail_silently=True)
    Caller.create_table(fail_silently=True)
    seed_towns()
    print "Done."
Ejemplo n.º 3
0
def createdb():
    print "Creating schema..."
    Subscriber.drop_table(fail_silently=True)
    Subscriber.create_table(fail_silently=True)
    Alert.drop_table(fail_silently=True)
    Alert.create_table(fail_silently=True)
    Caller.drop_table(fail_silently=True)
    Caller.create_table(fail_silently=True)
    seed_towns()
    print "Done."
Ejemplo n.º 4
0
def create_new():
    if request.method == 'POST':
        name = request.form['name']
        url = request.form['url']
        price_limit = float(request.form['price_limit'])

        Alert.new_alert(name, url, price_limit)

        return redirect(url_for('.index'))

    return render_template('alerts/create_new.html')
Ejemplo n.º 5
0
def main():
    while True:
        zipcode = input("Please enter a zipcode to see the weather report: ")
        if len(zipcode) != 5:
            print("Please enter a five-digit zipcode.")
            continue
        else:
            break

    conditions = 'http://api.wunderground.com/api/96fd72e23c579a3a/conditions/lang:EN/q/' + zipcode + '.json'
    forecast10day = 'http://api.wunderground.com/api/96fd72e23c579a3a/forecast10day/lang:EN/q/' + zipcode + '.json'
    astronomy = 'http://api.wunderground.com/api/96fd72e23c579a3a/astronomy/lang:EN/q/' + zipcode + '.json'
    alerts = 'http://api.wunderground.com/api/96fd72e23c579a3a/alerts/lang:EN/q/' + zipcode + '.json'
    currenthurricane = 'http://api.wunderground.com/api/96fd72e23c579a3a/currenthurricane/lang:EN/q/' + zipcode + '.json'

# """code for creating JSON test files:"""
# def make_json_test(filename, string):
#     response = requests.get(url=string)
#     data = response.json()
#     with open(filename, 'w') as f:
#         json.dump(data, f)

# filename_list = ['conditions_test.json', 'forecast10day.json', 'astronomy.json', 'alerts.json', 'currenthurricane.json']
# string_list = [conditions, forecast10day, astronomy, alerts, currenthurricane]
# for filename, item in zip(filename_list, string_list):
#     make_json_test(filename, item)

    response1 = requests.get(url=conditions)
    conditions_dict = response1.json()

    response2 = requests.get(url=forecast10day)
    forecast10day_dict = response2.json()

    response3 = requests.get(url=astronomy)
    astronomy_dict = response3.json()

    response4 = requests.get(url=alerts)
    alerts_dict = response4.json()

    response5 = requests.get(url=currenthurricane)
    currenthurricane_dict = response5.json()

    weather = Weather(conditions_dict)
    print(weather)
    #below forecast code does not work, returns "TypeError: 'str' object is not callable"
    # for day in forecast10day_dict["forecast"]["simpleforecast"]["forecastday"]:
    #     forecast = forecast10day(forecast10day_dict)
    #     print(forecast)
    sun = Sun(astronomy_dict)
    print(sun)
    alerts = Alert(alerts_dict)
    print(alerts.print_alerts())
    hurricane = Hurricane(currenthurricane_dict)
    print(hurricane.print_alerts())
Ejemplo n.º 6
0
def settings(request):
	if request.method == 'POST':
		form = SettingsForm(request.POST)
		if form.is_valid():
			try:
				Alert.objects.get(user=request.user).delete()
			except:
				pass

			data = form.cleaned_data

			client_t = data['user_time']
			server_t = datetime.now().strftime('%H')
			tz_diff = int(server_t) - int(client_t)
			as_time = datetime(2013, 1, 1, data['time'].hour, data['time'].minute)			
			as_time = as_time + timedelta(hours=tz_diff)

			a = Alert(
				user=request.user,
				alert_server_time=as_time,
				start=data['start'],
				finish=data['finish'],
				period=data['period'],
				time=data['time'],
				alert_email=data['alert_email'],
				email=data['email'],
				alert_sms=data['alert_sms'],
				phone=data['phone']
			)

			alert = {}

			try:
				a.save()
				alert['txt'] = 'Настройки сохранены'
				alert['cls'] = 'alert-success'
			except:
				alert['txt'] = 'Не удалось сохранить настройки, попробуйте повторить позже'
				alert['cls'] = 'alert-danger'

			return render_to_response('settings.html', {'form': form, 'alert': alert}, RequestContext(request))
	else:
		try:
			a = Alert.objects.get(user=request.user)
			form = SettingsForm(instance=a)
		except: 
			form = SettingsForm()

	return render_to_response('settings.html', {'form': form}, RequestContext(request))
Ejemplo n.º 7
0
def get_alert():
    # get request
    if request.method == 'GET':
        all_alerts = Alert.query.all()
        all_alerts = list(map(lambda x: x.serialize(), all_alerts))

        return jsonify(all_alerts), 200

    if request.method == 'POST':
        body = request.get_json()
        if body is None:
            raise APIException("Specify JSON body", status_code=400)
        if "message" not in body:
            raise APIException("Specify Message", status_code=400)
        if "email" not in body:
            raise APIException("Specify Email", status_code=400)

        alert1 = Alert(message=body['message'],
                       email=body['email'],
                       name=body['name'],
                       petname=body['petname'],
                       phone=body['phone'])
        db.session.add(alert1)
        db.session.commit()

        # print('kevin', alert1.__repr__())
        return "ok", 200

    return "invalid method", 404
Ejemplo n.º 8
0
def new_alert():
    form = NewAlertForm()
    #mongoalchemy
    #alerts = session.query(Alert).filter(Alert.name == 'Second_Alert')
    new = 'New'
    if form.validate_on_submit():
        title = form.title.data
        ip = [form.ip.data]
        mac = [form.mac.data]
        atype = [form.atype.data]
        entered = datetime.utcnow()
        comments = [form.comments.data]

        print title
        #mongoalchemy
        iris_db.insert(
            Alert(title=title,
                  ip=ip,
                  mac=mac,
                  status=new,
                  atype=atype,
                  comments=comments,
                  entered=entered))
        return redirect(url_for('alert'))

    return render_template("alert/new_alert.html",
                           title='New Alert',
                           form=form)
Ejemplo n.º 9
0
 def _doAlert(self):
     a = Alert.query().order(-Alert.date).get()
     la = LatestAlert()
     for field in la.all_fields():
         if field.name == 'content':
             setattr(la, field.name, a.content)
     la.check_initialized()
     return la
Ejemplo n.º 10
0
def remove_alert(alert_id):
    alert = Alert.get_by_id(alert_id)

    if alert.user_email == session['email']:

        alert.remove_from_mongo(alert_id)

    return redirect(url_for('.index'))
Ejemplo n.º 11
0
def alert_list():
    alerts = Alert.query.order_by(Alert.alert_id.desc()).all()
    if request.method == 'POST':
        content = request.form['alert']
        alert = Alert(alert_content=content)
        db.session.add(alert)
        db.session.commit()
        return redirect(url_for("admin_app.alert_list"))
    return render_template('admin/alert/alert_list.html', alerts=alerts)
Ejemplo n.º 12
0
def index():
    alerts = Alert.find_many_by("user_email", session['email'])
    for a in alerts:
        item1 = Item.find_only_one("_id", a.item_id)
        item1.calc()
        a.price = item1.price
        a.update_mongo(a._id)

    return render_template('alerts/index.html', alerts = alerts)
Ejemplo n.º 13
0
def addAlert():
    deviceID = request.form['deviceID']
    personNo = request.form['personNo']
    confidence = request.form['confidence']
    new_alert = Alert(deviceID, personNo, confidence)
    from myapp import db
    db.session.add(new_alert)
    db.session.commit()
    return jsonify({"deviceID":deviceID,
            "personNo":personNo, "confidence":confidence})
Ejemplo n.º 14
0
def add_alert(bot, update, market_id=None, price=None):
    if market_id is None:
        return market_list(update, 'add_alert')
    chat = get_chat(update)
    if chat.market_id != market_id:
        chat.market_id = market_id
        session.commit()
    market = chat.market
    if price is None:
        text = "_Precio actual = {} {}_".format(market.ask, market.currency)
        update.message.reply_text(text, parse_mode=ParseMode.MARKDOWN)
        return update.message.reply_text(ALERT_INPUT_TEXT,
                                         reply_markup=ForceReply())
    try:
        price = round(float(price), market.decimals)
        if price % 1 == 0:
            price = int(price)
        if price <= 0:
            raise ValueError
    except ValueError:
        update.message.reply_text("El precio debe ser un número mayor a 0.")
        return update.message.reply_text(ALERT_INPUT_TEXT,
                                         reply_markup=ForceReply())
    if chat.alert_count() == MAX_ALERT_NUMBER:
        text = "Lo siento, sólo puedes agregar un máximo de {} /alertas.".format(
            MAX_ALERT_NUMBER)
        return update.message.reply_text(text)
    market = chat.market
    alert = chat.get_alert(market, str(price))
    if alert is None:  # Create alert only if it doesn't exist
        alert_data = {
            'chat': chat,
            'market': market,
            'price': str(price),
            'trigger_on_lower': price <= float(market.ask)
        }
        alert = Alert(**alert_data)
        session.add(alert)
        session.commit()
    values = {
        'coin': market.coin,
        'sign': '<' if alert.trigger_on_lower else '>',
        'price': price,
        'currency': market.currency,
    }
    text = "Perfecto, te enviaré una alerta si\n*1 {coin} {sign} {price} {currency}*".format(
        **values)
    keyboard = [[
        InlineKeyboardButton("Añadir otra alerta",
                             callback_data='add_alert {}'.format(market.id)),
        InlineKeyboardButton("Ver alertas", callback_data='alert_list')
    ]]
    update.message.reply_text(text,
                              parse_mode=ParseMode.MARKDOWN,
                              reply_markup=InlineKeyboardMarkup(keyboard))
 def _doAlert(self):
     """Get latest alert and return to user"""
     # retrieve latest alert from datastore
     a = Alert.query().order(-Alert.date).get()
     la = LatestAlert()
     for field in la.all_fields():
         # retrieve content string and discard the rest
         if field.name == 'content':
             setattr(la, field.name, a.content)
     la.check_initialized()
     return la
 def _doAlert(self):
     """Get latest alert and return to user"""
     # retrieve latest alert from datastore
     a = Alert.query().order(-Alert.date).get()
     la = LatestAlert()
     for field in la.all_fields():
         # retrieve content string and discard the rest
         if field.name == 'content':
             setattr(la, field.name, a.content)
     la.check_initialized()
     return la
Ejemplo n.º 17
0
    def post(self):
        """
        Create a new alert
        """
        global users_connected
        json_data, error = get_data(request)
        if not error:
            device_data, error = validate_json_payload(json_data, self.fields)
            if not error:
                error = self.validate_jetson_data(device_data,
                                                  data_dict={
                                                      'truck_id':
                                                      device_data['truck_id'],
                                                      'behavior':
                                                      device_data['behavior'],
                                                      'date_':
                                                      device_data['alert_init']
                                                  })
                if not error:

                    try:
                        alert_data = self.alert_schema.load({
                            'truck_id':
                            device_data['truck_id'],
                            'behavior':
                            device_data['behavior'],
                            'alert_init':
                            device_data['alert_init']
                        })
                    except marshmallow.exceptions.ValidationError as errors:
                        return response(400, str(errors))

                    alert = Alert(**alert_data)
                    error = alert.save()
                    if not error:
                        # TODO: add alert.id to msg
                        alert_event_notify(socket_obj, users_connected)
                        return response(200, data={'alert_id': alert.id})
        return error
Ejemplo n.º 18
0
    def check_new_alerts(self, created_ts):
        """ Looks at the alerts created this run for ones we care about """

        # Keep track of alerts that match our watched counties
        matched_alerts = []

        # Iterate over the alerts in the latest run
        for alert_record in Alert.select().where(Alert.created == created_ts):

            # Get all the UGC codes for the alert
            if alert_record.ugc_codes:
                ugc_codes = alert_record.ugc_codes.split(',')
            else:
                ugc_codes = []

            # Get all the FIPS codes for the alert
            if alert_record.fips_codes:
                fips_codes = alert_record.fips_codes.split(',')
            else:
                fips_codes = []

            # Compare the fips and ugc codes to see if they overlap. If they
            # do, then we have a match
            ugc_match = set(ugc_codes).intersection(self.ugc_watch_list)
            fips_match = set(fips_codes).intersection(self.fips_watch_list)

            matched_counties = []

            # See if any of the UGC codes match our target counties
            for ugc_code in ugc_match:
                matched_counties = [
                    county for county in self.counties
                    if county['ugc'] == ugc_code
                ]

            # See if any of the FIPS codes match our target counties
            for fips_code in fips_match:
                matched_counties = [
                    county for county in self.counties
                    if county['fips'] == fips_code
                ]

            if len(matched_counties) > 0:
                # Because the counties we check are very far apart, the matched
                # counties should never be more than one. We only care about the
                # first, so just assign it.
                alert_record.county = matched_counties[0]['name']
                alert_record.state = matched_counties[0]['state']
                matched_alerts.append(alert_record)

        return matched_alerts
Ejemplo n.º 19
0
def edit_alert(alert_id):
    alert = Alert.get_by_id(alert_id)

    if alert.user_email == session['email']:
        if request.method == 'POST':
            price_limit = request.form['price_limit']

            alert.price_limit = price_limit

            alert.update_mongo(alert._id)

            return redirect(url_for('.index'))

    return render_template('alerts/edit_alert.html', alert=alert)
Ejemplo n.º 20
0
    def post(self, request, *args, **kwargs):
        try:
            alert_d = request.data
            # alert_d['employer_profile_id'] = alert_d.pop('employer_profile')['profile_id']
            # alert_d['location_id'] = alert_d.pop('location')['location_id']
            if 'alert_id' not in alert_d:
                okStatus = status.HTTP_201_CREATED
            else:
                okStatus = status.HTTP_200_OK

            if alert_d['query']['terms']:
                alert_d['query']['terms'] = ','.join(alert_d['query']['terms'])
            else:
                alert_d['query']['terms'] = None

            if alert_d['query']['employer_names']:
                alert_d['query']['employer_names'] = ','.join(alert_d['query']['employer_names'])
            else:
                alert_d['query']['employer_names'] = None

            if alert_d['query']['locations']:
                alert_d['query']['locations'] = ','.join(alert_d['query']['locations'])
            else:
                alert_d['query']['locations'] = None
            query = Query(**alert_d.pop('query'))
            query.save()

            alert_d['query'] = query

            alert = Alert(**alert_d)
            alert.save()

            return Response(AlertSerializer(alert).data, status=okStatus)

        except Exception as e:
            print '%s (%s)' % (e, type(e))
            return Response(e.message)
Ejemplo n.º 21
0
 def save(self, **kwargs):
     lat, long = self.cleaned_data['location'].split(',')
     poi = PrivatePlace.get_or_insert(id = self.cleaned_data['poi_id'],
                                      name = '',
                                      point = GeoPt(float(lat), float(long)),
                                      address = kwargs.get('address', ''),)
     alert = Alert.update_or_insert(
                      id = kwargs.get('id', None), name = self.cleaned_data['name'],
                      description = self.cleaned_data['description'], 
                      date_starts = self.cleaned_data['starts'],
                      date_ends = self.cleaned_data['ends'], poi = poi,
                      user = kwargs['user'], done = self.cleaned_data.get('done', False),
                      active = self.cleaned_data.get('active', False)
                      )
     
     return alert
Ejemplo n.º 22
0
 def save(self, **kwargs):
     lat, long = self.cleaned_data['location'].split(',')
     poi = PrivatePlace.get_or_insert(id = self.cleaned_data['poi_id'],
                                      name = '',
                                      location = GeoPt(float(lat), float(long)),
                                      address = kwargs.get('address', ''),
                                      user = kwargs['user'])
     alert = Alert.update_or_insert(
                      id = kwargs.get('id', None), name = self.cleaned_data['name'],
                      description = self.cleaned_data['description'], 
                      date_starts = self.cleaned_data['starts'],
                      date_ends = self.cleaned_data['ends'], poi = poi,
                      user = kwargs['user'], done = self.cleaned_data.get('done', False),
                      active = None  # self.cleaned_data.get('active', True)
                      )
     
     return alert
Ejemplo n.º 23
0
    def check_new_alerts(self, created_ts):
        """ Looks at the alerts created this run for ones we care about """

        # Keep track of alerts that match our watched counties
        matched_alerts = []

        # Iterate over the alerts in the latest run
        for alert_record in Alert.select().where(Alert.created == created_ts):

            # Get all the UGC codes for the alert
            if alert_record.ugc_codes:
                ugc_codes = alert_record.ugc_codes.split(',')
            else:
                ugc_codes = []

            # Get all the FIPS codes for the alert
            if alert_record.fips_codes:
                fips_codes = alert_record.fips_codes.split(',')
            else:
                fips_codes = []

            # Compare the fips and ugc codes to see if they overlap. If they
            # do, then we have a match
            ugc_match = set(ugc_codes).intersection(self.ugc_watch_list)
            fips_match = set(fips_codes).intersection(self.fips_watch_list)

            matched_counties = []

            # See if any of the UGC codes match our target counties
            for ugc_code in ugc_match:
                matched_counties = [county for county in self.counties if county['ugc'] == ugc_code]

            # See if any of the FIPS codes match our target counties
            for fips_code in fips_match:
                matched_counties = [county for county in self.counties if county['fips'] == fips_code]

            if len(matched_counties) > 0:
                # Because the counties we check are very far apart, the matched
                # counties should never be more than one. We only care about the
                # first, so just assign it.
                alert_record.county = matched_counties[0]['name']
                alert_record.state = matched_counties[0]['state']
                matched_alerts.append(alert_record)

        return matched_alerts
Ejemplo n.º 24
0
def subscribe(argument):
    """Search: Filter post by user"""
    if argument not in ["S", "U"]:
        abort(405)
    user = get_user(session.get("user_id"), db_session)
    sub = db_session.query(Alert).filter(Alert.user_id == user.id_).first()

    if argument == "S":
        if sub is None:
            sub = Alert(user.id_)
            db_session.add(sub)
            db_session.commit()
    elif argument == "U":
        if sub is not None:
            db_session.delete(sub)
            db_session.commit()

    return redirect(url_for('user', id=user.id_))
Ejemplo n.º 25
0
def alert(client, userdata, msg):
    # unpack json payload
    json_data = msg.payload
    raw_data = json.loads(str(json_data, encoding="utf-8"))

    with app.app_context():
        device = Device.query.filter_by(name=raw_data["serial-number"]).first()
        alert = Alert(deviceID=device.ID,
                      personNo=raw_data["personNo"],
                      confidence=raw_data["confidence"])
        db.session.add(alert)
        db.session.commit()

        deviceID = device.ID
        alertID = alert.alertID
        personNo = alert.personNo
        confidence = alert.confidence
        time = alert.time

    #save alert image
    os.makedirs(app.config['MQTT_IMG_PATH'], exist_ok=True)
    img_path = app.config['MQTT_IMG_PATH'] + 'alert-' + str(alertID) + '.gif'
    base64_string = raw_data["image_base64_string"]
    img_data = base64.b64decode(base64_string)
    with open(img_path, 'wb') as imgf:
        imgf.write(img_data)

    # notify HTTP server in localhost
    payload = {
        'deviceID': deviceID,
        'alertID': alertID,
        'time': time.isoformat(' ', 'seconds'),
        # 'alertInfo':{
        # 'personNo': personNo,
        # 'confidence':confidence,
        # }
    }
    print(payload)
    requests.post("http://127.0.0.1:" + str(app.config['PORT']) +
                  "/device/alert",
                  headers={"Content-Type": "application/json"},
                  data=json.dumps(payload))
Ejemplo n.º 26
0
def parse_upload(directory, filename):
    #note: not taking into account that ip,mac,... can be a list or our we forcing one value one column for this
    with open(directory + '/' + filename) as f:
        records = csv.DictReader(f)
        for r in records:
            title = r['title']
            status = r['status']
            ip = [r['ip']]
            mac = [r['mac']]
            atype = [r['atype']]
            entered = datetime.utcnow()
            comments = [r['comments']]

            iris_db.insert(
                Alert(title=title,
                      ip=ip,
                      mac=mac,
                      status=status,
                      atype=atype,
                      comments=comments,
                      entered=entered))
Ejemplo n.º 27
0
    def start_alert(self):
        start_time = datetime.now()
        arm = self._db_session.query(Arm).filter_by(end_time=None).first()
        self._alert = Alert(arm=arm, start_time=start_time, sensors=[])
        self._db_session.add(self._alert)
        self._db_session.commit()
        self.handle_sensors()

        send_alert_state(self._alert.serialize)
        self._syren.alert(True)
        send_syren_state(True)

        self._logger.debug("Alerting sensors: %s", self._alert.sensors)
        sensor_descriptions = list(
            map(
                lambda item: f"{item.sensor.description}(id:{item.sensor.id}/CH{item.sensor.channel+1})",
                self._alert.sensors,
            )
        )
        Notifier.notify_alert_started(self._alert.id, sensor_descriptions, start_time)

        self._logger.info("Alert started")
Ejemplo n.º 28
0
def create_alert():
    if request.method == 'POST':
        body = request.form.get('body')
        file = request.files['file']
        alert = Alert(body=body, author=current_user.login)
        db.session.add(alert)
        db.session.commit()
        photo = Photos(link=(UPLOAD_FOLDER + '/' +
                             slugify(str(alert.created), '') + file.filename),
                       alert_id=alert.id)
        db.session.add(photo)
        db.session.commit()
        file.save(
            os.path.join(UPLOAD_FOLDER,
                         slugify(str(alert.created), '') + file.filename))

    last_post = Post.query.order_by(Post.id.desc()).first()
    last_alert = Alert.query.order_by(Alert.id.desc()).first()

    return render_template('create_alert.html',
                           last_post=last_post,
                           last_alert=last_alert,
                           weekDays=rusWeekDays,
                           months=rusMonths)
Ejemplo n.º 29
0
def _create_alerts(data, person, unit):
    print "Creating alerts!"
    try:
        alerts = data['alerts']
        if not isinstance(alerts, (list, tuple)):
            raise ValidationError("Problems not in list format")
    except KeyError:
        alerts = []

    errors = []

    for alert in alerts:
        comments = None
        try:
            emplid = alert['emplid']
            code = alert['code']
            description = alert['description']
            unique_id = alert['unique_id']
        except KeyError:
            raise ValidationError("Necessary fields not present in alert: " +
                                  str(alert))

        try:
            if not isinstance(emplid, int):
                raise ValidationError("Alert emplid:'%s' must be integer" %
                                      str(emplid))
            try:
                student = Person.objects.get(emplid=emplid)
            except Person.DoesNotExist:
                try:
                    p = coredata.queries.add_person(emplid)
                except SIMSProblem:
                    raise ValidationError(
                        "Person %s could not be found; SIMS not working." %
                        str(emplid))
                except IOError:
                    raise ValidationError(
                        "Person %s could not be found; SIMS not working." %
                        str(emplid))

            if not isinstance(code, basestring) or not isinstance(
                    description, basestring):
                raise ValidationError(
                    "Alert code & description must be strings")
            if len(code) >= 30:
                raise ValidationError(
                    "Alert code must be less than or equal to 30 characters")

            try:
                details = alert['details']
            except KeyError:
                details = {}

            try:
                alertType = AlertType.objects.get(code=code)
            except AlertType.DoesNotExist:
                # If the AlertType doesn't exist, try to create one.
                # This requires that the User has provided a unit at the
                # base level of the JSON
                alertType = AlertType()
                alertType.code = code
                alertType.unit = unit
                alertType.description = "Not set."
                alertType.save()

            alert = Alert()
            alert.person = student
            alert.alerttype = alertType
            alert.description = description
            alert.details = details

            alert.safe_create(unique_id)

        except ValidationError as e:
            print e
            errors.append(str(e))
    return errors
Ejemplo n.º 30
0
def add_info(form):
    item = 0
    oid = form['oid']

    iid = str(time.time())[:9] + str(random.randint(10, 99))

    score = 0

    if float(form['toxic']) > 30:
        score += 1
        item += pow(2, 5)
    elif float(form['toxic']) > 28:
        score += 0.1
        item += pow(2, 5)
    elif float(form['toxic']) > 25:
        score += 0.01
        item += pow(2, 5)

    if float(form['med']) < 90:
        score += 1
        item += pow(2, 4)
    elif float(form['med']) < 93:
        score += 0.1
        item += pow(2, 4)
    elif float(form['med']) < 95:
        score += 0.01
        item += pow(2, 4)

    if float(form['water']) < 73:
        score += 1
        item += pow(2, 3)
    elif float(form['water']) < 75:
        score += 0.1
        item += pow(2, 3)
    elif float(form['water']) < 80:
        score += 0.01
        item += pow(2, 3)

    if float(form['temp']) > 4 or float(form['temp']) < -0.5:
        score += 1
        item += pow(2, 2)
    elif float(form['temp']) > 3.5 or float(form['temp']) < 0:
        score += 0.1
        item += pow(2, 2)
    elif float(form['temp']) != 1:
        score += 0.01
        item += pow(2, 2)

    if float(form['moist']) > 95 or float(form['moist']) < 85:
        score += 1
        item += pow(2, 1)
    elif float(form['moist']) > 93 or float(form['moist']) < 87:
        score += 0.1
        item += pow(2, 1)
    elif float(form['moist']) != 90:
        score += 0.01
        item += pow(2, 1)

    if int(form['store']) > 14:
        score += 1
        item += pow(2, 0)
    elif int(form['store']) > 10:
        score += 0.1
        item += pow(2, 0)
    elif int(form['store']) > 7:
        score += 0.01
        item += pow(2, 0)

    level = 0
    if score > 2:
        level = 4
    elif score > 0.2:
        level = 3
    elif score > 0.02:
        level = 2
    else:
        level = 1

    new_info = Info(
        toxic=form['toxic'],
        med=form['med'],
        water=form['water'],
        moist=form['moist'],
        store=form['store'],
        temp=form['temp'],
        iid=iid,
        level=level,
        oid=oid,
    )

    order = Order.query.filter_by(oid=oid).first()
    order.iid = iid

    db.session.add(new_info)
    db.session.commit()

    if level != 0:
        aids = db.session.query(Admin.aid).all()
        for i in aids:
            new_alert = Alert(
                msgfor=2,
                toid=str(i[0]),
                msg=ALERT_MSG[level-1],
                title=ALERT_TITLE[level-1],
                oid=oid,
                item=item,
                valid=1,
                level=level-1,
                iid=iid,
            )
            order.alevel = level-1
            db.session.add(new_alert)
            db.session.commit()

    return redirect(url_for("superadd_page"))
Ejemplo n.º 31
0
from twilio.rest import Client
import os
from models import Alert, Person

Q = Alert()


def first_function(number, mess):

    account_sid = os.environ.get('SID_KEV')
    auth_token = os.environ.get('TOKEN_KEV')
    client = Client(account_sid, auth_token)

    message = client.messages \
        .create(
            body=mess,
            from_='+12064660790',
            to=number
        )

    print(message.sid)
    # return repr(Q._queue)
Ejemplo n.º 32
0
    def fetch(self, run_timestamp):
        """ Fetches the NOAA alerts XML feed and inserts into database """

        # Create an XML doc from the URL contents
        logger.info('Fetching Alerts Feed')
        request = requests.get('http://alerts.weather.gov/cap/us.php?x=1')
        tree = lxml.etree.fromstring(request.text.encode('utf-8'))

        # Keep track of how many alerts we create
        total_count = 0
        insert_count = 0
        existing_count = 0

        for entry_el in tree.findall(ATOM_NS + 'entry'):

            total_count += 1

            alert_id = hashlib.sha224(entry_el.find(ATOM_NS + 'id').text).hexdigest()
            title = entry_el.find(ATOM_NS + 'title').text
            event = entry_el.find(CAP_NS + 'event').text
            expires_dt = arrow.get(entry_el.find(CAP_NS + 'expires').text)
            url = entry_el.find(ATOM_NS + 'link').attrib['href']
            api_url = entry_el.find(ATOM_NS + 'id').text
            
            # Calculate the expiration timetamp
            expires = expires_dt.isoformat()
            expires_utc_ts = int(expires_dt.to('UTC').timestamp)

            fips_list = []
            ugc_list = []

            # Get the FIPS and UGC codes that this alert applies to
            geocode_el = entry_el.find(CAP_NS + 'geocode')

            if geocode_el is not None:
                for value_name_el in geocode_el.findall(ATOM_NS + 'valueName'):
                    if value_name_el.text == 'FIPS6':
                        fips_el = value_name_el.getnext()
                        if fips_el is not None and fips_el.text is not None:
                            fips_list = fips_el.text.split(' ')
                    elif value_name_el.text == 'UGC':
                        ugc_el = value_name_el.getnext()
                        if ugc_el is not None and ugc_el.text is not None:
                            ugc_list = ugc_el.text.split(' ')

            # If it's a special or severe weather statement, look inside it to see
            # if we can extract any keywords. We'll store these separately but put them
            # in any push messages we send out.
            sub_events = []
            if event in ('Severe Weather Statement', 'Special Weather Statement'):
                summary = entry_el.find(ATOM_NS + 'summary').text.upper()
                for item in ('Thunderstorm', 'Strong Storm', 'Wind', 'Rain', 'Hail', 'Tornado', 'Flood'):
                    if item.upper() in summary:
                        sub_events.append(item)

            # Concatenate the sub events (if any) into a detail string
            detail = ', '.join(sub_events)

            # See if this alert exists. If it does, don't do anything since
            # we don't update existing alerts. (NOAA doesn't do this I think?)
            try:
                alert_record = Alert.get(Alert.alert_id == alert_id)
                existing_count += 1
            except Exception, _:
                insert_count += 1
                alert_record = Alert.create(
                    alert_id=alert_id,
                    title=title,
                    event=event,
                    details=detail,
                    description = None,
                    expires=expires,
                    expires_utc_ts=expires_utc_ts,
                    url=url,
                    api_url=api_url,
                    fips_codes=','.join(fips_list),
                    ugc_codes=','.join(ugc_list),
                    created=run_timestamp,
                )
Ejemplo n.º 33
0
def _create_alerts(data, person, unit):
    print "Creating alerts!"
    try:
        alerts = data['alerts']
        if not isinstance(alerts, (list, tuple)):
            raise ValidationError("Problems not in list format")
    except KeyError:
        alerts = []

    errors = []
    
    for alert in alerts:
        comments = None
        try:
            emplid = alert['emplid']
            code = alert['code']
            description = alert['description']
            unique_id = alert['unique_id']
        except KeyError:
            raise ValidationError("Necessary fields not present in alert: " + str(alert) )
            
        try:
            if not isinstance(emplid, int):
                raise ValidationError("Alert emplid:'%s' must be integer" % str(emplid) )
            try:
                student = Person.objects.get(emplid=emplid)
            except Person.DoesNotExist:
                try:
                    p = coredata.queries.add_person( emplid )
                except SIMSProblem:
                    raise ValidationError("Person %s could not be found; SIMS not working." % str(emplid) )
                except IOError:
                    raise ValidationError("Person %s could not be found; SIMS not working." % str(emplid) )
            
            if not isinstance(code, basestring) or not isinstance(description, basestring):
                raise ValidationError("Alert code & description must be strings")
            if len(code) >= 30:
                raise ValidationError("Alert code must be less than or equal to 30 characters")

            try: 
                details = alert['details']
            except KeyError:
                details = {}
            
            try:
                alertType = AlertType.objects.get(code=code)
            except AlertType.DoesNotExist:
                # If the AlertType doesn't exist, try to create one. 
                # This requires that the User has provided a unit at the 
                # base level of the JSON
                alertType = AlertType()
                alertType.code = code
                alertType.unit = unit
                alertType.description = "Not set."
                alertType.save() 
            
            alert = Alert()
            alert.person = student
            alert.alerttype = alertType
            alert.description = description
            alert.details = details

            alert.safe_create(unique_id)

        except ValidationError as e:
            print e
            errors.append(str(e))
    return errors
Ejemplo n.º 34
0
    parser = Parser(PUSHOVER_TOKEN, PUSHOVER_USER, CUR_DIR)

    # Load the counties we want to monitor
    counties_filepath = os.path.join(CUR_DIR, 'counties.json')
    with open(counties_filepath, 'r') as f:
        parser.counties = json.loads(f.read())

    # Assign the fips and ugc codes to watch for
    parser.fips_watch_list = [str(c['fips']) for c in parser.counties]
    parser.ugc_watch_list = [str(c['ugc']) for c in parser.counties]

    # If we got a command-line flag to purge the saved alerts, do
    # that before we fetch new alerts. If we didn't get the purge command,
    # delete any alerts that are now expired.
    if args['purge']:
        Alert.delete().execute()
    else:
        ago_ts = arrow.utcnow().replace(days=-1).timestamp
        count = Alert.delete().where(Alert.expires_utc_ts < ago_ts).execute()
        logger.debug("Deleted %d expired alerts." % count)

    # Create a timestamp that will act as a numeric identifier for
    # this fetching run. We'll use this later to see if a record
    # has been added in this run
    run_ts = arrow.utcnow().timestamp

    # Go grab the current alerts and process them
    parser.fetch(run_ts)

    # Find any new alerts that match our counties
    for alert in parser.check_new_alerts(run_ts):
Ejemplo n.º 35
0
    parser = Parser(PUSHOVER_TOKEN, PUSHOVER_USER, CUR_DIR)

    # Load the counties we want to monitor
    counties_filepath = os.path.join(CUR_DIR, 'counties.json')
    with open(counties_filepath, 'r') as f:
        parser.counties = json.loads(f.read())

    # Assign the fips and ugc codes to watch for
    parser.fips_watch_list = [str(c['fips']) for c in parser.counties]
    parser.ugc_watch_list = [str(c['ugc']) for c in parser.counties]

    # If we got a command-line flag to purge the saved alerts, do
    # that before we fetch new alerts. If we didn't get the purge command,
    # delete any alerts that are now expired.
    if args['purge']:
        Alert.delete().execute()
    else:
        ago_ts = arrow.utcnow().replace(days=-1).timestamp
        count = Alert.delete().where(Alert.expires_utc_ts < ago_ts).execute()
        logger.debug("Deleted %d expired alerts." % count)

    # Create a timestamp that will act as a numeric identifier for
    # this fetching run. We'll use this later to see if a record
    # has been added in this run
    run_ts = arrow.utcnow().timestamp

    # Go grab the current alerts and process them
    parser.fetch(run_ts)

    # Find any new alerts that match our counties
    for alert in parser.check_new_alerts(run_ts):
Ejemplo n.º 36
0
    def fetch(self, run_timestamp):
        """ Fetches the NOAA alerts XML feed and inserts into database """

        # Create an XML doc from the URL contents
        logger.info('Fetching Alerts Feed')
        request = requests.get('http://alerts.weather.gov/cap/us.php?x=1')
        tree = lxml.etree.fromstring(request.text.encode('utf-8'))

        # Keep track of how many alerts we create
        total_count = 0
        insert_count = 0
        existing_count = 0

        for entry_el in tree.findall(ATOM_NS + 'entry'):

            total_count += 1

            alert_id = hashlib.sha224(entry_el.find(ATOM_NS +
                                                    'id').text).hexdigest()
            title = entry_el.find(ATOM_NS + 'title').text
            event = entry_el.find(CAP_NS + 'event').text
            expires_dt = arrow.get(entry_el.find(CAP_NS + 'expires').text)
            url = entry_el.find(ATOM_NS + 'link').attrib['href']
            api_url = entry_el.find(ATOM_NS + 'id').text

            # Calculate the expiration timetamp
            expires = expires_dt.isoformat()
            expires_utc_ts = int(expires_dt.to('UTC').timestamp)

            fips_list = []
            ugc_list = []

            # Get the FIPS and UGC codes that this alert applies to
            geocode_el = entry_el.find(CAP_NS + 'geocode')

            if geocode_el is not None:
                for value_name_el in geocode_el.findall(ATOM_NS + 'valueName'):
                    if value_name_el.text == 'FIPS6':
                        fips_el = value_name_el.getnext()
                        if fips_el is not None and fips_el.text is not None:
                            fips_list = fips_el.text.split(' ')
                    elif value_name_el.text == 'UGC':
                        ugc_el = value_name_el.getnext()
                        if ugc_el is not None and ugc_el.text is not None:
                            ugc_list = ugc_el.text.split(' ')

            # If it's a special or severe weather statement, look inside it to see
            # if we can extract any keywords. We'll store these separately but put them
            # in any push messages we send out.
            sub_events = []
            if event in ('Severe Weather Statement',
                         'Special Weather Statement'):
                summary = entry_el.find(ATOM_NS + 'summary').text.upper()
                for item in ('Thunderstorm', 'Strong Storm', 'Wind', 'Rain',
                             'Hail', 'Tornado', 'Flood'):
                    if item.upper() in summary:
                        sub_events.append(item)

            # Concatenate the sub events (if any) into a detail string
            detail = ', '.join(sub_events)

            # See if this alert exists. If it does, don't do anything since
            # we don't update existing alerts. (NOAA doesn't do this I think?)
            try:
                alert_record = Alert.get(Alert.alert_id == alert_id)
                existing_count += 1
            except Exception, _:
                insert_count += 1
                alert_record = Alert.create(
                    alert_id=alert_id,
                    title=title,
                    event=event,
                    details=detail,
                    description=None,
                    expires=expires,
                    expires_utc_ts=expires_utc_ts,
                    url=url,
                    api_url=api_url,
                    fips_codes=','.join(fips_list),
                    ugc_codes=','.join(ugc_list),
                    created=run_timestamp,
                )
Ejemplo n.º 37
0
def alert(request):
    a = Alert()
    a.alert = request.body
    a.save()
    return HttpResponse('success')