def fetch(): name = request.args.get('name') query = District.query() if name: query = District.query(District.slug == name.lower()) districts = query.order(-District.ts_created).fetch() return { 'districts': [d.to_dict() for d in districts] }
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
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
def execute(self): user = self.command.sms.user words = self.command.msg.split() message = self.command.msg farmers = None district = None for i in range(self.MULTI_DISTRICT_LIMIT): if district: message = ' '.join(words[i - 1:]) break district_name = ' '.join(words[:i]) slug = district_name.lower() district = District.query(District.slug == slug).get() if BROADCAST_ALL in user.permissions: # district must be specified if self.command.district != EVERYONE and not district: logging.info('{} - District {} is unknown'.format( self.command.sms.id, words[0])) return _('District {} is unknown').format(words[0]) if self.command.district == EVERYONE: farmers = User.query(User.role == User.ROLE_FARMER).fetch() if district: farmers = User.query( ndb.AND(User.role == User.ROLE_FARMER, User.district_id == district.key.id())).fetch() elif BROADCAST_OWN_DISTRICT in user.permissions: if self.command.district == EVERYONE: words.insert(0, EVERYONE) # own district is not specified but valid if not district or \ (district and district.key.id() != user.district_id): message = ' '.join(words) farmers = User.query( ndb.AND(User.role == User.ROLE_FARMER, User.district_id == user.district_id)).fetch() else: return _('Command not allowed') phone_numbers = [farmer.phone_number for farmer in farmers] if phone_numbers: taskqueue.add(queue_name=self.QUEUE_NAME, url=self.QUEUE_URL, payload=json.dumps({ 'task': { 'phone_numbers': phone_numbers, 'message': message } })) return _('Message delivered') return _('Message delivery failed')
def insert(): name = request.form.get('name') slug = name.lower() existing = District.query(District.slug == slug).fetch() if existing: abort(400, 'district {} is already registered'.format(name)) new = District(id=District.id(), name=name, slug=slug) new.put() return new.to_dict()