Skip to content

Change main.py #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Python3-Flask-Blog
# Python3- Flask-Blog
This is a flask based blog whose frontend is created using bootstrap.
If you have any questions or suggestions, feel free to open an issue or pull request :)

# External Module Required For This Blog
1. flask
2. flask_sqlalchemy
3. werkzeug
4. flask_mail
24 changes: 10 additions & 14 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
{
"params":
{
"local_server":"True",
"params": {
"local_server": "True",
"local_uri": "mysql://root:@localhost/codingthunder",
"prod_uri":"mysql://root:@localhost/codingthunder",
"fb_url":"https://facebook.com/codingthunder",
"tw_url":"https://twitter.com/codingthunder",
"prod_uri": "mysql://root:@localhost/codingthunder",
"fb_url": "https://facebook.com/codingthunder",
"tw_url": "https://twitter.com/codingthunder",
"gh_url": "https://github.com/codingthunder",
"blog_name": "Coding Thunder",
"tag_line": "Heaven for programmers",
"gmail-user":"[email protected]",
"gmail-password":"gmailpassword",
"gmail-user": "[email protected]",
"gmail-password": "gmailpassword",
"about_text": "Hi my name is harry and I create programming tutorials and I am a good boy",
"no_of_posts": 3,
"login_image":"login.svg",
"admin_user":"harry",
"login_image": "login.svg",
"admin_user": "harry",
"admin_password": "subscribenow",
"upload_location":"C:\\Users\\Haris\\PycharmProjects\\Flask-tut\\static"


"upload_location": "C:\\Users\\Haris\\PycharmProjects\\Flask-tut\\static"
}

}
78 changes: 37 additions & 41 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from flask import Flask, render_template, request, session, redirect
from flask_sqlalchemy import SQLAlchemy
from werkzeug import secure_filename
from werkzeug.utils import secure_filename
from flask_mail import Mail
import json
import os
import math
from datetime import datetime


with open('config.json', 'r') as c:
params = json.load(c)["params"]

Expand All @@ -16,14 +15,14 @@
app.secret_key = 'super-secret-key'
app.config['UPLOAD_FOLDER'] = params['upload_location']
app.config.update(
MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = '465',
MAIL_USE_SSL = True,
MAIL_USERNAME = params['gmail-user'],
MAIL_PASSWORD= params['gmail-password']
MAIL_SERVER='smtp.gmail.com',
MAIL_PORT='465',
MAIL_USE_SSL=True,
MAIL_USERNAME=params['gmail-user'],
MAIL_PASSWORD=params['gmail-password']
)
mail = Mail(app)
if(local_server):
if (local_server):
app.config['SQLALCHEMY_DATABASE_URI'] = params['local_uri']
else:
app.config['SQLALCHEMY_DATABASE_URI'] = params['prod_uri']
Expand Down Expand Up @@ -53,28 +52,27 @@ class Posts(db.Model):
@app.route("/")
def home():
posts = Posts.query.filter_by().all()
last = math.ceil(len(posts)/int(params['no_of_posts']))
#[0: params['no_of_posts']]
#posts = posts[]
last = math.ceil(len(posts) / int(params['no_of_posts']))
# [0: params['no_of_posts']]
# posts = posts[]
page = request.args.get('page')
if(not str(page).isnumeric()):
if (not str(page).isnumeric()):
page = 1
page= int(page)
posts = posts[(page-1)*int(params['no_of_posts']): (page-1)*int(params['no_of_posts'])+ int(params['no_of_posts'])]
#Pagination Logic
#First
if (page==1):
page = int(page)
posts = posts[(page - 1) * int(params['no_of_posts']): (page - 1) * int(params['no_of_posts']) + int(
params['no_of_posts'])]
# Pagination Logic
# First
if (page == 1):
prev = "#"
next = "/?page="+ str(page+1)
elif(page==last):
next = "/?page=" + str(page + 1)
elif (page == last):
prev = "/?page=" + str(page - 1)
next = "#"
else:
prev = "/?page=" + str(page - 1)
next = "/?page=" + str(page + 1)



return render_template('index.html', params=params, posts=posts, prev=prev, next=next)


Expand All @@ -83,32 +81,31 @@ def post_route(post_slug):
post = Posts.query.filter_by(slug=post_slug).first()
return render_template('post.html', params=params, post=post)


@app.route("/about")
def about():
return render_template('about.html', params=params)


@app.route("/dashboard", methods=['GET', 'POST'])
def dashboard():

if ('user' in session and session['user'] == params['admin_user']):
posts = Posts.query.all()
return render_template('dashboard.html', params=params, posts = posts)

return render_template('dashboard.html', params=params, posts=posts)

if request.method=='POST':
if request.method == 'POST':
username = request.form.get('uname')
userpass = request.form.get('pass')
if (username == params['admin_user'] and userpass == params['admin_password']):
#set the session variable
# set the session variable
session['user'] = username
posts = Posts.query.all()
return render_template('dashboard.html', params=params, posts = posts)
return render_template('dashboard.html', params=params, posts=posts)

return render_template('login.html', params=params)


@app.route("/edit/<string:sno>", methods = ['GET', 'POST'])
@app.route("/edit/<string:sno>", methods=['GET', 'POST'])
def edit(sno):
if ('user' in session and session['user'] == params['admin_user']):
if request.method == 'POST':
Expand All @@ -119,7 +116,7 @@ def edit(sno):
img_file = request.form.get('img_file')
date = datetime.now()

if sno=='0':
if sno == '0':
post = Posts(title=box_title, slug=slug, content=content, tagline=tline, img_file=img_file, date=date)
db.session.add(post)
db.session.commit()
Expand All @@ -132,27 +129,27 @@ def edit(sno):
post.img_file = img_file
post.date = date
db.session.commit()
return redirect('/edit/'+sno)
return redirect('/edit/' + sno)
post = Posts.query.filter_by(sno=sno).first()
return render_template('edit.html', params=params, post=post, sno=sno)


@app.route("/uploader", methods = ['GET', 'POST'])
@app.route("/uploader", methods=['GET', 'POST'])
def uploader():
if ('user' in session and session['user'] == params['admin_user']):
if (request.method == 'POST'):
f= request.files['file1']
f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename) ))
f = request.files['file1']
f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
return "Uploaded successfully"



