コード例 #1
0
    def execute(self):
        cmd = self.command
        user = self.command.sms.user

        if RECORD_SELL not in user.permissions:
            logging.info('{} - User {} does not have permission {}'.format(
                cmd.sms.id, user.id, RECORD_SELL))
            return _('Command not allowed')

        harvest_total = Farm.query(ndb.AND(Farm.district_id == user.district_id,
                                           Farm.crop_name == cmd.plant,
                                           Farm.action == 'harvest')).get()

        if not harvest_total or harvest_total.quantity < cmd.amount:
            logging.info('{} - Not enough {} harvested'.format(cmd.sms.id,
                                                               cmd.plant))
            return _('Not enough {} harvested').format(cmd.plant)

        sell_total = Farm.query(ndb.AND(Farm.district_id == user.district_id,
                                        Farm.crop_name == cmd.plant,
                                        Farm.action == 'sell')).get()
        if not sell_total:
            sell_total = Farm(id=Farm.id(),
                              district_id=user.district_id,
                              action=self.CMD,
                              crop_name=cmd.plant,
                              quantity=0)

        harvest_total.quantity -= cmd.amount
        harvest_total.put()
        sell_total.quantity += cmd.amount
        sell_total.put()

        return _('Sell command succeeded')
コード例 #2
0
    def execute(self):
        """
        Update plant and harvest totals
        """
        cmd = self.command
        user = cmd.sms.user

        if RECORD_HARVEST not in user.permissions:
            logging.info("{} - User {} does not have permission {}".format(cmd.sms, user.id, RECORD_HARVEST))
            return _("Command not allowed")

        plant_total = Farm.query(
            ndb.AND(Farm.district_id == user.district_id, Farm.crop_name == cmd.plant, Farm.action == "plant")
        ).get()

        if not plant_total or plant_total.quantity < cmd.amount:
            logging.info("{} - Not enough {} planted".format(cmd.sms.id, cmd.plant))
            return _("Not enough {} planted").format(cmd.plant)

        harvest_total = Farm.query(
            ndb.AND(Farm.district_id == user.district_id, Farm.crop_name == cmd.plant, Farm.action == "harvest")
        ).get()
        if not harvest_total:
            harvest_total = Farm(
                id=Farm.id(), district_id=user.district_id, action=self.CMD, crop_name=cmd.plant, quantity=0
            )

        plant_total.quantity -= cmd.amount
        plant_total.put()
        harvest_total.quantity += cmd.amount
        harvest_total.put()

        return _("Harvest command succeeded")
コード例 #3
0
    def execute(self):
        cmd = self.command
        user = self.command.sms.user

        if RECORD_SELL not in user.permissions:
            logging.info('{} - User {} does not have permission {}'.format(
                cmd.sms.id, user.id, RECORD_SELL))
            return _('Command not allowed')

        harvest_total = Farm.query(ndb.AND(Farm.district_id == user.district_id,
                                           Farm.crop_name == cmd.plant,
                                           Farm.action == 'harvest')).get()

        if not harvest_total or harvest_total.quantity < cmd.amount:
            logging.info('{} - Not enough {} harvested'.format(cmd.sms.id,
                                                               cmd.plant))
            return _('Not enough {} harvested').format(cmd.plant)

        sell_total = Farm.query(ndb.AND(Farm.district_id == user.district_id,
                                        Farm.crop_name == cmd.plant,
                                        Farm.action == 'sell')).get()
        if not sell_total:
            sell_total = Farm(id=Farm.id(),
                              district_id=user.district_id,
                              action=self.CMD,
                              crop_name=cmd.plant,
                              quantity=0)

        harvest_total.quantity -= cmd.amount
        harvest_total.put()
        sell_total.quantity += cmd.amount
        sell_total.put()

        return _('Sell command succeeded')
コード例 #4
0
    def test_should_subtract_from_planted_quantity(self):
        HarvestAction(self._harvest('potato', 2)).execute()

        plant_data = Farm.query(Farm.action == 'plant',
                                Farm.crop_name == 'potato').get()

        self.assertEqual(8, plant_data.quantity)
