def save(request): try: group_id = long(post_param(request, 'group_id')) group = get_group(group_id) require = check_user(group) if require: return redirect("/redirect/?%s" % require) amount = float(post_param(request, 'amount')) if amount <0: raise ValueError('amount can not be negative') payer_id = long(post_param(request, 'payer')) participants = map(lambda x: long(x), post_params(request, 'participants')) if len(participants) <1: raise ValueError('a meal with no attendee?') if payer_id not in participants: raise ValueError('...I have nothing to say.') date = None date_str = post_param(request, 'date') if date_str: date = datetime.strptime(date_str, "%Y/%m/%d") except: return redirect('/redirect/group/%s?%s' % (group_id, 'param')) else: save_fee(group, amount, payer_id, participants, date) return redirect('/redirect/group/%s' % group_id)
def edit(request, group_id): param = {} if group_id: group = get_group(group_id) require = check_user(group) if require: return redirect("/redirect/?%s" % require) users = get_users(group) param.update({'group': group, 'users': users,}) errs = get_param(request, 'errs') if errs: errs = errs.split(',') for err in errs: param.update({"%s_error" % err: True,}) return respond('group_edit.html', param)
def detail(request, group_id): group = get_group(group_id) if not group: raise Http404() require_privilege = check_user(group) summaries = [] +group.summaries summaries.sort() due_payer_index = group.summaries.index(summaries[0]) due_payer = get_user(group.members[due_payer_index]) params = {'fees': get_fees(group), 'members': get_users(group), 'is_valid_user': not require_privilege, 'group': group, 'due_payer': due_payer.name, 'due_amount': -summaries[0], } return respond('group_detail.html', params)
def save(request): password = post_param(request, 'password') name = post_param(request, 'name') members = post_param(request, 'members').split() group_id = post_param(request, 'group_id') owner_email = post_param(request, 'owner') group = None if group_id: try: group = get_group(long(group_id)) require = check_user(group) if require: return redirect("/redirect/?%s" % require) except: return redirect('/group/edit/?errs=group_id') for member in get_users(group): if member.name not in members: return redirect('/group/edit/%s?errs=members' % group.key().id()) members = save_users(members, group) owner = None try: owner = users.User(owner_email) except: owner = users.get_current_user() if group: group.members = members group.owner = owner else: group = Group(members = members, password = password, name = name, owner = owner) while len(group.summaries) < len(members): group.summaries.append(0.0) group.put() return redirect('/redirect/group/')
def delete(request, group_id): try: group = get_group(group_id) require = check_user(group) if require: raise Exception("invalid user") if not request.GET.has_key('confirm'): params = {'name': group.name, 'confirm': "%s?confirm" % request.path, 'cancel': "/group/%s" % group_id,} return respond("confirm.html", params) except ValueError: return redirect('/redirect/?param') except: return redirect("/redirect/?%s" % require) else: for member_id in group.members: member = get_user(member_id) member.delete() group.delete() return home(request)
def index(): if current_user.is_authenticated() is False: return render_template("index_signed_out.html") # list of tuples (twitter_name, twitter_id) where twitter_id is None # if that user hasn't registered group = helpers.get_group(user=current_user) senders = helpers.get_received_noticed_glances(user=current_user) senders_dict = dict( [(r.sender_twitter_id, r) for r in senders] ) # we want an entry in received_glances for every person in group received_glances = [] for sender_name, sender_id in group: if sender_id is None: # this member of the group is not registered received_glances.append((sender_name, None)) elif senders_dict.has_key(sender_id): # this member of the group is registered, and # has also sent a glance at receiver received_glances.append( (sender_name, senders_dict[sender_id].most_recent) ) else: # this member of the group is registered, but # has never sent a glance at receiver received_glances.append((sender_name, None)) last_sent_glance = helpers.get_most_recent_sent_glance(user=current_user) group_energy = helpers.calculate_group_energy(last_sent_glance=last_sent_glance, received_glances=received_glances) received_glances_human = [(s, helpers.time_ago_human_readable(w) % s) for (s, w) in received_glances] # get count of unnoticed glances unnoticed_count = helpers.get_received_unnoticed_glances_count( user=current_user, db_session=db.session) # get list of Twitter display names of transitory glances # this returns *all* glances from Twitter followers who are also friends, # so strip out the ones in the user's group before displaying all_transitory = helpers.get_received_transitory_glances( user=current_user) group_names = [g[0] for g in group] transitory_glances = [x for x in all_transitory if x not in group_names] twitter_conn = app.social.twitter.get_connection() current_user_twitter_display_name = twitter_conn.display_name glance_form = DoGlanceForm() group_tweet_text = "hey you :) " group_tweet_text += " ".join([s for (s, w) in received_glances]) # will be True if do_glance was called show_group = session.pop('glance_done', False) return render_template("index.html", show_group=show_group, received_glances=received_glances_human, glance_form=glance_form, current_user_twitter_display_name=current_user_twitter_display_name, group_energy=group_energy, group_size=len(current_user.who_they_lookin_at), group_tweet_text=group_tweet_text, unnoticed_count=unnoticed_count, transitory_glances=transitory_glances)