Example #1
0
def add(request):
    DetailsFormSet = formset_factory(LocationDetailsForm, formset=BaseArticleFormSet)
    if request.method == 'POST':
        hierarchy_form = LocationHierarchyForm(request.POST)
        details_formset = DetailsFormSet(request.POST,prefix='form')

        if hierarchy_form.is_valid():
            selected_country = Location.objects.get(id=request.POST['country'])

            if details_formset.is_valid():
                for form in details_formset.forms:
                    location_type, status = LocationType.objects.get_or_create(name=form.cleaned_data.get('levels'), slug =slugify(form.cleaned_data.get('levels')))
                    details = form.save(commit=False)
                    details.location_type = location_type
                    details.country = selected_country
                    details.save()
                messages.success(request, "Location Hierarchy successfully created.")
                return HttpResponseRedirect('/')
        else:
            messages.error(request,"levels not saved")

    else:

        hierarchy_form = LocationHierarchyForm()
        details_formset = DetailsFormSet()

    context = {'hierarchy_form': hierarchy_form, 'button_label': 'Create Hierarchy', 'id': 'hierarchy-form',
               'details_formset':details_formset, 'cancel_url':'/'}
    return render(request,'location_hierarchy/new.html', context)
 def test_should_not_be_valid_if_country_is_blank(self):
     data = {
         'country':'',
         'levels': 'Region'
     }
     hierarchy_form = LocationHierarchyForm(data=data)
     self.assertFalse(hierarchy_form.is_valid())
Example #3
0
    def test_should_populate_countries_name(self):
        hierarchy_form = LocationHierarchyForm()

        field = 'country'
        all_countries = Location.objects.filter(type__name='country')
        country_choices = hierarchy_form.fields[field].choices

        [
            self.assertIn((country_option.id, country_option.name),
                          country_choices) for country_option in all_countries
        ]
Example #4
0
def add(request):
    DetailsFormSet = formset_factory(LocationDetailsForm,
                                     formset=BaseArticleFormSet)
    if request.method == 'POST':
        hierarchy_form = LocationHierarchyForm(request.POST)
        details_formset = DetailsFormSet(request.POST, prefix='form')

        if hierarchy_form.is_valid():
            selected_country = Location.objects.get(id=request.POST['country'])

            if details_formset.is_valid():
                for form in details_formset.forms:
                    location_type, status = LocationType.objects.get_or_create(
                        name=form.cleaned_data.get('levels'),
                        slug=slugify(form.cleaned_data.get('levels')))
                    details = form.save(commit=False)
                    details.location_type = location_type
                    details.country = selected_country
                    details.save()
                messages.success(request,
                                 "Location Hierarchy successfully created.")
                return HttpResponseRedirect('/')
        else:
            messages.error(request, "levels not saved")

    else:

        hierarchy_form = LocationHierarchyForm()
        details_formset = DetailsFormSet()

    context = {
        'hierarchy_form': hierarchy_form,
        'button_label': 'Create Hierarchy',
        'id': 'hierarchy-form',
        'details_formset': details_formset,
        'cancel_url': '/'
    }
    return render(request, 'location_hierarchy/new.html', context)
Example #5
0
    def test_should_populate_countries_name_case_insensitive(self):
        LocationType.objects.all().delete()
        Location.objects.all().delete()
        country_1 = LocationType.objects.create(name='Country',
                                                slug=slugify('Country'))
        some_country = Location.objects.create(type=country_1,
                                               name='some_country')

        field = 'country'
        hierarchy_form = LocationHierarchyForm()
        all_countries = Location.objects.filter(type__name='country')
        country_choices = hierarchy_form.fields[field].choices

        [
            self.assertIn((country_option.id, country_option.name),
                          country_choices) for country_option in all_countries
        ]
        self.assertIn((some_country.id, some_country.name), country_choices)
Example #6
0
    def test_should_show_used_country_as_available_choices_if_any_otherwise_show_all_countries(
            self):
        LocationTypeDetails.objects.all().delete()
        other_country = Location.objects.create(name="some other country",
                                                type=self.uganda.type)
        LocationTypeDetails.objects.create(required=True,
                                           has_code=False,
                                           location_type=self.uganda.type,
                                           country=self.uganda)
        hierarchy_form = LocationHierarchyForm()

        field = 'country'
        country_choices = hierarchy_form.fields[field].choices

        self.assertEqual(1, len(country_choices))
        self.assertNotIn((other_country.id, other_country.name),
                         country_choices)
        self.assertEqual((self.uganda.id, self.uganda.name),
                         country_choices[0])
Example #7
0
 def test_should_not_be_valid_if_country_is_blank(self):
     data = {'country': '', 'levels': 'Region'}
     hierarchy_form = LocationHierarchyForm(data=data)
     self.assertFalse(hierarchy_form.is_valid())
Example #8
0
    def test_knows_the_fields_in_form(self):
        hierarchy_form = LocationHierarchyForm()

        fields = ['country']
        [self.assertIn(field, hierarchy_form.fields) for field in fields]