def post(self): fan = db.get( self.request.get('fan') ) star = db.get( self.request.get('star') ) already = Friendship.all().filter('fan =', fan).filter('star =', star).fetch(1) if fan and star and len( already ) == 0: follow = Friendship.follow( fan, star ) if follow: self.response.out.write( simplejson.dumps( { 'status' : 'success', 'message' : 'follow', 'star' : star.nickname, 'star_key' : self.request.get('star'), 'fan_key' : self.request.get('fan'), 'star_followers' : follow.star.followers } ) ) else: self.response.out.write( simplejson.dumps( { 'status' : 'error', 'message' : 'follow' } ) )
def post(self, request, *args, **kwargs): friend_list = get_friends(request.user) kwargs["friends"] = friend_list # Determine if user exists. friend_name = request.POST["friend_name"] friend = None try: friend = User.objects.get(username=friend_name) except User.DoesNotExist: kwargs["error"] = "Couldn't find user %s" % friend_name if friend: # Determine if user is already a friend. isAlreadyFriend = False for friend in friend_list: if friend_name == friend.username: isAlreadyFriend = True if not isAlreadyFriend: f = Friendship(user1=request.user, user2=friend) f.save() friend_list.append(friend) else: msg = "You're already friends with %s." % friend_name kwargs["error"] = msg return super(FriendsView, self).get(self, request, *args, **kwargs)
def add_friend(current_user, friend): user2 = User.query.filter_by(username=friend).first() if user2 is None: return jsonify({'msg': 'User not found.'}) user1 = current_user #print(user2) query = Friendship.query.filter(( (Friendship.user_a == current_user.public_id) & (Friendship.user_b == user2.public_id)) | ( (Friendship.user_b == current_user.public_id) & (Friendship.user_a == user2.public_id))).first() #print('Query is',query) if query is None: if user1.id > user2.id: user1, user2 = user2, user1 new_friendship = Friendship(user_a=user1.public_id, user_b=user2.public_id, status=1, action_user=current_user.public_id) #print('New is', new_friendship) db.session.add(new_friendship) db.session.commit() return jsonify({'msg': 'Request Sent.'}) else: query.status = 0 if query.status == 1 else 1 db.session.commit() return jsonify({'msg': 'Invalid Request.'})
def get(self, cook): user = User.is_logged(self) author = User.all().filter('nickname =', cook.lower()).fetch(1) if not len( author ): self.error(404) return author = author[0] if user: alreadyfollow = Friendship.alreadyfollow( user, author ) title = "%s's CookBook" %(author.nickname) subhead = "Discover what %s has shared" % (author.nickname) author_recipes_count = Koch.all().filter('author =', author).count(); if author.fb_profile_url: avatar = "https://graph.facebook.com/%s/picture" % (author.nickname) elif not author.usegravatar and author.avatar: avatar = "/avatar/?user_id=%s" %(author.key()) else: avatar = helpers.get_gravatar( author.email, 90 ) page = self.request.get_range('page', min_value=0, max_value=1000, default=0) tmp_kochs, next_page, prev_page = helpers.paginate( Koch.all().filter('author =', author).order('-created'), page ) kochs = helpers.get_kochs_data(tmp_kochs) last_kochs = Koch.all().filter('author =', author).order('-created').fetch(5); last_from_all = Koch.get_random() current = "explore" self.response.out.write(template.render('templates/list_kochs.html', locals()))
def post(self): fan = db.get( self.request.get('fan') ) star = db.get( self.request.get('star') ) if fan and star: query = Friendship.all().filter('fan =', fan).filter('star =', star).fetch(1) if len( query ) == 1: follow = query[0] if follow: follow.unfollow() self.response.out.write( simplejson.dumps( { 'status' : 'success', 'message' : 'unfollow', 'star' : star.nickname, 'star_key' : self.request.get('star'), 'fan_key' : self.request.get('fan'), 'star_followers' : follow.star.followers } ) ) else: self.response.out.write( simplejson.dumps( { 'status' : 'error', 'message' : 'unfollow' } ) )
def friend_add(friend_id): if session.get('user'): new_friendship = Friendship(friend_id, session['user_id']) db.session.add(new_friendship) db.session.commit() return redirect(url_for('get_friends')) else: return redirect(url_for('login'))
def get(self, slug): user = User.is_logged(self) query = Koch.all().filter( 'slug =', slug).fetch(1) if len( query ): koch = query[0]; alreadylike = False alreadyfollow = False likesusers = [] koch.views += 1 koch.put() author = koch.author avatar = helpers.get_gravatar( author.email, 90 ) author_recipes_count = Koch.all().filter('author =', author).count(); for like in Like.all().filter( 'koch =', koch ): if like.user.fb_profile_url: lavatar = "https://graph.facebook.com/%s/picture" % (like.user.nickname) elif not like.user.usegravatar and like.user.avatar: lavatar = "/avatar/?user_id=%s" %(like.user.key()) else: lavatar = helpers.get_gravatar( like.user.email, 90 ) likesusers.append({ 'nickname' : like.user.nickname, 'avatar' : lavatar }) if user: alreadylike = Like.alreadylike( koch, user ) alreadyfollow = Friendship.alreadyfollow( user, author ) is_owner = True if user.key() == author.key() else False if author.fb_profile_url: avatar = "https://graph.facebook.com/%s/picture" % (author.nickname) elif not author.usegravatar and author.avatar: avatar = "/avatar/?user_id=%s" %(author.key()) else: avatar = helpers.get_gravatar( author.email, 90 ) last_kochs = Koch.all().filter('author =', author).order('-created').fetch(5); last_from_all = Koch.get_random() current = "explore" humanlikes = intcomma( int( koch.likes) ) self.response.out.write(template.render('templates/details_koch.html', locals())) else: self.error(404)
def get(self, star): user = User.is_logged(self) query = User.all().filter('nickname =', star.lower()).fetch(1) if len( query ) == 1: fan = query[0] title = "%s is following" % ( fan.nickname ) subhead = "Favorites cooks" page = self.request.get_range('page', min_value=0, max_value=1000, default=0) foll_tmp, next_page, prev_page = helpers.paginate( Friendship.all().filter('fan =', fan).order('-created'), page, 12 ) followers = helpers.get_following_data( foll_tmp ) last_from_all = Koch.get_random() self.response.out.write(template.render('templates/followers.html', locals())) else: self.error(404)
def get_following_data(entities): friends = [] user = User.is_logged() for friend in entities: if not friend.star.usegravatar and friend.star.avatar: avatar = "/avatar/?user_id=%s" %(friend.star.key()) else: avatar = get_gravatar( friend.star.email ) friendship = False if user: friendship = Friendship.alreadyfollow( user, friend.star ) friends.append({ 'fan': friend.star, 'avatar' : avatar, 'friend' : friendship }) return friends
def addFriendView(request): creator = request.user friend_name = request.GET.get('friend', '') action = request.GET.get('accepted', '') friend = dbUser.objects.get(username=friend_name) if (action == 'false'): Friendship.objects.filter(creator=friend, friend=creator).delete() return redirect('/friends/') if not Friendship.objects.filter(creator=creator, friend=friend): if Friendship.objects.filter(creator=friend, friend=creator).exists(): friendship1 = Friendship.objects.get(creator=friend, friend=creator) friendship1.status = 'accepted' friendship2 = Friendship(creator=creator, friend=friend, status='accepted') friendship1.save() notification1 = Notification( user=friend, user2=creator, notification_type="friend-accept", date=datetime.date.today(), time=datetime.datetime.now().strftime('%H:%M:%S'), notification="You are now friends with %s" % creator.username) notification2 = Notification( user=creator, user2=friend, notification_type="friend-accept", date=datetime.date.today(), time=datetime.datetime.now().strftime('%H:%M:%S'), notification="You are now friends with %s" % friend.username) notification1.save() notification2.save() else: friendship2 = Friendship(creator=creator, friend=friend) notification = Notification( user=friend, user2=creator, notification_type="friend-request", date=datetime.date.today(), time=datetime.datetime.now().strftime('%H:%M:%S'), notification="You have a friend request from %s" % creator.username) notification.save() friendship2.save() sent = True context_dict = {'sent': sent} return redirect('/friends/')
def add_facebook_friend(user_id1, user_id2): row1 = Friendship(user_id1=user_id1, user_id2=user_id2, is_facebook=True) row2 = Friendship(user_id1=user_id2, user_id2=user_id1, is_facebook=True) session.add(row1) session.add(row2) session.commit()
def add_friend(user_id1, user_id2): row1 = Friendship(user_id1=user_id1, user_id2=user_id2) row2 = Friendship(user_id1=user_id2, user_id2=user_id1) session.add(row1) session.add(row2) session.commit()
except Users.DoesNotExist: Users.create(pk=follower['pk'], full_name=str(follower['full_name']), has_anonymous_profile_picture=str( follower['has_anonymous_profile_picture']), is_private=str(follower['is_private']), is_verified=str(follower['is_verified']), profile_pic_url=str(follower['profile_pic_url']), username=str(follower['username']), insta_link='https://www.instagram.com/' + str(follower['username']) + '/') date = datetime.datetime.now() for item in Profile.select().order_by(Profile.id.desc()).limit(1): previous_followers = list(map(int, item.followers.split())) unfollow = set(previous_followers) - set(followers_ids) follow = set(followers_ids) - set(previous_followers) unfollow_len = len(unfollow) follow_len = len(follow) follow_str = " ".join(str(pk) for pk in follow) unfollow_str = " ".join(str(pk) for pk in follow) Friendship.create( date=date, followed=follow_str, followed_counter=follow_len, unfollowed=unfollow_str, unfollowed_counter=unfollow_len, ) followers_string = " ".join(str(pk) for pk in followers_ids) Profile.create(date=date, count=len(followers_ids), followers=followers_string)
def setUp(self): self.user1 = User.objects.get(pk=1) self.user2 = User.objects.get(pk=2) friendship = Friendship(from_user=self.user1, to_user=self.user2) friendship.save()
def setUp(self): super(TestFriendshipAcceptReject, self).setUp() users = User.query.all() friendship = Friendship(sender=users[0], receiver=users[1]) self.db.session.add(friendship) self.db.session.commit()