refactor: introduce app factory + blueprints (auth, core, admin users, invite); add utils and wsgi entrypoint; keep legacy routes for now
This commit is contained in:
90
app/blueprints/invite.py
Normal file
90
app/blueprints/invite.py
Normal file
@@ -0,0 +1,90 @@
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, flash
|
||||
import datetime
|
||||
import json
|
||||
from app.models import SurveyInvite, Product, Feature, Platform, PlatformFeature, UserSurvey, SurveyType
|
||||
from app.utils.mail import send_result_email
|
||||
|
||||
bp = Blueprint('invite', __name__)
|
||||
|
||||
|
||||
@bp.route('/invite/<uuid_str>', methods=['GET', 'POST'], endpoint='handle_invite')
|
||||
def handle_invite(uuid_str):
|
||||
invite = SurveyInvite.get_or_none(SurveyInvite.uuid == uuid_str)
|
||||
if not invite:
|
||||
return "Ссылка недействительна", 404
|
||||
|
||||
user_survey = invite.survey
|
||||
survey_type = user_survey.survey_type
|
||||
products = Product.select().where(Product.survey_type == survey_type)
|
||||
|
||||
if request.method == 'GET':
|
||||
questions = {}
|
||||
for product in products:
|
||||
features = Feature.select().where(Feature.product == product)
|
||||
questions[product.name] = {f.name: f.question_text for f in features}
|
||||
return render_template("invite/form.html", invite=invite, survey=user_survey, questions=questions)
|
||||
|
||||
full_name = request.form.get('full_name')
|
||||
phone = request.form.get('phone')
|
||||
organization = request.form.get('organization')
|
||||
answers = dict(request.form)
|
||||
for k in ['full_name', 'phone', 'organization']:
|
||||
answers.pop(k, None)
|
||||
selected_keys = list(answers.keys())
|
||||
|
||||
platforms = Platform.select().where(Platform.survey_type == survey_type)
|
||||
features = Feature.select().join(Product).where(
|
||||
Feature.name.in_(selected_keys),
|
||||
Product.survey_type == survey_type
|
||||
)
|
||||
comment = request.form.get('comment')
|
||||
scores = {}
|
||||
unsupported = {}
|
||||
total = len(selected_keys)
|
||||
|
||||
for platform in platforms:
|
||||
match = 0
|
||||
unsup = []
|
||||
for feature in features:
|
||||
pf = PlatformFeature.get_or_none(platform=platform, feature=feature)
|
||||
if pf and pf.supported:
|
||||
match += 1
|
||||
else:
|
||||
unsup.append(feature.question_text)
|
||||
scores[platform.name] = round((match / total) * 100) if total else 0
|
||||
unsupported[platform.name] = unsup
|
||||
|
||||
SurveyResult = __import__('app.models', fromlist=['SurveyResult']).SurveyResult
|
||||
SurveyResult.create(
|
||||
invite=invite,
|
||||
full_name=full_name,
|
||||
phone=phone,
|
||||
organization=organization,
|
||||
answers=json.dumps(answers),
|
||||
platform_scores=json.dumps(scores),
|
||||
unsupported=json.dumps(unsupported),
|
||||
submitted_at=datetime.datetime.now(),
|
||||
comment=comment
|
||||
)
|
||||
|
||||
invite.responded = True
|
||||
invite.save()
|
||||
|
||||
send_result_email(
|
||||
to_email=invite.survey.user.email,
|
||||
from_email=invite.recipient_email,
|
||||
survey_name=survey_type.name,
|
||||
scores=scores,
|
||||
unsupported=unsupported,
|
||||
sender_name=invite.survey.user.full_name
|
||||
)
|
||||
|
||||
if not invite.show_result:
|
||||
flash("Спасибо! Ваши ответы отправлены.", "success")
|
||||
return redirect("https://www.mont.ru/ru-ru")
|
||||
|
||||
return render_template("invite/result.html",
|
||||
scores=scores,
|
||||
unsupported_features=unsupported,
|
||||
chart_labels=list(scores.keys()),
|
||||
chart_data=list(scores.values()))
|
||||
Reference in New Issue
Block a user