def get_form_data(formset): """Return the form data for formset as a dict.""" form_data = flatten_to_dict(formset) for form in formset: form_data.update(flatten_to_dict(form.nested)) return form_data
def get_form_data(formset): """Return the form data for formset as a dict.""" form_data = flatten_to_dict(formset) for form in formset: form_data.update( flatten_to_dict(form.nested) ) return form_data
def get_form_data(self, formset): """ Get from Formset the data field :param formset: :return: """ form_data = flatten_to_dict(formset) for form in formset: form_data.update(flatten_to_dict(form.nested)) return form_data
def get_form_data(self, formset): """ Get from Formset the data field :param formset: :return: """ form_data = flatten_to_dict(formset) for form in formset: form_data.update( flatten_to_dict(form.nested) ) return form_data
def test_inline_formsets_saved_after_parent_saved(self): form_data = flatten_to_dict(MultiEmailFormGroup()) form_data.update( { 'group-name-first_name': 'Joe', 'group-name-last_name': 'Smith', 'group-email-0-email': '*****@*****.**', 'group-email-1-email': '*****@*****.**', 'group-email-TOTAL_FORMS': '2', } ) instance = FakeModel() self.assertEqual(instance.id, None) formgroup = MultiEmailFormGroup( data=form_data, instance=instance, ) self.assertTrue(formgroup.is_valid()) self.assertEqual(len(formgroup.email), 2) formgroup.save() self.assertEqual(instance.id, 42)
def userPreference(request): if request.method == 'POST': # create a form instance and populate it with data from the request: form = PredictCollege(request.POST) # check whether it's valid: if form.is_valid(): form_data = flatten_to_dict(form) # collect college list according to the seleted criteria colleges = generate_list.generate(form_data['score'], form_data['branch'], form_data['category'], form_data['location'], 'myapp/data/pickles/') flag = True if len(colleges) != 0 else False form_data['branch'] = CollegeData.get_branch_name( form_data['branch']) form_data['category'] = CollegeData.get_category_name( form_data['category']) return render(request, 'smart_list.html', { 'colleges': colleges, 'flag': flag, 'form_data': form_data }) else: form = PredictCollege() return render(request, 'user_preference.html', {'form': form})
def index(request): flag = False if request.method == 'POST': # create a form instance and populate it with data from the request: form = UserForm(request.POST) # check whether it's valid: if form.is_valid(): # process the data in form.cleaned_data as required flag = True form_data = flatten_to_dict(form) filename = 'myapp/data/pickles/colleges/' + form_data[ 'college'] + '.pkl' test = np.array([ 2019, form_data['branch'], form_data['category'], form_data['score'] ]) form_data['probability'] = predict_proba.main(filename, test) form_data['branch'] = CollegeData.get_branch_name( form_data['branch']) form_data['college'] = CollegeData.get_college_name( form_data['college']) form_data['category'] = CollegeData.get_category_name( form_data['category']) # redirect to a new URL: # return HttpResponseRedirect('/index/') return render(request, 'index.html', { 'form': form, 'form_data': form_data, 'flag': flag }) # if a GET (or any other method) we'll create a blank form else: form = UserForm() return render(request, 'index.html', {'form': form, 'flag': flag})
def test_same_email_is_valid(self): form_data = flatten_to_dict(forms.ContactForm()) form_data['first_name'] = 'Foo' form_data['last_name'] = 'Bar' form_data['email'] = '*****@*****.**' form_data['confirm_email'] = '*****@*****.**' bound_form = forms.ContactForm(data=form_data) self.assert_(bound_form.is_valid())
def test_mismatch_email_is_valid(self): form_data = flatten_to_dict(ContactForm()) form_data['first_name'] = 'Foo' form_data['last_name'] = 'Bar' form_data['email'] = '*****@*****.**' form_data['confirm_email'] = '*****@*****.**' bound_form = ContactForm(data=form_data) self.assertFalse(bound_form.is_valid())
def test_redirects_after_save(self): from transaction.forms import TransactionForm transaction = mommy.make('Transaction') form_data = flatten_to_dict(TransactionForm(instance=transaction)) _, response = self.post(transaction, form_data) self.assertEqual(302, response.status_code) self.assertEqual(reverse('transaction:transaction_list'), response._headers['location'][1])
def test_show_alert_message_after_save(self): from transaction.forms import TransactionForm old = mommy.make('Transaction') form_data = flatten_to_dict(TransactionForm(instance=old)) request, response = self.post(old, form_data) self.assert_redirect(response, reverse('transaction:transaction_list')) message = 'Transaction was updated successfuly!' self.assert_message_exists(request, messages.SUCCESS, message)
def test_redirects_after_save(self): from transaction.forms import TransactionForm category = mommy.make('Category') transaction = mommy.prepare('Transaction', category=category) form_data = flatten_to_dict(TransactionForm(instance=transaction)) _, response = self.post(form_data) self.assertEqual(302, response.status_code) self.assertEqual(('Location', reverse('transaction:transaction_list')), response._headers['location'])
def test_mismatch_email_is_invalid(self): form_data = flatten_to_dict(forms.ContactForm()) form_data['first_name'] = 'Foo' form_data['last_name'] = 'Bar' form_data['email'] = '*****@*****.**' form_data['confirm_email'] = '*****@*****.**' bound_form = forms.ContactForm(data=form_data) self.assertFalse(bound_form.is_valid())
def test_initial_hidden_included(self): self.assertEqual( flatten_to_dict(NameForm()), { 'initial-first_name': 'Larry', 'first_name': 'Larry', 'last_name': '', }, )
def test_same_email_is_valid(self): form_data = flatten_to_dict(forms.PurchaseForm()) form_data['customer_email'] = '*****@*****.**' form_data['customer_name'] = 'foo' form_data['customer_phone'] = '5112435544' form_data['qunatity'] = 1 form_data['item_id'] = 101 bound_form = forms.PurchaseForm(data=form_data) self.assert_(bound_form.is_valid())
def test_mismatch_email_is_invalid(self): form_data = flatten_to_dict(forms.ContactForm()) form_data['first_name'] = 'Edwin' form_data['last_name'] = 'Chuky' form_data['email'] = '*****@*****.**' form_data['confirm_email'] = '*****@*****.**' bound_form = forms.ContactForm(data=form_data) self.assertFalse(bound_form.is_valid())
def test_redirects_after_save(self): from budget.forms import BudgetEstimateForm budget = mommy.make('Budget') category = mommy.make('Category') estimate = mommy.prepare('BudgetEstimate', category=category, budget=budget) form_data = flatten_to_dict(BudgetEstimateForm(instance=estimate)) _, response = self.post(estimate.budget, form_data) self.assertEqual(302, response.status_code) self.assertEqual(('Location', reverse('budget:budget_list')), response._headers['location'])
def test_flatten_formset(self): expected_result = { "initial-form-0-first_name": "Larry", "form-0-first_name": "Larry", "form-0-last_name": "", "form-INITIAL_FORMS": 0, "form-MAX_NUM_FORMS": 1000, "form-TOTAL_FORMS": 1, } if VERSION >= (1, 7): # 1.7 added min-version expected_result["form-MIN_NUM_FORMS"] = 0 self.assertEqual(flatten_to_dict(ContactFormSet()), expected_result)
def test_confirm_saved_object(self): from transaction.models import Transaction from transaction.forms import TransactionForm old = mommy.make('Transaction', notes='Foo') form_data = flatten_to_dict(TransactionForm(instance=old)) form_data['notes'] = 'Bar' self.post(old, form_data) new = Transaction.active.get(pk=1) self.assertEqual(1, Transaction.active.count()) self.assertEqual(old.transaction_type, new.transaction_type) self.assertEqual('Bar', new.notes) self.assertEqual(old.category, new.category) self.assertEqual(old.amount, new.amount) self.assertEqual(old.date, new.date)
def test_confirm_saved_object(self): from transaction.models import Transaction from transaction.forms import TransactionForm category = mommy.make('Category') old = mommy.prepare('Transaction', category=category) form_data = flatten_to_dict(TransactionForm(instance=old)) self.post(form_data) new = Transaction.objects.get(pk=1) self.assertEqual(1, Transaction.objects.count()) self.assertEqual(old.transaction_type, new.transaction_type) self.assertEqual(old.category, new.category) self.assertEqual(old.amount, new.amount) self.assertEqual(old.notes, new.notes) self.assertEqual(old.date, new.date)
def test_flatten_formset(self): expected_result = { 'initial-form-0-first_name': 'Larry', 'form-0-first_name': 'Larry', 'form-0-last_name': '', 'form-INITIAL_FORMS': 0, 'form-MAX_NUM_FORMS': 1000, 'form-TOTAL_FORMS': 1, } if VERSION >= (1, 7): # 1.7 added min-version expected_result['form-MIN_NUM_FORMS'] = 0 self.assertEqual( flatten_to_dict(ContactFormSet()), expected_result, )
def test_save_calls_save_m2m_after_parent_saved(self): form_data = flatten_to_dict(MultiEmailFormGroup()) form_data.update({ 'group-name-first_name': 'Joe', 'group-name-last_name': 'Smith', 'group-email-0-email': '*****@*****.**', 'group-email-1-email': '*****@*****.**', 'group-email-TOTAL_FORMS': '2', }) formgroup = MultiEmailFormGroup( data=form_data, instance=FakeModel(), ) self.assertTrue(formgroup.is_valid()) formgroup.save() self.assertTrue(formgroup.name.called['save_m2m'])
def test_save_calls_save_m2m_after_parent_saved(self): form_data = flatten_to_dict(MultiEmailFormGroup()) form_data.update( { 'group-name-first_name': 'Joe', 'group-name-last_name': 'Smith', 'group-email-0-email': '*****@*****.**', 'group-email-1-email': '*****@*****.**', 'group-email-TOTAL_FORMS': '2', } ) formgroup = MultiEmailFormGroup( data=form_data, instance=FakeModel(), ) self.assertTrue(formgroup.is_valid()) formgroup.save() self.assertTrue(formgroup.name.called['save_m2m'])
def test_ajax_sucess_revalidate_lock(self): revalidar_form = ReValidateForm( prefix=self.DEFAULT_LOCK_REVALIDATE_FORM_PREFIX, initial={ 'hashe': self.model_instance.pk, 'idd': self.model_instance.pk }) data = flatten_to_dict(revalidar_form) data.update({ str(self.DEFAULT_LOCK_REVALIDATE_FORM_ID): str(self.DEFAULT_LOCK_REVALIDATE_FORM_ID) }) response_post = self.client.post( path=self.view_url, data=data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual( json.loads(response_post.content.decode()), { "status": "success", "id": self.model_instance.pk, "mensagem": "revalidado com sucesso" }) from time import sleep sleep(self.DEFAULT_LOCK_EXPIRE_TIME_IN_SECONDS) response_post = self.client.post( path=self.view_url, data=data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual( json.loads(response_post.content.decode()), { "status": "success", "id": self.model_instance.pk, "mensagem": "revalidado com sucesso" })
def test_ajax_fail_deletar_lock(self): deletar_form = DeleteForm(prefix=self.DEFAULT_LOCK_DELETE_FORM_PREFIX, initial={ 'hashe': 'errado', 'idd': self.model_instance.pk }) data = flatten_to_dict(deletar_form) data.update({ str(self.DEFAULT_LOCK_DELETE_FORM_ID): str(self.DEFAULT_LOCK_DELETE_FORM_ID) }) response_post = self.client.post( path=self.view_url, data=data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual( json.loads(response_post.content.decode()), { "status": "fail", "id": self.model_instance.pk, "mensagem": "erro ao deletar" })
def test_inline_formsets_saved_after_parent_saved(self): form_data = flatten_to_dict(MultiEmailFormGroup()) form_data.update({ 'group-name-first_name': 'Joe', 'group-name-last_name': 'Smith', 'group-email-0-email': '*****@*****.**', 'group-email-1-email': '*****@*****.**', 'group-email-TOTAL_FORMS': '2', }) instance = FakeModel() self.assertEqual(instance.id, None) formgroup = MultiEmailFormGroup( data=form_data, instance=instance, ) self.assertTrue(formgroup.is_valid()) self.assertEqual(len(formgroup.email), 2) formgroup.save() self.assertEqual(instance.id, 42)
def test_flatten_respects_prefix(self): self.assertEqual(flatten_to_dict(EmailForm(prefix="foo")), {"foo-email": ""})
def test_flatten_respects_prefix(self): self.assertEqual( flatten_to_dict(EmailForm(prefix='foo')), {'foo-email': ''}, )
def test_returns_dict(self): self.assertIsInstance(flatten_to_dict(NameForm()), dict)
def test_form_to_dict(self): self.assertEqual( flatten_to_dict(EmailForm()), {'email': ''}, )
def test_form_to_dict(self): self.assertEqual(flatten_to_dict(EmailForm()), {"email": ""})
def test_initial_hidden_included(self): self.assertEqual( flatten_to_dict(NameForm()), {"initial-first_name": "Larry", "first_name": "Larry", "last_name": ""} )