def task_post_helper(self, request, task, form_data=None): """Special post helper exclusive to ResponseTask""" if request.POST.get('proxy') or request.POST.get('save'): form = ResponseTaskForm(request.POST, task=task) if not form.is_valid(): messages.error(request, 'Form is invalid') return action_taken, error_msgs = form.process_form(task, request.user) for msg in error_msgs: messages.error(request, msg) if action_taken and not error_msgs: form_data = form.cleaned_data if form_data['price'] is not None: # cast from decimal to float, since decimal # is not json serializable form_data['price'] = float(form_data['price']) if form_data.get('date_estimate'): # to string for json form_data['date_estimate'] = form_data[ 'date_estimate'].isoformat() if task.scan: task.communication.hidden = False task.communication.create_agency_notifications() task.communication.save() task.resolve(request.user, form.cleaned_data) return super(ResponseTaskList, self).task_post_helper(request, task)
def set_status(self, status): """Forward to form logic, for use in classify_status task""" # pylint: disable=import-outside-toplevel from muckrock.task.forms import ResponseTaskForm form = ResponseTaskForm(task=self) form.set_status(status, set_foia=True, comms=[self.communication])
def setUp(self): agency = AgencyFactory() comm = FOIACommunicationFactory(response=True, foia__agency=agency) self.task = ResponseTask.objects.create(communication=comm) self.form = ResponseTaskForm() self.user = UserFactory()
class ResponseTaskTests(TestCase): """Test the ResponseTask class""" def setUp(self): agency = AgencyFactory() comm = FOIACommunicationFactory(response=True, foia__agency=agency) self.task = ResponseTask.objects.create(communication=comm) self.form = ResponseTaskForm() self.user = UserFactory() def test_get_absolute_url(self): eq_( self.task.get_absolute_url(), reverse('response-task', kwargs={ 'pk': self.task.pk }) ) def test_task_creates_successfully(self): ok_( self.task, 'Response tasks should creates successfully given a communication' ) def test_set_status_to_ack(self): self.form.set_status('ack', True, [self.task.communication]) eq_( self.task.communication.foia.datetime_done, None, 'The FOIA should not be set to done if the status does not indicate it is done.' ) eq_( self.task.communication.status, 'ack', 'The communication should be set to the proper status.' ) eq_( self.task.communication.foia.status, 'ack', 'The FOIA should be set to the proper status.' ) def test_set_status_to_done(self): self.form.set_status('done', True, [self.task.communication]) eq_( self.task.communication.foia.datetime_done is None, False, 'The FOIA should be set to done if the status indicates it is done.' ) eq_( self.task.communication.status, 'done', 'The communication should be set to the proper status.' ) eq_( self.task.communication.foia.status, 'done', 'The FOIA should be set to the proper status.' ) def test_set_comm_status_only(self): foia = self.task.communication.foia existing_status = foia.status self.form.set_status('done', False, [self.task.communication]) foia.refresh_from_db() eq_( foia.datetime_done is None, True, 'The FOIA should not be set to done because we are not settings its status.' ) eq_( foia.status, existing_status, 'The FOIA status should not be changed.' ) eq_( self.task.communication.status, 'done', 'The Communication status should be changed, however.' ) def test_set_tracking_id(self): new_tracking = u'dogs-r-cool' self.form.set_tracking_id(new_tracking, [self.task.communication]) self.task.refresh_from_db() eq_( self.task.communication.foia.current_tracking_id(), new_tracking, 'Should update the tracking number on the request.' ) def test_set_date_estimate(self): new_date = timezone.now() self.form.set_date_estimate(new_date, [self.task.communication]) eq_( self.task.communication.foia.date_estimate, new_date, 'Should update the estimated completion date on the request.' ) def test_set_price(self): price = 1.23 self.form.set_price(price, [self.task.communication]) eq_( self.task.communication.foia.price, price, 'Should update the price on the request.' ) def test_move(self): move_to_foia = FOIARequestFactory() self.form.move_communication( self.task.communication, [move_to_foia.pk], self.user ) eq_( self.task.communication.foia, move_to_foia, 'Should move the communication to a different request.' ) @raises(ValueError) def test_bad_status(self): """Should raise an error if given a nonexistant status.""" self.form.set_status('foo', True, [self.task.communication]) @raises(ValueError) def test_bad_tracking_number(self): """Should raise an error if not given a string.""" self.form.set_tracking_id(['foo'], [self.task.communication]) @raises(ValueError) def test_bad_move(self): """Should raise a value error if non-existant move destination.""" self.form.move_communication( self.task.communication, [111111], self.user ) @raises(ValueError) def test_bad_price(self): """Should raise an error if not given a value convertable to a float""" self.form.set_price('foo', [self.task.communication])
def task_post_helper(self, request, task): """Special post helper exclusive to ResponseTask""" # pylint: disable=too-many-branches task = super(ResponseTaskList, self).task_post_helper(request, task) error_happened = False form = ResponseTaskForm(request.POST) if not form.is_valid(): messages.error(request, 'Form is invalid') return cleaned_data = form.cleaned_data status = cleaned_data['status'] set_foia = cleaned_data['set_foia'] move = cleaned_data['move'] tracking_number = cleaned_data['tracking_number'] date_estimate = cleaned_data['date_estimate'] price = cleaned_data['price'] proxy = cleaned_data['proxy'] # move is executed first, so that the status and tracking # operations are applied to the correct FOIA request comms = None if move: try: comms = task.move(move) except (Http404, ValueError): messages.error(request, 'No valid destination for moving the request.') error_happened = True if status: try: task.set_status(status, set_foia, comms) except ValueError: messages.error( request, 'You tried to set the request to an invalid status.') error_happened = True if tracking_number: try: task.set_tracking_id(tracking_number, comms) except ValueError: messages.error( request, 'You tried to set an invalid tracking id. Just use a string of characters.' ) error_happened = True if date_estimate: try: task.set_date_estimate(date_estimate, comms) except ValueError: messages.error( request, 'You tried to set the request to an invalid date.') error_happened = True if price: try: task.set_price(price, comms) except ValueError: messages.error(request, 'You tried to set a non-numeric price.') error_happened = True if proxy: task.proxy_reject(comms) action_taken = move or status or tracking_number or price or proxy if action_taken and not error_happened: task.resolve(request.user)
def set_status(self, status): """Forward to form logic, for use in classify_status task""" from muckrock.task.forms import ResponseTaskForm form = ResponseTaskForm() form.set_status(status, set_foia=True, comms=[self.communication])