コード例 #5
0
    def test_should_subtract_from_harvested_quantity(self):
        SellAction(self._sell('potato', 2)).execute()

        harvest_data = Farm.query(Farm.action == 'harvest',
                                  Farm.crop_name == 'potato').get()

        self.assertEqual(8, harvest_data.quantity)
コード例 #6
0
    def execute(self):
        """
        Update plant totals
        """
        cmd = self.command
        user = cmd.sms.user

        if RECORD_PLANT not in user.permissions:
            logging.info('{} - User {} does not have permission {}'.format(
                cmd.sms.id, user.id, RECORD_PLANT))
            return _('Command not allowed')

        plant = Farm.query(
            ndb.AND(Farm.district_id == user.district_id,
                    Farm.crop_name == cmd.plant,
                    Farm.action == 'plant')).get()
        if not plant:
            plant = Farm(id=Farm.id(),
                         district_id=user.district_id,
                         action=self.CMD,
                         crop_name=cmd.plant,
                         quantity=0)

        plant.quantity += cmd.amount
        plant.put()

        return _('Plant command succeeded')
コード例 #7
0
    def execute(self):
        filter = self.command.filter
        user = self.command.sms.user

        if RETRIEVE_ALL_DISTRICT not in user.permissions:
            logging.info('{} - User {} does not have permission {}'.format(
                self.command.sms.key.id(), user.key.id(),
                RETRIEVE_ALL_DISTRICT))
            return _('Command not allowed')

        district_name = self.command.district
        slug = district_name.lower()
        district = District.query(District.slug == slug).get()

        if not district:
            logging.info('{} - District {} is unknown'.format(
                self.command.sms.key.id(), district_name))
            return _('District {} is unknown').format(district_name)

        query = Farm.query(
            ndb.AND(Farm.action == filter,
                    Farm.district_id == district.key.id()))
        crops = query.order(-Farm.ts_updated).fetch()

        if not crops:
            return _('{} data is none').format(_(filter))

        response = _('Total {} in {}:').format(_(filter), district.name)
        for crop in crops:
            response += '\n{} {}'.format(
                _(crop.crop_name).title(), crop.quantity)
        return response
コード例 #8
0
    def execute(self):
        """
        Update plant totals
        """
        cmd = self.command
        user = cmd.sms.user

        if RECORD_PLANT not in user.permissions:
            logging.info('{} - User {} does not have permission {}'.format(
                cmd.sms.id, user.id, RECORD_PLANT))
            return _('Command not allowed')

        plant = Farm.query(ndb.AND(Farm.district_id == user.district_id,
                                   Farm.crop_name == cmd.plant,
                                   Farm.action == 'plant')).get()
        if not plant:
            plant = Farm(id=Farm.id(),
                         district_id=user.district_id,
                         action=self.CMD,
                         crop_name=cmd.plant,
                         quantity=0)

        plant.quantity += cmd.amount
        plant.put()

        return _('Plant command succeeded')
コード例 #9
0
    def execute(self):
        filter = self.command.filter
        user = self.command.sms.user

        if RETRIEVE_ALL_DISTRICT not in user.permissions:
            logging.info('{} - User {} does not have permission {}'.format(
                self.command.sms.key.id(), user.key.id(), RETRIEVE_ALL_DISTRICT))
            return _('Command not allowed')

        district_name = self.command.district
        slug = district_name.lower()
        district = District.query(District.slug == slug).get()

        if not district:
            logging.info('{} - District {} is unknown'.format(
                self.command.sms.key.id(), district_name))
            return _('District {} is unknown').format(district_name)

        query = Farm.query(ndb.AND(Farm.action == filter,
                                   Farm.district_id == district.key.id()))
        crops = query.order(-Farm.ts_updated).fetch()

        if not crops:
            return _('{} data is none').format(_(filter))

        response = _('Total {} in {}:').format(_(filter), district.name)
        for crop in crops:
            response += '\n{} {}'.format(_(crop.crop_name).title(),
                                         crop.quantity)
        return response
コード例 #10
0
    def test_should_store_total_harvest_for_one_plant(self):
        HarvestAction(self._harvest('potato', 1)).execute()
        HarvestAction(self._harvest('potato', 2)).execute()

        all_data = Farm.query(Farm.action == 'harvest').fetch()
        self.assertEqual(1, len(all_data))

        data = all_data[0]
        self.assertEqual('harvest', data.action)
        self.assertEqual('potato', data.crop_name)
        self.assertEqual(3, data.quantity)
