def test_now_date_set_on_save(self): """ 'now' messages automatically get date assignment """ data = {"when": "now", "body": self.random_string(140), "groups": [self.group.pk]} form = BroadcastForm(data) self.assertTrue(form.is_valid()) broadcast = form.save() self.assertTrue(broadcast.date is not None)
def test_field_clearing(self): """ Non related frequency fields should be cleared on form clean """ weekday = self.get_weekday_for_date(datetime.datetime.now()) before = self.create_broadcast(when="future", data={"groups": [self.group.pk], "weekdays": [weekday]}) data = { "when": "later", "date": before.date, "body": before.body, "schedule_frequency": "monthly", "groups": [self.group.pk], "weekdays": [weekday.pk], } form = BroadcastForm(data, instance=before) after = form.save() self.assertEqual(after.weekdays.count(), 0)
def test_field_clearing(self): """ Non related frequency fields should be cleared on form clean """ weekday = self.get_weekday_for_date(datetime.datetime.now()) before = self.create_broadcast(when='future', data={'groups': [self.group.pk], 'weekdays': [weekday]}) data = { 'when': 'later', 'date': before.date, 'body': before.body, 'schedule_frequency': 'monthly', 'groups': [self.group.pk], 'weekdays': [weekday.pk], } form = BroadcastForm(data, instance=before) after = form.save() self.assertEqual(after.weekdays.count(), 0)
def test_update(self): """ Test broadcast edit functionality """ before = self.create_broadcast(when="future", data={"groups": [self.group.pk]}) data = { "when": "later", "date": before.date, "body": self.random_string(30), "schedule_frequency": before.schedule_frequency, "groups": [self.group.pk], } form = BroadcastForm(data, instance=before) self.assertTrue(form.is_valid(), form._errors.as_text()) before = Broadcast.objects.get(pk=before.pk) after = form.save() # same broadcast self.assertEqual(before.pk, after.pk) # new message self.assertNotEqual(before.body, after.body)
def test_update(self): """ Test broadcast edit functionality """ before = self.create_broadcast(when='future', data={'groups': [self.group.pk]}) data = { 'when': 'later', 'date': before.date, 'body': self.random_string(30), 'schedule_frequency': before.schedule_frequency, 'groups': [self.group.pk], } form = BroadcastForm(data, instance=before) self.assertTrue(form.is_valid(), form._errors.as_text()) before = Broadcast.objects.get(pk=before.pk) after = form.save() # same broadcast self.assertEqual(before.pk, after.pk) # new message self.assertNotEqual(before.body, after.body)
def send_message(request, broadcast_id=None): if broadcast_id: broadcast = get_object_or_404(Broadcast, pk=broadcast_id) else: broadcast = None if request.method == 'POST': form = BroadcastForm(request.POST, instance=broadcast) if form.is_valid(): broadcast = form.save() if broadcast_id: info = 'Broadcast successfully saved' else: info = 'Message queued for delivery' messages.info(request, info) return HttpResponseRedirect(reverse('broadcast-schedule')) else: form = BroadcastForm(instance=broadcast) broadcasts = Broadcast.objects.exclude(schedule_frequency__isnull=True) context = { 'form': form, 'broadcasts': broadcasts.order_by('date'), } return render_to_response('broadcast/send_message.html', context, RequestContext(request))