Skip to content

Commit

Permalink
master
Browse files Browse the repository at this point in the history
  • Loading branch information
Anas Arbaoui committed Sep 22, 2020
1 parent fbc77e1 commit edb8629
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 192 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ venv
.vscode
__pycache__
dummy.sqlite3
test.py
test.py
test
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ python3.8 main.py
* Simple design using bootstrap.

## Planned Features
* User and admin panels.
* ~~User~~ and admin panels.
* A better way to write blog posts.
* Easy first time run install.

## Release History

* 0.0.2
* Added the user panel to change your password and email and incase you're admin delete blogs you made
* Changed the structure of the code now it in blueprints instead of one file
* Changed the way links are handeled in the read() function


* 0.0.1
* initial release

Expand Down
40 changes: 40 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from datetime import timedelta
from datetime import datetime
from skgen import SkGen


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///dummy.sqlite3'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)


class users(db.Model):
_id = db.Column("id", db.Integer, primary_key=True)
username = db.Column("username", db.String(16))
email = db.Column("email", db.String(320))
password = db.Column("password", db.String(32))
type = db.Column("type", db.String(60))

def __init__(self, username, email, password, type):
self.username = username
self.email = email
self.password = password
self.type = type


class posts(db.Model):
_id = db.Column("id", db.Integer, primary_key=True)
title = db.Column("title", db.String(64))
post = db.Column("post", db.String)
posted_by = db.Column("posted_by", db.String)
created_date = db.Column("created_date",
db.String, default=datetime.utcnow, nullable=False)
views = db.Column("views", db.Integer)

def __init__(self, title, post, posted_by):
self.title = title
self.post = post
self.posted_by = posted_by
56 changes: 11 additions & 45 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,20 @@
from flask import Flask, render_template, request, redirect, url_for, session, flash
from skgen import SkGen
from cryptpw import Crypt
from datetime import timedelta
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import desc
from datetime import timedelta
from datetime import datetime
from __init__ import app, db, posts, users
from admin import admin
from user import user

# Generates a unique secret key
app.register_blueprint(admin, url_prefix='/admin')
app.register_blueprint(user, url_prefix='/user')
sk = SkGen(64)
cookie_life_time_days = 31
app = Flask(__name__)
app.secret_key = sk.gen()
app.permanent_session_lifetime = timedelta(days=cookie_life_time_days)
app.register_blueprint(admin, url_prefix='/admin')
app.register_blueprint(user, url_prefix='/user')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///dummy.sqlite3'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)


class users(db.Model):
_id = db.Column("id", db.Integer, primary_key=True)
username = db.Column("username", db.String(16))
email = db.Column("email", db.String(320))
password = db.Column("password", db.String(32))
type = db.Column("type", db.String(60))

def __init__(self, username, email, password, type):
self.username = username
self.email = email
self.password = password
self.type = type


class posts(db.Model):
_id = db.Column("id", db.Integer, primary_key=True)
title = db.Column("title", db.String(64))
post = db.Column("post", db.String)
posted_by = db.Column("posted_by", db.String)
created_date = db.Column("created_date",
db.String, default=datetime.utcnow, nullable=False)
views = db.Column("views", db.Integer)

def __init__(self, title, post, posted_by):
self.title = title
self.post = post
self.posted_by = posted_by


@app.route('/')
Expand All @@ -61,7 +28,7 @@ def login():
if request.method == "POST":
# Getting request form data
req = request.form
email = email = req.get('email')
email = req.get('email')
password = req.get('password')
# Checking the login credantials and if False sends an error
email_found = users.query.filter_by(email=email).first()
Expand Down Expand Up @@ -136,6 +103,7 @@ def post():
data = posts(title=title, post=post, posted_by=username)
db.session.add(data)
db.session.commit()
flash('Your thread has been posted.')
if 'email' and 'username' and 'type' in session:
if session['type'] == 'reader':
flash('you are not allowed to be on this page.')
Expand All @@ -146,16 +114,14 @@ def post():
return redirect(url_for('login'))


@app.route('/read/<link>')
def read(link):
title = link.replace('-', ' ')
return render_template('public/read.html', title=title, post=posts.query.filter_by(title=title).first())
@app.route('/read/<id>')
def read(id):
return render_template('public/read.html', post=posts.query.filter_by(_id=id).first())


@app.route('/logout')
def logout():
session.pop("email", None)
session.pop("password", None)
session.pop("username", None)
session.pop("type", None)
flash('you have been logged off.')
Expand All @@ -164,4 +130,4 @@ def logout():

if __name__ == "__main__":
db.create_all()
app.run(debug=True, host='192.168.1.9', port=80)
app.run(debug=True, host='192.168.1.6', port=80)
1 change: 1 addition & 0 deletions static/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
}
.black-text{
color: black;
font-size: 1vw;
}
Binary file added static/img/favicon.ico
Binary file not shown.
68 changes: 36 additions & 32 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@1,500&display=swap" rel="stylesheet">
<link rel="icon" href="{{ url_for('static', filename='img/favicon.ico') }}" type="image/x-icon">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
Expand All @@ -16,9 +17,9 @@
</head>

