Browse Source

Black theme applied

chopin42 4 years ago
parent
commit
2dceff7810
4 changed files with 224 additions and 159 deletions
  1. 95 48
      app.py
  2. 80 75
      helper.py
  3. 48 35
      login.py
  4. 1 1
      todo.md

+ 95 - 48
app.py

@@ -1,79 +1,126 @@
-from flask import Flask, redirect, render_template, request, flash # import flask
+from flask import Flask, redirect, render_template, request, flash  # import flask
-from login import * # import the login helper file
+from login import *  # import the login helper file
-import helper, json, time # Import the helper file and other modules
+import helper, json, time  # Import the helper file and other modules
-app = Flask(__name__) # Create the app
+
-db = helper.createDb(app) # Get the database
+app = Flask(__name__)  # Create the app
-Topic, Reply, db = db['Topic'], db['Reply'], db['db'] # Get the classes
+db = helper.createDb(app)  # Get the database
-User = initLogin(app, db) # Create and init the login manager (login helper file)
+Topic, Reply, db = db["Topic"], db["Reply"], db["db"]  # Get the classes
+User = initLogin(app, db)  # Create and init the login manager (login helper file)
 db.create_all()
 db.create_all()
-def getTime(): return time.asctime( time.localtime(time.time()) ) # Get the current time and date
 
 
-@app.route('/login') # Render the login page, nothing more
-def renderLogin(): 	logout_user(); return render_template('login.html')
 
 
-@app.route('/login/post', methods=['POST'])
+def getTime():
-def login(): # Login backend
+    return time.asctime(time.localtime(time.time()))  # Get the current time and date
-	try: loginUser(request.form['username'], request.form['password'], User); return redirect('/')
+
-	except: 
+
-		try: createUser(request.form['username'], request.form['password'], db, User); return redirect('/')
+@app.route("/login")  # Render the login page, nothing more
-		except: return redirect('/login')
+def renderLogin():
+    logout_user()
+    return render_template("login.html")
+
+
+@app.route("/login/post", methods=["POST"])
+def login():  # Login backend
+    try:
+        loginUser(request.form["username"], request.form["password"], User)
+        return redirect("/")
+    except:
+        try:
+            createUser(request.form["username"], request.form["password"], db, User)
+            return redirect("/")
+        except:
+            return redirect("/login")
+
 
 
-@app.route('/') # Render the homepage
+@app.route("/")  # Render the homepage
 def renderHome():
 def renderHome():
-    return render_template('index.html', topics=Topic.query.order_by(Topic.lastActivity.desc())) # List all the topics in the reversed order
+    return render_template(
+        "index.html", topics=Topic.query.order_by(Topic.lastActivity.desc())
+    )  # List all the topics in the reversed order
 
 
-@app.route('/post') # Render the 'write new topic' box
-@login_required 
-def renderCreateTopic(): return render_template('post.html')
 
 
-@app.route('/post/post', methods=['POST']) # Backend of the new topic box
+@app.route("/post")  # Render the 'write new topic' box
+@login_required
+def renderCreateTopic():
+    return render_template("post.html")
+
+
+@app.route("/post/post", methods=["POST"])  # Backend of the new topic box
 def createTopic():
 def createTopic():
-    topic = Topic(request.form['title'], request.form['content'], getTime(), getUsername(), request.form['category'])
+    topic = Topic(
+        request.form["title"],
+        request.form["content"],
+        getTime(),
+        getUsername(),
+        request.form["category"],
+    )
     db.session.add(topic)
     db.session.add(topic)
     db.session.commit()
     db.session.commit()
-    return redirect('/topic/' + str(topic.id))
+    return redirect("/topic/" + str(topic.id))
 
 
-@app.route('/topic/<id>') # Render a topic
+
+@app.route("/topic/<id>")  # Render a topic
 def renderTopic(id):
 def renderTopic(id):
-    topic = Topic.query.filter_by(id=id).first_or_404(); topic.views += 1 # Add one view
+    topic = Topic.query.filter_by(id=id).first_or_404()
-    db.session.add(topic); db.session.commit() # Change the value of the view in the database
+    topic.views += 1  # Add one view
-    return render_template('topic.html', topic=topic, replies=Reply.query.filter_by(inReplyTo=id)) # Render the page
+    db.session.add(topic)
+    db.session.commit()  # Change the value of the view in the database
+    return render_template(
+        "topic.html", topic=topic, replies=Reply.query.filter_by(inReplyTo=id)
+    )  # Render the page
 
 
-@app.route('/reply/<id>', methods=['POST']) # Reply to a post.
+
+@app.route("/reply/<id>", methods=["POST"])  # Reply to a post.
 @login_required
 @login_required
 def replyTo(id):
 def replyTo(id):