コード例 #11
0
    def test_should_store_harvest_for_one_plant(self):
        HarvestAction(self._harvest('potato', 5)).execute()

        all_data = Farm.query(Farm.action == 'harvest').fetch()
        self.assertEqual(1, len(all_data))

        data = all_data[0]
        self.assertEqual('harvest', data.action)
        self.assertEqual('potato', data.crop_name)
        self.assertEqual(5, data.quantity)
        self.assertEqual('sul123', data.district_id)
コード例 #12
0
    def test_should_store_total_sale_for_one_plant(self):
        SellAction(self._sell('potato', 1)).execute()
        SellAction(self._sell('potato', 2)).execute()

        all_data = Farm.query(Farm.action == 'sell').fetch()
        self.assertEqual(1, len(all_data))

        data = all_data[0]
        self.assertEqual('sell', data.action)
        self.assertEqual('potato', data.crop_name)
        self.assertEqual(3, data.quantity)
コード例 #13
0
    def test_should_store_total_harvest_for_one_plant(self):
        HarvestAction(self._harvest("potato", 1)).execute()
        HarvestAction(self._harvest("potato", 2)).execute()

        all_data = Farm.query(Farm.action == "harvest").fetch()
        self.assertEqual(1, len(all_data))

        data = all_data[0]
        self.assertEqual("harvest", data.action)
        self.assertEqual("potato", data.crop_name)
        self.assertEqual(3, data.quantity)
コード例 #14
0
    def test_should_store_harvest_for_one_plant(self):
        HarvestAction(self._harvest("potato", 5)).execute()

        all_data = Farm.query(Farm.action == "harvest").fetch()
        self.assertEqual(1, len(all_data))

        data = all_data[0]
        self.assertEqual("harvest", data.action)
        self.assertEqual("potato", data.crop_name)
        self.assertEqual(5, data.quantity)
        self.assertEqual("sul123", data.district_id)
コード例 #15
0
    def test_should_store_one_item(self):
        cmd = self._plant('potato', 20)
        PlantAction(cmd).execute()

        all_data = Farm.query().fetch()
        self.assertEqual(1, len(all_data))

        data = all_data[0]
        self.assertEqual('plant', data.action)
        self.assertEqual('potato', data.crop_name)
        self.assertEqual(20, data.quantity)
        self.assertEqual('sul123', data.district_id)
コード例 #16
0
    def test_should_store_aggregate_amount_for_the_same_item(self):
        PlantAction(self._plant('potato', 50)).execute()
        PlantAction(self._plant('potato', 20)).execute()

        all_data = Farm.query().fetch()
        self.assertEqual(1, len(all_data))

        data = all_data[0]
        self.assertEqual('plant', data.action)
        self.assertEqual('potato', data.crop_name)
        self.assertEqual(70, data.quantity)
        self.assertEqual('sul123', data.district_id)
コード例 #17
0
    def test_should_store_one_item(self):
        cmd = self._plant('potato', 20)
        PlantAction(cmd).execute()

        all_data = Farm.query().fetch()
        self.assertEqual(1, len(all_data))

        data = all_data[0]
        self.assertEqual('plant', data.action)
        self.assertEqual('potato', data.crop_name)
        self.assertEqual(20, data.quantity)
        self.assertEqual('sul123', data.district_id)
コード例 #18
0
    def test_should_store_aggregate_amount_for_the_same_item(self):
        PlantAction(self._plant('potato', 50)).execute()
        PlantAction(self._plant('potato', 20)).execute()

        all_data = Farm.query().fetch()
        self.assertEqual(1, len(all_data))

        data = all_data[0]
        self.assertEqual('plant', data.action)
        self.assertEqual('potato', data.crop_name)
        self.assertEqual(70, data.quantity)
        self.assertEqual('sul123', data.district_id)
