implement account modifying
This commit is contained in:
parent
d5edddba36
commit
1306eea0bd
@ -4,6 +4,7 @@ from werkzeug.security import generate_password_hash, check_password_hash
|
|||||||
from flask_login import login_required, current_user, login_manager
|
from flask_login import login_required, current_user, login_manager
|
||||||
from .models import User
|
from .models import User
|
||||||
from . import db
|
from . import db
|
||||||
|
import time
|
||||||
|
|
||||||
auth = Blueprint('auth', __name__)
|
auth = Blueprint('auth', __name__)
|
||||||
|
|
||||||
@ -68,8 +69,8 @@ def signup_post():
|
|||||||
|
|
||||||
# first user is always admin
|
# first user is always admin
|
||||||
if not User.query.count():
|
if not User.query.count():
|
||||||
new_user.role='admin'
|
new_user.role = 'admin'
|
||||||
new_user.isActivated=True
|
new_user.isActivated = True
|
||||||
# add the new user to the database
|
# add the new user to the database
|
||||||
db.session.add(new_user)
|
db.session.add(new_user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
@ -81,3 +82,44 @@ def logout():
|
|||||||
if current_user.is_authenticated:
|
if current_user.is_authenticated:
|
||||||
logout_user()
|
logout_user()
|
||||||
return redirect(url_for('main.index'))
|
return redirect(url_for('main.index'))
|
||||||
|
|
||||||
|
|
||||||
|
@auth.route('/manage', methods=['POST'])
|
||||||
|
@login_required
|
||||||
|
def manage_post():
|
||||||
|
if current_user.role == "admin":
|
||||||
|
method = request.form.get('method')
|
||||||
|
id = request.form.get('id')
|
||||||
|
email = request.form.get('email')
|
||||||
|
name = request.form.get('name')
|
||||||
|
role = request.form.get('role')
|
||||||
|
isActivated = True if request.form.get(
|
||||||
|
'isActivated') == "true" else False
|
||||||
|
if method == "update":
|
||||||
|
account = User.query.filter_by(
|
||||||
|
id=id, email=email, name=name).first()
|
||||||
|
if account:
|
||||||
|
print(account)
|
||||||
|
if db.session.query(User).filter(User.id == id).update({"role": role, "isActivated": isActivated}) and not db.session.commit():
|
||||||
|
time.sleep(0.05)
|
||||||
|
return "success"
|
||||||
|
else:
|
||||||
|
time.sleep(0.1)
|
||||||
|
return "fail db_commit"
|
||||||
|
time.sleep(1)
|
||||||
|
return "fail no account"
|
||||||
|
if method == "delete":
|
||||||
|
account = User.query.filter_by(
|
||||||
|
id=id, email=email, name=name, role=role, isActivated=isActivated).first()
|
||||||
|
if account:
|
||||||
|
if db.session.query(User).filter(User.id == id).delete() and not db.session.commit():
|
||||||
|
time.sleep(0.05)
|
||||||
|
return "success"
|
||||||
|
else:
|
||||||
|
time.sleep(0.2)
|
||||||
|
return "fail db_commit"
|
||||||
|
time.sleep(1)
|
||||||
|
return "fail no account"
|
||||||
|
|
||||||
|
flash("您无权管理其他账户")
|
||||||
|
return redirect(url_for('main.index'))
|
||||||
|
@ -37,16 +37,6 @@ def manage():
|
|||||||
return redirect(url_for('main.index'))
|
return redirect(url_for('main.index'))
|
||||||
|
|
||||||
|
|
||||||
@main.route('/manage', methods=['POST'])
|
|
||||||
@login_required
|
|
||||||
def manage_post():
|
|
||||||
if current_user.role == "admin":
|
|
||||||
return "manage for account id "+str(request.args.get('id'))
|
|
||||||
|
|
||||||
flash("您无权管理其他账户")
|
|
||||||
return redirect(url_for('main.index'))
|
|
||||||
|
|
||||||
|
|
||||||
@main.route('/chat')
|
@main.route('/chat')
|
||||||
@login_required
|
@login_required
|
||||||
def chat():
|
def chat():
|
||||||
|
@ -30,8 +30,8 @@
|
|||||||
</select>
|
</select>
|
||||||
</td>
|
</td>
|
||||||
<td><input type="checkbox" {% if account.isActivated %}checked{% else %}unchecked{% endif %}></td>
|
<td><input type="checkbox" {% if account.isActivated %}checked{% else %}unchecked{% endif %}></td>
|
||||||
<td><button class="btn btn-info">提交</button></td>
|
<td><button class="btn btn-info" onclick="submit_account(this)">提交</button></td>
|
||||||
<td><button class="btn btn-danger">删除</button></td>
|
<td><button class="btn btn-danger" onclick="delete_account(this)">删除</button></td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
@ -58,4 +58,47 @@
|
|||||||
});;
|
});;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function submit_account(obj) {
|
||||||
|
var account = obj.parentElement.parentElement
|
||||||
|
var data = {
|
||||||
|
method: "update",
|
||||||
|
id: account?.children[0]?.innerHTML,
|
||||||
|
email: account?.children[1]?.innerHTML,
|
||||||
|
name: account?.children[2]?.innerHTML,
|
||||||
|
role: account?.children[3]?.children[0].value,
|
||||||
|
isActivated: account?.children[4].children[0].checked
|
||||||
|
}
|
||||||
|
$.ajax({
|
||||||
|
type: 'POST',
|
||||||
|
url: "{{ url_for('main.manage') }}",
|
||||||
|
data: data,
|
||||||
|
success: null,
|
||||||
|
dataType: null
|
||||||
|
});
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
function delete_account(obj) {
|
||||||
|
var account = obj.parentElement.parentElement
|
||||||
|
var data = {
|
||||||
|
method: "delete",
|
||||||
|
id: account?.children[0]?.innerHTML,
|
||||||
|
email: account?.children[1]?.innerHTML,
|
||||||
|
name: account?.children[2]?.innerHTML,
|
||||||
|
role: account?.children[3]?.children[0].value,
|
||||||
|
isActivated: account?.children[4].children[0].checked
|
||||||
|
}
|
||||||
|
$.ajax({
|
||||||
|
type: 'POST',
|
||||||
|
url: "{{ url_for('main.manage') }}",
|
||||||
|
data: data,
|
||||||
|
success: null,
|
||||||
|
dataType: null
|
||||||
|
});
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
@ -6,7 +6,7 @@
|
|||||||
</h1>
|
</h1>
|
||||||
{% if user.isActivated %}
|
{% if user.isActivated %}
|
||||||
<h3 class="subtitle" style="text-align: left;">账户状态:</h3>
|
<h3 class="subtitle" style="text-align: left;">账户状态:</h3>
|
||||||
<h3 class="subtitle" style="text-align: left;">服务状态:</h3>
|
<!-- <h3 class="subtitle" style="text-align: left;">服务状态:</h3> -->
|
||||||
{% if user.role=='admin' %}
|
{% if user.role=='admin' %}
|
||||||
<!-- admin -->
|
<!-- admin -->
|
||||||
<h3 class="subtitle" style="text-align: left;">注册账户列表:</h3>
|
<h3 class="subtitle" style="text-align: left;">注册账户列表:</h3>
|
||||||
|
Reference in New Issue
Block a user