stuffz
This commit is contained in:
parent
d81118260f
commit
f9df856f78
295
solo-base/app.py
295
solo-base/app.py
@ -0,0 +1,295 @@
|
||||
from flask import Flask, render_template, redirect, url_for, request, flash, jsonify
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
import os
|
||||
import json
|
||||
from sqlalchemy import Column, Integer, String, Float, Boolean
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = 'your-secret-key'
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
|
||||
db = SQLAlchemy(app)
|
||||
login_manager = LoginManager(app)
|
||||
login_manager.login_view = 'login'
|
||||
|
||||
Base = declarative_base()
|
||||
dynamic_models = {}
|
||||
|
||||
class User(UserMixin, db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(150), unique=True, nullable=False)
|
||||
password_hash = db.Column(db.String(256), nullable=False)
|
||||
email = db.Column(db.String(150), unique=True, nullable=True)
|
||||
phone = db.Column(db.String(20))
|
||||
verified = db.Column(db.Boolean, default=False)
|
||||
is_admin = db.Column(db.Boolean, default=False)
|
||||
is_mod = db.Column(db.Boolean, default=False)
|
||||
is_premium = db.Column(db.Boolean, default=False)
|
||||
api_token = db.Column(db.String(256), unique=True, nullable=True)
|
||||
group = db.Column(db.String(50), default="default")
|
||||
|
||||
def set_password(self, password):
|
||||
self.password_hash = generate_password_hash(password)
|
||||
|
||||
def check_password(self, password):
|
||||
return check_password_hash(self.password_hash, password)
|
||||
|
||||
def generate_api_token(self):
|
||||
import secrets
|
||||
self.api_token = secrets.token_hex(32)
|
||||
|
||||
|
||||
@login_manager.user_loader
|
||||
def load_user(user_id):
|
||||
return User.query.get(int(user_id))
|
||||
|
||||
# Home route
|
||||
@app.route('/')
|
||||
def home():
|
||||
return redirect(url_for('dashboard'))
|
||||
|
||||
@app.route('/register', methods=['GET', 'POST'])
|
||||
def register():
|
||||
if request.method == 'POST':
|
||||
username = request.form['username']
|
||||
password = request.form['password']
|
||||
email = request.form['email']
|
||||
|
||||
# Check if the username already exists
|
||||
existing_user = User.query.filter_by(username=username).first()
|
||||
if existing_user:
|
||||
flash('Username already exists. Please choose a different one.')
|
||||
return redirect(url_for('register'))
|
||||
|
||||
hashed_password = generate_password_hash(password)
|
||||
is_admin = False
|
||||
if User.query.first() is None:
|
||||
is_admin = True
|
||||
new_user = User(username=username, password_hash=hashed_password, is_admin=is_admin, email=email)
|
||||
# Add and commit the new user to the database
|
||||
db.session.add(new_user)
|
||||
db.session.commit()
|
||||
flash('Registration successful. You can now log in.')
|
||||
return redirect(url_for('login'))
|
||||
return render_template('register.html')
|
||||
|
||||
# Login route
|
||||
@app.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
if request.method == 'POST':
|
||||
username = request.form['username']
|
||||
password = request.form['password']
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if user and user.check_password(password):
|
||||
login_user(user)
|
||||
return redirect(url_for('dashboard'))
|
||||
flash('Invalid username or password.')
|
||||
return render_template('login.html')
|
||||
|
||||
# Logout route
|
||||
@app.route('/logout')
|
||||
@login_required
|
||||
def logout():
|
||||
logout_user()
|
||||
return redirect(url_for('login'))
|
||||
|
||||
# Dashboard route
|
||||
@app.route('/dashboard')
|
||||
@login_required
|
||||
def dashboard():
|
||||
tables = list(dynamic_models.keys())
|
||||
return render_template('dashboard.html', tables=tables)
|
||||
|
||||
# Create table route
|
||||
@app.route('/create_table', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def create_table():
|
||||
if not current_user.is_admin:
|
||||
flash('Access denied.')
|
||||
return redirect(url_for('dashboard'))
|
||||
|
||||
if request.method == 'POST':
|
||||
table_name = request.form['table_name']
|
||||
columns_json = request.form['columns']
|
||||
try:
|
||||
columns_list = json.loads(columns_json)
|
||||
|
||||
if table_name in dynamic_models:
|
||||
flash('Table already exists.')
|
||||
return redirect(url_for('create_table'))
|
||||
|
||||
attrs = {
|
||||
'__tablename__': table_name,
|
||||
'id': Column(Integer, primary_key=True)
|
||||
}
|
||||
|
||||
for col in columns_list:
|
||||
col_name = col['name']
|
||||
col_type = col['type']
|
||||
|
||||
if col_type == 'text':
|
||||
attrs[col_name] = Column(String(255))
|
||||
elif col_type == 'number':
|
||||
attrs[col_name] = Column(Integer)
|
||||
elif col_type == 'boolean':
|
||||
attrs[col_name] = Column(Boolean)
|
||||
else:
|
||||
flash(f'Unsupported column type: {col_type}')
|
||||
return redirect(url_for('create_table'))
|
||||
|
||||
model = type(table_name.capitalize(), (Base,), attrs)
|
||||
dynamic_models[table_name] = model
|
||||
|
||||
model.__table__.create(bind=db.engine)
|
||||
flash(f'Table {table_name} created successfully.')
|
||||
return redirect(url_for('dashboard'))
|
||||
|
||||
except Exception as e:
|
||||
flash(f'Error creating table: {e}')
|
||||
return redirect(url_for('create_table'))
|
||||
|
||||
return render_template('create_table.html')
|
||||
|
||||
|
||||
# View table route
|
||||
@app.route('/view_table/<table_name>')
|
||||
@login_required
|
||||
def view_table(table_name):
|
||||
model = dynamic_models.get(table_name)
|
||||
if not model:
|
||||
flash('Table not found.')
|
||||
return redirect(url_for('dashboard'))
|
||||
conn = db.engine.connect()
|
||||
result = conn.execute(model.__table__.select())
|
||||
columns = result.keys()
|
||||
rows = result.fetchall()
|
||||
conn.close()
|
||||
return render_template('view_table.html', table_name=table_name, columns=columns, rows=rows)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## ! API
|
||||
|
||||
|
||||
@app.route('/api/register', methods=['POST'])
|
||||
def api_register():
|
||||
data = request.json
|
||||
username = data.get('username')
|
||||
password = data.get('password')
|
||||
email = data.get('email')
|
||||
|
||||
if not username or not password:
|
||||
return jsonify({'error': 'Username and password required.'}), 400
|
||||
|
||||
if User.query.filter_by(username=username).first():
|
||||
return jsonify({'error': 'Username already exists.'}), 400
|
||||
|
||||
user = User(username=username, email=email)
|
||||
user.set_password(password)
|
||||
user.generate_api_token()
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({'message': 'Registration successful.', 'api_token': user.api_token})
|
||||
|
||||
|
||||
|
||||
@app.route('/api/login', methods=['POST'])
|
||||
def api_login():
|
||||
data = request.json
|
||||
username = data.get('username')
|
||||
password = data.get('password')
|
||||
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if user and user.check_password(password):
|
||||
if not user.api_token:
|
||||
user.generate_api_token()
|
||||
db.session.commit()
|
||||
return jsonify({'api_token': user.api_token})
|
||||
else:
|
||||
return jsonify({'error': 'Invalid credentials'}), 401
|
||||
|
||||
|
||||
@app.route('/api/get_table/<table_name>', methods=['GET'])
|
||||
def api_get_table(table_name):
|
||||
token = request.headers.get('Authorization')
|
||||
if not token:
|
||||
return jsonify({'error': 'Missing API token.'}), 401
|
||||
|
||||
user = User.query.filter_by(api_token=token).first()
|
||||
if not user:
|
||||
return jsonify({'error': 'Invalid API token.'}), 401
|
||||
|
||||
model = dynamic_models.get(table_name)
|
||||
if not model:
|
||||
return jsonify({'error': 'Table not found.'}), 404
|
||||
|
||||
allowed_groups = table_permissions.get(table_name, {}).get('read', [])
|
||||
if user.group not in allowed_groups:
|
||||
return jsonify({'error': 'Access denied.'}), 403
|
||||
|
||||
conn = db.engine.connect()
|
||||
result = conn.execute(model.__table__.select())
|
||||
columns = result.keys()
|
||||
rows = [dict(zip(columns, row)) for row in result.fetchall()]
|
||||
conn.close()
|
||||
|
||||
return jsonify({'columns': columns, 'rows': rows})
|
||||
|
||||
|
||||
@app.route('/api/set_table/<table_name>', methods=['POST'])
|
||||
def api_set_table(table_name):
|
||||
token = request.headers.get('Authorization')
|
||||
if not token:
|
||||
return jsonify({'error': 'Missing API token.'}), 401
|
||||
|
||||
user = User.query.filter_by(api_token=token).first()
|
||||
if not user:
|
||||
return jsonify({'error': 'Invalid API token.'}), 401
|
||||
|
||||
model = dynamic_models.get(table_name)
|
||||
if not model:
|
||||
return jsonify({'error': 'Table not found.'}), 404
|
||||
|
||||
allowed_groups = table_permissions.get(table_name, {}).get('write', [])
|
||||
if user.group not in allowed_groups:
|
||||
return jsonify({'error': 'Access denied.'}), 403
|
||||
|
||||
data = request.json
|
||||
if not isinstance(data, dict):
|
||||
return jsonify({'error': 'Invalid data format.'}), 400
|
||||
|
||||
conn = db.engine.connect()
|
||||
try:
|
||||
conn.execute(model.__table__.insert(), [data])
|
||||
conn.close()
|
||||
return jsonify({'message': 'Insert successful.'})
|
||||
except Exception as e:
|
||||
conn.close()
|
||||
return jsonify({'error': str(e)}), 400
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
app.run(debug=True)
|
BIN
solo-base/instance/database.db
Normal file
BIN
solo-base/instance/database.db
Normal file
Binary file not shown.
0
solo-base/static/styles.css
Normal file
0
solo-base/static/styles.css
Normal file
57
solo-base/templates/base.html
Normal file
57
solo-base/templates/base.html
Normal file
@ -0,0 +1,57 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Solo Base | {% block title %}Welcome{% endblock %}</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="{{ url_for('dashboard') }}">Solo Base</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
|
||||
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav ms-auto">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ url_for('dashboard') }}">Dashboard</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ url_for('create_table') }}">Create Table</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ url_for('logout') }}">Logout</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<!-- Main Content -->
|
||||
<main class="container my-4">
|
||||
{% with messages = get_flashed_messages() %}
|
||||
{% if messages %}
|
||||
{% for message in messages %}
|
||||
<div class="alert alert-info alert-dismissible fade show" role="alert">
|
||||
{{ message }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
{% block content %}{% endblock %}
|
||||
</main>
|
||||
|
||||
<footer class="bg-light text-center py-3">
|
||||
<div class="container">
|
||||
<span class="text-muted">© 2025 Solo Base</span>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
||||
</body>
|
||||
</html>
|
77
solo-base/templates/create_table.html
Normal file
77
solo-base/templates/create_table.html
Normal file
@ -0,0 +1,77 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Create Table{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h2>Create New Table</h2>
|
||||
|
||||
<form method="post" onsubmit="return prepareFieldsJSON();">
|
||||
<label for="table_name">Table Name:</label>
|
||||
<input type="text" name="table_name" id="table_name" required>
|
||||
|
||||
<h3>Fields</h3>
|
||||
<div id="fields-container"></div>
|
||||
|
||||
<button type="button" onclick="addField()">➕ Add Field</button>
|
||||
|
||||
<!-- Hidden input that will store JSON -->
|
||||
<input type="hidden" name="columns" id="columns">
|
||||
|
||||
<br><br>
|
||||
<button type="submit">Create Table</button>
|
||||
</form>
|
||||
|
||||
<script>
|
||||
let fieldCounter = 0;
|
||||
|
||||
function addField() {
|
||||
const container = document.getElementById('fields-container');
|
||||
|
||||
const fieldDiv = document.createElement('div');
|
||||
fieldDiv.className = 'field-row';
|
||||
fieldDiv.style.marginBottom = '8px';
|
||||
|
||||
fieldDiv.innerHTML = `
|
||||
<input type="text" name="field_name_${fieldCounter}" placeholder="Field Name" required>
|
||||
<select name="field_type_${fieldCounter}" required>
|
||||
<option value="text">Text</option>
|
||||
<option value="number">Number</option>
|
||||
<option value="boolean">Boolean</option>
|
||||
</select>
|
||||
<button type="button" onclick="this.parentElement.remove()">🗑️</button>
|
||||
`;
|
||||
|
||||
container.appendChild(fieldDiv);
|
||||
fieldCounter++;
|
||||
}
|
||||
|
||||
function prepareFieldsJSON() {
|
||||
const fields = [];
|
||||
const container = document.getElementById('fields-container');
|
||||
const fieldRows = container.getElementsByClassName('field-row');
|
||||
|
||||
for (let row of fieldRows) {
|
||||
const inputs = row.querySelectorAll('input, select');
|
||||
const field = {
|
||||
name: inputs[0].value.trim(),
|
||||
type: inputs[1].value
|
||||
};
|
||||
if (!field.name) {
|
||||
alert('Field name cannot be empty.');
|
||||
return false;
|
||||
}
|
||||
fields.push(field);
|
||||
}
|
||||
|
||||
document.getElementById('columns').value = JSON.stringify(fields);
|
||||
return true; // Allow form submit
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.field-row input, .field-row select {
|
||||
margin-right: 8px;
|
||||
}
|
||||
</style>
|
||||
|
||||
{% endblock %}
|
25
solo-base/templates/dashboard.html
Normal file
25
solo-base/templates/dashboard.html
Normal file
@ -0,0 +1,25 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Dashboard{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h2>Welcome, {{ current_user.username }}!</h2>
|
||||
<p>Your roles:</p>
|
||||
<ul>
|
||||
{% if current_user.is_admin %}
|
||||
<li>Admin</li>
|
||||
{% endif %}
|
||||
{% if current_user.is_mod %}
|
||||
<li>Moderator</li>
|
||||
{% endif %}
|
||||
{% if current_user.is_premium %}
|
||||
<li>Premium User</li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
<h3>Available Tables</h3>
|
||||
<ul>
|
||||
{% for table in tables %}
|
||||
<li><a href="{{ url_for('view_table', table_name=table) }}">{{ table }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endblock %}
|
15
solo-base/templates/login.html
Normal file
15
solo-base/templates/login.html
Normal file
@ -0,0 +1,15 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Login{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h2>Login</h2>
|
||||
<form method="post">
|
||||
<label for="username">Username:</label>
|
||||
<input type="text" name="username" id="username" required>
|
||||
<label for="password">Password:</label>
|
||||
<input type="password" name="password" id="password" required>
|
||||
<button type="submit">Login</button>
|
||||
</form>
|
||||
<p>Don't have an account? <a href="{{ url_for('register') }}">Register here</a>.</p>
|
||||
{% endblock %}
|
19
solo-base/templates/register.html
Normal file
19
solo-base/templates/register.html
Normal file
@ -0,0 +1,19 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Register{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h2>Register</h2>
|
||||
<form method="post">
|
||||
<label for="username">Username:</label>
|
||||
<input type="text" name="username" id="username" required>
|
||||
<label for="email">Email:</label>
|
||||
<input type="email" name="email" id="email" required>
|
||||
<label for="phone">Phone:</label>
|
||||
<input type="tel" name="phone" id="phone">
|
||||
<label for="password">Password:</label>
|
||||
<input type="password" name="password" id="password" required>
|
||||
<button type="submit">Register</button>
|
||||
</form>
|
||||
<p>Already have an account? <a href="{{ url_for('login') }}">Login here</a>.</p>
|
||||
{% endblock %}
|
29
solo-base/templates/view_table.html
Normal file
29
solo-base/templates/view_table.html
Normal file
@ -0,0 +1,29 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}View Table{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h2 style="margin-bottom: 20px;">Table: {{ table_name }}</h2>
|
||||
|
||||
<div style="overflow-x: auto;">
|
||||
<table style="width: 100%; border-collapse: collapse; font-family: sans-serif; font-size: 14px;">
|
||||
<thead style="background-color: #f5f5f5;">
|
||||
<tr>
|
||||
{% for column in columns %}
|
||||
<th style="padding: 10px; border-bottom: 2px solid #ddd; text-align: left;">{{ column }}</th>
|
||||
{% endfor %}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for row in rows %}
|
||||
<tr style="border-bottom: 1px solid #eee;">
|
||||
{% for column in columns %}
|
||||
<td style="padding: 10px; border-bottom: 1px solid #eee;">{{ row[column] }}</td>
|
||||
{% endfor %}
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
Loading…
Reference in New Issue
Block a user