form progress

This commit is contained in:
Xevion
2019-07-04 04:43:51 -05:00
parent 92a6f8ad49
commit d969cd5009
11 changed files with 91 additions and 24 deletions

View File

@@ -15,5 +15,6 @@ login.login_view = 'login'
db = SQLAlchemy(app)
migrate = Migrate(app, db)
from app import routes, models
from app import models
from app import routes, dashboard, custom
app.jinja_env.globals.update(get_hidden=routes.get_hidden)

14
app/custom.py Normal file
View File

@@ -0,0 +1,14 @@
from flask import abort
from flask_login import current_user
from functools import wraps
def require_role(roles=["User"]):
def wrap(func):
@wraps(func)
def decorated_view(*args, **kwargs):
if current_user.is_authenticated:
if current_user.has_roles(roles):
return func(*args, **kwargs)
return abort(401)
return decorated_view
return wrap

26
app/dashboard.py Normal file
View File

@@ -0,0 +1,26 @@
from app import app, db, login
from app.models import User, Search
from app.custom import require_role
from flask import render_template, redirect, url_for, request, jsonify
from flask_login import current_user, login_required
@app.route('/dashboard')
@login_required
def dashboard():
return render_template('/dashboard/dashboard.html')
@app.route('/dashboard/profile_settings')
@login_required
def profile_settings():
return render_template('/dashboard/profile_settings.html')
@app.route('/dashboard/constants')
@login_required
@require_role(roles=['Admin'])
def constants():
return render_template('/dashboard/constants.html')
@app.route('/dashboard/rbac')
@login_required
def rbac():
return render_template('/dashboard/rbac.html')

View File

@@ -24,4 +24,7 @@ class RegistrationForm(FlaskForm):
def validate_email(self, email):
user = User.query.filter_by(email=email.data).first()
if user is not None:
raise ValidationError('That email address is not available.')
raise ValidationError('That email address is not available.')
class ProfileSettingsForm(FlaskForm):
show_email = BooleanField('Show ')

View File

@@ -1,18 +1,9 @@
from flask import abort
from flask_login import UserMixin
from datetime import datetime
from app import db, login
from werkzeug.security import generate_password_hash, check_password_hash
def require_role(roles=["User"]):
def wrap(func):
@wraps(func)
def decorated_view(*args, **kwargs):
if current_user.is_authenticated:
if current_user.has_roles(roles):
return func(*args, **kwargs)
return abort(401)
return decorated_view
return wrap
@login.user_loader
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)

View File

@@ -1,10 +1,10 @@
from app import app, db, login
from app.models import User, Search, require_role
from app.models import User, Search
from app.forms import LoginForm, RegistrationForm
from app.custom import require_role
from werkzeug.urls import url_parse
from flask import render_template, redirect, url_for, flash, request, jsonify, abort
from flask_login import current_user, login_user, logout_user, login_required
from functools import wraps
import requests
import xmltodict
import base64
@@ -21,11 +21,6 @@ def strgen(length): return ''.join(random.choices(list(string.ascii_letters), k=
def unauthorized(e):
return redirect(url_for('login'))
@app.route('/dashboard')
@login_required
def dashboard():
return render_template('/dashboard/dashboard.html')
@app.route('/profile/')
@login_required
def profile():

View File

@@ -1,4 +1,4 @@
{% extends 'dashboard_base' %}
{% extends '/dashboard/dashboard_base.html' %}
{% set constants_active = True %}
{% block dashboard_body %}
constants

View File

@@ -1,4 +1,4 @@
{% extends 'dashboard/dashboard_base.html' %}
{% extends '/dashboard/dashboard_base.html' %}
{% set dashboard_home_active = True %}
{% block dashboard_body %}
<section></section>

View File

@@ -8,7 +8,7 @@
</p>
<ul class="menu-list">
<li><a href="{{ url_for('dashboard') }}" {% if dashboard_home_active %}class="is-active"{% endif %}>Home</a></li>
<li><a href="{{ url_for('profile_settings') }}" {% if dashboard_profile_settings_active %}class="is-active"{% endif %}>Profile Settings</a></li>
<li><a href="{{ url_for('profile_settings') }}" {% if profile_settings_active %}class="is-active"{% endif %}>Profile Settings</a></li>
</ul>
<ul class="menu-list">
@@ -19,7 +19,7 @@
<ul class="menu-list">
<li><a href="{{ url_for('constants') }}" {% if constants_active %}class="is-active"{% endif %}>Constants</a></li>
<li>
<a {% if role_settings_active %}class="is-active"{% endif %}><abbr title="Role Based Access Control">RBAC</abbr> Settings</a>
<a href="{{ url_for('rbac') }}" {% if role_settings_active %}class="is-active"{% endif %}>RBAC Settings</a>
<ul>
<li><a href="{{ url_for('rbac') + '#apply-user-roles' }}">Apply Roles to User(s)</a></li>
<li><a href="{{ url_for('rbac') + '#view-user-roles' }}">View all Roles</a></li>

View File

@@ -0,0 +1,37 @@
{% extends '/dashboard/dashboard_base.html' %}
{% set profile_settings_active = True %}
{% block head %}
{{ super() }}
<script>
$(document).ready(function () {
$('form').submit(function (e) {
var url = "{# { url_for('something') } #}"; // send the form data here.
$.ajax({
type: "POST",
url: url,
data: $('form').serialize(), // serializes the form's elements.
success: function (data) {
console.log(data) // display the returned data in the console.
}
});
e.preventDefault(); // block the traditional submission of the form.
});
// Inject our CSRF token into our AJAX request.
$.ajaxSetup({
beforeSend: function (xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", "{#{ form.csrf_token._value() }#}")
}
}
})
});
</script>
{% endblock head %}
{% block dashboard_body %}
<section>
<h1 class="title">Profile Settings</h1>
<span>
form goes here nibba
</span>
</section>
{% endblock dashboard_body %}

View File

@@ -1,4 +1,4 @@
{% extends 'dashboard_base.html' %}
{% extends '/dashboard/dashboard_base.html' %}
{% set role_settings_active = True %}
{% block dashboard_body %}