コード例 #19
0
    def execute(self):
        """
        Update plant and harvest totals
        """
        cmd = self.command
        user = cmd.sms.user

        if RECORD_HARVEST not in user.permissions:
            logging.info('{} - User {} does not have permission {}'.format(
                cmd.sms, user.id, RECORD_HARVEST))
            return _('Command not allowed')

        plant_total = Farm.query(ndb.AND(Farm.district_id == user.district_id,
                                         Farm.crop_name == cmd.plant,
                                         Farm.action == 'plant')).get()

        if not plant_total or plant_total.quantity < cmd.amount:
            logging.info('{} - Not enough {} planted'.format(cmd.sms.id,
                                                             cmd.plant))
            return _('Not enough {} planted').format(cmd.plant)

        harvest_total = Farm.query(ndb.AND(Farm.district_id == user.district_id,
                                           Farm.crop_name == cmd.plant,
                                           Farm.action == 'harvest')).get()
        if not harvest_total:
            harvest_total = Farm(id=Farm.id(),
                                 district_id=user.district_id,
                                 action=self.CMD,
                                 crop_name=cmd.plant,
                                 quantity=0)

        plant_total.quantity -= cmd.amount
        plant_total.put()
        harvest_total.quantity += cmd.amount
        harvest_total.put()

        return _('Harvest command succeeded')
コード例 #20
0
    def test_should_store_harvest_for_different_plants_independently(self):
        HarvestAction(self._harvest("potato", 2)).execute()
        HarvestAction(self._harvest("carrot", 3)).execute()

        all_data = Farm.query(Farm.action == "harvest").fetch()
        self.assertEqual(2, len(all_data))

        data = all_data[0]
        self.assertEqual("harvest", data.action)
        self.assertEqual("potato", data.crop_name)
        self.assertEqual(2, data.quantity)
        self.assertEqual("sul123", data.district_id)

        data = all_data[1]
        self.assertEqual("harvest", data.action)
        self.assertEqual("carrot", data.crop_name)
        self.assertEqual(3, data.quantity)
        self.assertEqual("sul123", data.district_id)
コード例 #21
0
    def test_should_store_multiple_different_items(self):
        PlantAction(self._plant('carrot', 50)).execute()
        PlantAction(self._plant('potato', 20)).execute()

        all_data = Farm.query().fetch()
        self.assertEqual(2, len(all_data))

        data = all_data[0]
        self.assertEqual('plant', data.action)
        self.assertEqual('carrot', data.crop_name)
        self.assertEqual(50, data.quantity)
        self.assertEqual('sul123', data.district_id)

        data = all_data[1]
        self.assertEqual('plant', data.action)
        self.assertEqual('potato', data.crop_name)
        self.assertEqual(20, data.quantity)
        self.assertEqual('sul123', data.district_id)
コード例 #22
0
    def test_should_store_multiple_different_items(self):
        PlantAction(self._plant('carrot', 50)).execute()
        PlantAction(self._plant('potato', 20)).execute()

        all_data = Farm.query().fetch()
        self.assertEqual(2, len(all_data))

        data = all_data[0]
        self.assertEqual('plant', data.action)
        self.assertEqual('carrot', data.crop_name)
        self.assertEqual(50, data.quantity)
        self.assertEqual('sul123', data.district_id)

        data = all_data[1]
        self.assertEqual('plant', data.action)
        self.assertEqual('potato', data.crop_name)
        self.assertEqual(20, data.quantity)
        self.assertEqual('sul123', data.district_id)
コード例 #23
0
    def test_should_store_sale_for_different_plants_independently(self):
        SellAction(self._sell('potato', 2)).execute()
        SellAction(self._sell('carrot', 3)).execute()

        all_data = Farm.query(Farm.action == 'sell').fetch()
        self.assertEqual(2, len(all_data))

        data = all_data[0]
        self.assertEqual('sell', data.action)
        self.assertEqual('potato', data.crop_name)
        self.assertEqual(2, data.quantity)
        self.assertEqual('sul123', data.district_id)

        data = all_data[1]
        self.assertEqual('sell', data.action)
        self.assertEqual('carrot', data.crop_name)
        self.assertEqual(3, data.quantity)
        self.assertEqual('sul123', data.district_id)
コード例 #24
0
    def test_should_subtract_from_planted_quantity(self):
        HarvestAction(self._harvest("potato", 2)).execute()

        plant_data = Farm.query(Farm.action == "plant", Farm.crop_name == "potato").get()

        self.assertEqual(8, plant_data.quantity)