def get(klass, key): if type(key) == type([]): newKey = [IP4DB.hrdKey(k) for k in key] else: newKey = IP4DB.hrdKey(key) return db.Model.get(newKey)
def getAuthorizedModelOfClasses(self, ability, modelClasses, user=None, useKey=None, requestKey='key', returnNone=False, returnMigrated=False): """ Usually raises ValueError if no model found. If returnNone is True, returns None instead. If returnMigrated is set True, we return a 2-tuple (model, isMigrated), where isMigrated indicates whether we forwarded an old character to a new. """ import models if not modelClasses: raise RuntimeError, 'no modelClasses specified' # They may have manually provided the key. key = useKey if key is None: # Usually, the key is a GET parameter. But sometimes it's in the URL in # the format /characters/KEY/main key = self.request.get(requestKey, None) if key is None: urlPieces = self.request.url.split('/') if urlPieces[-1] == 'main': key = urlPieces[-2] if key is None: if returnNone: return returnMigrated and (None, False) or None raise ValueError try: model = IP4DB.get(key) assert model is not None except (datastore_errors.BadKeyError, AssertionError): if returnNone: return None raise ValueError # Auto-forward old characters to new. isMigrated = False if model.__class__ == models.Character and getattr( models, models.VERSIONED_CHARACTER_CLASS) in modelClasses: try: newCharacter = model.newCharacter except db.Error: newCharacter = None model, isMigrated = (newCharacter or model.migrate()), True if not (model.__class__ in modelClasses \ and model.authorizeUserAbility(user or users.get_current_user(), ability)): if returnNone: return returnMigrated and (None, False) or None raise ValueError return returnMigrated and (model, isMigrated) or model
def getAuthorizedModelOfClasses( self, ability, modelClasses, user=None, useKey=None, requestKey="key", returnNone=False, returnMigrated=False ): """ Usually raises ValueError if no model found. If returnNone is True, returns None instead. If returnMigrated is set True, we return a 2-tuple (model, isMigrated), where isMigrated indicates whether we forwarded an old character to a new. """ import models if not modelClasses: raise RuntimeError, "no modelClasses specified" # They may have manually provided the key. key = useKey if key is None: # Usually, the key is a GET parameter. But sometimes it's in the URL in # the format /characters/KEY/main key = self.request.get(requestKey, None) if key is None: urlPieces = self.request.url.split("/") if urlPieces[-1] == "main": key = urlPieces[-2] if key is None: if returnNone: return returnMigrated and (None, False) or None raise ValueError try: model = IP4DB.get(key) assert model is not None except (datastore_errors.BadKeyError, AssertionError): if returnNone: return None raise ValueError # Auto-forward old characters to new. isMigrated = False if model.__class__ == models.Character and getattr(models, models.VERSIONED_CHARACTER_CLASS) in modelClasses: try: newCharacter = model.newCharacter except db.Error: newCharacter = None model, isMigrated = (newCharacter or model.migrate()), True if not ( model.__class__ in modelClasses and model.authorizeUserAbility(user or users.get_current_user(), ability) ): if returnNone: return returnMigrated and (None, False) or None raise ValueError return returnMigrated and (model, isMigrated) or model
def emailsToGoodUsers(thisClass, emailAddresses, returnBad=False): """ Provided a list of email addresses, return a list of users.User objects for valid addresses. This is a workaround for the fact that users.User('*****@*****.**').user_id() returns None. If returnBad is True, we return a 2-tuple: (userObjects, badEmailAddresses) """ newKeys = [ thisClass(user=users.User(ea)).put() for ea in emailAddresses ] emailToUsers = IP4DB.get(newKeys) db.delete(emailToUsers) retUsers = [ eto.user for eto in emailToUsers if eto.user.user_id() ] if not returnBad: return retUsers badAddresses = [ eto.user.email() for eto in emailToUsers if eto.user.user_id() is None ] return (retUsers, badAddresses)
def emailsToGoodUsers(thisClass, emailAddresses, returnBad=False): """ Provided a list of email addresses, return a list of users.User objects for valid addresses. This is a workaround for the fact that users.User('*****@*****.**').user_id() returns None. If returnBad is True, we return a 2-tuple: (userObjects, badEmailAddresses) """ newKeys = [ thisClass(user=users.User(ea)).put() for ea in emailAddresses ] emailToUsers = IP4DB.get(newKeys) db.delete(emailToUsers) retUsers = [eto.user for eto in emailToUsers if eto.user.user_id()] if not returnBad: return retUsers badAddresses = [ eto.user.email() for eto in emailToUsers if eto.user.user_id() is None ] return (retUsers, badAddresses)