def test_incorrect_deadline(self): """The crowdfund deadline cannot be set in the past. That makes no sense!""" data = self.data yesterday = datetime.now() - timedelta(1) data['date_due'] = yesterday.strftime('%Y-%m-%d') form = CrowdfundForm(data) ok_(not form.is_valid(), 'The form should not validate.')
def test_incorrect_duration(self): """The crowdfund duration should be capped at 30 days.""" data = self.data too_long = datetime.now() + timedelta(45) data['date_due'] = too_long.strftime('%Y-%m-%d') form = CrowdfundForm(data) ok_(not form.is_valid(), 'The form should not validate.')
def setUp(self): self.form = CrowdfundForm() foia = Mock(FOIARequest) foia.id = 1 foia.price = Decimal(100.00) due = date.today() + timedelta(30) self.data = { "name": "Crowdfund this Request", "description": 'Let\'s "payve" the way forward!', "payment_required": foia.price, "date_due": due.strftime("%Y-%m-%d"), "foia": foia.id, }
def setUp(self): self.form = CrowdfundForm() foia = Mock(FOIARequest) foia.id = 1 foia.price = Decimal(100.00) due = datetime.today() + timedelta(30) self.data = { 'name': 'Crowdfund this Request', 'description': 'Let\'s "payve" the way forward!', 'payment_required': foia.price, 'date_due': due.strftime('%Y-%m-%d'), 'foia': foia.id }
class TestCrowdfundForm(TestCase): """Tests the form used to create a crowdfund campaign.""" def setUp(self): self.form = CrowdfundForm() foia = Mock(FOIARequest) foia.id = 1 foia.price = Decimal(100.00) due = date.today() + timedelta(30) self.data = { 'name': 'Crowdfund this Request', 'description': 'Let\'s "payve" the way forward!', 'payment_required': foia.price, 'date_due': due.strftime('%Y-%m-%d'), 'foia': foia.id } def test_prefilled_request_form(self): """An empty crowdfund form should prefill with everything it needs to validate.""" form = CrowdfundForm(initial=self.data) ok_(form) def test_empty_validation(self): """An empty form should not validate.""" ok_(not self.form.is_valid()) def test_expected_validation(self): """Given a correct set of data, the form should validate.""" form = CrowdfundForm(self.data) ok_(form.is_valid(), 'The form should validate.') def test_zero_amount(self): """Payment required should not be zero.""" data = self.data data['payment_required'] = 0 form = CrowdfundForm(data) ok_(not form.is_valid(), 'The form should not validate.') def test_negative_amount(self): """Payment required should not be negative.""" data = self.data data['payment_required'] = -10.00 form = CrowdfundForm(data) ok_(not form.is_valid(), 'The form should not validate.') def test_incorrect_deadline(self): """The crowdfund deadline cannot be set in the past. That makes no sense!""" data = self.data yesterday = timezone.now() - timedelta(1) data['date_due'] = yesterday.strftime('%Y-%m-%d') form = CrowdfundForm(data) ok_(not form.is_valid(), 'The form should not validate.') def test_incorrect_duration(self): """The crowdfund duration should be capped at 30 days.""" data = self.data too_long = timezone.now() + timedelta(45) data['date_due'] = too_long.strftime('%Y-%m-%d') form = CrowdfundForm(data) ok_(not form.is_valid(), 'The form should not validate.')
def get_context_data(self, **kwargs): """Add extra context data""" context = super(Detail, self).get_context_data(**kwargs) foia = context['foia'] user = self.request.user user_can_edit = foia.has_perm(self.request.user, 'change') user_can_embargo = foia.has_perm(self.request.user, 'embargo') is_past_due = foia.date_due < datetime.now().date() if foia.date_due else False include_draft = user.is_staff or foia.status == 'started' context['all_tags'] = Tag.objects.all() context['past_due'] = is_past_due context['user_can_edit'] = user_can_edit context['user_can_pay'] = user_can_edit and foia.is_payable() context['embargo'] = { 'show': user_can_embargo or foia.embargo, 'edit': user_can_embargo, 'add': user_can_embargo, 'remove': user_can_edit and foia.embargo, } context['embargo_form'] = FOIAEmbargoForm(initial={ 'permanent_embargo': foia.permanent_embargo, 'date_embargo': foia.date_embargo }) context['note_form'] = FOIANoteForm() context['access_form'] = FOIAAccessForm() context['question_form'] = QuestionForm(user=user, initial={'foia': foia}) context['crowdfund_form'] = CrowdfundForm(initial={ 'name': u'Crowdfund Request: %s' % unicode(foia), 'description': 'Help cover the request fees needed to free these docs!', 'payment_required': foia.get_stripe_amount(), 'date_due': datetime.now() + timedelta(30), 'foia': foia }) context['embargo_needs_date'] = foia.status in END_STATUS context['user_actions'] = foia.user_actions(user) context['contextual_request_actions'] = \ foia.contextual_request_actions(user, user_can_edit) context['status_choices'] = STATUS if include_draft else STATUS_NODRAFT context['show_estimated_date'] = foia.status not in ['submitted', 'ack', 'done', 'rejected'] context['change_estimated_date'] = FOIAEstimatedCompletionDateForm(instance=foia) all_tasks = Task.objects.filter_by_foia(foia, user) open_tasks = [task for task in all_tasks if not task.resolved] context['task_count'] = len(all_tasks) context['open_task_count'] = len(open_tasks) context['open_tasks'] = open_tasks context['stripe_pk'] = settings.STRIPE_PUB_KEY context['sidebar_admin_url'] = reverse('admin:foia_foiarequest_change', args=(foia.pk,)) context['is_thankable'] = self.request.user.has_perm( 'foia.thank_foiarequest', foia) context['files'] = foia.files.all()[:50] if foia.sidebar_html: messages.info(self.request, foia.sidebar_html) return context
def crowdfund_request(request, idx, **kwargs): """Crowdfund a request""" # pylint: disable=unused-argument # select for update locks this request in order to prevent a race condition # allowing multiple crowdfunds to be created for it foia = get_object_or_404( FOIARequest.objects.select_for_update().select_related( "agency__jurisdiction", "composer" ), pk=idx, ) # check for unauthorized access if not foia.has_perm(request.user, "crowdfund"): messages.error(request, "You may not crowdfund this request.") return redirect(foia) if request.method == "POST": # save crowdfund object form = CrowdfundForm(request.POST) if form.is_valid(): crowdfund = form.save() foia.crowdfund = crowdfund foia.save(comment="added a crowdfund") messages.success(request, "Your crowdfund has started, spread the word!") new_action( request.user, "began crowdfunding", action_object=crowdfund, target=foia ) crowdfund.send_intro_email(request.user) mixpanel_event( request, "Launch Request Crowdfund", foia.mixpanel_data( { "Name": crowdfund.name, "Payment Capped": crowdfund.payment_capped, "Payment Required": float(crowdfund.payment_required), "Date Due": crowdfund.date_due.isoformat(), } ), ) return redirect(foia) elif request.method == "GET": # create crowdfund form default_crowdfund_duration = 30 date_due = timezone.now() + timedelta(default_crowdfund_duration) initial = { "name": "Crowdfund Request: %s" % str(foia), "description": "Help cover the request fees needed to free these docs!", "payment_required": foia.get_stripe_amount(), "date_due": date_due, "foia": foia, } form = CrowdfundForm(initial=initial) mixpanel_event(request, "Start Request Crowdfund", foia.mixpanel_data()) return render(request, "forms/foia/crowdfund.html", {"form": form})
def crowdfund_request(request, idx, **kwargs): """Crowdfund a request""" # pylint: disable=unused-argument # select for update locks this request in order to prevent a race condition # allowing multiple crowdfunds to be created for it foia = FOIARequest.objects.select_for_update().get(pk=idx) # check for unauthorized access if not foia.has_perm(request.user, 'crowdfund'): messages.error(request, 'You may not crowdfund this request.') return redirect(foia) if request.method == 'POST': # save crowdfund object form = CrowdfundForm(request.POST) if form.is_valid(): crowdfund = form.save() foia.crowdfund = crowdfund foia.save(comment='added a crowdfund') messages.success( request, 'Your crowdfund has started, spread the word!' ) new_action( request.user, 'began crowdfunding', action_object=crowdfund, target=foia ) crowdfund.send_intro_email(request.user) return redirect(foia) elif request.method == 'GET': # create crowdfund form default_crowdfund_duration = 30 date_due = timezone.now() + timedelta(default_crowdfund_duration) initial = { 'name': u'Crowdfund Request: %s' % unicode(foia), 'description': 'Help cover the request fees needed to free these docs!', 'payment_required': foia.get_stripe_amount(), 'date_due': date_due, 'foia': foia } form = CrowdfundForm(initial=initial) return render( request, 'forms/foia/crowdfund.html', {'form': form}, )
def _get_form_context_data(self, context): """Get context data about forms""" context["access_form"] = FOIAAccessForm() context["admin_fix_form"] = self.admin_fix_form context["agency_passcode_form"] = self.agency_passcode_form context["agency_reply_form"] = self.agency_reply_form context["appeal_contact_info_form"] = ContactInfoForm( foia=self.foia, appeal=True, prefix="appeal" ) context["change_estimated_date"] = FOIAEstimatedCompletionDateForm( instance=self.foia ) context["contact_info_form"] = ContactInfoForm( foia=self.foia, prefix="followup" ) context["crowdfund_form"] = CrowdfundForm( initial={ "name": "Crowdfund Request: %s" % str(self.foia), "description": "Help cover the request fees needed to free these docs!", "payment_required": self.foia.get_stripe_amount(), "date_due": timezone.now() + timedelta(30), "foia": self.foia, } ) context["embargo_form"] = FOIAEmbargoForm( initial={ "permanent_embargo": self.foia.permanent_embargo, "date_embargo": self.foia.date_embargo, } ) context["fee_form"] = self.fee_form context["note_form"] = FOIANoteForm() context["portal_form"] = PortalForm(foia=self.foia) context["resend_forms"] = self.resend_forms context["tracking_id_form"] = TrackingNumberForm() # this data used in a form context["status_choices"] = [(k, v) for (k, v) in STATUS if k != "submitted"] context["user_actions"] = self.foia.user_actions( self.request.user, context["is_agency_user"] )
def crowdfund_request(request, idx, **kwargs): """Crowdfund a request""" # pylint: disable=unused-argument foia = FOIARequest.objects.get(pk=idx) owner_or_staff = request.user == foia.user or request.user.is_staff # check for unauthorized access if not owner_or_staff: messages.error(request, 'You may only crowdfund your own requests.') return redirect(foia) if foia.has_crowdfund(): messages.error(request, 'You may only run one crowdfund per request.') return redirect(foia) if foia.status != 'payment': messages.error(request, 'You may only crowfund when payment is required.') return redirect(foia) if request.method == 'POST': # save crowdfund object form = CrowdfundForm(request.POST) if form.is_valid(): crowdfund = form.save() foia.crowdfund = crowdfund foia.save(comment='added a crowdfund') messages.success(request, 'Your crowdfund has started, spread the word!') new_action(request.user, 'began crowdfunding', action_object=crowdfund, target=foia) return redirect(foia) elif request.method == 'GET': # create crowdfund form default_crowdfund_duration = 30 date_due = datetime.now() + timedelta(default_crowdfund_duration) initial = { 'name': u'Crowdfund Request: %s' % unicode(foia), 'description': 'Help cover the request fees needed to free these docs!', 'payment_required': foia.get_stripe_amount(), 'date_due': date_due, 'foia': foia } form = CrowdfundForm(initial=initial) return render_to_response('forms/foia/crowdfund.html', {'form': form}, context_instance=RequestContext(request))
def test_negative_amount(self): """Payment required should not be negative.""" data = self.data data['payment_required'] = -10.00 form = CrowdfundForm(data) ok_(not form.is_valid(), 'The form should not validate.')
def test_zero_amount(self): """Payment required should not be zero.""" data = self.data data['payment_required'] = 0 form = CrowdfundForm(data) ok_(not form.is_valid(), 'The form should not validate.')
def test_expected_validation(self): """Given a correct set of data, the form should validate.""" form = CrowdfundForm(self.data) ok_(form.is_valid(), 'The form should validate.')
def test_prefilled_request_form(self): """An empty crowdfund form should prefill with everything it needs to validate.""" form = CrowdfundForm(initial=self.data) ok_(form)
def get_context_data(self, **kwargs): """Add extra context data""" # pylint: disable=too-many-statements context = super(Detail, self).get_context_data(**kwargs) foia = context['foia'] user = self.request.user user_can_edit = foia.has_perm(self.request.user, 'change') user_can_embargo = foia.has_perm(self.request.user, 'embargo') is_past_due = foia.date_due < timezone.now().date( ) if foia.date_due else False context['all_tags'] = Tag.objects.all() context['past_due'] = is_past_due context['user_can_edit'] = user_can_edit context['user_can_pay'] = (foia.has_perm(self.request.user, 'pay') and foia.status == 'payment') context['embargo'] = { 'show': user_can_embargo or foia.embargo, 'edit': user_can_embargo, 'add': user_can_embargo, 'remove': user_can_edit and foia.embargo, } context['embargo_form'] = FOIAEmbargoForm( initial={ 'permanent_embargo': foia.permanent_embargo, 'date_embargo': foia.date_embargo }) context['note_form'] = FOIANoteForm() context['access_form'] = FOIAAccessForm() context['question_form'] = QuestionForm(user=user, initial={'foia': foia}) context['crowdfund_form'] = CrowdfundForm( initial={ 'name': u'Crowdfund Request: %s' % unicode(foia), 'description': 'Help cover the request fees needed to free these docs!', 'payment_required': foia.get_stripe_amount(), 'date_due': timezone.now() + timedelta(30), 'foia': foia }) context['embargo_needs_date'] = foia.status in END_STATUS context['user_actions'] = foia.user_actions(user) context['status_choices'] = STATUS context['show_estimated_date'] = foia.status not in [ 'submitted', 'ack', 'done', 'rejected' ] context['change_estimated_date'] = FOIAEstimatedCompletionDateForm( instance=foia) context['tracking_id_form'] = TrackingNumberForm() context['contact_info_form'] = ContactInfoForm(foia=foia) if user_can_edit or user.is_staff: all_tasks = Task.objects.filter_by_foia(foia, user) open_tasks = [task for task in all_tasks if not task.resolved] context['task_count'] = len(all_tasks) context['open_task_count'] = len(open_tasks) context['open_tasks'] = open_tasks context['asignees'] = User.objects.filter( is_staff=True, ).order_by('last_name') context['stripe_pk'] = settings.STRIPE_PUB_KEY context['sidebar_admin_url'] = reverse('admin:foia_foiarequest_change', args=(foia.pk, )) context['is_thankable'] = self.request.user.has_perm( 'foia.thank_foiarequest', foia) context['files'] = foia.get_files()[:50] if self.request.user.is_authenticated(): context['foia_cache_timeout'] = 0 else: context['foia_cache_timeout'] = settings.DEFAULT_CACHE_TIMEOUT context['settings'] = settings context['agency_status_choices'] = AGENCY_STATUS context['agency_reply_form'] = self.agency_reply_form context['admin_fix_form'] = self.admin_fix_form context['resend_forms'] = self.resend_forms context['cc_emails'] = json.dumps( [unicode(e) for e in foia.cc_emails.all()]) if (foia.composer.status == 'submitted' and foia.composer.datetime_submitted is not None): context['revoke_deadline'] = ( foia.composer.datetime_submitted + timedelta(seconds=COMPOSER_EDIT_DELAY)) context['can_revoke'] = foia.composer.revokable() if foia.sidebar_html: messages.info(self.request, foia.sidebar_html) return context