-    topic = Topic.query.filter_by(id=id).first_or_404(); topic.reply(getTime()) # Reply to the topic
+    topic = Topic.query.filter_by(id=id).first_or_404()
-    reply = Reply(request.form['body'], getTime(), current_user.username, id) # Add the reply
+    topic.reply(getTime())  # Reply to the topic
-    db.session.add(reply); db.session.add(topic); db.session.commit() # Add everything in the database
+    reply = Reply(
-    return redirect('/topic/' + str(id)) # Redirect to the correct page
+        request.form["body"], getTime(), current_user.username, id
+    )  # Add the reply
+    db.session.add(reply)
+    db.session.add(topic)
+    db.session.commit()  # Add everything in the database
+    return redirect("/topic/" + str(id))  # Redirect to the correct page
 
 
-@app.route('/like/<id>') # Like a topic
+
+@app.route("/like/<id>")  # Like a topic
 @login_required
 @login_required
 def likeTopic(id):
 def likeTopic(id):
     topic = Topic.query.filter_by(id=id).first_or_404()
     topic = Topic.query.filter_by(id=id).first_or_404()
-    topic.like(current_user.username) # Call the 'like' function of the class 'Topic'
+    topic.like(current_user.username)  # Call the 'like' function of the class 'Topic'
-    db.session.add(topic); db.session.commit()
+    db.session.add(topic)
-    return redirect('/topic/' + str(id))
+    db.session.commit()
+    return redirect("/topic/" + str(id))
 
 
-@app.route('/like/reply/<id>/<idt>') # Like a reply
+
+@app.route("/like/reply/<id>/<idt>")  # Like a reply
 @login_required
 @login_required
 def likeReply(id, idt):
 def likeReply(id, idt):
     reply = Reply.query.filter_by(id=id).first_or_404()
     reply = Reply.query.filter_by(id=id).first_or_404()
-    reply.like(current_user.username) # Call the like function of the class Reply
+    reply.like(current_user.username)  # Call the like function of the class Reply
-    db.session.add(reply); db.session.commit()
+    db.session.add(reply)
-    return redirect('/topic/' + str(idt)) # Return to the topic
+    db.session.commit()
+    return redirect("/topic/" + str(idt))  # Return to the topic
+
 
 
-@app.route('/top') # Order the list of posts by thoses who have the biggest number of replies
+@app.route(
+    "/top"
+)  # Order the list of posts by thoses who have the biggest number of replies
 def topList():
 def topList():
     topics = Topic.query.order_by(Topic.repliesNum.desc())
     topics = Topic.query.order_by(Topic.repliesNum.desc())
-    return render_template('index.html', topics=topics)
+    return render_template("index.html", topics=topics)
 
 
-@app.route('/new') # Order the list like normal (redirect)
-def redirectIndex(): return redirect('/')
 
 
-@app.route('/cat/<category>') # Get the list of posts in a category
+@app.route("/new")  # Order the list like normal (redirect)
+def redirectIndex():
+    return redirect("/")
+
+
+@app.route("/cat/<category>")  # Get the list of posts in a category
 def catList(category):
 def catList(category):
     topics = Topic.query.filter_by(category=category).order_by(Topic.id.desc())
     topics = Topic.query.filter_by(category=category).order_by(Topic.id.desc())
-    return render_template('index.html', topics=topics)
+    return render_template("index.html", topics=topics)
+
 
 
-app.run(debug=True) # Run the app in mode debug (change to False otherwise)
+app.run(debug=True)  # Run the app in mode debug (change to False otherwise)

+ 80 - 75
helper.py

@@ -4,81 +4,86 @@ import json
 
 
 # This file contains all the databases.
 # This file contains all the databases.
 def createDb(app):
 def createDb(app):
