def comment(message_id): form = FeedPostForm() message = Message.getMessage(message_id) if not message: abort(404) if message and message.parent: abort(404) if form.validate_on_submit() and session.get('username'): # process post from_user = User.getByName(session.get('username')) post = form.post.data # process write comment_ = Message( from_user=from_user.id, text=post, parent=message_id, message_type=MESSAGE_TYPE.get(COMMENT), ) comment_.save_database() return redirect(url_for('feed_blueprint.comment', message_id=message_id)) return render_template('feed/message.html', message=message, form=form )
def message(message_id): form = FeedPostForm() message = None message = Message.objects.filter(id=message_id).first() if not message: abort(404) if message and message.parent: abort(404) if form.validate_on_submit() and session.get("username"): # Process post from_user = User.objects.get(username=session.get("username")) post = form.post.data # Write the message comment = Message( from_user=from_user, text=post, message_type=COMMENT, parent=message_id, ).save() return redirect(url_for("feed_app.message", message_id=message.id)) return render_template("feed/message.html", message=message, form=form)
def edit_message(message_id): message = None user = User.objects.filter(username=session.get("username")).first() message = Message.objects.filter(id=message_id).first() if not message: abort(404) if message and message.parent: abort(404) if user: form = FeedPostForm(obj=message) if form.validate_on_submit(): # Process post # from_user = User.objects.get(username=session.get("username")) post = form.post.data form.populate_obj(message) # Edit the comment comment = Message( from_user=user, text=post, message_type=COMMENT, parent=message_id, ).save() return redirect(url_for("feed_app.message", message_id=message.id)) return render_template("feed/message.html", message=message, form=form, action="edit") else: abort(404)
def message(message_id): form = FeedPostForm() message = None message = Message.objects.filter(id=message_id).first() if not message: abort(404) if message and message.parent: abort(404) if form.validate_on_submit() and session.get('username'): # process post from_user = User.objects.get(username=session.get('username')) post = form.post.data # write the message comment = Message( from_user=from_user, text=post, message_type=COMMENT, parent=message_id ).save() return redirect(url_for('feed_app.message', message_id=message.id)) return render_template('feed/message.html', message=message, form=form )
def add_message(): ref = request.referrer form = FeedPostForm() if form.validate_on_submit(): # process images post_images = [] uploaded_files = request.files.getlist('images') if uploaded_files and uploaded_files[0].filename != '': for file in uploaded_files: filename = secure_filename(file.filename) file_path = os.path.join(UPLOAD_FOLDER, 'posts', filename) file.save(file_path) post_images.append(file_path) # process post from_user = User.objects.get(username=session.get('username')) to_user = User.objects.get(username=request.values.get('to_user')) post = form.post.data # if this is a self post if to_user == from_user: to_user = None # write the message message = Message( from_user=from_user, to_user=to_user, text=post, message_type=POST, ).save() # store on the same user's feed feed = Feed( user=from_user, message=message, ).save() # store images if len(post_images): images = [] for file_path in post_images: (image_ts, width) = image_height_transform(file_path, 'posts', str(message.id)) images.append({"ts": str(image_ts), "w": str(width)}) message.images = images message.save() # process the message process_message(message) if ref: return redirect(ref) else: return redirect(url_for('home_app.home')) else: return 'Error!'
def add_message(): ref = request.referrer form = FeedPostForm() if form.validate_on_submit(): from_user = User.getByName(session.get('username')) from_user = from_user.id # process images post_images = [] uploaded_files = request.files.getlist('images') if uploaded_files and uploaded_files[0].filename != '': for file in uploaded_files: filename = secure_filename(file.filename) folder_path = os.path.join(Config.UPLOAD_FOLDER, 'posts_' + from_user) if not os.path.exists(folder_path): os.makedirs(folder_path) file_path = os.path.join(folder_path, filename) file.save(file_path) post_images.append(file_path) # process post to_user = User.getByName(request.values.get('to_user')) to_user = to_user.id post = form.post.data # if this is a self post if from_user == to_user: to_user = None message = Message( from_user=from_user, to_user=to_user, text=post, ) message.save_database() feed = Feed( user=from_user, message=message.id ) feed.save_database() # store images if len(post_images): images = [] for file_path in post_images: (image_ts, width) = image_height_transform(file_path, 'posts', str(message.id)) images.append({"ts": str(image_ts), "w": str(width)}) message.images = images message.update_record() # process the message process_message(message) if ref: return redirect(ref) else: return redirect(url_for('user_blueprint.home')) else: return abort(404)
def add_message(): ref = request.referrer form = FeedPostForm() if form.validate_on_submit(): # process images post_images = [] uploaded_files = request.files.getlist('images') if uploaded_files and uploaded_files[0].filename != '': for file in uploaded_files: filename = secure_filename(file.filename) file_path = os.path.join(UPLOAD_FOLDER, 'posts', filename) file.save(file_path) post_images.append(file_path) # process post from_user = User.objects.get(username=session.get('username')) to_user = User.objects.get(username=request.values.get('to_user')) post = form.post.data # if this is a self post if to_user == from_user: to_user = None # write the message message = Message( from_user=from_user, to_user=to_user, text=post, message_type=POST, ).save() # store on the same user's feed feed = Feed( user=from_user, message=message, ).save() # store images if len(post_images): images = [] for file_path in post_images: (image_ts, width) = image_height_transform(file_path, 'posts', str(message.id)) images.append({"ts": str(image_ts), "w": str(width)}) message.images = images message.save() # process the message process_message(message) if ref: return redirect(ref) else: return redirect(url_for('home_app.home')) else: return 'Error!'
def message(message_id): form = FeedPostForm() message = None message = Message.objects.filter(id=message_id).first() if not message: abort(404, '82 message not found') # if message and message.parent: # abort(404,'82 message==parent') if form.validate_on_submit() and session.get('username'): # process post from_user = User.objects.get(username=session.get('username')) post = form.post.data # write the message comment = Message(from_user=from_user, text=post, message_type=COMMENT, parent=message_id).save() return redirect(url_for('feed_app.message', message_id=message.id)) return render_template('feed/message.html', message=message, form=form) # @feed_app.route('/like/<message_id>', methods=('GET', 'POST')) # @login_required # def like_message(message_id): # message = None # # message = Message.objects.filter(id=message_id).first() # if not message: # abort(404) # # if message and message.parent: # abort(404) # # from_user = User.objects.get(username=session.get('username')) # check if first like # existing_like = Message.objects.filter( # parent=message_id, # message_type=LIKE, # from_user=from_user).count() # # if not existing_like: # # write the like # like = Message( # from_user=from_user, # to_user=message.from_user, # message_type=LIKE, # parent=message_id # ).save() return redirect(url_for('feed_app.message', message_id=message.id))
def home(feed_page_num=1): feed_messages_page = False if session.get("username"): form = FeedPostForm() user = User.objects.filter(username=session.get("username")).first() feed_messages_total = Feed.objects.filter(user=user).count() feed_messages = Feed.objects.filter(user=user).order_by("-create_date") if feed_messages_total > 10: feed_messages_page = True feed_messages = feed_messages.paginate(page=feed_page_num, per_page=10) else: feed_messages = feed_messages[:10] return render_template( "home/feed_home.html", user=user, form=form, feed_messages_total=feed_messages_total, feed_messages=feed_messages, feed_messages_page=feed_messages_page, feed_page_num=feed_page_num, ) else: return render_template("home/home.html")
def profile(username, friends_page_number=1): logged_user = None rel = None friends_page = False friends_per_page = 3 profile_messages = [] user = User.getByName(username) if user: if session['username']: logged_user = User.getByName(session['username']) rel = Relationship.get_relationship_status(logged_user.id, user.id) # get user friends friends_list = Relationship.get_friends( user=logged_user.id, rel_type=RELATIONSHIP_TYPE.get(FRIENDS), status=STATUS_TYPE.get(APPROVED) ) friends_total = len(friends_list) if 'friends' in request.url: friends_page = True # pagination limit = friends_per_page * friends_page_number offset = limit - friends_per_page if friends_total >= limit: friends = friends_list[offset:limit] else: friends = friends_list[offset:friends_total] else: if friends_total >= 5: friends = friends_list[:5] else: friends = friends_list form = FeedPostForm() if logged_user and (rel == "SAME" or rel == "FRIENDS_APPROVED"): profile_messages = Message.getMessages(logged_user.id) return render_template('user/profile.html', user=user, logged_user=logged_user, rel=rel, friends=friends, friends_total=friends_total, friends_page=friends_page, form=form, profile_messages=profile_messages ) else: abort(404)
def home(): if session.get('username'): form = FeedPostForm() user = User.getByName(session.get('username')) feed_messages = Feed.get_feed(user.id) return render_template('home/feed_home.html', user=user, form=form, feed_messages=feed_messages ) else: return render_template('home/home.html')
def profile(username, friends_page_number=1): # Removed everything with edit_profile variable because it isn't needed logged_user = None rel = None friends_page = False user = User.objects.filter(username=username).first() profile_messages = [] if user: if session.get("username"): logged_user = User.objects.filter(username=session.get("username")).first() rel=Relationship.get_relationship(logged_user, user) # Get friends friends = Relationship.objects.filter( from_user=user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED ) friends_total = friends.count() if "friends" in request.url: friends_page = True friends = friends.paginate(page=friends_page_number, per_page=3) # paginate is a mongoengine helper else: friends = friends[:5] form = FeedPostForm() # Get user messages if friends or self if logged_user and (rel == "SAME" or rel == "FRIENDS_APPROVED"): profile_messages = Message.objects.filter( Q(from_user=user) | Q(to_user=user), message_type=POST ).order_by("-create_date")[:10] return render_template("user/profile.html", user=user, rel=rel, logged_user=logged_user, friends=friends, friends_total=friends_total, friends_page=friends_page, form=form, profile_messages=profile_messages, ) else: # Don't find the user abort(404)
def home(): if session.get("username"): form = FeedPostForm() user = User.objects.filter(username=session.get("username")).first() feed_messages = Feed.objects.filter( user=user).order_by("-create_date")[:10] return render_template("home/feed_home.html", user=user, form=form, feed_messages=feed_messages) else: return render_template("home/home.html")
def profile(username, friends_page_number=1): logged_user = None edit_profile = False rel = None friends_page = False user = User.objects.filter(username=username).first() if user: if session.get('username'): logged_user = User.objects.filter( username=session.get('username')).first() rel = Relationship.get_relationship(logged_user, user) if session.get('username') and user.username == session.get( 'username'): edit_profile = True # get friends friends = Relationship.objects.filter(from_user=user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED) friends_total = friends.count() if 'friends' in request.url: friends_page = True friends = friends.paginate(page=friends_page_number, per_page=3) else: friends = friends[:5] form = FeedPostForm() # get user messages profile_messages = Message.objects.filter( Q(from_user=user) | Q(to_user=user)).order_by('-create_date')[:10] return render_template('user/profile.html', user=user, logged_user=logged_user, rel=rel, edit_profile=edit_profile, friends=friends, friends_total=friends_total, friends_page=friends_page, form=form, profile_messages=profile_messages, images=None) else: abort(404)
def home(): if session.get('username'): form = FeedPostForm() user = User.objects.filter(username=session.get('username')).first() # get user messages feed_messages = Feed.objects.filter( user=user).order_by('-create_date')[:10] return render_template('home/feed_home.html', user=user, form=form, feed_messages=feed_messages) else: return render_template('home/home.html')
def profile(username, friends_page_number=1): logged_user = None rel = None friends_page = None user = User.objects.filter(username=username).first() profile_messages = [] if user: if session.get('username'): logged_user = User.objects.filter( username=session.get('username')).first() rel = Relationship.get_relationship(logged_user, user) # Get friends friends = Relationship.objects.filter( from_user=user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED) friends_total = friends.count() if 'friends' in request.url: friends_page = True friends = friends.paginate(page=page, per_page=3) else: friends = friends[:5] form = FeedPostForm() # Get user messages if friends or self if logged_user and (rel == "SAME" or rel == "FRIENDS_APPROVED"): profile_messages = Message.objects.filter( Q(from_user=user) | Q(to_user=user), message_type=POST).order_by('-create_date')[:10] return render_template('user/profile.html', user=user, logged_user=logged_user, rel=rel, friends=friends, friends_total=friends_total, friends_page=friends_page, form=form, profile_messages=profile_messages) else: abort(404)
def profile(username): logged_user = None edit_profile = False rel = None user = User.objects.filter(username=username).first() profile_messages = [] if user: if session.get('username'): # compare relationship between user being looked at and user that is logged in logged_user = User.objects.filter( username=session.get('username')).first() rel = Relationship.get_relationship(logged_user, user) if user.username == session.get( 'username'): #if user is looking at his own profile page edit_profile = True #get friends friends = Relationship.objects.filter(from_user=user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED) friends_total = friends.count() if 'friends' in request.url: friends_page = True friends = friends.paginate(page=page, per_page=3) else: friends = friends[:5] form = FeedPostForm() return render_template('user/profile.html', user=user, logged_user=logged_user, rel=rel, edit_profile=edit_profile, friends=friends, friends_total=friends_total, form=form) else: abort(404)
def add_message(): ref = request.referrer form = FeedPostForm() # if form.validate_on_submit() or request.method == "POST": if form.validate_on_submit(): # For this to work, have to include text when uploading a picture # It's because post in forms.py has DataRequired() as a validator # At least I think that's why # Process images post_images = [] uploaded_files = request.files.getlist("images") if uploaded_files and uploaded_files[0].filename != "": for file in uploaded_files: filename = secure_filename(file.filename) file_path = os.path.join(UPLOAD_FOLDER, "posts", filename) file.save(file_path) post_images.append(file_path) # Process post from_user = User.objects.get(username=session.get("username")) to_user = User.objects.get(username=request.values.get("to_user")) post = form.post.data # If this a self post if to_user == from_user: to_user = None # Write the message to the database message = Message( from_user=from_user, to_user=to_user, text=post, message_type=POST, ).save() # Store on the same user's feed feed = Feed( user=from_user, message=message, ).save() # Store images if len(post_images): images = [] for file_path in post_images: (image_ts, width) = image_height_transform(file_path, "posts", str(message.id)) images.append({"ts": str(image_ts), "w": str(width)}) message.images = images message.save() # Process the message process_message(message) if ref: return redirect(ref) else: return redirect(url_for("home_app.home"))