My code:
from flask import Flask, render_template, url_for
from flask import request, flash, redirect, session, abort
from flask_login import login_user, logout_user, login_required, current_user, UserMixin
from werkzeug.security import check_password_hash, generate_password_hash
from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask_login import LoginManager, UserMixin
import os
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///nexus.db'
nexusdb = SQLAlchemy(app)
lm = LoginManager(app)
mem0_dir = os.environ.get("MEM0_DIR", "/tmp/.mem0")
class Users (nexusdb.Model, UserMixin):
__tablename__ = 'users'
id = nexusdb.Column(nexusdb.Integer, primary_key=True)
login = nexusdb.Column(nexusdb.String(128), nullable=False)
password = nexusdb.Column(nexusdb.String(255), nullable=False)
description = nexusdb.Column(nexusdb.String(255), default="Нет описания...")
is_admin = nexusdb.Column(nexusdb.String(18), default="FALSE")
class Posts (nexusdb.Model):
__tablename__ = 'posts'
id = nexusdb.Column(nexusdb.Integer, primary_key=True)
user_name = nexusdb.Column(nexusdb.Integer, nexusdb.ForeignKey('users.id'))
text = nexusdb.Column(nexusdb.Text, nullable=False)
name = nexusdb.Column(nexusdb.String(64), nullable=False)
board = nexusdb.Column(nexusdb.String(64), nullable=False)
class Comments (nexusdb.Model):
__tablename__ = 'comments'
id = nexusdb.Column(nexusdb.Integer, primary_key=True)
user_name = nexusdb.Column(nexusdb.Integer, nexusdb.ForeignKey('users.id'))
post_id = nexusdb.Column(nexusdb.Integer, nexusdb.ForeignKey('posts.id'))
text = nexusdb.Column(nexusdb.Text, nullable=False)
def CREATE_ALL():
with app.app_context():
nexusdb.create_all()
admin = Admin(app, name="Админ-панель")
app.secret_key = "salty_web3418"
@lm.user_loader
def load_user(user_id):
return Users.query.get(user_id)
@app.route("/")
def homeRedirect():
return redirect("/home")
@app.route("/home")
def home():
l = current_user.is_authenticated
p = Posts.query.limit(3).all()
return render_template("home.html", loginned=l, posts = p)
@app.route("/boards")
def boards():
l = current_user.is_authenticated
return render_template("boards.html", loginned=l)
@app.route("/boards/random", methods=['GET', 'POST'])
@login_required
def boards_random():
if request.method == "POST":
postname = request.form.get("RandomPostName")
posttext = request.form.get("RandomPostText")
if not postname or not posttext:
print("ERROR: Имя или текст поста неправельны!")
post = Posts(name = postname, text = posttext, board = "random", user_name = current_user.login)
nexusdb.session.add(post)
nexusdb.session.commit()
return redirect("/boards/random")
else:
posts = Posts.query.filter_by(board = "random").order_by(Posts.name).all()
return render_template("random-b.html", posts = posts)
@app.route("/boards/clean", methods=['GET', 'POST'])
@login_required
def boards_clean():
if request.method == "POST":
postname = request.form.get("CleanPostName")
posttext = request.form.get("CleanPostText")
if not postname or not posttext:
print("ERROR: Имя или текст поста неправельны!")
post = Posts(name = postname, text = posttext, board = "clean", user_name = current_user.login)
nexusdb.session.add(post)
nexusdb.session.commit()
return redirect("/boards/clean")
else:
posts = Posts.query.filter_by(board = "clean").order_by(Posts.name).all()
return render_template("clean-b.html", posts = posts)
@app.route("/boards/creative", methods=['GET', 'POST'])
@login_required
def boards_creative():
if request.method == "POST":
postname = request.form.get("CreativePostName")
posttext = request.form.get("CreativePostText")
if not postname or not posttext:
print("ERROR: Имя или текст поста неправельны!")
post = Posts(name = postname, text = posttext, board = "creative", user_name = current_user.login)
nexusdb.session.add(post)
nexusdb.session.commit()
return redirect("/boards/creative")
else:
posts = Posts.query.filter_by(board = "creative").order_by(Posts.name).all()
return render_template("creative-b.html", posts = posts)
@app.route("/user", methods=['GET', 'POST'])
@login_required
def user():
user = current_user
if request.method == 'POST':
newlogin = request.form.get("userName")
newdes = request.form.get("userDes")
user.login = newlogin
user.description = newdes
nexusdb.session.commit()
newuser = current_user
return render_template("user.html", u=newuser)
return render_template("user.html", u=user)
@app.route("/login", methods=['GET', 'POST'])
def login():
login = request.form.get("loginName")
password = request.form.get("loginPassword")
if request.method == "POST":
if login and password:
user = Users.query.filter_by(login=login).first()
if user and check_password_hash(user.password, password):
login_user(user)
return redirect("/home")
else:
print("ERROR: Логин или пароль неправельны!")
else:
print("ERROR: Логин и пароль неправельны!")
return render_template("login.html")
@app.route('/register', methods=['GET', 'POST'])
def register():
login = request.form.get("regName")
password = request.form.get("regPassword")
password_retype = request.form.get("passRepeat")
if request.method == "POST":
if not (login or password or password_retype):
print("ERROR: Логин, пароль или его повторение неправельны!")
elif password != password_retype:
print("ERROR: Пароль и его повторение должны быть одинаковы!")
else:
hash_pwd = generate_password_hash(password)
new_user = Users(login = login, password = hash_pwd)
nexusdb.session.add(new_user)
nexusdb.session.commit()
return redirect(url_for('login')) # В url_for имя функции
return render_template("register.html")
@app.route('/logout', methods=['GET', 'POST'])
@login_required
def logout():
logout_user()
return redirect("/")
@app.route('/boards/post/<int:postid>', methods=['GET', 'POST'])
@login_required
def post_detail(postid):
if request.method == "POST":
text = request.form.get("CommentText")
user = current_user.login
if text and user:
comment = Comments(user_name = user, text=text, post_id = postid)
nexusdb.session.add(comment)
nexusdb.session.commit()
return redirect(f"/boards/post/{postid}")
post = Posts.query.filter_by(id = postid).one()
comments = Comments.query.filter_by(post_id = post.id).all()
return render_template("post-detail.html", post = post, coms = comments)
@app.errorhandler(401)
def login_regirect(p):
return redirect("/login")
@app.before_request
def before_request():
if request.full_path.startswith('/admin/'):
if current_user.is_admin == "FALSE":
abort(400, 'Отказанно в доступе...')
admin.add_view(ModelView(Users, nexusdb.session, name="Пользователи"))
admin.add_view(ModelView(Posts, nexusdb.session, name="Посты"))
admin.add_view(ModelView(Comments, nexusdb.session, name="Комментарии"))
Error Message
I get the following error when deploying to Vercel:
OSError: [Errno 30] Read-only file system: ‘/var/task/instance’
Error importing api/app.py:
Traceback (most recent call last):
File “/var/task/_vendor/vercel_runtime/vc_init.py”, line 303, in
__vc_spec.loader.exec_module(__vc_module)
File “<frozen importlib._bootstrap_external>”, line 999, in exec_module
File “<frozen importlib._bootstrap>”, line 488, in _call_with_frames_removed
File “/var/task/api/app.py”, line 16, in
nexusdb = SQLAlchemy(app)
^^^^^^^^^^^^^^^
File “/var/task/_vendor/flask_sqlalchemy/extension.py”, line 278, in __init__
self.init_app(app)
File “/var/task/_vendor/flask_sqlalchemy/extension.py”, line 373, in init_app
self._apply_driver_defaults(options, app)
File “/var/task/_vendor/flask_sqlalchemy/extension.py”, line 627, in _apply_driver_defaults
os.makedirs(app.instance_path, exist_ok=True)
File “”, line 225, in makedirs
OSError: [Errno 30] Read-only file system: ‘/var/task/instance’
Python process exited with exit status: 1.
Help me pls