-	app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
+    app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.db"
-	app.config['SECRET_KEY'] = 'hard to guess thing'
+    app.config["SECRET_KEY"] = "hard to guess thing"
-	db = SQLAlchemy(app)
+    db = SQLAlchemy(app)
 
 
-	# Create the tables
+    # Create the tables
-	class Topic(db.Model):
+    class Topic(db.Model):
-	        id = db.Column(db.Integer, primary_key=True)
+        id = db.Column(db.Integer, primary_key=True)
-	        title = db.Column(db.Text)
+        title = db.Column(db.Text)
-	        content = db.Column(db.Text)
+        content = db.Column(db.Text)
-	        date = db.Column(db.Text)
+        date = db.Column(db.Text)
-	        lastActivity = db.Column(db.Text)
+        lastActivity = db.Column(db.Text)
-	        author = db.Column(db.Text)
+        author = db.Column(db.Text)
-	        category = db.Column(db.Text)
+        category = db.Column(db.Text)
-	        private = db.Column(db.Boolean)
+        private = db.Column(db.Boolean)
-	        likes = db.Column(db.Text)
+        likes = db.Column(db.Text)
-	        replies = db.Column(db.Text)
+        replies = db.Column(db.Text)
-	
-	        # Numbers
-	        likesNum = db.Column(db.Integer)
-	        repliesNum = db.Column(db.Integer)
-	        views = db.Column(db.Integer)
-		
-	        def __init__(self, title, content, date, author, category, private=False):
-	            self.title = title
-	            self.content = content
-	            self.date = date
-	            self.author = author
-	            self.category = category
-	            self.private = private
-	            self.likesNum = 0
-	            self.repliesNum = 0
-	            self.views = 0
-	            self.likes = '[]'
-	            self.lastActivity = date
-	        # Create like and other def for this thing
-	        def like(self, username):
-	            l = json.loads(self.likes)
-	            if username in l:
-	                l.remove(username)
-	                self.likesNum -= 1
-	                self.likes = json.dumps(l)
-	            else:
-	                l.append(username)
-	                self.likesNum += 1
-	                self.likes = json.dumps(l)
-	        def reply(self, date):
-	            self.lastActivity = date
-	            self.repliesNum += 1
-	class Reply(db.Model):
-	        id = db.Column(db.Integer, primary_key=True)
-	        content = db.Column(db.Text)
-	        date = db.Column(db.Text)
-	        author = db.Column(db.Text)
-	        inReplyTo = db.Column(db.Integer)
-	        likes = db.Column(db.Text)
 
 
-	        # Adding the numbers
+        # Numbers
-	        likesNum = db.Column(db.Integer)
+        likesNum = db.Column(db.Integer)
-	        def __init__(self, content, date, author, inReplyTo):
+        repliesNum = db.Column(db.Integer)
-	            self.content = content
+        views = db.Column(db.Integer)
-	            self.date = date
-	            self.author = author
-	            self.inReplyTo = inReplyTo
-	            self.likesNum = 0
-	            self.likes = '[]'
-	        def like(self, username):
-	            l = json.loads(self.likes)
-	            if username in l:
-	                l.remove(username)
-	                self.likesNum -= 1
-	                self.likes = json.dumps(l)
-	            else:
-	                l.append(username)
-	                self.likesNum += 1
-	                self.likes = json.dumps(l)
 
 
-	# Return the data
+        def __init__(self, title, content, date, author, category, private=False):
-	return {'db': db, 'Topic': Topic, 'Reply': Reply}
+            self.title = title
+            self.content = content
+            self.date = date
+            self.author = author
+            self.category = category
+            self.private = private
+            self.likesNum = 0
+            self.repliesNum = 0
+            self.views = 0
+            self.likes = "[]"
+            self.lastActivity = date
+
+        # Create like and other def for this thing
+        def like(self, username):
+            l = json.loads(self.likes)
+            if username in l:
+                l.remove(username)
+                self.likesNum -= 1
+                self.likes = json.dumps(l)
+            else:
+                l.append(username)
+                self.likesNum += 1
+                self.likes = json.dumps(l)
+
+        def reply(self, date):
+            self.lastActivity = date
+            self.repliesNum += 1
+
+    class Reply(db.Model):
+        id = db.Column(db.Integer, primary_key=True)
+        content = db.Column(db.Text)
+        date = db.Column(db.Text)
+        author = db.Column(db.Text)
+        inReplyTo = db.Column(db.Integer)
+        likes = db.Column(db.Text)
+
+        # Adding the numbers
+        likesNum = db.Column(db.Integer)
+
+        def __init__(self, content, date, author, inReplyTo):
+            self.content = content
+            self.date = date
+            self.author = author
+            self.inReplyTo = inReplyTo
+            self.likesNum = 0
+            self.likes = "[]"
+
+        def like(self, username):
+            l = json.loads(self.likes)
+            if username in l:
+                l.remove(username)
+                self.likesNum -= 1
+                self.likes = json.dumps(l)
+            else:
+                l.append(username)
+                self.likesNum += 1
+                self.likes = json.dumps(l)
+
+    # Return the data
+    return {"db": db, "Topic": Topic, "Reply": Reply}

