Advertisement
IrinaPenzina

Project index

Nov 22nd, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. basedir = os.path.abspath(os.path.dirname(__file__))
  2. UPLOAD_FOLDER = '/static/images'
  3. ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
  4.  
  5.  
  6. app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  7.  
  8. @app.route('/', methods=['GET', 'POST'])
  9. @login_required
  10. def index():
  11.     if request.method == "POST":
  12.  
  13.         # storing pictures
  14.         files = request.files.getlist("img")
  15.  
  16.         if not files:
  17.             return apology("No files")
  18.  
  19.         # if user does not select file, browser also
  20.         # submit an empty part without filename
  21.  
  22.         for file in files:
  23.             if allowed_file(file.filename):
  24.                 filename = secure_filename(file.filename)
  25.                 img_path=file.save(os.path.join(basedir, app.config['UPLOAD_FOLDER'], filename))
  26.                 # insert into the database
  27.                 db.execute("INSERT INTO images (id, img_path) VALUES (:id, :img_path)", id=session["user_id"],
  28.                 img_path=img_path)
  29.             else:
  30.                 return apology("Give me some images!")
  31.  
  32.         # storing text storing text
  33.         text = request.form.get("Inspirational text")
  34.         if text:
  35.             if len(text) > 15:
  36.                 return apology("Message has to be less then 15 characters.")
  37.                 #db.execute("INSERT INTO users (id, text) VALUES(:id, :text)", id=session["user_id"], text = text)
  38.         else:
  39.             return apology("Say something inspiring!")
  40.  
  41.         db.execute("UPDATE users SET text = :text WHERE id = :id", id=session["user_id"], text = text)
  42.  
  43.         user_images = db.execute("SELECT img_path FROM images WHERE id = :id", id=session["user_id"])
  44.         user_texts = db.execute("SELECT text FROM users WHERE id = :id", id =session["user_id"])
  45.  
  46.         return render_template("show.html", user_images = user_images, user_texts = user_texts)
  47.  
  48.     else:
  49.         return render_template('index.html')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement