def _user_snippet(user_email, weeks_back=0): """Return the user's most recent Snippet. If one doesn't exist, one will be automatically filled from the template (but not saved). By using the optional `weeks_back` parameter, you can step backwards in time. Note that if you go back before the user's *first* snippet, they will not be filled (the default filling seems to only go forwardwise in time), and an IndexError will be raised. Raises an IndexError if requested snippet week comes before user birth. Raises ValueError if user couldn't be found. """ account = util.get_user_or_die(user_email) # can raise ValueError user_snips = util.snippets_for_user(user_email) logging.debug('User %s got snippets from db: %s', user_email, len(user_snips)) filled_snips = util.fill_in_missing_snippets(user_snips, account, user_email, _TODAY_FN()) logging.debug('User %s snippets *filled* to: %s', user_email, len(filled_snips)) index = (-1) - weeks_back return filled_snips[index]
def _user_snippet(user_email, weeks_back=0): """Return the user's most recent Snippet. If one doesn't exist, one will be automatically filled from the template (but not saved). By using the optional `weeks_back` parameter, you can step backwards in time. Note that if you go back before the user's *first* snippet, they will not be filled (the default filling seems to only go forwardwise in time), and an IndexError will be raised. Raises an IndexError if requested snippet week comes before user birth. Raises ValueError if user couldn't be found. """ account = util.get_user_or_die(user_email) # can raise ValueError user_snips = util.snippets_for_user(user_email) logging.debug( 'User %s got snippets from db: %s', user_email, len(user_snips) ) filled_snips = util.fill_in_missing_snippets(user_snips, account, user_email, _TODAY_FN()) logging.debug( 'User %s snippets *filled* to: %s', user_email, len(filled_snips) ) index = (-1) - weeks_back return filled_snips[index]
def get(self): if not users.get_current_user(): return _login_page(self.request, self) user_email = self.request.get('u', _current_user_email()) user = util.get_user(user_email) if not user: template_values = { 'login_url': users.create_login_url(self.request.uri), 'logout_url': users.create_logout_url('/'), 'username': user_email, } self.render_response('new_user.html', template_values) return snippets = util.snippets_for_user(user_email) if not _can_view_private_snippets(_current_user_email(), user_email): snippets = [snippet for snippet in snippets if not snippet.private] snippets = util.fill_in_missing_snippets(snippets, user, user_email, _TODAY_FN()) snippets.reverse() # get to newest snippet first template_values = { 'logout_url': users.create_logout_url('/'), 'message': self.request.get('msg'), 'username': user_email, 'is_admin': users.is_current_user_admin(), 'domain': user_email.split('@')[-1], 'view_week': util.existingsnippet_monday(_TODAY_FN()), # Snippets for the week of <one week ago> are due today. 'one_week_ago': _TODAY_FN().date() - datetime.timedelta(days=7), 'eight_days_ago': _TODAY_FN().date() - datetime.timedelta(days=8), 'editable': (_logged_in_user_has_permission_for(user_email) and self.request.get('edit', '1') == '1'), 'user': user, 'snippets': snippets, 'null_snippet_text': models.NULL_SNIPPET_TEXT, 'null_category': models.NULL_CATEGORY, } self.render_response('user_snippets.html', template_values)
def get(self): if not users.get_current_user(): return _login_page(self.request, self) user_email = self.request.get('u', _current_user_email()) user = util.get_user(user_email) if not user: # If there are no app settings, set those up before setting # up the user settings. if users.is_current_user_admin(): try: models.AppSettings.get() except ValueError: self.redirect("/admin/settings?redirect_to=user_setting" "&msg=Welcome+to+the+snippet+server!+" "Please+take+a+moment+to+configure+it.") return template_values = { 'new_user': True, 'login_url': users.create_login_url(self.request.uri), 'logout_url': users.create_logout_url('/'), 'username': user_email, } self.render_response('new_user.html', template_values) return snippets = util.snippets_for_user(user_email) if not _can_view_private_snippets(_current_user_email(), user_email): snippets = [snippet for snippet in snippets if not snippet.private] snippets = util.fill_in_missing_snippets(snippets, user, user_email, _TODAY_FN()) snippets.reverse() # get to newest snippet first template_values = { 'logout_url': users.create_logout_url('/'), 'message': self.request.get('msg'), 'username': user_email, 'is_admin': users.is_current_user_admin(), 'domain': user_email.split('@')[-1], 'view_week': util.existingsnippet_monday(_TODAY_FN()), # Snippets for the week of <one week ago> are due today. 'one_week_ago': _TODAY_FN().date() - datetime.timedelta(days=7), 'eight_days_ago': _TODAY_FN().date() - datetime.timedelta(days=8), 'editable': (_logged_in_user_has_permission_for(user_email) and self.request.get('edit', '1') == '1'), 'user': user, 'snippets': snippets, 'null_category': models.NULL_CATEGORY, } self.render_response('user_snippets.html', template_values)
def get(self): if not users.get_current_user(): return _login_page(self.request, self) user_email = self.request.get("u", _current_user_email()) user = util.get_user(user_email) if not user: # If there are no app settings, set those up before setting # up the user settings. if users.is_current_user_admin(): try: models.AppSettings.get() except ValueError: self.redirect( "/admin/settings?redirect_to=user_setting" "&msg=Welcome+to+the+snippet+server!+" "Please+take+a+moment+to+configure+it." ) return template_values = { "new_user": True, "login_url": users.create_login_url(self.request.uri), "logout_url": users.create_logout_url("/"), "username": user_email, } self.render_response("new_user.html", template_values) return snippets = util.snippets_for_user(user_email) if not _can_view_private_snippets(_current_user_email(), user_email): snippets = [snippet for snippet in snippets if not snippet.private] snippets = util.fill_in_missing_snippets(snippets, user, user_email, _TODAY_FN()) snippets.reverse() # get to newest snippet first template_values = { "logout_url": users.create_logout_url("/"), "message": self.request.get("msg"), "username": user_email, "is_admin": users.is_current_user_admin(), "domain": user_email.split("@")[-1], "view_week": util.existingsnippet_monday(_TODAY_FN()), # Snippets for the week of <one week ago> are due today. "one_week_ago": _TODAY_FN().date() - datetime.timedelta(days=7), "eight_days_ago": _TODAY_FN().date() - datetime.timedelta(days=8), "editable": (_logged_in_user_has_permission_for(user_email) and self.request.get("edit", "1") == "1"), "user": user, "snippets": snippets, "null_category": models.NULL_CATEGORY, } self.render_response("user_snippets.html", template_values)
def get(self): # options are 'email', 'creation_time', 'last_snippet_time' sort_by = self.request.get('sort_by', 'creation_time') # First, check if the user had clicked on a button. for (name, value) in self.request.params.iteritems(): if name.startswith('hide '): email_of_user_to_hide = name[len('hide '):] user = util.get_user_or_die(email_of_user_to_hide) user.is_hidden = True user.put() time.sleep(0.1) # encourage eventual consistency self.redirect('/admin/manage_users?sort_by=%s&msg=%s+hidden' % (sort_by, email_of_user_to_hide)) return if name.startswith('unhide '): email_of_user_to_unhide = name[len('unhide '):] user = util.get_user_or_die(email_of_user_to_unhide) user.is_hidden = False user.put() time.sleep(0.1) # encourage eventual consistency self.redirect( '/admin/manage_users?sort_by=%s&msg=%s+unhidden' % (sort_by, email_of_user_to_unhide)) return if name.startswith('delete '): email_of_user_to_delete = name[len('delete '):] user = util.get_user_or_die(email_of_user_to_delete) db.delete(user) time.sleep(0.1) # encourage eventual consistency self.redirect('/admin/manage_users?sort_by=%s&msg=%s+deleted' % (sort_by, email_of_user_to_delete)) return user_q = models.User.all() results = user_q.fetch(1000) # Tuple: (email, is-hidden, creation-time, days since last snippet) user_data = [] for user in results: # Get the last snippet for that user. snippets = util.snippets_for_user(user.email) if snippets: seconds_since_snippet = ((_TODAY_FN().date() - snippets[-1].week).total_seconds()) weeks_since_snippet = int( seconds_since_snippet / datetime.timedelta(days=7).total_seconds()) else: weeks_since_snippet = None user_data.append((user.email, user.is_hidden, user.created, weeks_since_snippet)) # We have to use 'cmp' here since we want ascending in the # primary key and descending in the secondary key, sometimes. if sort_by == 'email': user_data.sort(lambda x, y: cmp(x[0], y[0])) elif sort_by == 'creation_time': user_data.sort(lambda x, y: (-cmp(x[2] or datetime.datetime.min, y[ 2] or datetime.datetime.min) or cmp(x[0], y[0]))) elif sort_by == 'last_snippet_time': user_data.sort(lambda x, y: (-cmp(1000 if x[3] is None else x[ 3], 1000 if y[3] is None else y[3]) or cmp(x[0], y[0]))) else: raise ValueError('Invalid sort_by value "%s"' % sort_by) template_values = { 'logout_url': users.create_logout_url('/'), 'message': self.request.get('msg'), 'username': _current_user_email(), 'is_admin': users.is_current_user_admin(), 'view_week': util.existingsnippet_monday(_TODAY_FN()), 'user_data': user_data, 'sort_by': sort_by, } self.render_response('manage_users.html', template_values)