Implement site settings backend and bulk visibility apply
This commit is contained in:
1 parent
70d3ad8f96
commit
aac2b60517
1 file changed
+39
-3
+39
-3
@@ -5,6 +5,7 @@ from app import db
|
|||||||
from app.models.user import User
|
from app.models.user import User
|
||||||
from app.models.set import Set
|
from app.models.set import Set
|
||||||
from app.models.instruction import Instruction
|
from app.models.instruction import Instruction
|
||||||
|
from app.models.site_setting import SiteSetting, VISIBILITIES
|
||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
@@ -218,8 +219,33 @@ def delete_set(set_id):
|
|||||||
def site_settings():
|
def site_settings():
|
||||||
"""Site-wide settings configuration."""
|
"""Site-wide settings configuration."""
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
# This is a placeholder for future site settings
|
action = request.form.get('action', 'save')
|
||||||
# You could store settings in a database table or config file
|
|
||||||
|
if action == 'apply_visibility':
|
||||||
|
# Bulk-apply one visibility to every existing set. Handy right
|
||||||
|
# after an upgrade, when old sets are all still public.
|
||||||
|
target = request.form.get('bulk_visibility', '')
|
||||||
|
if target not in VISIBILITIES:
|
||||||
|
flash('Unknown visibility setting.', 'danger')
|
||||||
|
return redirect(url_for('admin.site_settings'))
|
||||||
|
changed = Set.query.update({Set.visibility: target},
|
||||||
|
synchronize_session=False)
|
||||||
|
db.session.commit()
|
||||||
|
flash(f'Set {changed} existing set(s) to {target}.', 'success')
|
||||||
|
return redirect(url_for('admin.site_settings'))
|
||||||
|
|
||||||
|
SiteSetting.set('allow_registration',
|
||||||
|
request.form.get('allow_registration') == 'on')
|
||||||
|
SiteSetting.set('home_redirect_dashboard',
|
||||||
|
request.form.get('home_redirect_dashboard') == 'on')
|
||||||
|
|
||||||
|
default_visibility = request.form.get('default_visibility', 'private')
|
||||||
|
if default_visibility not in VISIBILITIES:
|
||||||
|
default_visibility = 'private'
|
||||||
|
SiteSetting.set('default_visibility', default_visibility)
|
||||||
|
|
||||||
|
db.session.commit()
|
||||||
|
SiteSetting.clear_cache()
|
||||||
flash('Settings updated successfully', 'success')
|
flash('Settings updated successfully', 'success')
|
||||||
return redirect(url_for('admin.site_settings'))
|
return redirect(url_for('admin.site_settings'))
|
||||||
|
|
||||||
@@ -231,7 +257,16 @@ def site_settings():
|
|||||||
'total_storage': db.session.query(func.sum(Instruction.file_size)).scalar() or 0
|
'total_storage': db.session.query(func.sum(Instruction.file_size)).scalar() or 0
|
||||||
}
|
}
|
||||||
|
|
||||||
return render_template('admin/settings.html', stats=stats)
|
visibility_counts = dict(
|
||||||
|
db.session.query(Set.visibility, func.count(Set.id))
|
||||||
|
.group_by(Set.visibility).all()
|
||||||
|
)
|
||||||
|
|
||||||
|
return render_template('admin/settings.html',
|
||||||
|
stats=stats,
|
||||||
|
settings=SiteSetting.all_settings(),
|
||||||
|
visibilities=VISIBILITIES,
|
||||||
|
visibility_counts=visibility_counts)
|
||||||
|
|
||||||
|
|
||||||
@admin_bp.route('/api/stats')
|
@admin_bp.route('/api/stats')
|
||||||
@@ -332,6 +367,7 @@ def bulk_import():
|
|||||||
year_released=set_data.get('year', datetime.now().year),
|
year_released=set_data.get('year', datetime.now().year),
|
||||||
piece_count=set_data.get('pieces', 0),
|
piece_count=set_data.get('pieces', 0),
|
||||||
image_url=set_data.get('image', {}).get('imageURL'),
|
image_url=set_data.get('image', {}).get('imageURL'),
|
||||||
|
visibility=SiteSetting.get('default_visibility', 'private'),
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
is_moc=False
|
is_moc=False
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in new issue
Block a user