Eine voting seite für falsch zugeordnete Zitate https://zitate.prapsschnalinen.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
zitate/webvoting.py

358 lines
11 KiB

import os
from random import choice, shuffle, random, randint
from flask import Flask
from flask import session, flash, g, redirect, render_template, request, Response, url_for, make_response
from db import WrongQuote, Quote, Author
from api import api
def render_ab():
g.page = "start"
minscore = max(50, session["minscore"] if "minscore" in session else 0)
quotes = list(WrongQuote.select().where( (WrongQuote.checked == True) & (WrongQuote.score > minscore) ))
if len(quotes) == 0:
quotes = list(WrongQuote.select().where(WrongQuote.checked == True))
quotes = sorted(quotes, key=lambda quote: quote.showed)
final_quotes = []
for quote in quotes:
if quote.showed != quotes[-1].showed and not (
("votes" in session) and (quote._pk in session["votes"])
):
final_quotes.append(quote)
if len(final_quotes) == 1:
final_quotes.append(choice(quotes))
session["votes"] = []
elif len(final_quotes) == 0:
final_quotes = quotes
session["votes"] = []
shuffle(final_quotes)
for quote in final_quotes[:]:
if quote.showed > 5 and (quote.voted / quote.showed) > 0.5:
final_quotes.append(quote)
elif quote.showed <= 5:
final_quotes.append(quote)
if quote.showed > 15 and (quote.voted / quote.showed) < 0.1:
quote.delete_instance()
final_quotes.remove(quote)
zit1 = choice(final_quotes)
zit2 = choice(final_quotes)
while zit1 == zit2:
zit2 = choice(final_quotes)
g.zit1id = zit1._pk
g.zit2id = zit2._pk
g.zit1q = zit1.quote.quote
g.zit1a = zit1.author.author
g.zit2q = zit2.quote.quote
g.zit2a = zit2.author.author
g.url = request.base_url
return render_template("start.html")
def render_isfunny(minscore):
quotes = WrongQuote.select().where(WrongQuote.score > minscore)
if len(quotes) == 0:
quotes = WrongQuote.select()
valid_quotes = []
for quote in quotes:
if "votes" in session:
if quote._pk in session["votes"]:
continue
valid_quotes.append(quote)
if len(valid_quotes) == 0:
valid_quotes = list(quotes)
chosen = choice(valid_quotes)
g.zit1id = chosen._pk
g.zit1q = chosen.quote.quote
g.zit1a = chosen.author.author
g.url = request.base_url
g.deg1 = randint(-20,20)
g.deg2 = randint(-20,20)
return render_template("istwitzig.html")
def create_app(test_config=None):
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY="dev",
DATABASE=os.path.join(app.instance_path, "voting.sqlite"),
)
if test_config is None:
app.config.from_pyfile("config.py", silent=True)
else:
app.config.from_mapping(test_config)
try:
os.makedirs(app.instance_path)
except OSError:
pass
app.register_blueprint(api)
@app.route("/hello")
def hello():
return "Hello, World"
@app.route("/", methods=("GET",))
def start():
if "minscore" not in session:
session["minscore"] = 85
if "quotenum" not in session:
session["quotenum"] = 0
if "abshare" not in session:
session["abshare"] = 0
session["minscore"] -= 1
session["quotenum"] += 1
if session["abshare"]<.5:
session["abshare"] += .01
print(session)
if random() < session["abshare"]:
return render_ab()
return render_isfunny(session["minscore"])
@app.route("/abstimmung", methods=("POST",))
def abstimmung():
if "zit" in request.form:
voted, not_voted = request.form["zit"].split("-")
voted_quote = WrongQuote.get_by_id(int(voted))
voted_quote.voted += 1
voted_quote.showed += 1
voted_quote.save()
not_voted_quote = WrongQuote.get_by_id(int(not_voted))
not_voted_quote.showed += 1
not_voted_quote.save()
if "votes" not in session:
session["votes"] = []
session["votes"] += [int(voted), int(not_voted)]
elif "witzig" in request.form:
try:
qid = int(request.form["witzig"])
except ValueError:
return "ValueError"
voted_quote = WrongQuote.get_by_id(qid)
voted_quote.rating += 1
voted_quote.save()
elif "nichtwitzig" in request.form:
try:
qid = int(request.form["nichtwitzig"])
except ValueError:
return "ValueError"
voted_quote = WrongQuote.get_by_id(qid)
voted_quote.rating -= 2
voted_quote.save()
else:
if "minscore" in session:
session["minscore"] += 2
print(url_for("start"))
resp = make_response()
resp.location = "https://zitate.prapsschnalinen.de/"
resp.status = 302
return resp
@app.route("/einreichen", methods=("GET", "POST"))
def einreichen():
g.page = "einreichen"
g.email = session["email"] if "email" in session else ""
if request.method == "POST":
quote = request.form["quote"].strip()
new_author = request.form["wrongauthor"].strip()
real_author = request.form["realauthor"].strip()
contributed_by = request.form["email"]
if real_author not in [a.author for a in Author.select()]:
real_author_db = Author.create(
author=real_author, contributed_by=contributed_by
)
else:
real_author_db = Author.get(Author.author == real_author)
if quote not in [q.quote for q in Quote.select()]:
quote_db = Quote.create(
quote=quote, author=real_author_db, contributed_by=contributed_by
)
else:
quote_db = Quote.get(Quote.quote == quote)
if new_author not in [a.author for a in Author.select()]:
new_author_db = Author.create(
author=new_author, contributed_by=contributed_by
)
else:
new_author_db = Author.get(Author.author == new_author)
WrongQuote.create(
quote=quote_db, author=new_author_db, contributed_by=contributed_by
)
flash(
"Dein Zitat wurde gespeichert! Sobald es überprüft worden ist, wird es öffentlich sein."
)
session["email"] = request.form["email"]
return redirect("/einreichen")
g.url = request.base_url
return render_template("einreichen.html")
@app.route("/rss")
def rss():
g.page = "top"
quotes = sorted(
list(WrongQuote.select()),
key=lambda quote: quote.get_score(),
reverse=True,
)
g.quotes = [
(
f'{quote.quote.quote}“ - {quote.author.author}',
quote.get_score(),
quote._pk,
)
for quote in quotes[:6]
]
return Response(
response=render_template("top.rss"),
status=200,
mimetype="application/rss+xml",
)
@app.route("/top")
def top():
g.page = "top"
quotes = sorted(
list(WrongQuote.select()),
key=lambda quote: quote.get_score(),
reverse=True,
)
g.quotes = [
(
f'{quote.quote.quote}“ - {quote.author.author}',
quote.get_score(),
)
for quote in quotes[:6]
]
g.url = request.base_url
return render_template("top.html")
@app.route("/topall")
def topall():
g.page = "top"
quotes = sorted(
list(WrongQuote.select()),
key=lambda quote: quote.get_score(),
reverse=True,
)
g.quotes = [
(
f'{quote.quote.quote}“ - {quote.author.author}',
quote.get_score(),
)
for quote in quotes
]
g.url = request.base_url
return render_template("top.html")
@app.route("/zitate")
def zitate():
g.page = "zitate"
g.quotes = [
(f'{quote.quote.quote}“ - {quote.author.author}', "/"+str(quote._pk))
for quote in WrongQuote.select().where(WrongQuote.checked == True)
]
g.count = len(g.quotes)
g.url = request.base_url
return render_template("zitate.html")
@app.route("/stats")
def stats():
g.page = "stats"
g.votes = 0
g.shows = 0
g.quotes = 0
quotes = list(WrongQuote.select())
for quote in quotes:
g.votes += quote.voted
g.shows += quote.showed
g.quotes += 1
g.shows = round(g.shows / len(quotes))
g.url = request.base_url
return render_template("stats.html")
@app.route("/<int:pk>")
def falsches_zitat_by_id(pk):
wrong_quote = WrongQuote.get_by_id(pk)
g.score = wrong_quote.get_score()
wrong_quote_dict = wrong_quote.get_dict()
g.quote = wrong_quote_dict["quote"]["quote"]
g.qid = wrong_quote_dict["quote"]["id"]
g.author = wrong_quote_dict["author"]["author"]
g.aid = wrong_quote_dict["author"]["id"]
g.title = "Falschzugeordnetes Zitat"
g.url = request.base_url
return render_template("falscheszitat.html")
@app.route("/<int:q>-<int:a>")
def falsches_zitat_by_ids(q, a):
wrong_quote = WrongQuote.select().where(
(WrongQuote.author == a) & (WrongQuote.quote == q)
)
if len(wrong_quote) > 0:
return redirect("/" + str(wrong_quote[0].id))
quote = Quote.get_by_id(q).get_dict()
g.quote = quote["quote"]
g.qid = q
if quote["author"]["id"] == a: # quote is real
return redirect("q/" + str(q))
author = Author.get_by_id(a).get_dict()
g.author = author["author"]
g.aid = a
g.title = "Falschzugeordnetes Zitat"
g.url = request.base_url
return render_template("zitat.html")
@app.route("/q/<int:pk>")
def zitat_by_id(pk):
quote = Quote.get_by_id(pk).get_dict()
g.quote = quote["quote"]
g.qid = quote["id"]
g.author = quote["author"]["author"]
g.aid = quote["author"]["id"]
g.quotes = WrongQuote.select().where((WrongQuote.checked == True) & (WrongQuote.quote == g.qid))
g.title = "Richtiges Zitat"
g.url = request.base_url
return render_template("richtigeszitat.html")
@app.route("/a/<int:pk>")
def autor_by_id(pk):
author = Author.get_by_id(pk).get_dict()
g.author = author["author"]
g.quotes = WrongQuote.select().where((WrongQuote.checked == True) & (WrongQuote.author == author["id"]))
g.title = "Autor"
g.url = request.base_url
return render_template("autor.html")
@app.route("/removecookies")
def removecookies():
session.clear()
return redirect("/")
return app