Fixed editing service

This commit is contained in:
2025-04-16 15:17:03 +02:00
parent 7fc3cc4de8
commit 98dc56beeb
3 changed files with 16 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
from flask_wtf import FlaskForm # type: ignore from flask_wtf import FlaskForm # type: ignore
from wtforms import StringField, SubmitField, URLField from wtforms import StringField, SubmitField, URLField
from wtforms.validators import DataRequired from wtforms.validators import DataRequired
from flask_wtf.file import FileField, FileAllowed, FileRequired # type: ignore from flask_wtf.file import FileField, FileAllowed # type: ignore
class ServiceForm(FlaskForm): class ServiceForm(FlaskForm):
@@ -10,7 +10,6 @@ class ServiceForm(FlaskForm):
image = FileField( image = FileField(
"Icon:", "Icon:",
validators=[ validators=[
FileRequired(),
FileAllowed(["jpg", "jpeg", "png"], "Unsupported file format"), FileAllowed(["jpg", "jpeg", "png"], "Unsupported file format"),
], ],
) )

View File

@@ -5,7 +5,7 @@ class Service(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False)
url = db.Column(db.String, nullable=False) url = db.Column(db.String, nullable=False)
icon = db.Column(db.String, nullable=False, default="google.png") icon = db.Column(db.String, default="google.png")
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)

View File

@@ -42,20 +42,23 @@ def service():
image = service_form.image.data image = service_form.image.data
name = service_form.name.data name = service_form.name.data
url = service_form.url.data url = service_form.url.data
filename = secure_filename(image.filename) filename2 = "google.png"
save_path = os.path.join( if image:
app.config["UPLOAD_FOLDER"], # type: ignore filename = secure_filename(image.filename)
str(current_user.id), save_path = os.path.join(
filename, app.config["UPLOAD_FOLDER"], # type: ignore
) str(current_user.id),
os.makedirs(os.path.dirname(save_path), exist_ok=True) filename,
image.save(save_path) # type: ignore )
os.makedirs(os.path.dirname(save_path), exist_ok=True)
image.save(save_path) # type: ignore
filename2 = str(current_user.id) + "/" + filename
new_service = Service( new_service = Service(
name=name, # type: ignore name=name, # type: ignore
url=url, # type: ignore url=url, # type: ignore
user_id=current_user.id, user_id=current_user.id,
icon=str(current_user.id) + "/" + filename, icon=filename2,
) # type: ignore ) # type: ignore
db.session.add(new_service) db.session.add(new_service)
db.session.commit() db.session.commit()
@@ -82,7 +85,9 @@ def edit_service(service_id: int):
# Correcte gebruiker # Correcte gebruiker
form = ServiceForm() form = ServiceForm()
print("test")
if form.validate_on_submit(): # type: ignore if form.validate_on_submit(): # type: ignore
print("test2")
if service.name != form.name.data or service.url != form.url.data: if service.name != form.name.data or service.url != form.url.data:
service.name = form.name.data service.name = form.name.data
service.url = form.url.data service.url = form.url.data