def tellme_command(self, message=None): """Handles /tellme requests, asking the Guru a question. Args: message: xmpp.Message: The message that was sent by the user. """ im_from = datastore_types.IM('xmpp', bare_jid(message.sender)) asked_question = Question.get_asked(im_from) if asked_question: # Already have a question message.reply(WAIT_MSG) else: # Asking a question asked_question = Question(question=message.arg, asker=im_from) asked_question.put() currently_answering = Question.get_answering(im_from) if not currently_answering: # Try and find one for them to answer question = Question.assign_question(im_from) if question: message.reply(TELLME_MSG.format(question.question)) return message.reply(PONDER_MSG)
def __create_value_for_mongo_value(self, mongo_value): if isinstance(mongo_value, Binary): return datastore_types.Blob(str(mongo_value)) if isinstance(mongo_value, types.DictType): if mongo_value['class'] == 'rating': return datastore_types.Rating(int(mongo_value["rating"])) if mongo_value['class'] == 'category': return datastore_types.Category(mongo_value["category"]) if mongo_value['class'] == 'key': return self.__key_for_id(mongo_value['path']) if mongo_value['class'] == 'list': return [ self.__create_value_for_mongo_value(v) for v in mongo_value['list'] ] if mongo_value['class'] == 'user': return users.User(email=mongo_value["email"]) if mongo_value['class'] == 'text': return datastore_types.Text(mongo_value['string']) if mongo_value['class'] == 'im': return datastore_types.IM(mongo_value['protocol'], mongo_value['address']) if mongo_value['class'] == 'geopt': return datastore_types.GeoPt(mongo_value['lat'], mongo_value['lon']) if mongo_value['class'] == 'email': return datastore_types.Email(mongo_value['value']) if mongo_value['class'] == 'bytes': return datastore_types.ByteString(mongo_value['value']) if mongo_value['class'] == 'blobkey': return datastore_types.BlobKey(mongo_value['value']) return mongo_value
def testDatastoreTypes(self): """Puts and gets different basic datastore types.""" entity = datastore.Entity('TestKind') entity.update({ 'rating': datastore_types.Rating(1), 'category': datastore_types.Category('bugs'), 'key': datastore_types.Key.from_path('foo', 'bar'), 'user': users.User('*****@*****.**'), 'text': datastore_types.Text('some text'), 'blob': datastore_types.Blob('data'), 'bytestring': datastore_types.ByteString('data'), 'im': datastore_types.IM('http://example.com/', 'Larry97'), 'geopt': datastore_types.GeoPt(1.1234, -1.1234), 'email': datastore_types.Email('*****@*****.**'), 'blobkey': datastore_types.BlobKey('27f5a7'), }) datastore.Put(entity) e = datastore.Get(entity) datastore.Delete(entity)
def _from_base_type(self, value): """Converts datastore type (string) to native type (datastore_types.IM). Args: value: The value to be converted. Should be a string. Returns: String corresponding to the IM value. """ return datastore_types.IM(value)
def askme_command(self, message=None): """Responds to the /askme command. Args: message: xmpp.Message: The message that was sent by the user. """ im_from = datastore_types.IM('xmpp', bare_jid(message.sender)) currently_answering = Question.get_answering(im_from) question = Question.assign_question(im_from) if question: message.reply(TELLME_MSG.format(question.question)) else: message.reply(EMPTYQ_MSG) # Don't unassign their current question until we've picked a new one. if currently_answering: currently_answering.unassign(im_from)
def post(self, status): """POST handler for XMPP presence. Args: status: A string which will be either available or unavailable and will indicate the status of the user. """ sender = self.request.get('from') im_from = datastore_types.IM('xmpp', bare_jid(sender)) suspend = (status == 'unavailable') query = Question.query(Question.asker == im_from, Question.answer == None, Question.suspended == (not suspend)) question = query.get() if question: question.suspended = suspend question.put()
def text_message(self, message=None): """Called when a message not prefixed by a /cmd is sent to the XMPP bot. Args: message: xmpp.Message: The message that was sent by the user. """ im_from = datastore_types.IM('xmpp', bare_jid(message.sender)) question = Question.get_answering(im_from) if question: other_assignees = question.assignees other_assignees.remove(im_from) # Answering a question question.answer = message.arg question.answerer = im_from question.assignees = [] question.answered = datetime.datetime.now() question.put() # Send the answer to the asker xmpp.send_message([question.asker.address], ANSWER_INTRO_MSG.format(question.question)) xmpp.send_message([question.asker.address], ANSWER_MSG.format(message.arg)) # Send acknowledgement to the answerer asked_question = Question.get_asked(im_from) if asked_question: message.reply(TELLME_THANKS_MSG) else: message.reply(THANKS_MSG) # Tell any other assignees their help is no longer required if other_assignees: xmpp.send_message([user.address for user in other_assignees], SOMEONE_ANSWERED_MSG) else: self.unhandled_command(message)
def parse(self, value): return datastore_types.IM(value)
def test_entity_from_json_data(self): """Create a datastore.Entity instance from JSON data.""" from datetime import datetime from gaesynkit import handlers from google.appengine.api import datastore_types data = { 'kind': u'A', 'key': u'dGVzdEBkZWZhdWx0ISFBCGE=', 'id': 2, 'properties': { 'string': { 'type': 'string', 'value': 'A string.' }, 'boolean': { 'type': 'bool', 'value': True }, 'int': { 'type': 'int', 'value': 42 }, 'float': { 'type': 'float', 'value': 1.8200000000000001 }, 'key': { 'type': 'key', 'value': 'agR0ZXN0cg4LEgRLaW5kIgRuYW1lDA' }, 'byte_string': { 'type': 'byte_string', 'value': 'Byte String' }, 'date': { 'type': 'gd:when', 'value': '2011/01/06 00:00:00' }, 'list': { 'type': 'int', 'value': [1, 2, 3, 4] }, 'email': { 'type': 'gd:email', 'value': u'*****@*****.**' }, 'location': { 'type': 'georss:point', 'value': u'52.5,13.3' }, 'category': { 'type': 'atom:category', 'value': u'coding' }, 'url': { 'type': 'atom:link', 'value': u'http://www.google.com' }, 'im': { 'type': 'gd:im', 'value': u'sip foobar' }, 'phone': { 'type': 'gd:phonenumber', 'value': u'1 (206) 555-1212' }, 'address': { 'type': 'gd:postaladdress', 'value': u'Address' }, 'rating': { 'type': 'gd:rating', 'value': 99L } } } entity = handlers.entity_from_json_data(data) self.assertEqual(entity['string'], u'A string.') self.assertEqual(entity['boolean'], True) self.assertEqual(entity['int'], 42) self.assertEqual(entity['float'], 1.8200000000000001) self.assertEqual( entity['key'], datastore_types.Key.from_path(u'Kind', u'name', _app=u'test')) self.assertEqual(entity['byte_string'], 'Byte String') self.assertEqual(entity['date'], datetime(2011, 1, 6, 0, 0)) self.assertEqual(entity['list'], [1, 2, 3, 4]) self.assertEqual(entity['email'], u'*****@*****.**') self.assertEqual(entity['location'], datastore_types.GeoPt(52.5, 13.300000000000001)) self.assertEqual(entity['category'], u'coding') self.assertEqual(entity['url'], u'http://www.google.com') self.assertEqual(entity['im'], datastore_types.IM(u'sip', u'foobar')) self.assertEqual(entity['phone'], u'1 (206) 555-1212') self.assertEqual(entity['address'], u'Address') self.assertEqual(entity['rating'], 99)
}, 'key': { 'type': 'key', 'value': 'agR0ZXN0cg4LEgRLaW5kIgRuYW1lDA' }, 'address': { 'type': 'gd:postaladdress', 'value': u'Address' }, 'date': { 'type': 'gd:when', 'value': '2011/01/06 00:00:00' }, 'im': { 'type': 'gd:im', 'value': datastore_types.IM('sip', 'foobar') }, 'email': { 'type': 'gd:email', 'value': u'*****@*****.**' }, 'user': { 'type': 'user', 'value': 'tester' }, 'location': { 'type': 'georss:point', 'value': datastore_types.GeoPt(52.500556000000003, 13.398889) }