Fixed login with group functions
This commit is contained in:
@@ -94,6 +94,7 @@ def login():
|
||||
def register():
|
||||
if request.method == "POST":
|
||||
name = request.form["name"].strip()
|
||||
group = request.form["group"]
|
||||
password = request.form["password"]
|
||||
|
||||
existing_user = User.query.filter_by(name=name).first()
|
||||
@@ -101,7 +102,7 @@ def register():
|
||||
return render_template("register.html", error="That name is already registered.")
|
||||
|
||||
user = User(name=name)
|
||||
group_id = user.set_group_id(password)
|
||||
group_id = user.set_group_id(group)
|
||||
if group_id == 0:
|
||||
return render_template("register.html", error="Unknown registration key.")
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
from flask import Blueprint, render_template, request
|
||||
from models import db
|
||||
from models import Group
|
||||
from flask_login import login_required, current_user
|
||||
|
||||
groups_bp = Blueprint("groups", __name__)
|
||||
|
||||
@groups_bp.route("/groups", methods=["GET", "POST"])
|
||||
@login_required
|
||||
def groups():
|
||||
if not current_user.is_admin:
|
||||
return "Forbidden", 403
|
||||
error = ""
|
||||
|
||||
if request.method == "POST":
|
||||
secret = request.form["secret"].strip()
|
||||
if not secret:
|
||||
error = "Group secret is required."
|
||||
elif Group.query.filter_by(secret=secret).first():
|
||||
error = "That group already exists."
|
||||
else:
|
||||
group = Group(secret=secret)
|
||||
db.session.add(group)
|
||||
db.session.commit()
|
||||
|
||||
return render_template("groups.html",
|
||||
groups=Group.query.order_by(Group.id).all(),
|
||||
error=error)
|
||||
+7
-3
@@ -1,6 +1,6 @@
|
||||
from lapp import create_app
|
||||
import sqlalchemy as sa
|
||||
from models import db, User, ListOfItems, Shared, Item
|
||||
from models import db, User, ListOfItems, Shared, Item, Group
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = create_app()
|
||||
@@ -9,10 +9,14 @@ if __name__ == "__main__":
|
||||
users = db.session.scalars(query).all()
|
||||
print(users)
|
||||
if len(users) == 0:
|
||||
admin = User(name="admin", is_admin=True, is_approved=True, is_private=True)
|
||||
g = Group(secret="This is the group just for the admin")
|
||||
db.session.add(g)
|
||||
g = Group(secret="GroupForIgnaceAndLoversThatDoGroceries")
|
||||
db.session.add(g)
|
||||
admin = User(name="admin", group_id=1, is_admin=True, is_approved=True, is_private=True)
|
||||
admin.set_password("suy2025")
|
||||
db.session.add(admin)
|
||||
user1 = User(name="ignace", is_admin=False, is_approved=True)
|
||||
user1 = User(name="ignace", group_id=2, is_admin=False, is_approved=True)
|
||||
user1.set_password("xanderj2")
|
||||
db.session.add(user1)
|
||||
db.session.commit()
|
||||
|
||||
@@ -7,6 +7,7 @@ from admin import admin_bp
|
||||
from lists import lists_bp
|
||||
from items import items_bp
|
||||
from api import api_bp
|
||||
from groups import groups_bp
|
||||
|
||||
login_manager = LoginManager()
|
||||
|
||||
@@ -24,6 +25,7 @@ def create_app():
|
||||
app.register_blueprint(lists_bp)
|
||||
app.register_blueprint(items_bp)
|
||||
app.register_blueprint(api_bp)
|
||||
app.register_blueprint(groups_bp)
|
||||
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
from lapp import create_app
|
||||
from models import db
|
||||
from models import Group, Item, ListOfItems, Shared, User
|
||||
|
||||
|
||||
def print_section(title):
|
||||
print()
|
||||
print("=" * len(title))
|
||||
print(title)
|
||||
print("=" * len(title))
|
||||
|
||||
|
||||
def print_model_rows(model, columns):
|
||||
primary_key = next(iter(model.__table__.primary_key.columns))
|
||||
rows = model.query.order_by(primary_key).all()
|
||||
if not rows:
|
||||
print("(empty)")
|
||||
return
|
||||
|
||||
for row in rows:
|
||||
values = []
|
||||
for column in columns:
|
||||
values.append(f"{column}={getattr(row, column)!r}")
|
||||
print(", ".join(values))
|
||||
|
||||
|
||||
def main():
|
||||
app = create_app()
|
||||
with app.app_context():
|
||||
print(f"Database: {app.config['SQLALCHEMY_DATABASE_URI']}")
|
||||
|
||||
print_section("Users")
|
||||
print_model_rows(User, [
|
||||
"id",
|
||||
"updated_at",
|
||||
"group_id",
|
||||
"name",
|
||||
"password_hash",
|
||||
"is_admin",
|
||||
"is_approved",
|
||||
"is_private",
|
||||
"is_guest",
|
||||
])
|
||||
|
||||
print_section("Groups")
|
||||
print_model_rows(Group, [
|
||||
"id",
|
||||
"updated_at",
|
||||
"secret",
|
||||
])
|
||||
|
||||
print_section("Lists")
|
||||
print_model_rows(ListOfItems, [
|
||||
"id",
|
||||
"updated_at",
|
||||
"owner_user_id",
|
||||
"name",
|
||||
"is_active",
|
||||
])
|
||||
|
||||
print_section("Items")
|
||||
print_model_rows(Item, [
|
||||
"id",
|
||||
"updated_at",
|
||||
"listofitems_id",
|
||||
"label",
|
||||
"quantity",
|
||||
"unit",
|
||||
"label_alt1",
|
||||
"label_alt2",
|
||||
"is_checked",
|
||||
"is_suggestion",
|
||||
"category",
|
||||
])
|
||||
|
||||
print_section("Shares")
|
||||
print_model_rows(Shared, [
|
||||
"user_id",
|
||||
"listofitem_id",
|
||||
])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,27 @@
|
||||
{% extends "base.html" %}
|
||||
{% block content %}
|
||||
<h4 class="mb-4">Groups</h4>
|
||||
|
||||
{% if error %}
|
||||
<div class="alert alert-danger" role="alert">
|
||||
{{ error }}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<form method="post" class="mb-4">
|
||||
<div class="d-flex justify-content-between">
|
||||
<input class="form-control" name="secret" placeholder="Group secret" required>
|
||||
|
||||
<button class="btn btn-success">+</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<ul class="list-group">
|
||||
{% for group in groups %}
|
||||
<li class="list-group-item">
|
||||
<div class="fw-semibold">Group {{ group.id }}</div>
|
||||
<div class="text-muted small">{{ group.secret }}</div>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock %}
|
||||
@@ -8,6 +8,7 @@
|
||||
{% endif %}
|
||||
<form method="post">
|
||||
<input class="form-control mb-3" name="name" placeholder="New Name" required>
|
||||
<input class="form-control mb-3" name="group" type="password" placeholder="Existing Group" required>
|
||||
<input class="form-control mb-3" name="password" type="password" placeholder="New Password" required>
|
||||
<input
|
||||
type="image"
|
||||
|
||||
Reference in New Issue
Block a user