Fixed some things

This commit is contained in:
2024-10-14 00:25:06 +01:00
parent 449c3a2dc2
commit 9f4d380950
2 changed files with 93 additions and 76 deletions

66
app.py
View File

@@ -96,45 +96,47 @@ def vote(id, action):
else: else:
vote_data = {} vote_data = {}
# If the user has already voted, check for undoing or switching vote # If no prior vote, apply the new vote
if str(id) in vote_data: if str(id) not in vote_data:
if action == 'upvote':
quote.votes += 1
vote_data[str(id)] = 'upvote'
elif action == 'downvote':
quote.votes -= 1
vote_data[str(id)] = 'downvote'
flash("Thank you for voting!", 'success')
else:
previous_action = vote_data[str(id)] previous_action = vote_data[str(id)]
if previous_action == action: if previous_action == action:
# Undo the vote # If the user clicks the same action again, undo the vote
if action == 'upvote': if action == 'upvote':
quote.votes -= 1 quote.votes -= 1
elif action == 'downvote': elif action == 'downvote':
quote.votes += 1 quote.votes += 1
del vote_data[str(id)] # Remove vote from record del vote_data[str(id)] # Remove the vote record (undo)
flash("Your vote has been undone.", 'success') flash("Your vote has been undone.", 'success')
else: else:
# Switching from upvote to downvote or vice versa # If the user switches votes (upvote -> downvote or vice versa)
if action == 'upvote': if previous_action == 'upvote' and action == 'downvote':
quote.votes += 2 # From downvote to upvote, effectively +2 quote.votes -= 2 # Undo upvote (+1) and apply downvote (-1)
elif action == 'downvote': vote_data[str(id)] = 'downvote'
quote.votes -= 2 # From upvote to downvote, effectively -2 elif previous_action == 'downvote' and action == 'upvote':
vote_data[str(id)] = action # Update vote action quote.votes += 2 # Undo downvote (-1) and apply upvote (+1)
vote_data[str(id)] = 'upvote'
flash("Your vote has been changed.", 'success') flash("Your vote has been changed.", 'success')
else:
# First-time voting on this quote (starting from neutral)
if action == 'upvote':
quote.votes += 1 # Add +1 vote
elif action == 'downvote':
quote.votes -= 1 # Subtract -1 vote
vote_data[str(id)] = action # Store vote action
flash("Thank you for voting!", 'success')
# Save the updated vote data to the cookie # Save the updated vote data to the cookie
try: try:
db.session.commit() db.session.commit()
page = request.args.get('page', 1) page = request.args.get('page', 1)
resp = make_response(redirect(url_for('browse', page=page))) resp = make_response(redirect(url_for('browse', page=page)))
resp.set_cookie('votes', json.dumps(vote_data), max_age=60*60*24*365) # Cookie for 1 year resp.set_cookie('votes', json.dumps(vote_data), max_age=60*60*24*365) # Store vote history in cookies for 1 year
return resp return resp
except Exception as e: except Exception as e:
db.session.rollback() db.session.rollback()
flash(f"Error voting on quote: {e}", 'error') flash(f"Error while voting: {e}", 'error')
return redirect(url_for('browse', page=page)) return redirect(url_for('browse', page=page))
# Route for displaying a random quote # Route for displaying a random quote
@@ -247,20 +249,36 @@ def delete_quote(quote_id):
@app.route('/search', methods=['GET']) @app.route('/search', methods=['GET'])
def search(): def search():
query = request.args.get('q', '').strip() # Get the search query and trim whitespace query = request.args.get('q', '').strip() # Get the search query
quotes = [] quotes = []
# Query the counts of approved and pending quotes # Query counts of approved and pending quotes
approved_count = Quote.query.filter_by(status=1).count() approved_count = Quote.query.filter_by(status=1).count()
pending_count = Quote.query.filter_by(status=0).count() pending_count = Quote.query.filter_by(status=0).count()
if query: if query:
# Perform the search only if the query is provided # Perform text search in quotes
quotes = Quote.query.filter(Quote.text.like(f'%{query}%'), Quote.status == 1).all() quotes = Quote.query.filter(Quote.text.like(f'%{query}%'), Quote.status == 1).all()
# Render the search page with the results, counts, and search query
return render_template('search.html', quotes=quotes, query=query, approved_count=approved_count, pending_count=pending_count) return render_template('search.html', quotes=quotes, query=query, approved_count=approved_count, pending_count=pending_count)
@app.route('/read', methods=['GET'])
def read_quote():
quote_id = request.args.get('id', type=int) # Get the quote number
if not quote_id:
flash("Quote number is required.", 'error')
return redirect(url_for('search'))
# Find the quote by ID (only approved quotes)
quote = Quote.query.filter_by(id=quote_id, status=1).first()
if quote:
return render_template('quote.html', quote=quote)
else:
flash(f"No quote found with ID {quote_id}", 'error')
return redirect(url_for('search'))
# Route for browsing approved quotes # Route for browsing approved quotes
@app.route('/browse', methods=['GET']) @app.route('/browse', methods=['GET'])
def browse(): def browse():

View File

