from flask import Blueprint, render_template, request, redirect, url_for, session, flash, jsonify from app.models.models import Submission, Settings from app import db from functools import wraps bp = Blueprint('submissions', __name__) def login_required(f): @wraps(f) def decorated_function(*args, **kwargs): if 'token' not in session: return redirect(url_for('auth.login')) return f(*args, **kwargs) return decorated_function def contributor_only(f): @wraps(f) def decorated_function(*args, **kwargs): if 'token' not in session or session.get('type') == 'admin': return redirect(url_for('auth.login')) return f(*args, **kwargs) return decorated_function @bp.route('/submit', methods=['GET', 'POST']) @login_required @contributor_only def submit(): submission_open = Settings.get_setting('submission_open', 'true') == 'true' contributor_type = session.get('type') if request.method == 'POST': if not submission_open: flash('Submission period is currently closed.', 'error') return redirect(url_for('submissions.submit')) message = request.form.get('message', '').strip() latitude = request.form.get('latitude') longitude = request.form.get('longitude') if not message: flash('Please enter a message', 'error') return redirect(url_for('submissions.submit')) new_submission = Submission( message=message, contributor_type=contributor_type, latitude=float(latitude) if latitude else None, longitude=float(longitude) if longitude else None ) db.session.add(new_submission) db.session.commit() flash('Contribution submitted successfully!', 'success') return redirect(url_for('submissions.submit')) # Get submission count for this user submission_count = Submission.query.filter_by(contributor_type=contributor_type).count() return render_template('submit.html', submission_open=submission_open, contributor_type=contributor_type, submission_count=submission_count)