User can update/create referral name

This commit is contained in:
Son NK 2020-05-02 18:11:10 +02:00
parent 3ce3a05c7b
commit 1667356742
2 changed files with 54 additions and 20 deletions

View File

@ -19,8 +19,23 @@
{% for referral in referrals %}
<div class="card p-4 shadow-sm {% if referral.id == highlight_id %} highlight-row {% endif %}">
<div class="mb-3">Referral Code: <b>{{ referral.code }}</b>
</div>
<form method="post">
<input type="hidden" name="form-name" value="update">
<input type="hidden" name="referral-id" value="{{ referral.id }}">
<b>Name</b>
<div class="d-flex mb-3">
<div class="mr-2" style="min-width: 20em">
<input name="name" class="form-control" value="{{ referral.name or '' }}">
</div>
<div>
<button class="btn btn-outline-success">Update</button>
</div>
</div>
</form>
{% if referral.nb_user() > 0 %}
<div class="mb-3">
@ -33,8 +48,8 @@
<div>
Please use this referral link to invite your friends trying out SimpleLogin: <br>
<div class="d-flex mb-5">
<div class="flex-grow-1">
<div class="d-flex mb-5" style="max-width: 40em">
<div class="flex-grow-1 mr-1">
<input class="form-control" id="referral-{{ referral.id }}" readonly
value="{{ referral.link() }}">
</div>
@ -49,6 +64,11 @@
</div>
</div>
<div class="mb-3">
You can also use the referral code <b>{{ referral.code }}</b> when sharing any link on SimpleLogin. <br>
Just append <em>?slref={{ referral.code }}</em> to any link on SimpleLogin website.
</div>
</div>
{% endfor %}
@ -57,9 +77,10 @@
{% endif %}
<form method="post" class="mt-6">
<button class="btn btn-lg btn-success mt-2">Create a new referral code</button>
<input type="hidden" name="form-name" value="create">
<input name="name" class="form-control"
placeholder="Referral name, something to help you remember why you create it :)">
<button class="btn btn-success mt-2">Create a new referral code</button>
</form>
</div>
{% endblock %}

View File

@ -4,7 +4,7 @@ from flask_login import login_required, current_user
from app.dashboard.base import dashboard_bp
from app.extensions import db
from app.log import LOG
from app.models import Referral
from app.models import Referral, User
from app.utils import random_string
@ -12,20 +12,33 @@ from app.utils import random_string
@login_required
def referral_route():
if request.method == "POST":
# Generate a new unique ref code
code = random_string(15)
for _ in range(100):
if not Referral.get_by(code=code):
# found
break
LOG.warning("Referral Code %s already used", code)
if request.form.get("form-name") == "create":
# Generate a new unique ref code
code = random_string(15)
for _ in range(100):
if not Referral.get_by(code=code):
# found
break
referral = Referral.create(user_id=current_user.id, code=code)
db.session.commit()
flash("A new referral code has been created", "success")
return redirect(url_for("dashboard.referral_route", highlight_id=referral.id))
LOG.warning("Referral Code %s already used", code)
code = random_string(15)
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)
# Highlight a referral
highlight_id = request.args.get("highlight_id")