Пример #1
0
def farmer_new(request):
    actor = request.user.get_profile()
    agent = actor.as_agent()
    if request.POST:
        form = forms.FarmerForm(request.POST)
        if form.is_valid():
            # msisdn = form.cleaned_data['msisdn']

            msisdns = [
                form.cleaned_data['msisdn1'],
                form.cleaned_data['msisdn2'],
                form.cleaned_data['msisdn3'],
            ]
            [msisdn1, msisdn2, msisdn3] = msisdns

            name = form.cleaned_data['name']
            surname = form.cleaned_data['surname']
            id_number = form.cleaned_data['id_number']
            markets = form.cleaned_data['markets']
            matched_farmer = form.cleaned_data['matched_farmer']
            gender = form.cleaned_data["gender"]

            if not id_number:
                id_number = None

            if matched_farmer:
                messages.info(request, 'Farmer added.')
                if agent:
                    for market in markets:
                        matched_farmer.operates_at(market, agent)
                farmer_url = reverse('fncs:farmer_edit', kwargs={
                    'farmer_pk': matched_farmer.pk,
                })
                return redirect(farmer_url)

            possible_matches = Farmer.match(msisdns=msisdns,
                                            id_number=id_number)
            if possible_matches:
                form.fields['matched_farmer'].queryset = possible_matches
                messages.info(request, "This farmer possibly already exists. "
                              "Please review the list of matched farmers to "
                              "avoid double registrations")
            else:
                farmer = Farmer.create(msisdn1, name, surname,
                                       id_number=id_number, gender=gender)
                farmer.actor.update_msisdns(msisdns)
                if agent:
                    for market in markets:
                        farmer.operates_at(market, agent)

                messages.success(request, "Farmer Created")
                return HttpResponseRedirect(
                    reverse("fncs:farmer_location_search", kwargs={
                            'farmer_pk': farmer.pk}))
    else:
        form = forms.FarmerForm()
        form.fields['matched_farmer'].widget = HiddenInput()
    return render_to_response('farmers/new.html', {
        'form': form
    }, context_instance=RequestContext(request))
Пример #2
0
 def test_farmer_create_helper(self):
     rpiarea = utils.create_rpiarea("rpiarea")
     zone = utils.create_zone("zone", rpiarea)
     province = utils.create_province("province")
     district = utils.create_district("district", province)
     ward = utils.create_ward("ward", district)
     self.assertFalse(Farmer.objects.exists())
     farmer1 = Farmer.create("0761234567", "name", "surname")
     self.assertTrue(Farmer.objects.count(), 1)
     self.assertEqual(farmer1.actor.name, 'name surname')
     farmer2 = Farmer.create("0761234567", "new name", "new surname")
     self.assertTrue(Farmer.objects.count(), 1)
     self.assertEqual(farmer2.actor.name, 'new name new surname')
Пример #3
0
    def test_get_specific_farmer(self):
        """
        Get Specific Farmer
        """
        self.assertFalse(Farmer.objects.exists())
        Farmer.create("27721111111", "test_first_name", "test_surname")
        Farmer.create("27721111112", "test_first_name2", "test_surname2")
        Farmer.create("27721111113", "test_first_name3", "test_surname3")
        Farmer.create("27721111114", "test_first_name4", "test_surname4")

        url = reverse('fncs:api_dispatch_list',
                      kwargs={'resource_name': 'farmer',
                              'api_name': 'v1'})
        response = self.api_client.get("%s?actor__user__username=27721111111" % url)
        json_item = json.loads(response.content)
        self.assertEqual("application/json", response["Content-Type"])
        self.assertEqual(response.status_code, 200)
        self.assertIn("meta", json_item)
        self.assertIn("objects", json_item)

        self.assertEqual(len(json_item["objects"]), 1)
        self.assertEqual("27721111111",
                         json_item["objects"][0]["actor"]["user"]["username"])
        self.assertEqual("test_first_name",
                         json_item["objects"][0]["actor"]["user"]["first_name"])
        self.assertEqual("test_surname",
                         json_item["objects"][0]["actor"]["user"]["last_name"])

        response = self.api_client.get("%s?actor__user__username=27721111110" % url)
        json_item = json.loads(response.content)
        self.assertEqual(len(json_item["objects"]), 0)
Пример #4
0
 def match(*args, **kw):
     return sorted(Farmer.match(*args, **kw),
                   key=lambda x: x.actor.user.first_name)