@@ -3,12 +3,12 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ircquotes: Search</title> <title>ircquotes: Search & Read</title>
<link rel="stylesheet" href="/static/css/styles.css"> <link rel="stylesheet" href="/static/css/styles.css">
</head> </head>
<body bgcolor="#ffffff" text="#000000" link="#c08000" vlink="#c08000" alink="#c08000"> <body>
<!-- Top Navigation Bar --> <!-- Header -->
<center> <center>
<table cellpadding="2" cellspacing="0" width="80%" border="0"> <table cellpadding="2" cellspacing="0" width="80%" border="0">
<tr> <tr>
@@ -16,85 +16,84 @@
<font size="+1"><b><i>ircquotes</i></b></font> <font size="+1"><b><i>ircquotes</i></b></font>
</td> </td>
<td bgcolor="#c08000" align="right"> <td bgcolor="#c08000" align="right">
<font face="arial" size="+1"><b>Search Quotes</b></font> <font face="arial" size="+1"><b>Search & Read Quotes</b></font>
</td> </td>
</tr> </tr>
</table> </table>
</center>
<!-- Navigation Links --> <!-- Navigation Bar -->
<center>
<table cellpadding="2" cellspacing="0" width="80%" border="0"> <table cellpadding="2" cellspacing="0" width="80%" border="0">
<tr> <tr>
<td class="footertext" align="left" bgcolor="#f0f0f0"></td>
<td align="right" bgcolor="#f0f0f0" class="toplinks" colspan="2"> <td align="right" bgcolor="#f0f0f0" class="toplinks" colspan="2">
<a href="/">Home</a> / <a href="/">Home</a> /
<a href="/random">Random</a> / <a href="/random">Random</a> /
<a href="/submit">Submit</a> / <a href="/submit">Submit</a> /
<a href="/browse">Browse</a> / <a href="/browse">Browse</a> /
<a href="/modapp">ModApp</a> / <a href="/modapp">ModApp</a> /
<a href="/search">Search</a> <a href="/search">Search</a> /
<a href="/faq">FAQ</a> <a href="/faq">FAQ</a>
</td> </td>
</tr> </tr>
</table> </table>
</center> </center>
<!-- Search Form --> <!-- Content Section -->
<center> <center>
<h2>Search for Quotes</h2> <div class="content-box">
<form action="/search" method="GET"> <!-- Search for Quotes -->
<input type="text" name="q" class="text" placeholder="Enter search term" required> <h2>Search for Quotes by Keyword</h2>
<input type="submit" value="Search" class="button"> <form action="/search" method="GET">
</form> <input type="text" name="q" class="text" placeholder="Enter search term" required>
<input type="submit" value="Search" class="button">
</form>
<!-- Show Search Results only if there is a search query --> <!-- Read Quote by Number -->
{% if query %} <h2>Read a Quote by Number</h2>
<h3>Search Results for "{{ query }}"</h3> <form action="/read" method="GET">
<input type="number" name="id" class="text" placeholder="Enter quote number" required>
<input type="submit" value="Read" class="button">
</form>
</div>
{% if quotes %} <!-- Results Section -->
<table cellpadding="0" cellspacing="3" width="80%"> <div class="results-box">
<tr> {% if query %}
<td class="bodytext" width="100%" valign="top"> <h3>Search Results for "{{ query }}"</h3>
{% for quote in quotes %} {% if quotes %}
<p class="quote"> <table cellpadding="0" cellspacing="3" width="80%">
<a href="/quote?id={{ quote.id }}" title="Permanent link to this quote."> <tr>
<b>#{{ quote.id }}</b> <td class="bodytext" width="100%" valign="top">
</a> {% for quote in quotes %}
<a href="/vote/{{ quote.id }}/upvote" class="qa">+</a> <p class="quote">
(<font color="green">{{ quote.votes }}</font>) <a href="/quote?id={{ quote.id }}" title="Permanent link to this quote.">
<a href="/vote/{{ quote.id }}/downvote" class="qa">-</a> <b>#{{ quote.id }}</b>
</p> </a>
<p class="qt">{{ quote.text }}</p> <a href="/vote/{{ quote.id }}/upvote" class="qa">+</a>
<hr> (<font color="green">{{ quote.votes }}</font>)
{% endfor %} <a href="/vote/{{ quote.id }}/downvote" class="qa">-</a>
</td> </p>
</tr> <p class="qt">{{ quote.text }}</p>
</table> <hr>
{% else %} {% endfor %}
<h4>No quotes found for "{{ query }}".</h4> </td>
</tr>
</table>
{% else %}
<h4>No quotes found for "{{ query }}".</h4>
{% endif %}
{% endif %} {% endif %}
{% endif %} </div>
</center> </center>
<!-- Footer --> <!-- Footer -->
<center> <center>
<table border="0" cellpadding="2" cellspacing="0" width="80%" bgcolor="#c08000"> <table border="0" cellpadding="2" cellspacing="0" width="80%" bgcolor="#c08000">
<tr> <tr>
<td bgcolor="#f0f0f0" class="toplinks" colspan="2">
<a href="/">Home</a> /
<a href="/random">Random</a> /
<a href="/submit">Submit</a> /
<a href="/browse">Browse</a> /
<a href="/modapp">ModApp</a> /
<a href="/search">Search</a>
<a href="/faq">FAQ</a>
</td>
</tr>
<tr>
<td class="footertext" align="left">&nbsp;</td>
<td class="footertext" align="right">{{ approved_count }} quotes approved; {{ pending_count }} quotes pending</td> <td class="footertext" align="right">{{ approved_count }} quotes approved; {{ pending_count }} quotes pending</td>
</tr> </tr>
</table> </table>
<font size="-1">&#169; ircquotes 2024, All Rights Reserved.</font> <font size="-1">&#169; ircquotes 2024, All Rights Reserved.</font>
</center> </center>