def explore(): username = request.args.get('user', None) if not username: if username is not None: flash('Enter a name, lover.', 'error') return render_template( 'explore.html', current_time=datetime.utcnow(), user=None ) username = username.lower().strip() user_key = Employee.query( Employee.username == username, Employee.terminated == False, # noqa ).get(keys_only=True) if not user_key: flash('Sorry, "{}" is not a valid user.'.format(username), 'error') return redirect(url_for('explore')) sent_love = logic.love.recent_sent_love(user_key, include_secret=False, limit=20) received_love = logic.love.recent_received_love(user_key, include_secret=False, limit=20) return render_template( 'explore.html', current_time=datetime.utcnow(), sent_loves=sent_love.get_result(), received_loves=received_love.get_result(), user=user_key.get() )
def love_link(link_id): try: loveLink = logic.love_link.get_love_link(link_id) recipients_str = loveLink.recipient_list message = loveLink.message recipients = sanitize_recipients(recipients_str) loved = [ Employee.get_key_for_username(recipient).get() for recipient in recipients ] return render_template( 'love_link.html', current_time=datetime.utcnow(), current_user=Employee.get_current_employee(), recipients=recipients_str, message=message, loved=loved, link_id=link_id, ) except NoSuchLoveLink: flash('Sorry, that link ({}) is no longer valid.'.format(link_id), 'error') return redirect(url_for('home'))
def send_love_email(l): """Send an email notifying the recipient of l about their love.""" sender_future = l.sender_key.get_async() recipient_future = l.recipient_key.get_async() # Remove this love from recent_love if present (datastore is funny sometimes) recent_love = recent_received_love(l.recipient_key, limit=4).get_result() index_to_remove = None for i, love in enumerate(recent_love): if l.sender_key == love.sender_key and l.recipient_key == love.recipient_key and l.message == love.message: index_to_remove = i break if index_to_remove is not None: del recent_love[index_to_remove] sender = sender_future.get_result() recipient = recipient_future.get_result() from_ = config.LOVE_SENDER_EMAIL to = recipient.user.email() subject = u'Love from {}'.format(sender.full_name) body_text = u'"{}"\n\n{}'.format(l.message, '(Sent secretly)' if l.secret else '') body_html = render_template('email.html', love=l, sender=sender, recipient=recipient, recent_love_and_lovers=[ (love, love.sender_key.get()) for love in recent_love[:3] ]) logic.email.send_email(from_, to, subject, body_html, body_text)
def employees(): return render_template( 'employees.html', pagination_result=Employee.paginate( order_by=Employee.username, prev_cursor_str=request.args.get('prev_cursor'), next_cursor_str=request.args.get('next_cursor'), ), )
def home(): recipients = request.args.get('recipients', request.args.get('recipient')) message = request.args.get('message') return render_template( 'home.html', current_time=datetime.utcnow(), current_user=Employee.get_current_employee(), recipients=recipients, message=message, )
def me(): current_employee = Employee.get_current_employee() sent_love = logic.love.recent_sent_love(current_employee.key, limit=20) received_love = logic.love.recent_received_love(current_employee.key, limit=20) return render_template('me.html', current_time=datetime.utcnow(), current_user=current_employee, sent_loves=sent_love.get_result(), received_loves=received_love.get_result())
def home(): link_id = request.args.get('link_id', None) recipients = request.args.get('recipients', request.args.get('recipient')) message = request.args.get('message') return render_template('home.html', current_time=datetime.utcnow(), current_user=Employee.get_current_employee(), recipients=recipients, message=message, url='{0}l/{1}'.format(config.APP_BASE_URL, link_id) if link_id else None)
def direct_import(): # WARNING: this is admin_required, and we're not protecting against things # like injection. Be very aware. employee_dict = dict( username=request.form.get('username').strip().lower(), first_name=request.form.get('first_name').strip(), last_name=request.form.get('last_name').strip(), department=request.form.get('department').strip(), photo_url='', ) logic.employee.direct_load_employee(employee_dict) flash('Employee added!') return render_template('import_direct.html')
def leaderboard(): timespan = request.args.get('timespan', TIMESPAN_THIS_WEEK) department = request.args.get('department', None) (top_lover_dicts, top_loved_dicts) = get_leaderboard_data(timespan, department) return render_template( 'leaderboard.html', top_loved=top_loved_dicts, top_lovers=top_lover_dicts, departments=logic.department.META_DEPARTMENTS, sub_departments=logic.department.META_DEPARTMENT_MAP, selected_dept=department, selected_timespan=timespan, org_title=config.ORG_TITLE, )
def internal_server_error(exc): # in general we don't want to give out a "support email" all the time support_email = None title = 'Internal Error' if type(exc) is NoSuchEmployee: # this is an error we know and can address, so showing an email is ok. support_email = config.SUPPORT_EMAIL message = ( "We couldn't find your {app_name} account. If you're logged in " 'with a @{domain} account, then this is probably an error on our ' "end. Contact us and we'll sort it out for you!" ).format(app_name=config.APP_NAME, domain=config.DOMAIN) else: message = 'Something went wrong. Sorry about that!' return render_template('error.html', title=title, message=message, support_email=support_email)
def leaderboard(): timespan = request.args.get('timespan', TIMESPAN_THIS_WEEK) department = request.args.get('department', None) # If last week, we need to subtract *before* getting the week limits to # avoid being off by one hour on weeks that include a DST transition utc_now = datetime.utcnow() if timespan == TIMESPAN_LAST_WEEK: utc_now -= timedelta(days=7) utc_week_start, _ = utc_week_limits(utc_now) top_lovers, top_lovees = logic.love_count.top_lovers_and_lovees(utc_week_start, dept=department) top_lover_dicts = [ { 'employee': employee_key.get_async(), 'num_sent': sent_count } for employee_key, sent_count in top_lovers ] top_loved_dicts = [ { 'employee': employee_key.get_async(), 'num_received': received_count } for employee_key, received_count in top_lovees ] # get results for the futures set up previously map(to_the_future, top_lover_dicts) map(to_the_future, top_loved_dicts) return render_template( 'leaderboard.html', top_loved=top_loved_dicts, top_lovers=top_lover_dicts, departments=logic.department.META_DEPARTMENTS, sub_departments=logic.department.META_DEPARTMENT_MAP, selected_dept=department, selected_timespan=timespan, org_title=config.ORG_TITLE, )
def sent(): link_id = request.args.get('link_id', None) recipients_str = request.args.get('recipients', None) message = request.args.get('message', None) if not link_id or not recipients_str or not message: return redirect(url_for('home')) recipients = sanitize_recipients(recipients_str) loved = [ Employee.get_key_for_username(recipient).get() for recipient in recipients ] return render_template( 'sent.html', current_time=datetime.utcnow(), current_user=Employee.get_current_employee(), message=message, loved=loved, url='{0}l/{1}'.format(config.APP_BASE_URL, link_id), )
def direct_import_form(): return render_template('import_direct.html')
def import_employees_form(): import_file_exists = os.path.isfile(logic.employee.csv_import_file()) return render_template( 'import.html', import_file_exists=import_file_exists, )
def aliases(): return render_template( 'aliases.html', aliases=Alias.query().fetch(), )
def subscriptions(): return render_template( 'subscriptions.html', subscriptions=Subscription.query().fetch(), events=logic.event.EVENTS, )
def keys(): api_keys = AccessKey.query().fetch() return render_template( 'keys.html', keys=api_keys )