Skip to content
Snippets Groups Projects
Commit a8961d42 authored by Alex's avatar Alex
Browse files

Fix CRUD on first deployment

parent d7782082
No related branches found
No related tags found
No related merge requests found
......@@ -10,27 +10,10 @@ app.config.from_object(get_app_config())
db.init_app(app)
# uri = os.getenv("DATABASE_URL") # or other relevant config var
# print(uri)
# # if uri and uri.startswith("postgres://"):
# # uri = uri.replace("postgres://", "postgresql://", 1)
# # app.config["SQLALCHEMY_DATABASE_URI"] = uri
# # db = SQLAlchemy(app)
# # db.create_all()
# # if db.session.query(User).filter_by(username="testuser").first() is not None:
# # db.session.add(User(username='testuser', email='admin@example.com'))
# # db.session.commit()
# # if db.session.query(Entity).filter_by(username="my entity").first() is not None:
# # db.session.add(Entity(username='my entity', email='entity@example.com'))
# # db.session.commit()
# # else:
# # print("No database created/linked with this application")
# TODO Find a workaround for migrations/db.create_all()
with app.app_context():
from models.entity import Entity
db.create_all()
# Serve all static assets for the frontend
......
......@@ -13,7 +13,6 @@ def login():
# Handle post request
username = request.form.get('username')
password = request.form.get('password')
print(f'Got username={username} and password={password}')
if username and password:
try:
r = ldap_login(username, password)
......
from flask import Blueprint, render_template, request, redirect
from config import url_for2
from database.db import db
from models.user import Entity
from models.entity import Entity
home_blueprint = Blueprint('home', __name__, url_prefix='/')
......@@ -26,14 +26,14 @@ def entities():
return rows
except:
return "App database error (have you setup a database for this app?)"
return "App database error"
else:
username = request.form.get('username')
email = request.form.get('email')
name = request.form.get('name')
age = request.form.get('age')
try:
db.session.add(Entity(username=username, email=email))
db.session.add(Entity(name=name, age=int(age)))
db.session.commit()
return redirect(url_for2('.entities'))
except:
return "Could not add entity, username and/or email are already in use."
return "Could not add entity."
......@@ -2,6 +2,7 @@ import os
# This should match exactly the name of the app you specified
APP_NAME = "MY_APP_NAME_HERE"
ENV = os.environ.get('ENV', 'prod').lower()
URL_PREFIX = f"/{APP_NAME}" if ENV == 'prod' else ""
......
from database.db import db
# class User(db.Model):
# id = db.Column(db.Integer, primary_key=True)
# username = db.Column(db.String(80), unique=True, nullable=False)
# email = db.Column(db.String(120), unique=True, nullable=False)
# def __repr__(self):
# return f'User({self.id}, {self.username}, {self.email})'
class Entity(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
age = db.Column(db.Integer, unique=False, nullable=False)
def __repr__(self):
return f'Entity({self.id}, {self.username}, {self.email})'
\ No newline at end of file
......@@ -22,17 +22,17 @@
Flask Template is a simple web app programmed in Python-3 using flask micro-framework. It is created for begginers to understand the basics of creating a flask web app and deploying it on the Heroku. It can also be used as a template to create your new flask web apps.
</p><br>
Example CRUD form:
<h3>Example CRUD form - Create a record in the database:</h3>
<form method="post" action="{{ url('home.entities') }}">
Username:<br>
<input type="text" name="username">
Name:<br>
<input type="text" name="name">
<br>
Email:<br>
<input type="email" name="email">
Age:<br>
<input type="number" name="age">
<br>
<button type="submit">Create Entry</button>
</form>
<br>
<a href="{{ url('home.entities') }}">View entities</a>
<a href="{{ url('home.entities') }}">View records</a>
</body>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment