~sircmpwn/meta.sr.ht

ref: 72548bd7545f78670878667674cc7645835a17bd meta.sr.ht/metasrht/blueprints/invites.py -rw-r--r-- 1.2 KiB
72548bd7Drew DeVault API: Updates per core-go auth changes 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from flask import Blueprint, render_template, redirect, abort
from metasrht.types import Invite, UserType
from srht.config import cfg
from srht.database import db
from srht.flask import session
from srht.oauth import current_user, loginrequired

invites = Blueprint('invites', __name__)

site_name = cfg("sr.ht", "site-name")
site_root = cfg("meta.sr.ht", "origin")

@invites.route("/invites")
@loginrequired
def index():
    return render_template("invite.html")

@invites.route("/invites/gen-invite", methods=["POST"])
@loginrequired
def gen_invite():
    if current_user.invites == 0 and current_user.user_type != UserType.admin:
        abort(401)
    invite = Invite()
    invite.sender_id = current_user.id
    if current_user.invites > 0:
        current_user.invites -= 1
    db.session.add(invite)
    db.session.commit()
    session["invite_link"] = "{}/register/{}".format(
            site_root, invite.invite_hash)
    return redirect("/invites/generated")

@invites.route("/invites/generated")
@loginrequired
def view_invite():
    invite_link = session.get("invite_link")
    if not invite_link:
        return redirect("/invites")
    del session["invite_link"]
    return render_template("invite-link-generated.html", link=invite_link)