Exemplo n.º 1
0
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!'
Exemplo n.º 2
0
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!'
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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"))