rDrama/files/routes/awards.py

372 lines
9.6 KiB
Python
Raw Normal View History

2021-10-07 06:04:29 +00:00
from files.__main__ import app, limiter
2021-08-04 15:35:10 +00:00
from files.helpers.wrappers import *
from files.helpers.alerts import *
from files.helpers.get import *
2021-08-21 11:06:28 +00:00
from files.helpers.const import *
2021-08-04 15:35:10 +00:00
from files.classes.award import *
2021-07-31 05:33:32 +00:00
from flask import g, request
2021-07-27 11:06:52 +00:00
2021-09-08 08:37:27 +00:00
@app.get("/shop")
2021-09-17 09:33:56 +00:00
@app.get("/settings/shop")
2021-09-08 08:37:27 +00:00
@auth_required
def shop(v):
2021-09-08 17:49:13 +00:00
if site_name == "Drama":
AWARDS = {
"ban": {
"kind": "ban",
"title": "One-Day Ban",
"description": "Bans the author for a day.",
"icon": "fas fa-gavel",
"color": "text-danger",
"price": 5000
},
"shit": {
"kind": "shit",
"title": "Shit",
"description": "Makes flies swarm a post.",
"icon": "fas fa-poop",
"color": "text-black-50",
"price": 1000
},
2021-09-20 13:11:10 +00:00
"fireflies": {
"kind": "fireflies",
2021-09-20 13:14:42 +00:00
"title": "Fireflies",
2021-09-08 17:49:13 +00:00
"description": "Puts stars on the post.",
"icon": "fas fa-sparkles",
"color": "text-warning",
"price": 1000
}
}
else:
AWARDS = {
"shit": {
"kind": "shit",
2021-09-20 13:14:42 +00:00
"title": "Shit",
2021-09-08 17:49:13 +00:00
"description": "Makes flies swarm a post.",
"icon": "fas fa-poop",
"color": "text-black-50",
"price": 1000
},
2021-09-20 13:11:10 +00:00
"fireflies": {
"kind": "fireflies",
2021-09-20 13:14:42 +00:00
"title": "Fireflies",
2021-09-08 17:49:13 +00:00
"description": "Puts stars on the post.",
"icon": "fas fa-sparkles",
"color": "text-warning",
"price": 1000
}
}
2021-09-08 08:53:57 +00:00
query = g.db.query(
User.id, User.username, User.patron, User.namecolor,
AwardRelationship.kind.label('last_award_kind'), func.count(AwardRelationship.id).label('last_award_count')
).filter(AwardRelationship.submission_id==None, AwardRelationship.comment_id==None, User.patron > 0) \
.group_by(User.username, User.patron, User.id, User.namecolor, AwardRelationship.kind) \
.order_by(User.patron.desc(), AwardRelationship.kind.desc()) \
.join(User).filter(User.id == v.id).all()
2021-09-08 08:55:10 +00:00
owned = []
2021-09-08 08:53:57 +00:00
for row in (r._asdict() for r in query):
kind = row['last_award_kind']
if kind in AWARDS.keys():
2021-09-08 08:57:40 +00:00
award = AWARDS[kind]
award["owned_num"] = row['last_award_count']
2021-09-08 08:58:39 +00:00
owned.append(award)
2021-09-08 08:53:57 +00:00
2021-09-08 17:21:04 +00:00
if v.patron:
2021-09-08 17:51:02 +00:00
for val in AWARDS.values():
2021-09-08 17:44:43 +00:00
if v.patron == 1: val["price"] = int(val["price"]*0.90)
elif v.patron == 2: val["price"] = int(val["price"]*0.85)
elif v.patron == 3: val["price"] = int(val["price"]*0.80)
2021-09-08 17:44:28 +00:00
elif v.patron == 4: val["price"] = int(val["price"]*0.75)
else: val["price"] = int(val["price"]*0.70)
2021-09-17 11:14:45 +00:00
return render_template("settings_shop.html", owned=owned, awards=list(AWARDS.values()), v=v)
2021-09-08 08:37:27 +00:00
2021-09-17 12:40:24 +00:00
2021-09-08 08:37:27 +00:00
@app.post("/buy/<award>")
@auth_required
def buy(v, award):
2021-09-08 17:49:46 +00:00
if site_name == "Drama":
AWARDS = {
"ban": {
"kind": "ban",
"title": "One-Day Ban",
"description": "Bans the author for a day.",
"icon": "fas fa-gavel",
"color": "text-danger",
"price": 5000
},
"shit": {
"kind": "shit",
"title": "Shit",
"description": "Makes flies swarm a post.",
"icon": "fas fa-poop",
"color": "text-black-50",
"price": 1000
},
2021-09-20 13:11:10 +00:00
"fireflies": {
"kind": "fireflies",
2021-09-20 13:14:42 +00:00
"title": "Fireflies",
2021-09-08 17:49:46 +00:00
"description": "Puts stars on the post.",
"icon": "fas fa-sparkles",
"color": "text-warning",
"price": 1000
}
}
else:
AWARDS = {
"shit": {
"kind": "shit",
2021-09-20 13:14:42 +00:00
"title": "Shit",
2021-09-08 17:49:46 +00:00
"description": "Makes flies swarm a post.",
"icon": "fas fa-poop",
"color": "text-black-50",
"price": 1000
},
2021-09-20 13:11:10 +00:00
"fireflies": {
"kind": "fireflies",
2021-09-20 13:14:42 +00:00
"title": "Fireflies",
2021-09-08 17:49:46 +00:00
"description": "Puts stars on the post.",
"icon": "fas fa-sparkles",
"color": "text-warning",
"price": 1000
}
}
2021-09-08 08:53:57 +00:00
if award not in AWARDS: abort(400)
price = AWARDS[award]["price"]
2021-09-08 17:15:53 +00:00
if v.patron:
2021-09-08 17:32:54 +00:00
if v.patron == 1: price = int(price*0.90)
elif v.patron == 2: price = int(price*0.85)
elif v.patron == 3: price = int(price*0.80)
elif v.patron == 4: price = int(price*0.75)
else: price = int(price*0.70)
2021-09-08 17:15:53 +00:00
2021-09-08 11:06:56 +00:00
if v.coins < price: return {"error": "Not enough coins."}, 400
2021-09-08 08:53:57 +00:00
v.coins -= price
g.db.add(v)
2021-09-08 08:37:27 +00:00
2021-09-08 08:53:57 +00:00
thing = g.db.query(AwardRelationship).order_by(AwardRelationship.id.desc()).first().id
thing += 1
2021-09-08 08:37:27 +00:00
2021-09-08 08:53:57 +00:00
award = AwardRelationship(id=thing, user_id=v.id, kind=award)
g.db.add(award)
2021-09-08 08:40:23 +00:00
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-08 10:57:56 +00:00
return {"message": "Award bought!"}
2021-09-08 08:37:27 +00:00
2021-07-27 11:06:52 +00:00
def banaward_trigger(post=None, comment=None):
2021-09-08 08:53:57 +00:00
author = post.author if post else comment.author
link = f"[this post]({post.permalink})" if post else f"[this comment]({comment.permalink})"
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
if not author.is_suspended:
author.ban(reason="one-day ban award used", days=1)
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
send_notification(NOTIFICATIONS_ACCOUNT, author, f"Your account has been suspended for a day for {link}. It sucked and you should feel bad.")
elif author.unban_utc > 0:
author.unban_utc += 24*60*60
g.db.add(author)
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
send_notification(NOTIFICATIONS_ACCOUNT, author, f"Your account has been suspended for yet another day for {link}. Seriously man?")
2021-07-27 11:06:52 +00:00
ACTIONS = {
2021-09-08 08:53:57 +00:00
"ban": banaward_trigger
2021-07-27 11:06:52 +00:00
}
2021-07-27 21:07:41 +00:00
ALLOW_MULTIPLE = (
2021-09-08 08:53:57 +00:00
"ban",
"shit",
2021-09-20 13:11:10 +00:00
"fireflies"
2021-07-27 21:07:41 +00:00
)
2021-09-17 15:20:38 +00:00
@app.post("/post/<pid>/awards")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-27 11:06:52 +00:00
@auth_required
def award_post(pid, v):
2021-09-22 15:23:56 +00:00
if v.is_suspended and v.unban_utc == 0: return {"error": "forbidden."}, 403
2021-07-27 11:06:52 +00:00
2021-10-12 05:23:17 +00:00
kind = request.values.get("kind", "").strip()
2021-09-17 15:20:38 +00:00
2021-09-08 08:53:57 +00:00
if kind not in AWARDS:
return {"error": "That award doesn't exist."}, 404
2021-07-27 11:06:52 +00:00
2021-09-17 08:29:05 +00:00
post_award = g.db.query(AwardRelationship).options(lazyload('*')).filter(
2021-09-08 08:53:57 +00:00
and_(
AwardRelationship.kind == kind,
AwardRelationship.user_id == v.id,
AwardRelationship.submission_id == None,
AwardRelationship.comment_id == None
)
).first()
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
if not post_award:
return {"error": "You don't have that award."}, 404
2021-07-27 11:06:52 +00:00
2021-09-17 08:29:05 +00:00
post = g.db.query(Submission).options(lazyload('*')).filter_by(id=pid).first()
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
if not post or post.is_banned or post.deleted_utc > 0:
return {"error": "That post doesn't exist or has been deleted or removed."}, 404
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
if post.author_id == v.id:
return {"error": "You can't award yourself."}, 403
2021-07-27 11:06:52 +00:00
2021-09-17 08:29:05 +00:00
existing_award = g.db.query(AwardRelationship).options(lazyload('*')).filter(
2021-09-08 08:53:57 +00:00
and_(
AwardRelationship.submission_id == post.id,
AwardRelationship.user_id == v.id,
AwardRelationship.kind == kind
)
).first()
2021-07-27 21:16:32 +00:00
2021-09-08 08:53:57 +00:00
if existing_award and kind not in ALLOW_MULTIPLE:
return {"error": "You can't give that award multiple times to the same post."}, 409
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
post_award.submission_id = post.id
g.db.add(post_award)
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
msg = f"@{v.username} has given your [post]({post.permalink}) the {AWARDS[kind]['title']} Award!"
2021-07-27 11:06:52 +00:00
2021-10-12 05:23:17 +00:00
note = request.values.get("note", "").strip()
2021-09-08 08:53:57 +00:00
if note:
msg += f"\n\n> {note}"
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
send_notification(NOTIFICATIONS_ACCOUNT, post.author, msg)
2021-07-27 11:06:52 +00:00
2021-09-17 08:55:55 +00:00
if kind in ACTIONS: ACTIONS[kind](post=post)
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
post.author.received_award_count += 1
g.db.add(post.author)
2021-09-05 18:31:19 +00:00
2021-09-16 17:15:07 +00:00
g.db.commit()
2021-09-28 19:39:51 +00:00
if request.referrer and len(request.referrer) > 1: return redirect(request.referrer)
2021-09-25 21:19:58 +00:00
else: return redirect("/")
2021-07-27 11:06:52 +00:00
2021-09-16 17:15:07 +00:00
@app.post("/comment/<cid>/awards")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-27 11:06:52 +00:00
@auth_required
def award_comment(cid, v):
2021-09-22 15:23:56 +00:00
if v.is_suspended and v.unban_utc == 0: return {"error": "forbidden"}, 403
2021-07-27 11:06:52 +00:00
2021-10-12 05:23:17 +00:00
kind = request.values.get("kind", "").strip()
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
if kind not in AWARDS:
return {"error": "That award doesn't exist."}, 404
2021-07-27 11:06:52 +00:00
2021-09-17 08:29:05 +00:00
comment_award = g.db.query(AwardRelationship).options(lazyload('*')).filter(
2021-09-08 08:53:57 +00:00
and_(
AwardRelationship.kind == kind,
AwardRelationship.user_id == v.id,
AwardRelationship.submission_id == None,
AwardRelationship.comment_id == None
)
).first()
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
if not comment_award:
return {"error": "You don't have that award."}, 404
2021-07-27 11:06:52 +00:00
2021-09-17 08:29:05 +00:00
c = g.db.query(Comment).options(lazyload('*')).filter_by(id=cid).first()
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
if not c or c.is_banned or c.deleted_utc > 0:
return {"error": "That comment doesn't exist or has been deleted or removed."}, 404
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
if c.author_id == v.id:
return {"error": "You can't award yourself."}, 403
2021-07-27 11:06:52 +00:00
2021-09-17 08:29:05 +00:00
existing_award = g.db.query(AwardRelationship).options(lazyload('*')).filter(
2021-09-08 08:53:57 +00:00
and_(
AwardRelationship.comment_id == c.id,
AwardRelationship.user_id == v.id,
AwardRelationship.kind == kind
)
).first()
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
if existing_award and kind not in ALLOW_MULTIPLE:
return {"error": "You can't give that award multiple times to the same comment."}, 409
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
comment_award.comment_id = c.id
g.db.add(comment_award)
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
msg = f"@{v.username} has given your [comment]({c.permalink}) the {AWARDS[kind]['title']} Award!"
2021-07-27 11:06:52 +00:00
2021-10-12 05:23:17 +00:00
note = request.values.get("note", "").strip()
2021-09-08 08:53:57 +00:00
if note:
msg += f"\n\n> {note}"
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
send_notification(NOTIFICATIONS_ACCOUNT, c.author, msg)
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
if kind in ACTIONS:
ACTIONS[kind](comment=c)
2021-07-27 11:06:52 +00:00
2021-09-08 08:53:57 +00:00
c.author.received_award_count += 1
g.db.add(c.author)
2021-09-05 18:31:19 +00:00
2021-09-16 17:15:07 +00:00
g.db.commit()
2021-10-02 16:32:17 +00:00
if request.referrer and len(request.referrer) > 1: return redirect(request.referrer)
else: return redirect("/")
2021-07-27 22:28:21 +00:00
@app.get("/admin/user_award")
@auth_required
def admin_userawards_get(v):
2021-09-08 08:53:57 +00:00
if v.admin_level < 6:
abort(403)
2021-07-27 22:28:21 +00:00
2021-09-08 08:53:57 +00:00
return render_template("admin/user_award.html", awards=list(AWARDS.values()), v=v)
2021-07-27 22:28:21 +00:00
@app.post("/admin/user_award")
2021-10-08 01:41:25 +00:00
@limiter.limit("1/second")
2021-07-27 22:28:21 +00:00
@auth_required
@validate_formkey
def admin_userawards_post(v):
2021-09-08 08:53:57 +00:00
if v.admin_level < 6:
abort(403)
2021-07-27 22:28:21 +00:00
2021-09-27 01:00:25 +00:00
try: u = request.values.get("username").strip()
except: abort(404)
u = get_user(u, graceful=False, v=v)
2021-07-27 22:28:21 +00:00
2021-09-08 08:53:57 +00:00
notify_awards = {}
2021-07-27 22:28:21 +00:00
2021-09-08 08:53:57 +00:00
latest = g.db.query(AwardRelationship).order_by(AwardRelationship.id.desc()).first()
thing = latest.id
2021-07-27 22:28:21 +00:00
2021-09-19 13:11:34 +00:00
for key, value in request.values.items():
2021-09-08 08:53:57 +00:00
if key not in AWARDS:
continue
2021-07-27 22:28:21 +00:00
2021-09-08 08:53:57 +00:00
if value:
2021-07-30 16:44:39 +00:00
2021-09-08 08:53:57 +00:00
if int(value) > 0:
notify_awards[key] = int(value)
2021-07-30 16:44:39 +00:00
2021-09-08 08:53:57 +00:00
for x in range(int(value)):
thing += 1
2021-07-27 22:28:21 +00:00
2021-09-18 18:04:05 +00:00
award = AwardRelationship(
2021-09-08 08:53:57 +00:00
id=thing,
user_id=u.id,
kind=key
2021-09-18 18:04:05 +00:00
)
g.db.add(award)
2021-07-27 22:28:21 +00:00
2021-09-08 08:53:57 +00:00
text = "You were given the following awards:\n\n"
2021-07-30 16:44:39 +00:00
2021-09-08 08:53:57 +00:00
for key, value in notify_awards.items():
text += f" - **{value}** {AWARDS[key]['title']} {'Awards' if value != 1 else 'Award'}\n"
2021-07-30 16:44:39 +00:00
2021-09-08 08:53:57 +00:00
send_notification(NOTIFICATIONS_ACCOUNT, u, text)
2021-07-27 22:28:21 +00:00
2021-09-16 17:02:58 +00:00
g.db.commit()
2021-09-08 08:53:57 +00:00
return render_template("admin/user_award.html", awards=list(AWARDS.values()), v=v)