def edit(self, card_id): # TODO: handle this some other way AppUser.record_access(users.get_current_user()) card = ReportCard.find_by_id(int(card_id)) if card.is_authorized(): template = JinjaEnv.get().get_template('templates/card/edit_form.html') self.response.out.write(template.render({'card': card}))
def add_owner(self, card_id): if AppUser.for_user(users.get_current_user()).is_admin: card = ReportCard.find_by_id(int(card_id)) card.owner_user_id.append(self.request.get('new_owner_id')) card.put() ReportCard.create(self.request.get('name')) return webapp2.redirect_to('card-list')
def demonstrate_consistency(consistency): # Which type of consistency? eventual (default), memcache, or institial consistency = request.form.get('consistency', consistency) # Look for existing user record if consistency == 'memcache': user = AppUser.find_by_ip_address_pseudo_strongly() else: user = AppUser.find_by_ip_address_eventually() # Create form form = AppUserConsistencyForm(request.form) form.ip_address.data = AppUser.get_ip_address_from_os() form.blank_slate.data = 'Form loaded at %s' % (datetime.utcnow()) form.consistency.data = consistency if user: form.stored_browserprint.data = user.browserprint # Delete if request.method == 'POST' and request.form.get('delete'): ip_address = AppUser.get_ip_address_from_os() user = AppUser.get_by_id(ip_address) if user: user.delete() flash('Deleted user.', 'local') else: flash('User not found', 'local') return redirect(url_for('demonstrate_consistency', consistency='eventual')) # Process form elif request.method == 'POST': if not form.validate(): flash('Unable to validate form.', 'local') return redirect(url_for('demonstrate_consistency', consistency=consistency)) if consistency == 'memcache': return process_using_memcache(request, user) elif consistency == 'interstitial': return process_using_interstitial(request, user) else: # eventual return process_using_eventual_consistency(request, user) return render_template('app_users/consistency.html', form=form, user=user, consistency=dict(form.consistency.choices).get(consistency))
def InitUser(): """Initialize application user. Retrieve existing user credentials from datastore or add new user. Returns: AppUser instance of the application user. """ result = AppUser.query(AppUser.user == users.get_current_user()).fetch() if result: app_user = result[0] else: app_user = AppUser(user=users.get_current_user(), email=users.get_current_user().email()) app_user.put() return app_user
def list_by_user(self, user=None): if not user: user = users.get_current_user() app_user = AppUser.for_user(user) if app_user.is_admin: cards = self.list() else: cards = self.gql("WHERE owner_user_id = :1", user.user_id()).fetch(100) return cards
def process_using_interstitial(request, user): if not user: user = AppUser.create(request.form.get('browserprint'), request.form.get('blank_slate')) flash('New user saved!', 'local') else: user.browserprint = request.form.get('browserprint') user.blank_slate = request.form.get('blank_slate') user.put() flash('User updated.', 'local') redirect_url = url_for('consistency_interstitial', user_safeurl=user.key.urlsafe()) return redirect(redirect_url)
def process_using_eventual_consistency(request, user): redirect_url = url_for('demonstrate_consistency', consistency='eventual') if not user: user = AppUser.create(request.form.get('browserprint'), request.form.get('blank_slate')) flash('New user saved!', 'local') else: user.browserprint = request.form.get('browserprint') user.blank_slate = request.form.get('blank_slate') user.put() flash('User updated.', 'local') return redirect(redirect_url)
def process_using_memcache(request, user): """Notice create_and_cache method used and cache called for update.""" redirect_url = url_for('demonstrate_consistency', consistency='memcache') if not user: user = AppUser.create_and_cache(request.form.get('browserprint'), request.form.get('blank_slate')) flash('New user saved and cached!', 'local') else: user.browserprint = request.form.get('browserprint') user.blank_slate = request.form.get('blank_slate') user.cache() user.put() flash('User updated and cached.', 'local') return redirect(redirect_url)
def UpdateUserCredentials(client_id, client_secret, refresh_token, mcc_cid, developer_token): """Update the credentials associated with application user. Args: client_id: str Client Id retrieved from the developer's console. client_secret: str Client Secret retrieved from the developer's console. refresh_token: str Refresh token generated with the above client id/secret. mcc_cid: str Customer Id for the AdWords MCC account. developer_token: str Developer Token for the AdWords account. """ app_user = AppUser.query(AppUser.user == users.get_current_user()).fetch()[0] app_user.client_id = client_id app_user.client_secret = client_secret app_user.refresh_token = refresh_token app_user.mcc_cid = mcc_cid app_user.developer_token = developer_token app_user.put()
def UpdateUserCredentials(client_id, client_secret, refresh_token, manager_account_id, developer_token): """Update the credentials associated with application user. Args: client_id: str Client Id retrieved from the developer's console. client_secret: str Client Secret retrieved from the developer's console. refresh_token: str Refresh token generated with the above client id/secret. manager_account_id: str Customer Id for the AdWords manager account. developer_token: str Developer Token for the AdWords account. """ app_user = AppUser.query(AppUser.user == users.get_current_user()).fetch()[0] app_user.client_id = client_id app_user.client_secret = client_secret app_user.refresh_token = refresh_token app_user.manager_account_id = manager_account_id app_user.developer_token = developer_token app_user.put()
def list(self, card_id): # TODO: handle this some other way AppUser.record_access(users.get_current_user()) current_user = AppUser.for_user(users.get_current_user()) template = JinjaEnv.get().get_template('templates/eval/list.html') self.response.out.write(template.render({'card': ReportCard.find_by_id(int(card_id)), 'current_user': current_user}))
def is_authorized(self, user=None): if not user: user = users.get_current_user() app_user = AppUser.for_user(user) return app_user.is_admin or user.user_id() in self.owner_user_id
def makeAdmin(self, user=None): if not user: user = users.get_current_user() u = AppUser.for_user(user) u.is_admin = True u.put()
def add_owner_form(self, card_id): current_user = AppUser.for_user(users.get_current_user()) if current_user.is_admin: template = JinjaEnv.get().get_template('templates/card/owner_add_form.html') self.response.out.write(template.render({'card': ReportCard.find_by_id(int(card_id)), 'users': AppUser.list()}))