Пример #1
0
def referral_route():
    if request.method == "POST":
        if request.form.get("form-name") == "create":
            code = request.form.get("code")
            if re.fullmatch(_REFERRAL_PATTERN, code) is None:
                flash(
                    "At least 3 characters. Only lowercase letters, "
                    "numbers, dashes (-) and underscores (_) are currently supported.",
                    "error",
                )
                return redirect(url_for("dashboard.referral_route"))

            if Referral.get_by(code=code):
                flash("Code already used", "error")
                return redirect(url_for("dashboard.referral_route"))

            name = request.form.get("name")
            referral = Referral.create(user_id=current_user.id,
                                       code=code,
                                       name=name)
            db.session.commit()
            flash("A new referral code has been created", "success")
            return redirect(
                url_for("dashboard.referral_route", highlight_id=referral.id))
        elif request.form.get("form-name") == "update":
            referral_id = request.form.get("referral-id")
            referral = Referral.get(referral_id)
            if referral and referral.user_id == current_user.id:
                referral.name = request.form.get("name")
                db.session.commit()
                flash("Referral name updated", "success")
                return redirect(
                    url_for("dashboard.referral_route",
                            highlight_id=referral.id))
        elif request.form.get("form-name") == "delete":
            referral_id = request.form.get("referral-id")
            referral = Referral.get(referral_id)
            if referral and referral.user_id == current_user.id:
                Referral.delete(referral.id)
                db.session.commit()
                flash("Referral deleted", "success")
                return redirect(url_for("dashboard.referral_route"))

    # Highlight a referral
    highlight_id = request.args.get("highlight_id")
    if highlight_id:
        highlight_id = int(highlight_id)

    referrals = Referral.query.filter_by(user_id=current_user.id).all()
    # make sure the highlighted referral is the first referral
    highlight_index = None
    for ix, referral in enumerate(referrals):
        if referral.id == highlight_id:
            highlight_index = ix
            break

    if highlight_index:
        referrals.insert(0, referrals.pop(highlight_index))

    return render_template("dashboard/referral.html", **locals())
Пример #2
0
def get_referral() -> Optional[Referral]:
    """Get the eventual referral stored in cookie"""
    # whether user arrives via a referral
    referral = None
    if request.cookies:
        ref_code = request.cookies.get(_REFERRAL_COOKIE)
        referral = Referral.get_by(code=ref_code)

    if not referral:
        if "slref" in session:
            ref_code = session["slref"]
            referral = Referral.get_by(code=ref_code)

    if referral:
        LOG.d("referral found %s", referral)

    return referral
Пример #3
0
def get_referral() -> Optional[Referral]:
    """Get the eventual referral stored in cookie"""
    # whether user arrives via a referral
    referral = None
    if request.cookies:
        ref_code = request.cookies.get(_REFERRAL_COOKIE)
        referral = Referral.get_by(code=ref_code)

    return referral