class TweetStreamer(TwythonStreamer): def setup(self): self.notifier = Notifier() def on_success(self, data): if 'text' in data: print('{:>30}:{}'.format( data['created_at'], data['text'].encode('utf-8'))) self.notifier.ontweetreceived(data) def on_error(self, status_code, data): print(status_code) self.disconnect()
def test_notify_when_no_projection(self): mock_client = MockClient() n = Notifier(mock_client) n.notify_set_fba_lineup( team_name="name", transitions=[ LineupTransition(PlayerTest.lebron, BasketballSlot.POINT_GUARD, BaseballSlot.BENCH) ], total_points=123.0, player_to_fp={} # no projection for lebron ) self.assertEqual(mock_client.messages[0][0],"name: 123.0 points\nJames: PG->BE (0.0)")
def test_notify_set_lineup(self): mock_client = MockClient() n = Notifier(mock_client) team = "Team1" ts = [self.t1, self.t2] n.notify_set_lineup(team, self.lt1, ts, [ScoringSetting(BaseballStat.H, False, 0.0)]) msg = team + ": 50.00PA\n10.00H " for t in ts: msg += "\n" + Notifier.transition_message(t) (msg_rec, url_rec) = mock_client.messages[0] self.assertEqual(msg, msg_rec) self.assertEqual(None, url_rec)
def test_notify_ignore_non_hitting_stat(self): mock_client = MockClient() n = Notifier(mock_client) team = "Name" ts = [self.t1, self.t2] n.notify_set_lineup( team, self.lt1, ts, [ScoringSetting(BaseballStat.WHIP, True, 0.0)] ) expected = team + ": 50.00PA\n" for t in ts: expected += "\n" + Notifier.transition_message(t) (msg_rec, url_rec) = mock_client.messages[0] self.assertEqual(expected, msg_rec) self.assertEqual(None, url_rec)
def edit_planning(request, planning_ekey): """ GET method: Displays the planning edition page POST method: -Gather the data of the planning form. -Test if the planning's form is valid. -Update the planning in the database -If the planning is protected, update the guests emails in the planning -Update the events in the planning -Redirects to the planning's display. """ planning = Planning.objects.get_by_ekey_or_404(planning_ekey) if request.user != planning.creator: redirect_url = f"{reverse('accounts:login')}?next={request.path}" return HttpResponseRedirect(redirect_url) if request.method == 'POST': planning_form = PlanningCreationForm(request.POST, instance=planning) if planning_form.is_valid(): planning_form.save() if planning.protected: new_emails = request.POST.getlist('guest_email') update_guests(planning, new_emails) event_formset = EventInlineFormSet(request.POST, instance=planning) if event_formset.is_valid(): event_formset.save() notifier = Notifier(planning) notifier.notify_events_changes(event_formset) return redirect('participations:view', planning.ekey) planning_form = PlanningCreationForm(instance=planning) event_formset = EventInlineFormSet(instance=planning) return render( request, 'plannings/create_planning.html', { 'event_formset': event_formset, 'planning_form': planning_form, 'form_url': request.path })
def test_notify_event_changes(self): planning = Planning.objects.first() participants = [user.email for user in get_user_model().objects.filter( event__planning__pk=planning.pk).distinct()] # Assert that there are participants self.assertTrue(len(participants)) events = [Event(planning=planning, date=datetime.date(2020, 1, i)) for i in range(1, 4)] formset = EventInlineFormSet() formset.new_objects = [events[0], events[1]] formset.changed_objects = [] formset.deleted_objects = [events[2]] notifier = Notifier(planning) notifier.notify_events_changes(formset) # Assert that all the participants got an email self.assertEqual(len(participants), len(mail.outbox)) for email in mail.outbox: self.assertEqual(1, len(email.to)) self.assertIn(email.to[0], participants) participants.remove(email.to[0]) self.assertEqual(len(participants), 0)
def setup(self): self.notifier = Notifier()
def test_transition_message(self): self.assertEqual(Notifier.transition_message(self.t1), "Muncy: 1B/3B->BE") self.assertEqual(Notifier.transition_message(self.t2), "Merrifield: OF->2B")
def notify_guest(sender, **kwargs): if kwargs.get('action') == 'post_add': notification = Notifier(kwargs.get('instance')) notification.notify_guests(kwargs.get('pk_set'))
def current_notifier(user): env = os.getenv("ENV", "DEV") return prod_notifier(user) if env == "PROD" else Notifier(DevClient())
def prod_notifier(user): return Notifier( PushedClient(user, user_pushed_id(user), app_key(), app_secret()))