-
Notifications
You must be signed in to change notification settings - Fork 0
/
bp_hurl.py
executable file
·141 lines (105 loc) · 4.21 KB
/
bp_hurl.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from flask import Blueprint, flash, redirect, render_template, url_for
from sqlalchemy import delete, select, update
from bp_auth import AuthActions, auth, login_required
from configs import CONSTS
from db import generate_unique_token
from forms import HurlForm, get_fields
from limiter import limiter
from models import Hurl, db
bp_hurl = Blueprint("bp_hurl", __name__, template_folder="templates")
@bp_hurl.route("/hurl_create", methods=["GET", "POST"])
@login_required
@limiter.limit("20/day", methods=["POST"], error_message="Currently, users can only create 20 hurls per day.")
def hurl_create():
form = HurlForm()
if form.validate_on_submit():
d = get_fields(Hurl, HurlForm, form)
token = generate_unique_token()
url = url_for("bp_visit.visit", token=token, pixel_name=d["pixel_name"])
user_id = auth(AuthActions.get_user_id)
hurl = Hurl(user_id=user_id, url=url, token=token, **d)
db.session.add(hurl)
db.session.flush()
hurl_id = hurl.id
db.session.commit()
form.data.clear()
flash("Hurl created.", "success")
return redirect(url_for("bp_hurl.hurl_read", hurl_id=hurl_id))
return render_template(
"hurl_create.html", CONSTS=CONSTS, form=form, logged_in=auth(AuthActions.is_logged_in), is_admin=auth(AuthActions.is_admin)
)
@bp_hurl.route("/hurl_edit/<int:hurl_id>", methods=["GET", "POST"])
@login_required
def hurl_edit(hurl_id):
form = HurlForm()
hurl = None
user_id = auth(AuthActions.get_user_id)
if user_id:
hurl = db.session.scalar(select(Hurl).where(Hurl.id == hurl_id).where(Hurl.user_id == user_id))
if not hurl:
return redirect(url_for("bp_hurl.hurl_list"))
form = HurlForm(obj=hurl)
if form.validate_on_submit():
d = get_fields(Hurl, HurlForm, form)
d["url"] = url_for("bp_visit.visit", token=hurl.token, pixel_name=d["pixel_name"])
db.session.execute(update(Hurl).where(Hurl.id == hurl.id).values(**d))
flash("Hurl updated.", "success")
db.session.commit()
form.data.clear()
return redirect(url_for("bp_hurl.hurl_list"))
return render_template(
"hurl_edit.html", CONSTS=CONSTS, form=form, hurl=hurl, logged_in=auth(AuthActions.is_logged_in), is_admin=auth(AuthActions.is_admin)
)
@bp_hurl.route("/hurl/<int:hurl_id>")
@login_required
def hurl_read(hurl_id):
hurl_visits = None
user_id = auth(AuthActions.get_user_id)
if user_id:
hurl_visits = db.session.scalars(select(Hurl).where(Hurl.id == hurl_id).where(Hurl.user_id == user_id)).all()
if hurl_visits and hurl_visits[0]:
hurl = hurl_visits[0]
visits = list(hurl.visits)
return render_template(
"hurl.html",
CONSTS=CONSTS,
hurl=hurl,
visits=visits,
logged_in=auth(AuthActions.is_logged_in),
is_admin=auth(AuthActions.is_admin),
)
return redirect(url_for("bp_hurl.hurl_list"))
@bp_hurl.route("/hurl_delete/<int:hurl_id>", methods=["DELETE"])
@login_required
def hurl_delete(hurl_id):
hurl = None
user_id = auth(AuthActions.get_user_id)
if user_id:
hurl = db.session.scalar(select(Hurl).where(Hurl.id == hurl_id).where(Hurl.user_id == user_id))
if hurl:
db.session.delete(hurl)
db.session.commit()
hurl = db.session.scalar(select(Hurl).where(Hurl.id == hurl_id).where(Hurl.user_id == user_id))
if not hurl:
flash("Hurl deleted.", "success")
return redirect(url_for("bp_hurl.hurl_list"))
flash(f"Couldn't find hurl #{hurl.id} to delete.")
return redirect(url_for("bp_hurl.hurl_list"))
@bp_hurl.route("/hurls")
@login_required
def hurl_list():
hurls = None
user_id = auth(AuthActions.get_user_id)
if user_id:
hurls = db.session.scalars(select(Hurl).where(Hurl.user_id == user_id)).all()
for i, h in enumerate(hurls):
hurls[i].__setattr__("read_count", len(h.visits))
header = "Showing all hurls."
return render_template(
"hurl_list.html",
CONSTS=CONSTS,
hurls=hurls,
header=header,
logged_in=auth(AuthActions.is_logged_in),
is_admin=auth(AuthActions.is_admin),
)