Merge pull request #149 from simple-login/referral-name
Referral improvements
This commit is contained in:
commit
b5120e78d6
|
@ -19,8 +19,23 @@
|
||||||
|
|
||||||
{% for referral in referrals %}
|
{% for referral in referrals %}
|
||||||
<div class="card p-4 shadow-sm {% if referral.id == highlight_id %} highlight-row {% endif %}">
|
<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 %}
|
{% if referral.nb_user() > 0 %}
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
|
@ -33,8 +48,8 @@
|
||||||
<div>
|
<div>
|
||||||
Please use this referral link to invite your friends trying out SimpleLogin: <br>
|
Please use this referral link to invite your friends trying out SimpleLogin: <br>
|
||||||
|
|
||||||
<div class="d-flex mb-5">
|
<div class="d-flex mb-5" style="max-width: 40em">
|
||||||
<div class="flex-grow-1">
|
<div class="flex-grow-1 mr-1">
|
||||||
<input class="form-control" id="referral-{{ referral.id }}" readonly
|
<input class="form-control" id="referral-{{ referral.id }}" readonly
|
||||||
value="{{ referral.link() }}">
|
value="{{ referral.link() }}">
|
||||||
</div>
|
</div>
|
||||||
|
@ -49,6 +64,19 @@
|
||||||
</div>
|
</div>
|
||||||
</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>
|
||||||
|
<form method="post">
|
||||||
|
<input type="hidden" name="form-name" value="delete">
|
||||||
|
<input type="hidden" name="referral-id" value="{{ referral.id }}">
|
||||||
|
<span class="delete-referral float-right btn btn-outline-danger">Delete</span>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
@ -57,9 +85,39 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<form method="post" class="mt-6">
|
<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>
|
</form>
|
||||||
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block script %}
|
||||||
|
<script>
|
||||||
|
$(".delete-referral").on("click", function (e) {
|
||||||
|
let that = $(this);
|
||||||
|
|
||||||
|
bootbox.confirm({
|
||||||
|
message: "This operation is irreversible, please confirm.",
|
||||||
|
buttons: {
|
||||||
|
confirm: {
|
||||||
|
label: 'Yes, delete it',
|
||||||
|
className: 'btn-danger'
|
||||||
|
},
|
||||||
|
cancel: {
|
||||||
|
label: 'Cancel',
|
||||||
|
className: 'btn-outline-primary'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
callback: function (result) {
|
||||||
|
if (result) {
|
||||||
|
that.closest("form").submit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
|
@ -4,7 +4,7 @@ from flask_login import login_required, current_user
|
||||||
from app.dashboard.base import dashboard_bp
|
from app.dashboard.base import dashboard_bp
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
from app.log import LOG
|
from app.log import LOG
|
||||||
from app.models import Referral
|
from app.models import Referral, User
|
||||||
from app.utils import random_string
|
from app.utils import random_string
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,20 +12,42 @@ from app.utils import random_string
|
||||||
@login_required
|
@login_required
|
||||||
def referral_route():
|
def referral_route():
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
# Generate a new unique ref code
|
if request.form.get("form-name") == "create":
|
||||||
code = random_string(15)
|
# Generate a new unique ref code
|
||||||
for _ in range(100):
|
|
||||||
if not Referral.get_by(code=code):
|
|
||||||
# found
|
|
||||||
break
|
|
||||||
|
|
||||||
LOG.warning("Referral Code %s already used", code)
|
|
||||||
code = random_string(15)
|
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)
|
LOG.warning("Referral Code %s already used", code)
|
||||||
db.session.commit()
|
code = random_string(15)
|
||||||
flash("A new referral code has been created", "success")
|
|
||||||
return redirect(url_for("dashboard.referral_route", highlight_id=referral.id))
|
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 a referral
|
||||||
highlight_id = request.args.get("highlight_id")
|
highlight_id = request.args.get("highlight_id")
|
||||||
|
|
|
@ -158,7 +158,9 @@ class User(db.Model, ModelMixin, UserMixin):
|
||||||
db.Boolean, default=True, nullable=False, server_default="1"
|
db.Boolean, default=True, nullable=False, server_default="1"
|
||||||
)
|
)
|
||||||
|
|
||||||
referral_id = db.Column(db.ForeignKey("referral.id"), nullable=True, default=None)
|
referral_id = db.Column(
|
||||||
|
db.ForeignKey("referral.id", ondelete="SET NULL"), nullable=True, default=None
|
||||||
|
)
|
||||||
|
|
||||||
referral = db.relationship("Referral", foreign_keys=[referral_id])
|
referral = db.relationship("Referral", foreign_keys=[referral_id])
|
||||||
|
|
||||||
|
@ -1153,6 +1155,7 @@ class Referral(db.Model, ModelMixin):
|
||||||
"""Referral code so user can invite others"""
|
"""Referral code so user can invite others"""
|
||||||
|
|
||||||
user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=False)
|
user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=False)
|
||||||
|
name = db.Column(db.String(512), nullable=True, default=None)
|
||||||
|
|
||||||
code = db.Column(db.String(128), unique=True, nullable=False)
|
code = db.Column(db.String(128), unique=True, nullable=False)
|
||||||
|
|
||||||
|
|
33
migrations/versions/2020_050218_f939d67374e4_.py
Normal file
33
migrations/versions/2020_050218_f939d67374e4_.py
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: f939d67374e4
|
||||||
|
Revises: b4146f7d5277
|
||||||
|
Create Date: 2020-05-02 18:07:42.275092
|
||||||
|
|
||||||
|
"""
|
||||||
|
import sqlalchemy_utils
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'f939d67374e4'
|
||||||
|
down_revision = 'b4146f7d5277'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.add_column('referral', sa.Column('name', sa.String(length=512), nullable=True))
|
||||||
|
op.drop_constraint('users_referral_id_fkey', 'users', type_='foreignkey')
|
||||||
|
op.create_foreign_key(None, 'users', 'referral', ['referral_id'], ['id'], ondelete='SET NULL')
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_constraint(None, 'users', type_='foreignkey')
|
||||||
|
op.create_foreign_key('users_referral_id_fkey', 'users', 'referral', ['referral_id'], ['id'])
|
||||||
|
op.drop_column('referral', 'name')
|
||||||
|
# ### end Alembic commands ###
|
13
server.py
13
server.py
|
@ -51,6 +51,7 @@ from app.models import (
|
||||||
DeletedAlias,
|
DeletedAlias,
|
||||||
Contact,
|
Contact,
|
||||||
EmailLog,
|
EmailLog,
|
||||||
|
Referral,
|
||||||
)
|
)
|
||||||
from app.monitor.base import monitor_bp
|
from app.monitor.base import monitor_bp
|
||||||
from app.oauth.base import oauth_bp
|
from app.oauth.base import oauth_bp
|
||||||
|
@ -216,6 +217,18 @@ def fake_data():
|
||||||
DeletedAlias.create(user_id=user.id, email="d2@ab.cd")
|
DeletedAlias.create(user_id=user.id, email="d2@ab.cd")
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
referral = Referral.create(user_id=user.id, code="REFCODE", name="First referral")
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
User.create(
|
||||||
|
email="winston@continental.com",
|
||||||
|
name="Winston",
|
||||||
|
password="password",
|
||||||
|
activated=True,
|
||||||
|
referral_id=referral.id,
|
||||||
|
)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
@login_manager.user_loader
|
@login_manager.user_loader
|
||||||
def load_user(user_id):
|
def load_user(user_id):
|
||||||
|
|
Loading…
Reference in a new issue