Created usable quote IDs, and working latest quotes page
This commit is contained in:
parent
fd574bf35c
commit
b594bec643
7 changed files with 88 additions and 24 deletions
1
Pipfile
1
Pipfile
|
@ -6,6 +6,7 @@ name = "pypi"
|
|||
[packages]
|
||||
flask = "*"
|
||||
pymongo = "*"
|
||||
nanoid = "*"
|
||||
|
||||
[dev-packages]
|
||||
|
||||
|
|
10
Pipfile.lock
generated
10
Pipfile.lock
generated
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"_meta": {
|
||||
"hash": {
|
||||
"sha256": "1e6dcb2055e48235dc444cfc753bcacfcf9395313c7e6c95327e169d78010881"
|
||||
"sha256": "21a18c278c3945c189875ebf5a376dec92fefbece3a19a7038e41584e4d8d301"
|
||||
},
|
||||
"pipfile-spec": 6,
|
||||
"requires": {
|
||||
|
@ -87,6 +87,14 @@
|
|||
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
|
||||
"version": "==1.1.1"
|
||||
},
|
||||
"nanoid": {
|
||||
"hashes": [
|
||||
"sha256:5a80cad5e9c6e9ae3a41fa2fb34ae189f7cb420b2a5d8f82bd9d23466e4efa68",
|
||||
"sha256:90aefa650e328cffb0893bbd4c236cfd44c48bc1f2d0b525ecc53c3187b653bb"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==2.0.0"
|
||||
},
|
||||
"pymongo": {
|
||||
"hashes": [
|
||||
"sha256:03dc64a9aa7a5d405aea5c56db95835f6a2fa31b3502c5af1760e0e99210be30",
|
||||
|
|
28
db.py
28
db.py
|
@ -1,21 +1,35 @@
|
|||
import pymongo
|
||||
import pymongo, nanoid
|
||||
connection = pymongo.MongoClient()
|
||||
db = connection.smash
|
||||
qdb = db.quotes
|
||||
adb = db.accounts
|
||||
|
||||
live_quotes_count = lambda: qdb.find({ "hidden": False, "approved": True })
|
||||
|
||||
def get_random_quote():
|
||||
x = dict( qdb.aggregate([
|
||||
{ "$match": { "hidden": False } },
|
||||
|
||||
#TODO: there might be a better way to get a random document
|
||||
x = list(qdb.aggregate([
|
||||
{ "$match": { "hidden": False, "approved": True } },
|
||||
{ "$sample": { "size": 1 } }
|
||||
]) )
|
||||
print(x)
|
||||
return(x)
|
||||
]))[0]
|
||||
|
||||
return(x if x else False)
|
||||
|
||||
|
||||
def add_quote(quote, tags, author):
|
||||
qdb.insert_one({
|
||||
"id": nanoid.generate(size=12),
|
||||
"quote": quote,
|
||||
"tags": tags,
|
||||
"author": author
|
||||
"author": author,
|
||||
"hidden": False,
|
||||
"approved": False
|
||||
})
|
||||
|
||||
|
||||
def get_latest_quotes(page=1):
|
||||
return list(qdb \
|
||||
.find({ "hidden": False, "approved": True }) \
|
||||
.sort( "_id", 1 ) \
|
||||
.limit(page*10)[(page-1)*10:])
|
||||
|
|
24
main.py
24
main.py
|
@ -1,14 +1,14 @@
|
|||
import datetime, random, json, pymongo
|
||||
from flask import Flask, render_template, Markup, request, abort, session, g
|
||||
import db as dblib
|
||||
import db
|
||||
#from smash import app, conf
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
#Connect to and define db
|
||||
connection = pymongo.MongoClient()
|
||||
db = connection.testdb
|
||||
qdb = db.quotes
|
||||
real_db = connection.testdb
|
||||
qdb = real_db.quotes
|
||||
|
||||
def timestamp():
|
||||
return datetime.datetime.now().strftime("%H:%M:%S %d/%m/%y")
|
||||
|
@ -26,13 +26,13 @@ def index():
|
|||
news = "No quotes yet!"
|
||||
#welcome = conf.config['MOTD']
|
||||
welcome = "MOTD"
|
||||
print(qdb.find().count())
|
||||
#print(qdb.find().count())
|
||||
qCount = qdb.find({"hidden": False}).count()
|
||||
print(type(qCount))
|
||||
news = "Home of " + str(qCount) + " dumb quotes!"
|
||||
#print(type(qCount))
|
||||
news = "Home of " + "5" + " dumb quotes!"
|
||||
if qCount > 0:
|
||||
rand_quote = dblib.get_random_quote()
|
||||
quote_text = Markup.escape(rand_quote['quote'])#.replace('\n', '<br />')
|
||||
rand_quote = db.get_random_quote()
|
||||
quote_text = Markup.escape(rand_quote['quote']) if rand_quote else "There are no quotes in the database!"
|
||||
news = quote_text
|
||||
permalink = str(rand_quote['id'])
|
||||
|
||||
|
@ -44,6 +44,14 @@ def index():
|
|||
permalink=permalink
|
||||
)
|
||||
|
||||
@app.route('/latest')
|
||||
def latest():
|
||||
return render_template(
|
||||
"latest.html",
|
||||
title="Latest",
|
||||
quotes=db.get_latest_quotes()
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", debug=True)
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
<h1>{{ title }}</h1>
|
||||
<p>{{ welcometext }}</p>
|
||||
<pre>{{ newstext|safe }}</pre>
|
||||
<a href="{{permalink}}">Permalink</a>
|
||||
<a href="/quote/{{permalink}}">Permalink</a>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
{% extends "layout.html" %}
|
||||
{% block customhead %}
|
||||
<title>{{ title }}</title>
|
||||
{% endblock %}
|
||||
{% block nav_latest %}active{% endblock %}
|
||||
{% block content %}
|
||||
|
||||
<div class = "container">
|
||||
<h1 class="display-4 mb-5">Latest Quotes</h1>
|
||||
|
||||
{% for quote in quotes %}
|
||||
<div>
|
||||
<div class="d-flex justify-content-between">
|
||||
<p class="h4">Quote {{quote.id}}</p>
|
||||
<span>
|
||||
<!-- These buttons will be admin tools -->
|
||||
<button class="btn btn-link btn-sm"><i class="fas fa-pencil-alt"></i></button>
|
||||
<button class="btn btn-link text-danger btn-sm"><i class="fas fa-times"></i></button>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<pre>{{ quote.quote }}</pre>
|
||||
|
||||
<p class="text-muted small">Tags: {% for tag in quote.tags %}<a href="/tag/{{tag}}">{{tag}}</a>{% if not loop.last %}, {% endif %}{% endfor %}</p>
|
||||
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
|
@ -1,11 +1,14 @@
|
|||
<html>
|
||||
<head>
|
||||
<!-- Bootstrap CSS -->
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
|
||||
|
||||
<meta name="viewport" content="width=device-with,initial-scale=1.0">
|
||||
{% block customhead %}
|
||||
{% endblock %}
|
||||
<!-- Font Awesome -->
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" />
|
||||
|
||||
<meta name="viewport" content="width=device-with,initial-scale=1.0">
|
||||
{% block customhead %}
|
||||
{% endblock %}
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-4">
|
||||
|
@ -16,10 +19,10 @@
|
|||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||
<ul class="navbar-nav mr-auto">
|
||||
<li class="nav-item {% block nav_home %}{% endblock %}">
|
||||
<a class="nav-link" href="#">Home</a>
|
||||
<a class="nav-link" href="/">Home</a>
|
||||
</li>
|
||||
<li class="nav-item {% block nav_latest %}{% endblock %}">
|
||||
<a class="nav-link" href="#">Latest</a>
|
||||
<a class="nav-link" href="/latest">Latest</a>
|
||||
</li>
|
||||
<li class="nav-item {% block nav_tags %}{% endblock %}">
|
||||
<a class="nav-link" href="#">Tags</a>
|
||||
|
@ -37,4 +40,4 @@
|
|||
{% endblock %}
|
||||
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
|
Loading…
Add table
Reference in a new issue