Exemplo n.º 1
0
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()

        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()

        District(id='d1', name='Sumatra', slug='sumatra').put()
        Farm(action='plant', district_id='d1', crop_name='potato', quantity=10).put()
        Farm(action='sell', district_id='d1', crop_name='potato', quantity=1).put()
        Farm(action='sell', district_id='d1', crop_name='carrot', quantity=2).put()

        self.user = User(id='u1', role=User.ROLE_FARMER, district_id='d1')
    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')
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()

        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()

        self.sms = SmsRequest()
        self.sms.user = User(role='farmer',
                             phone_number='6072809193',
                             first_name='Kat',
                             district_id='sul123')
        self.sms.body = ''

        Farm(action='harvest', district_id='sul123', crop_name='potato', quantity=10).put()
        Farm(action='harvest', district_id='sul123', crop_name='carrot', quantity=5).put()
Exemplo n.º 4
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')
    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')