def poke(request): email = request.POST.get('email', None) if not email: return HttpResponse(utils.json_encode({'status': 409, 'message': 'no friend of yours'})) if request.user.email == email: u = request.user else: f = models.Friend.objects.is_friend(request.user, email) if not f: return HttpResponse(utils.json_encode({'status': 409, 'message': 'no friend of yours'})) u = f.subject(request.user) if not u: return HttpResponse(utils.json_encode({'status': 409, 'message': 'no friend of yours'})) pokes = models.Poke.objects.filter(poker=request.user, pokee=u, poked_at__gte=datetime.datetime.now()-datetime.timedelta(hours=2)) if pokes.count(): return HttpResponse(utils.json_encode({'status': 403, 'message': 'you just poked %s' % utils.friend_name(u) })) p = models.Poke() p.poker = request.user p.pokee = u p.save() login_url = request.build_absolute_uri('/index.html') send_mail(_("You've Been Poked on Double Happiness"), _(""" %s has poked you on Double Happiness! Hop online and see what's up. %s We hope you will be delightfully happy. """) % (utils.friend_name(request.user), login_url), 'Double Happiness <*****@*****.**>', [u.email], fail_silently=False) return HttpResponse(utils.json_encode({'status': 200, 'message': 'consider them poked'}))
def finger(request): email = request.POST.get('email', None) if not email: return HttpResponse(utils.json_encode({'status': 409, 'message': 'no friend of yours'})) if request.user.email == email: u = request.user else: f = models.Friend.objects.is_friend(request.user, email) if not f: return HttpResponse(utils.json_encode({'status': 409, 'message': 'no friend of yours'})) u = f.subject(request.user) if not u: return HttpResponse(utils.json_encode({'status': 409, 'message': 'no friend of yours'})) # TODO add a 'last_status' field? data = {'status': 200, 'last_login': u.last_login, 'joined': u.date_joined, 'online': models.Active.objects.is_online(u), 'whom': utils.friend_name(u)} ss = u.statuses.all().order_by('-posted_at') if ss.count(): data['last_status'] = ss[0].status data['last_status_at'] = ss[0].posted_at return HttpResponse(utils.json_encode(data))
def w(request): users = [{'name': utils.friend_name(request.user), 'last_login': request.user.last_login, 'last_seen': models.Active.objects.last_seen(request.user) }] for f in models.Friend.objects.all_friends(request.user): u = f.subject(request.user) if u and models.Active.objects.is_online(u): last = models.Active.objects.last_seen(u) users.append({'name': utils.friend_name(u), 'last_login': u.last_login, }) users.sort(key=lambda u: u['name']) return HttpResponse(utils.json_encode({'status': 200, 'people': users}))
def last(request): email = request.POST.get('email', None) if email: if request.user.email == email: u = request.user else: f = models.Friend.objects.is_friend(request.user, email) if not f: return HttpResponse(utils.json_encode({'status': 409, 'message': 'no friend of yours'})) u = f.subject(request.user) if not u: return HttpResponse(utils.json_encode({'status': 409, 'message': 'no friend of yours'})) statuses = [{'friend': utils.friend_name(u), 'date': s.posted_at, 'status': s.get_status() } for s in u.statuses.all().order_by('-posted_at')[:20]] return HttpResponse(utils.json_encode({'status': 200, 'statuses': statuses, 'whom': utils.friend_name(u)})) else: statuses = [] ss = request.user.statuses.all().order_by('-posted_at') if ss.count(): statuses.append({'friend': utils.friend_name(ss[0].user), 'date': ss[0].posted_at, 'status': ss[0].get_status() }) for f in models.Friend.objects.all_friends(request.user): u = f.subject(request.user) if u: ss = u.statuses.all().order_by('-posted_at') if ss.count(): statuses.append({'friend': utils.friend_name(ss[0].user), 'date': ss[0].posted_at, 'status': ss[0].get_status() }) statuses.sort(key=lambda s: s['date'], reverse=True) print statuses return HttpResponse(utils.json_encode({'status': 200, 'statuses': statuses}))
def chfn(request): first = request.POST.get('first_name', '') last = request.POST.get('last_name', None) if not first: return HttpResponse(utils.json_encode({'status': 409, 'message': 'you need a name'})) u = request.user u.first_name = first u.last_name = last u.save() return HttpResponse(utils.json_encode({'status': 200, 'message': 'you are now %s' % (utils.friend_name(request.user))}))
def motd(request): msg = [u" ", u"Oh, hi there. Good to see you.", u" " ] w = int(request.GET.get('pw', '80')) h = int(request.GET.get('ph', '24')) resp = {'status': 200, 'motd': msg, 'email': request.user.email, 'background': utils.load_random_background(w, h), } pending_friends = models.Friend.objects.filter(recipient=request.user, accepted_at__isnull=True, revoked_at__isnull=True) if pending_friends.count(): resp['pending_friends'] = [utils.friend_name(p.initiator) for p in pending_friends] return HttpResponse(utils.json_encode(resp))
def whoami(request): return HttpResponse(utils.json_encode({'status': 200, 'response': utils.friend_name(request.user)}))
def befriend(request): recipient_email = request.POST.get('email', None) if not recipient_email: return HttpResponse(utils.json_encode({'status': 409, 'message': 'recipient email required'})) friend, created, invited = models.Friend.objects.request_friend(request.user, recipient_email) if invited: password = models.create_password() recipient = friend.recipient recipient.set_password(password) recipient.save() token_url = request.build_absolute_uri('/index.html') + "#register=%s" % recipient.username send_mail(_("You're Invited to Double Happiness"), _(""" %s has invited you to be a BFF on Double Happiness! Please click this link to activate your account. %s Your temporary password to sign in is: %s Once you've signed in, please use this command to accept their friendship: welcome %s We hope you will be delightfully happy. """) % (utils.friend_name(request.user), token_url, password, request.user.email), 'Double Happiness <*****@*****.**>', [recipient.email], fail_silently=False) return HttpResponse(utils.json_encode({'status': 200, 'message': 'friend request sent'})) elif created: recipient = friend.recipient login_url = request.build_absolute_uri('/index.html') send_mail(_("New Friends on Double Happiness"), _(""" %s has invited you to be a BFF on Double Happiness! Please signed in: %s Then use this command to accept their friendship: welcome %s We hope you will be delightfully happy. """) % (utils.friend_name(request.user), login_url, request.user.email), 'Double Happiness <*****@*****.**>', [recipient.email], fail_silently=False) return HttpResponse(utils.json_encode({'status': 200, 'message': 'friend request sent'})) else: return HttpResponse(utils.json_encode({'status': 200, 'message': 'friend request already sent'}))