+ 48 - 35
login.py

@@ -1,56 +1,69 @@
 # This is only a module to make the login process faster and easier. Don't care about it unless you also wants a simple login for your app
 # This is only a module to make the login process faster and easier. Don't care about it unless you also wants a simple login for your app
 import hashlib as hl
 import hashlib as hl
-from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
+from flask_login import (
+    LoginManager,
+    UserMixin,
+    login_user,
+    logout_user,
+    login_required,
+    current_user,
+)
+
 
 
 def initLogin(app, db):
 def initLogin(app, db):
     # Create the User clasis
     # Create the User clasis
-	class User(UserMixin, db.Model):
+    class User(UserMixin, db.Model):
-	    id = db.Column(db.Integer, primary_key=True)
+        id = db.Column(db.Integer, primary_key=True)
-	    username = db.Column(db.Text, unique=True)
+        username = db.Column(db.Text, unique=True)
-	    password = db.Column(db.Text)
+        password = db.Column(db.Text)
-	    def __init__(self, username, password):
+
-	        self.username = username
+        def __init__(self, username, password):
-	        self.password = password
+            self.username = username
+            self.password = password
 
 
     # Configure login
     # Configure login
-	login_manager = LoginManager()
+    login_manager = LoginManager()
-	login_manager.init_app(app)
+    login_manager.init_app(app)
-	@login_manager.user_loader
+
-	def load_user(user_id):
+    @login_manager.user_loader
-		return User.query.get(int(user_id))
+    def load_user(user_id):
+        return User.query.get(int(user_id))
+
+    return User
 
 
-	return User
-	
 
 
 def loginUser(username, password, User):
 def loginUser(username, password, User):
-	# Hash the username and the password
+    # Hash the username and the password
-	#username = hl.md5(bytes(username, 'utf-8')).hexdigest()
+    # username = hl.md5(bytes(username, 'utf-8')).hexdigest()
-	password = hl.md5(bytes(password, 'utf-8')).hexdigest()
+    password = hl.md5(bytes(password, "utf-8")).hexdigest()
-	
+
-	# Check if it exists
+    # Check if it exists
-	user = User.query.filter_by(username=username, password=password).first_or_404()
+    user = User.query.filter_by(username=username, password=password).first_or_404()
-	login_user(user)
+    login_user(user)
-	return True
+    return True
+
 
 
 def createUser(username, password, db, User):
 def createUser(username, password, db, User):
-	# hash the username and the password
+    # hash the username and the password
-	#username = hl.md5(bytes(username, 'utf-8')).hexdigest() # Comment this is you want a clear username
+    # username = hl.md5(bytes(username, 'utf-8')).hexdigest() # Comment this is you want a clear username
-	password = hl.md5(bytes(password, 'utf-8')).hexdigest()
+    password = hl.md5(bytes(password, "utf-8")).hexdigest()
-	
+
-	# Send them to db
+    # Send them to db
-	user = User(username, password)
+    user = User(username, password)
-	db.session.add(user)
+    db.session.add(user)
-	db.session.commit()
+    db.session.commit()
 
 
-	# Login the user
+    # Login the user
-	login_user(user)
+    login_user(user)
+
+    # return success
+    return True
 
 
-	# return success
-	return True
 
 
 def getUsername():
 def getUsername():
     return current_user.username
     return current_user.username
 
 
+
 # To restrict a page to a user just add @login_required
 # To restrict a page to a user just add @login_required
 # To logout just do logout_user()
 # To logout just do logout_user()
 # To get the current username do current_user.username
 # To get the current username do current_user.username

+ 1 - 1
todo.md

@@ -18,7 +18,7 @@ Trust system and badges
 Notifications
 Notifications
 Private msgs
 Private msgs
 Change password
 Change password
-Apply "black" code style to the project.
+x Apply "black" code style to the project.
 
 
 ## Warning
 ## Warning