{% if 'user' and 'email' in session %}
{% set logged_in = True %}
{% set logged_in = True %}
{% else %}
{% set logged_in = False %}
{% set logged_in = False %}
{% endif %}

{% set type = session['type'] %}
Expand All @@ -34,40 +35,40 @@
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav mr-auto">
{% if logged_in == False %}
<li class="nav-item">
<br>
</li>
<li class="nav-item">
<br>
</li>
{% endif %}
{% if logged_in %}
{% if type == 'admin' %}
<li class="nav-item">
<a class="nav-link" href="/post">Post</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/admin">Admin panel</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/user">User panel</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="/user">User panel</a>
</li>
{% endif %}
{% if type == 'admin' %}
<li class="nav-item">
<a class="nav-link" href="/post">Post</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/admin">Admin panel</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/user">User panel</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="/user">User panel</a>
</li>
{% endif %}
{% endif %}
</ul>
<ul class="navbar-nav">
{% if logged_in %}
<li class="nav-item">
<a class="nav-link" href="/logout">Logout</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/logout">Logout</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="/login">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/register">Register</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/login">Login</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/register">Register</a>
</li>
{% endif %}
</ul>
</div>
Expand All @@ -80,7 +81,10 @@
{% endblock %}
<br>
<nav class="navbar sticky-bottom navbar-dark">
<p><a style="color: white;" href="https://twitter.com/Anarbbb" target="_blank">Twitter <i class="fa fa-twitter" aria-hidden="true"></i></a> <a style="color: white;" href="https://github.com/Anarbb" target="_blank">Github <i class="fa fa-github" aria-hidden="true"></i></a></p>
<p><a style="color: white;" href="https://twitter.com/Anarbbb" target="_blank">Twitter <i
class="fa fa-twitter" aria-hidden="true"></i></a> <a style="color: white;"
href="https://github.com/Anarbb" target="_blank">Github <i class="fa fa-github"
aria-hidden="true"></i></a></p>
</nav>
</div>
<!-- Optional JavaScript -->
Expand All @@ -95,7 +99,7 @@
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.9/validator.min.js"></script>
{% block script %}{% endblock %}

</body>

</html>
6 changes: 3 additions & 3 deletions templates/public/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

{% block content %}
{% with messages = get_flashed_messages() %}
{% for msg in messages %}
<p class="text-light float-right">{{ msg }}</p>
{% endfor %}
{% for msg in messages %}
<p class="text-light float-right">{{ msg }}</p>
{% endfor %}
{% endwith %}
<br><br><br>
<h1>Still working on this one</h1>
Expand Down
34 changes: 18 additions & 16 deletions templates/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

{% block title %}Home{% endblock %}

{% block head %}<link href="{{ url_for('static', filename='css/index.css') }}" rel="stylesheet">{% endblock %}
{% block head %}
<link href="{{ url_for('static', filename='css/index.css') }}" rel="stylesheet">{% endblock %}


{% block content %}
{% with messages = get_flashed_messages() %}
{% for msg in messages %}
<p class="text-light float-right">{{ msg }}</p>
{% endfor %}
{% for msg in messages %}
<p class="text-light float-right">{{ msg }}</p>
{% endfor %}
{% endwith %}
<br><br>

<br><br><br><br><br>
{% if blogs == [] %}
<h2 style="color: white;">There is no post threads.</h2>
<br>
Expand All @@ -21,17 +22,18 @@ <h2 style="color: white;">There is no post threads.</h2>

<div class="card border-light mb-3">
<div class="card-body">
<h5 class="card-title">{{ blog.title }}</h5>
{% if blog.post|length >= 200 %}
{% set post = blog.post[:200] %}
<p class="card-text">{{ post }}...</p>
{% else %}
<p class="card-text">{{ blog.post }}...</p>
{% endif %}
<p class="card-text"><small class="black-text d-flex justify-content-start">{{ blog.created_date[:-7] }}</small></p>
<a href="/read/{{ blog.title.replace(' ', '-') }}" class="black-text d-flex justify-content-end">
<h5>Read more <i class="fa fa-angle-double-right" aria-hidden="true"></i></h5>
</a>
<h5 class="card-title">{{ blog.title }}</h5>
{% if blog.post|length >= 200 %}
{% set post = blog.post[:200] %}
<p class="card-text">{{ post }}...</p>
{% else %}
<p class="card-text">{{ blog.post }}...</p>
{% endif %}
<p class="card-text"><small class="black-text d-flex justify-content-start">{{ blog.created_date[:-7] }}</small>
</p>
<a href="/read/{{ blog._id }}" class="black-text d-flex justify-content-end">
<h5>Read more <i class="fa fa-angle-double-right" aria-hidden="true"></i></h5>
</a>
</div>
</div>
{% endfor %}
Expand Down
Loading

0 comments on commit edb8629

Please sign in to comment.