def test_set_target_url(self): """ can change target_url """ notification = pushpad.Notification(self._project, body="Hello world!", title="Website Name", target_url="http://example.com") self.assertEqual(notification._target_url, "http://example.com")
def test_instantiate(self): """ notification can be instantiated""" notification = pushpad.Notification(self._project, body="Hello world!", title="Website Name", target_url="http://example.com") self.assertIsNotNone(notification)
def test_set_icon_url(self): """ can change icon_url """ notification = pushpad.Notification( self._project, body="Hello world!", icon_url="http://example.com/assets/icon.png") self.assertEqual(notification._icon_url, "http://example.com/assets/icon.png")
def test_req_headers(self): headers = { 'Authorization': 'Token token="5374d7dfeffa2eb49965624ba7596a09"', 'Content-Type': 'application/json;charset=UTF-8', 'Accept': 'application/json', } notification = pushpad.Notification(self._project, body="Hello world!") self.assertDictEqual(notification._req_headers(), headers)
def test_broadcast_with_tags(self, deliver_mock): notification = pushpad.Notification(self._project, body="Hello world!") notification.broadcast(tags=('tag1', 'tag2')) deliver_mock.assert_called_once_with({ 'notification': { 'body': 'Hello world!' }, 'tags': ('tag1', 'tag2') })
def test_instantiate(self): self.assertIsNotNone( pushpad.Notification(self._project, body="Hello world!")) notification = pushpad.Notification( self._project, body="Hello world!", title="Website Name", target_url="http://example.com", icon_url="http://example.com/assets/icon.png", ttl=604800, require_interaction=True, image_url="http://example.com/assets/image.png", custom_data="123", custom_metrics=('examples', 'another_metric'), actions=({ 'title': "My Button 1", 'target_url': "http://example.com/button-link", 'icon': "http://example.com/assets/button-icon.png", 'action': "myActionName" }, ), starred=True, send_at=datetime.datetime(2016, 7, 25, 10, 9, 0, 0)) self.assertIsNotNone(notification) self.assertEqual(notification._body, "Hello world!") self.assertEqual(notification._title, "Website Name") self.assertEqual(notification._target_url, "http://example.com") self.assertEqual(notification._icon_url, "http://example.com/assets/icon.png") self.assertEqual(notification._ttl, 604800) self.assertEqual(notification._require_interaction, True) self.assertEqual(notification._image_url, "http://example.com/assets/image.png") self.assertEqual(notification._custom_data, "123") self.assertEqual(notification._custom_metrics, ('examples', 'another_metric')) self.assertEqual(notification._actions, ({ 'title': "My Button 1", 'target_url': "http://example.com/button-link", 'icon': "http://example.com/assets/button-icon.png", 'action': "myActionName" }, )) self.assertEqual(notification._starred, True) self.assertEqual(notification._send_at, datetime.datetime(2016, 7, 25, 10, 9, 0, 0))
def test_req_body_tags(self): body = { 'notification': { 'body': 'Hello world!' }, 'tags': ('tag1', 'tag2') } notification = pushpad.Notification(self._project, body="Hello world!") self.assertDictEqual(notification._req_body(tags=('tag1', 'tag2')), body)
def test_req_body_uids(self): body = { 'notification': { 'body': 'Hello world!' }, 'uids': ('user1', 'user2', 'user3') } notification = pushpad.Notification(self._project, body="Hello world!") self.assertDictEqual( notification._req_body(('user1', 'user2', 'user3')), body)
def test_deliver_to_with_tags(self, deliver_mock): notification = pushpad.Notification(self._project, body="Hello world!") notification.deliver_to(('user1', 'user2'), tags=('tag1', 'tag2')) deliver_mock.assert_called_once_with( req_body={ 'notification': { 'body': 'Hello world!' }, 'uids': ('user1', 'user2'), 'tags': ('tag1', 'tag2') })
def test_req_body_with_optional_fields(self): body = { 'notification': { 'body': 'Hello world!', 'title': 'Website Name', 'target_url': 'http://example.com', 'icon_url': 'http://example.com/assets/icon.png', 'ttl': 604800, 'require_interaction': True, 'image_url': 'http://example.com/assets/image.png', 'custom_data': '123', 'custom_metrics': ('examples', 'another_metric'), 'actions': ({ 'title': 'My Button 1', 'target_url': 'http://example.com/button-link', 'icon': 'http://example.com/assets/button-icon.png', 'action': 'myActionName' }, ), 'starred': True, 'send_at': '2016-07-25T10:09' } } notification = pushpad.Notification( self._project, body="Hello world!", title="Website Name", target_url="http://example.com", icon_url="http://example.com/assets/icon.png", ttl=604800, require_interaction=True, image_url="http://example.com/assets/image.png", custom_data="123", custom_metrics=('examples', 'another_metric'), actions=({ 'title': "My Button 1", 'target_url': "http://example.com/button-link", 'icon': "http://example.com/assets/button-icon.png", 'action': "myActionName" }, ), starred=True, send_at=datetime.datetime(2016, 7, 25, 10, 9, 0, 0)) self.assertDictEqual(notification._req_body(), body)
def test_broadcast(self, deliver_mock): notification = pushpad.Notification(self._project, body="Hello world!", title="Website Name", target_url="http://example.com") notification.broadcast() deliver_mock.assert_called_once_with({ 'notification': { 'title': 'Website Name', 'target_url': 'http://example.com', 'body': 'Hello world!' } })
def test_req_body_all(self): body = { 'notification': { 'body': 'Hello world!', 'title': 'Website Name', 'target_url': 'http://example.com', } } notification = pushpad.Notification(self._project, body="Hello world!", title="Website Name", target_url="http://example.com") self.assertDictEqual(notification._req_body(), body)
def test_deliver_to(self, deliver_mock): notification = pushpad.Notification(self._project, body="Hello world!", title="Website Name", target_url="http://example.com") notification.deliver_to('user1') deliver_mock.assert_called_once_with( req_body={ 'notification': { 'body': 'Hello world!', 'target_url': 'http://example.com', 'title': 'Website Name' }, 'uids': 'user1' })
def test_req_body_with_optional_fields(self): body = { 'notification': { 'body': 'Hello world!', 'title': 'Website Name', 'target_url': 'http://example.com', 'icon_url': 'http://example.com/assets/icon.png', 'ttl': 600 } } notification = pushpad.Notification( self._project, body="Hello world!", title="Website Name", target_url="http://example.com", icon_url='http://example.com/assets/icon.png', ttl=600) self.assertDictEqual(notification._req_body(), body)
def test_deliver(self, req_post_mock): body = { 'notification': { 'body': 'Hello world!', 'title': 'Website Name', 'target_url': 'http://example.com', }, 'uids': 'user1' } mock_response = mock.Mock() resp_json = {u'scheduled': 76} mock_response.status_code = 201 mock_response.json.return_value = resp_json req_post_mock.return_value = mock_response notification = pushpad.Notification( self._project, body="Hello world!", title="Website Name", target_url="http://example.com" ) notification._deliver(body) req_post_mock.assert_called_once_with( 'https://pushpad.xyz/projects/123/notifications', headers={ 'Content-Type': 'application/json;charset=UTF-8', 'Accept': 'application/json', 'Authorization': 'Token token="5374d7dfeffa2eb49965624ba7596a09"' }, json={ 'notification': { 'body': 'Hello world!', 'target_url': 'http://example.com', 'title': 'Website Name' }, 'uids': 'user1' } )
def test_deliver_to_never_broadcasts(self, req_post_mock): notification = pushpad.Notification(self._project, body="Hello world!") mock_response = mock.Mock() mock_response.status_code = 201 mock_response.json.return_value = {u'scheduled': 0} req_post_mock.return_value = mock_response notification.deliver_to(None) req_post_mock.assert_called_once_with( 'https://pushpad.xyz/projects/123/notifications', headers={ 'Content-Type': 'application/json;charset=UTF-8', 'Accept': 'application/json', 'Authorization': 'Token token="5374d7dfeffa2eb49965624ba7596a09"' }, json={ 'notification': { 'body': 'Hello world!' }, 'uids': [] })
# -*- coding: utf-8 -*- import pushpad user1 = 'user1' user2 = 'user2' user3 = 'user3' users = [user1, user2, user3] TOKEN = '5374d7dfeffa2eb49965624ba7596a09' PROJ_ID = 123 project = pushpad.Pushpad(TOKEN, PROJ_ID) print("HMAC signature for the uid: %s is: %s" % (user1, project.signature_for(user1))) print("Subscribe anonymous to push notifications: %s" % (project.path())) print("Subscribe current user: %s to push notifications: %s" % (user1, project.path_for(user1))) notification = pushpad.Notification(project, body="Hello world!", title="Website Name", target_url="http://example.com") print("Send notification to user: %s\nResult: %s" % (user1, notification.deliver_to(user1))) print("Send notification to users: %s\nResult: %s" % (users, notification.deliver_to(users))) print("Send broadcast notification\nResult: %s" % notification.broadcast())
def test_set_ttl(self): """ can change ttl """ notification = pushpad.Notification(self._project, body="Hello world!", ttl=600) self.assertEqual(notification._ttl, 600)
def report(): if 'email' not in session: return redirect(url_for('login')) if request.method == 'POST' and 'type_em' in request.form: address = request.form["address"] description = request.form["description"] type_em = request.form.get('type_em') imagename = "N" try: image = request.files['image'] except: imagename = "No image provided" if imagename != "No image provided": if image and allowed_file(image.filename): filename = secure_filename(image.filename) image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) imagename = filename geolocator = Nominatim(timeout=3) try: location = geolocator.geocode(address) lati = float(location.latitude) longi = float(location.longitude) except: lati= 0.0 longi=0.0 para = (lati,longi) email = session['email'] flag = True conn = mysql.connect() cur = conn.cursor() re = cur.execute("select * from report where type_em = '"+str(type_em)+"' and tstamp BETWEEN timestamp(DATE_SUB(NOW(), INTERVAL 20 MINUTE)) AND timestamp(NOW())") datarows = cur.fetchall() cur.close() conn.close() if re < 1: flag = True else: for row in datarows: lati2 = float(row[7]) longi2 = float(row[6]) desc2 = str(row[3]) para2 = (lati2,longi2) if great_circle(para,para2).meters < 200 and cosine_sim(desc2,description)> 0.60 : flag =False break # Execute query if flag==True: conn = mysql.connect() cur = conn.cursor() cur.execute("INSERT INTO report(email, address, description, image, type_em, longi,lati) VALUES(%s, %s, %s,%s, %s, %s, %s)", (email, address, description, imagename, type_em, str(longi),str(lati))) conn.commit() cur.close() conn.close() conn = mysql.connect() cur = conn.cursor() cur.execute("select email from users") to = cur.fetchall() cur.close() conn.close() tolist=[] for r in to: tolist.append(r[0]) subject = type_em + " at " + address body = type_em + " at " + address + "\n" + description smtpserver = smtplib.SMTP("smtp.gmail.com",587) smtpserver.ehlo() smtpserver.starttls() smtpserver.ehlo() smtpserver.login(gmail_user, gmail_pwd) header = 'To:' + ", ".join(tolist) + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:' + subject + ' \n' msg = header + '\n' + body + '\n\n' smtpserver.sendmail(gmail_user, to, msg) smtpserver.close() flash('Emergency reported successfully', 'success') notification = pushpad.Notification(project, body=type_em + " at " + address + "\n" +description) notification.broadcast() else: flash('Emergency has already been reported', 'success') return redirect(url_for('dashboard')) return render_template('report.html')