@app.route("/logout")
def logout():
session.pop('user')
return redirect('/dashboard')

@app.route("/delete/<string:sno>", methods = ['GET', 'POST'])

@app.route("/delete/<string:sno>", methods=['GET', 'POST'])
def delete(sno):
if ('user' in session and session['user'] == params['admin_user']):
post = Posts.query.filter_by(sno=sno).first()
Expand All @@ -161,21 +158,20 @@ def delete(sno):
return redirect('/dashboard')



@app.route("/contact", methods = ['GET', 'POST'])
@app.route("/contact", methods=['GET', 'POST'])
def contact():
if(request.method=='POST'):
if (request.method == 'POST'):
name = request.form.get('name')
email = request.form.get('email')
phone = request.form.get('phone')
message = request.form.get('message')
entry = Contacts(name=name, phone_num = phone, msg = message, date= datetime.now(),email = email )
entry = Contacts(name=name, phone_num=phone, msg=message, date=datetime.now(), email=email)
db.session.add(entry)
db.session.commit()
mail.send_message('New message from ' + name,
sender=email,
recipients = [params['gmail-user']],
body = message + "\n" + phone
recipients=[params['gmail-user']],
body=message + "\n" + phone
)
return render_template('contact.html', params=params)

Expand Down
5 changes: 5 additions & 0 deletions static/css/signin.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,29 @@ body {
padding: 15px;
margin: auto;
}

.form-signin .checkbox {
font-weight: 400;
}

.form-signin .form-control {
position: relative;
box-sizing: border-box;
height: auto;
padding: 10px;
font-size: 16px;
}

.form-signin .form-control:focus {
z-index: 2;
}

.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}

.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
Expand Down
30 changes: 15 additions & 15 deletions templates/about.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
{% extends "layout.html" %}

{% block body %}
<!-- Page Header -->
<header class="masthead" style="background-image: url('{{ url_for('static', filename='img/about-bg.jpg') }}')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="page-heading">
<h1>About Me</h1>
<span class="subheading">This is what I do.</span>
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="page-heading">
<h1>About Me</h1>
<span class="subheading">This is what I do.</span>
</div>
</div>
</div>
</div>
</div>
</div>
</header>

<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<p>{{params['about_text']}}</p>
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<p>{{ params['about_text'] }}</p>
</div>
</div>
</div>
</div>

{% endblock %}
{% endblock %}
Loading