def get_debug_user(self): user = users.get_current_user() if user: query = User.gql("WHERE auth_ids = :auth_ids", auth_ids="debug:" + user.user_id()) all_users = query.fetch(1) if all_users: user = all_users[0] if not hasattr(user, 'admin'): user.admin = False user.put() return user else: _attrs = {} _attrs['name'] = users.get_current_user().nickname() _attrs['share_report_key'] = generate_string(8) _attrs['share_report_and_list_key'] = generate_string(7) _attrs['provider'] = "debug" _attrs['admin'] = False ok, user = User.create_user("debug:" + user.user_id(), **_attrs) return user return None
def get_or_create_account(): user = users.get_current_user() if user: query = Account.gql("WHERE user_id = :user_id", user_id=user.user_id()) accounts = query.fetch(1) if accounts: return accounts[0] else: new_account = Account() new_account.user_id = user.user_id() new_account.share_report_key = generate_string(8) new_account.share_report_and_list_key = generate_string(7) db.put(new_account) new_account.nickname = user.nickname() return new_account return None
def get(self): if "example" in self.request.path: attacks = Example.get_example_attacks() uid = generate_string(6) else: if self.logged_in: attacks = self.current_user.get_attacks_as_dict() uid = self.current_user.share_report_and_list_key if attacks is not None: self.response.headers['Content-Transfer-Encoding'] = 'binary' self.response.headers['Accept-Range'] = 'bytes' self.response.headers['Content-Encoding'] = 'binary' if self.request.path.endswith(".xlsx"): self.response.headers['Content-Disposition'] = 'attachment; filename=migraines.xlsx' self.response.headers['Content-Type'] = 'application/xlsx' output_content = generate_xlsx_output(attacks) if self.request.path.endswith(".csv"): self.response.headers['Content-Disposition'] = 'attachment; filename=migraines.csv' self.response.headers['Content-Type'] = 'application/csv' output_content = generate_csv_output(attacks) if self.request.path.endswith(".ics"): self.response.headers['Content-Disposition'] = 'attachment; filename=migraines.ics' self.response.headers['Content-Type'] = 'application/ics' output_content = generate_ics_output(attacks, uid) self.response.out.write(output_content) else: self.response.out.write("We don't have what you're looking for.") self.response.status = 404
def _on_signin(self, data, auth_info, provider, extra=None): """Callback whenever a new or existing user is logging in. data is a user info dictionary. auth_info contains access token or oauth token and secret. extra is a dict with additional params passed to the auth init handler. See what's in it with e.g. logging.info(auth_info) """ logging.info(data) auth_id = '%s:%s' % (provider, data['id']) user = self.auth.store.user_model.get_by_auth_id(auth_id) _attrs = self._to_user_model_attrs(data, self.USER_ATTRS[provider]) if user: logging.debug('Found existing user to log in') # Existing users might've changed their profile data so we update our # local model anyway. This might result in quite inefficient usage # of the Datastore, but we do this anyway for demo purposes. # # In a real app you could compare _attrs with user's properties fetched # from the datastore and update local user in case something's changed. user.populate(**_attrs) user.provider = provider if not hasattr(user, 'admin'): user.admin = False user.put() self.auth.set_session(self.auth.store.user_to_dict(user)) else: # check whether there's a user currently logged in # then, create a new user if nobody's signed in, # otherwise add this auth_id to currently logged in user. if self.auth.get_user_by_session() is not None: logging.debug('Updating currently logged in user') u = self.current_user u.populate(**_attrs) # The following will also do u.put(). Though, in a real app # you might want to check the result, which is # (boolean, info) tuple where boolean == True indicates success # See webapp2_extras.appengine.auth.models.User for details. u.add_auth_id(auth_id) else: logging.debug('Creating a brand new user') _attrs['share_report_key'] = generate_string(8) _attrs['share_report_and_list_key'] = generate_string(7) _attrs['provider'] = provider _attrs['admin'] = False ok, user = self.auth.store.user_model.create_user(auth_id, **_attrs) if ok: self.auth.set_session(self.auth.store.user_to_dict(user)) support_email("New User", "A new account has been created: " + str(_attrs)) destination_url = '/report' if extra is not None: params = webob.multidict.MultiDict(extra) destination_url = str(params.get('destination_url', '/report')) return self.redirect(destination_url)