mirror of
https://github.com/Xevion/v1.xevion.dev.git
synced 2025-12-08 06:08:53 -06:00
csrf jquery ajax testing part 1
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from app import app, db, login
|
from app import app, db, login
|
||||||
|
from app.forms import ProfileSettingsForm
|
||||||
from app.models import User, Search
|
from app.models import User, Search
|
||||||
from app.custom import require_role
|
from app.custom import require_role
|
||||||
from flask import render_template, redirect, url_for, request, jsonify
|
from flask import render_template, redirect, url_for, request, jsonify
|
||||||
@@ -9,11 +10,19 @@ from flask_login import current_user, login_required
|
|||||||
def dashboard():
|
def dashboard():
|
||||||
return render_template('/dashboard/dashboard.html')
|
return render_template('/dashboard/dashboard.html')
|
||||||
|
|
||||||
@app.route('/dashboard/profile_settings')
|
@app.route('/dashboard/profile_settings', methods=['GET'])
|
||||||
@login_required
|
@login_required
|
||||||
def profile_settings():
|
def profile_settings():
|
||||||
return render_template('/dashboard/profile_settings.html')
|
form = ProfileSettingsForm()
|
||||||
|
return render_template('/dashboard/profile_settings.html', form=form)
|
||||||
|
|
||||||
|
@app.route('/dashboard/profile_settings/submit', methods=['POST'])
|
||||||
|
@login_required
|
||||||
|
def profile_settings_submit():
|
||||||
|
form = ProfileSettingsForm()
|
||||||
|
if form.validate_on_submit():
|
||||||
|
return jsonify(data={'message' : 'hello {}'.format(form.show_email.data)})
|
||||||
|
return '$'
|
||||||
@app.route('/dashboard/constants')
|
@app.route('/dashboard/constants')
|
||||||
@login_required
|
@login_required
|
||||||
@require_role(roles=['Admin'])
|
@require_role(roles=['Admin'])
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from wtforms import StringField, PasswordField, BooleanField, SubmitField
|
from wtforms import StringField, PasswordField, BooleanField, SubmitField, RadioField
|
||||||
from wtforms.validators import ValidationError, DataRequired, EqualTo, Email
|
from wtforms.validators import ValidationError, DataRequired, EqualTo, Email
|
||||||
from app.models import User
|
from app.models import User
|
||||||
|
|
||||||
@@ -27,4 +27,5 @@ class RegistrationForm(FlaskForm):
|
|||||||
raise ValidationError('That email address is not available.')
|
raise ValidationError('That email address is not available.')
|
||||||
|
|
||||||
class ProfileSettingsForm(FlaskForm):
|
class ProfileSettingsForm(FlaskForm):
|
||||||
show_email = BooleanField('Show ')
|
show_email = RadioField('Show Email', choices=[('p', 'Public'), ('r', 'Registered Users Only'), ('h', 'Hidden')])
|
||||||
|
submit = SubmitField('Save Profile Settings')
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
<script>
|
<script>
|
||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
$('form').submit(function (e) {
|
$('form').submit(function (e) {
|
||||||
var url = "{# { url_for('something') } #}"; // send the form data here.
|
var url = "{{ url_for('profile_settings_submit') }}"; // send the form data here.
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: "POST",
|
type: "POST",
|
||||||
url: url,
|
url: url,
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
$.ajaxSetup({
|
$.ajaxSetup({
|
||||||
beforeSend: function (xhr, settings) {
|
beforeSend: function (xhr, settings) {
|
||||||
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
|
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
|
||||||
xhr.setRequestHeader("X-CSRFToken", "{#{ form.csrf_token._value() }#}")
|
xhr.setRequestHeader("X-CSRFToken", "{{ form.csrf_token._value() }}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -30,8 +30,13 @@
|
|||||||
{% block dashboard_body %}
|
{% block dashboard_body %}
|
||||||
<section>
|
<section>
|
||||||
<h1 class="title">Profile Settings</h1>
|
<h1 class="title">Profile Settings</h1>
|
||||||
<span>
|
<form action="" method="POST" novalidate>
|
||||||
form goes here nibba
|
{{ form.hidden_tag() }}
|
||||||
</span>
|
<div class="field">
|
||||||
|
{{ form.show_email.label }}
|
||||||
|
{{ form.show_email(class="_input") }}
|
||||||
|
</div>
|
||||||
|
{{ form.submit }}
|
||||||
|
</form>
|
||||||
</section>
|
</section>
|
||||||
{% endblock dashboard_body %}
|
{% endblock dashboard_body %}
|
||||||
Reference in New Issue
Block a user