Exemplo n.º 1
0
    def test_notifications_hook(self):
        sensor_value = SensorValue(
            sensor_id=1,
            value=56,
            timestamp=datetime.now().replace(tzinfo=utc))
        sensor_value.save()

        threshold = Threshold(sensor_id=1)
        threshold.save()

        response = self.client.get('/api/notifications/')
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['total'], 0)
        self.assertEqual(len(data['notifications']), 0)

        notification = Notification(threshold=threshold,
                                    sensor_value=sensor_value,
                                    target=50)
        notification.save()

        response = self.client.get('/api/notifications/')
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['total'], 1)
        self.assertEqual(len(data['notifications']), 1)
        self.assertEqual(data['notifications'][0]['threshold']['id'],
                         threshold.id)
Exemplo n.º 2
0
    def test_read_all_notification(self):
        user = User.query.get(2)
        notification1 = Notification(message='test 1', receiver=user)
        notification2 = Notification(message='test 2', receiver=user)
        db.session.add_all([notification1, notification2])
        db.session.commit()

        self.login()

        response = self.client.post(url_for('main.read_all_notification'),
                                    follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('All notifications archived.', data)

        self.assertTrue(Notification.query.get(1).is_read)
        self.assertTrue(Notification.query.get(2).is_read)
Exemplo n.º 3
0
def push_collect_notification(collector, photo_id, receiver):
    message = 'User <a href="%s">%s</a> collected your <a href="%s">photo</a>' % \
              (url_for('user.index', username=collector.username),
               collector.username,
               url_for('main.show_photo', photo_id=photo_id))
    notification = Notification(message=message, receiver=receiver)
    db.session.add(notification)
    db.session.commit()
Exemplo n.º 4
0
 def getNotification(self,title=None,date=None,insertOnBd=False):
     payload = {'format': 'json','title':title,'date':date}
     r = requests.get(self.baseUrlAPI+ 'notification/', params=payload)
     print r.status_code
     aux = r.json()['objects']
     if len(aux) == 0 :
         stderr.write('Notification with title: \"'+title + '\" and date: \"' + str(date) + '\" doesn\'t exist in SIOrg+Pub')
         return None
     
     print aux
     data = aux[0]
     
     a = Notification(title=data['title'],description=data['description'],date=data['date'])
     
     if insertOnBd :
         a.save()
     return a
     
Exemplo n.º 5
0
def save_fetched_table_if_changed(wfm: WfModule, new_table: DataFrame,
                                  error_message: str) -> datetime.datetime:
    """Store retrieved data table, if it is a change from wfm's existing data.

    "Change" here means either a changed table or changed error message.
    
    The WfModule's `status` and `error_msg` will be set, according to
    `error_message`.

    Set wfm.last_update_check, regardless.

    Create (and run) a ChangeDataVersionCommand.

    Notify the user.

    Return the timestamp (if changed) or None (if not).
    """

    with wfm.workflow.cooperative_lock():
        wfm.last_update_check = timezone.now()

        # Store this data only if it's different from most recent data
        version_added = wfm.store_fetched_table_if_different(new_table)

        if version_added:
            enforce_storage_limits(wfm)

        wfm.error_msg = error_message or ''
        wfm.status = (WfModule.ERROR if error_message else WfModule.READY)
        wfm.save()

    # un-indent: COMMIT, so we can notify the client and the client sees changes
    if version_added:
        ChangeDataVersionCommand.create(wfm,
                                        version_added)  # also notifies client
        if wfm.notifications == True:
            Notification.create(wfm, "New data version available")
    else:
        # no new data version, but we still want client to update WfModule status and last update check time
        notify_client_workflow_version_changed(wfm.workflow)

    return version_added
Exemplo n.º 6
0
    def getNotification(self, title=None, date=None, insertOnBd=False):
        payload = {'format': 'json', 'title': title, 'date': date}
        r = requests.get(self.baseUrlAPI + 'notification/', params=payload)
        print r.status_code
        aux = r.json()['objects']
        if len(aux) == 0:
            stderr.write('Notification with title: \"' + title +
                         '\" and date: \"' + str(date) +
                         '\" doesn\'t exist in SIOrg+Pub')
            return None

        print aux
        data = aux[0]

        a = Notification(title=data['title'],
                         description=data['description'],
                         date=data['date'])

        if insertOnBd:
            a.save()
        return a
Exemplo n.º 7
0
def fake_admin():
    admin = User(name='Grey Li',
                 username='******',
                 email='*****@*****.**',
                 bio=fake.sentence(),
                 website='http://greyli.com',
                 confirmed=True)
    admin.set_password('helloflask')
    notification = Notification(message='Hello, welcome to TA.',
                                receiver=admin)
    db.session.add(notification)
    db.session.add(admin)
    db.session.commit()
Exemplo n.º 8
0
    def test_read_notification(self):
        user = User.query.get(2)
        notification1 = Notification(message='test 1', receiver=user)
        notification2 = Notification(message='test 2', receiver=user)
        db.session.add_all([notification1, notification2])
        db.session.commit()

        self.login(email='*****@*****.**', password='******')
        response = self.client.post(
            url_for('main.read_notification', notification_id=1))
        self.assertEqual(response.status_code, 403)

        self.logout()
        self.login()

        response = self.client.post(url_for('main.read_notification',
                                            notification_id=1),
                                    follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('Notification archived.', data)

        self.assertTrue(Notification.query.get(1).is_read)
Exemplo n.º 9
0
    def test_show_notifications(self):
        user = User.query.get(2)
        notification1 = Notification(message='test 1',
                                     is_read=True,
                                     receiver=user)
        notification2 = Notification(message='test 2',
                                     is_read=False,
                                     receiver=user)
        db.session.add_all([notification1, notification2])
        db.session.commit()

        self.login()
        response = self.client.get(url_for('main.show_notifications'))
        data = response.get_data(as_text=True)
        self.assertIn('test 1', data)
        self.assertIn('test 2', data)

        response = self.client.get(
            url_for('main.show_notifications', filter='unread'))
        data = response.get_data(as_text=True)
        self.assertNotIn('test 1', data)
        self.assertIn('test 2', data)
Exemplo n.º 10
0
    def test_notifications_hook(self):
        sensor_value = SensorValue(sensor_id=1, value=56, timestamp=datetime.now().replace(tzinfo=utc))
        sensor_value.save()

        threshold = Threshold(sensor_id=1)
        threshold.save()

        response = self.client.get('/api/notifications/')
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['total'], 0)
        self.assertEqual(len(data['notifications']), 0)

        notification = Notification(threshold=threshold, sensor_value=sensor_value, target=50)
        notification.save()

        response = self.client.get('/api/notifications/')
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content)
        self.assertEqual(data['total'], 1)
        self.assertEqual(len(data['notifications']), 1)
        self.assertEqual(
            data['notifications'][0]['threshold']['id'], threshold.id)
