Exemplo n.º 1
0
    def test_post_list_with_permissions_and_polled_today(self):
        """Post a single report to the API with authenticated and with add permissions"""
        add_powerreport = Permission.objects.get(codename="add_powerreport")
        self.user.user_permissions.add(add_powerreport)

        # Set enquiry to today - so that the contribution is accepted
        self.contributor_1 = Contributor(name="Marc", email="*****@*****.**")
        self.contributor_1.set_password("marc")
        self.contributor_1.enquiry = datetime.today().date()
        self.contributor_1.save()

        # Check how many there are first.
        nb = PowerReport.objects.count()
        rt = PowerReport(has_experienced_outage=True, duration=153, contributor=self.contributor_1, area=Area.objects.get(pk=1), happened_at=datetime.today().date())
        rt.save()
        # Verify that a new report has been added.
        self.assertEqual(PowerReport.objects.count(), nb + 1)
Exemplo n.º 2
0
def contribute_multiple(message_array, device, auto_mode):
    """
        Message: pc <area> <duration>, <area> <duration>
    """
    today = datetime.today().date()

    # If this user hasn't been asked today OR If has already answered today, then save the message and ignore contribution
    if (device.contributor.enquiry != today) or (device.contributor.response == today):
        if auto_mode:
            save_message(message_array, device)
        return Message.NO
    # else try to parse the contribution and save the report
    else:
        (parsed_data, parsed) = parse_contribute_multiple(message_array, device, auto_mode)
        #If we haven't been able to parse the message
        if not parsed_data:
            msg = _("Hello, your message couldn't be translated - please send us another SMS, e.g. ""PC douala1 40"". reply HELP for further information")
        #If user sent PC No - then no outage has been experienced
        elif parsed_data[0][0] == 0:
            report = PowerReport(
                has_experienced_outage=False,
                duration=parsed_data[0][0],
                contributor=device.contributor,
                device=device,
                area=parsed_data[0][1],
                happened_at=today
            )
            report.save()

            increment_refund(device.contributor)
            msg = _("You chose to report no power cut. If this is not what you wanted to say, please send us a new SMS")
        else:
            msg = _("You had {0} powercuts yesterday. Durations : ").format(len(parsed_data))
            for item in parsed_data:
                report = PowerReport(
                    duration=item[0],
                    contributor=device.contributor,
                    device=device,
                    area=item[1],
                    happened_at=today
                )
                report.save()
                increment_refund(device.contributor)
                msg += _(str(item[0]) + "min, ")
            msg += _("If the data have been misunderstood, please send us another SMS.")
            

        send_message(device.phone_number, msg)
    return parsed