def test_notification_form_validation(self): data = {"phone_number" : "0123456789", "way_of_communication" : get_woc('sms').id, "date" : "2010-08-12 12:00"} form = NotificationValidationForm(data) self.assertTrue(form.is_valid()) data = { "phone_number" : "0123456rttddbh789", "way_of_communication" : 4, "date" : "abc" } form = NotificationValidationForm(data) self.assertFalse(form.is_valid()) self.assertEquals(len(form['phone_number'].errors), 1) self.assertEquals(len(form['way_of_communication'].errors), 1) self.assertEquals(len(form['date'].errors), 1)
def create_notification(request, notification_type_name = None): ''' Display the form and creates a new notification, but does not save it yet. Redirect to authentication if switched on ''' notification_type = NotificationType.objects. \ filter(name = notification_type_name)[0] nexturl = "" backurl = reverse('web_index') ways_of_communication = get_ways_of_communication( notification_type.notify_immediately) if request.method == "POST": data = deepcopy(request.POST) if notification_type.notify_immediately: data['date'] = date.today().strftime('%Y-%m-%d') + \ ' ' + DEFAULT_SEND_TIME else: data['date'] = data.get('date', '') + ' ' + DEFAULT_SEND_TIME form = NotificationValidationForm(data) woc = get_woc_by_id( request.POST['way_of_communication'] ) if not woc.can_send_immediately: form = NotificationValidationFormBluetooth(data) if form.is_valid(): notification = Notification() patient = Patient() if woc.can_send_immediately: patient.phone_number = form.cleaned_data['phone_number'] notification.date = form.cleaned_data['date'] notification.notification_type = notification_type notification.hospital = Hospital.get_current_hospital() notification.way_of_communication = \ form.cleaned_data['way_of_communication'] request.session['notification'] = notification request.session['patient'] = patient logger.info("Create notification via %s" % notification.way_of_communication.verbose_name) if notification.way_of_communication == get_woc('bluetooth'): return HttpResponseRedirect(reverse("web_list_devices") + \ "?next=" + reverse("notifications_send")) elif notification.way_of_communication.name in ('sms', 'voice' ): return redirect_to_authentication_or( reverse("notifications_save")) else: logger.error("Unknown way of communication selected.") raise Exception ("Unknown way of communication %s " \ %notification.way_of_communication.\ verbose_name + "(this is neither " + \ "bluetooth nor sms or voice)") else: logger.info("create_notification: Invalid form.") return render_to_response('notifications/create.html', locals(), context_instance=RequestContext(request))