Exemplo n.º 11
0
def push_comment_notification(photo_id, receiver, page=1):
    message = '<a href="%s#comments">This photo</a> has new comment/reply.' % \
              (url_for('main.show_photo', photo_id=photo_id, page=page))
    notification = Notification(message=message, receiver=receiver)
    db.session.add(notification)
    db.session.commit()
Exemplo n.º 12
0
def push_follow_notification(follower, receiver):
    message = 'User <a href="%s">%s</a> followed you.' % \
              (url_for('user.index', username=follower.username), follower.username)
    notification = Notification(message=message, receiver=receiver)
    db.session.add(notification)
    db.session.commit()
import setup_environment
import datetime
from server.models import UserWeatherInfo, WeatherInfo, Notification

# Loop over all entries in the UserWeatherInfo table
user_weather_infos = UserWeatherInfo.objects.all()
for user_weather_info in user_weather_infos:
    user_id = user_weather_info.user_id
    location = user_weather_info.location
    # Obtain weather info for this location.
    weather_info = WeatherInfo.objects.get(location=location, date=datetime.date.today())
    notification = 'The weather prediction is %s with a max temperature of %f' % (weather_info.forecast_condition,
                                                                                  weather_info.forecast_temperature)
    tomorrow = datetime.date.today() + datetime.timedelta(days=1)
    morning_time = datetime.time(8, 00)
    tomorrow_time = datetime.datetime.combine(tomorrow, morning_time)
    notif = Notification(user_id=user_id, notification=notification,
                         schedule_time=tomorrow_time, done=False)
    notif.save()