def post(self): api_vcode = self.request.POST.get('api_vcode', None) api_id = self.request.POST.get('api_id', None) if not (api_id and api_vcode) or not(len(api_id) == 6 and len(api_vcode) == 64): env = dict(values=dict(api_id=api_id, api_vcode=api_vcode), errors=dict(api_id=(not api_id or len(api_id) != 6), api_vcode=(not api_vcode or len(api_vcode) != 64))) self.session.add_flash(env, key='env_flash') else: if Account.query().filter(Account.api_id == api_id).count(): self.session.add_flash('This API key has already been added to this profile', key='error_messages') else: api = Api(api_id, api_vcode) api.authenticate() if api.is_authenticated(): accounts_chars = [] for api_char in api.characters: if not filter(lambda c: api_char.charactedID == c.char_id, self.userprofile.characters): accounts_chars.append(Character(user=users.get_current_user(), name=api_char.name, char_id=str(api_char.characterID))) account = Account( user=users.get_current_user(), api_vcode=api_vcode, api_id=api_id, key_name=self.request.POST.get('name', None) ) account.put() for char in accounts_chars: char.account_key = account.key put_multi_async(accounts_chars) #self.userprofile.characters = (self.userprofile.characters or []) + accounts_chars #self.userprofile.put_async() self.session.add_flash('Key added successfully', key='messages') self.redirect('/profile')
def get_accounts_async(): accounts = yield Account.query().fetch_async() raise ndb.Return(accounts)