From 700e36286c763a87ab0a3dfac0681fd8eadba8f1 Mon Sep 17 00:00:00 2001 From: Mukunda Selvaraj Date: Fri, 3 Aug 2018 00:01:59 +0530 Subject: [PATCH] Add files via upload The WebApp has been updated with few features like deleting the blogs, editing and other features have been added. --- WebApp/kredo/README.md | 4 + WebApp/kredo/app.py | 206 +++++++++++++++++- WebApp/kredo/data.pyc | Bin 1186 -> 1186 bytes WebApp/kredo/templates/add_blog.html | 15 ++ WebApp/kredo/templates/contact.html | 7 + WebApp/kredo/templates/dashboard.html | 32 +++ WebApp/kredo/templates/edit_blog.html | 15 ++ WebApp/kredo/templates/home.html | 19 ++ .../kredo/templates/includes/_messages.html | 8 + WebApp/kredo/templates/includes/_navbar.html | 53 +++-- WebApp/kredo/templates/layout.html | 8 +- WebApp/kredo/templates/login.html | 16 ++ 12 files changed, 351 insertions(+), 32 deletions(-) create mode 100644 WebApp/kredo/templates/add_blog.html create mode 100644 WebApp/kredo/templates/contact.html create mode 100644 WebApp/kredo/templates/dashboard.html create mode 100644 WebApp/kredo/templates/edit_blog.html create mode 100644 WebApp/kredo/templates/login.html diff --git a/WebApp/kredo/README.md b/WebApp/kredo/README.md index 9fc9ab3..12a2194 100644 --- a/WebApp/kredo/README.md +++ b/WebApp/kredo/README.md @@ -1 +1,5 @@ # kredo + +This WebApp basically requires your skills from python , sql and basic of html. +If you do hav efollowed java, javascript and other OOPS you will be able to follow it. +Im here using Ubuntu (linux), and able to run the local host through terminal. diff --git a/WebApp/kredo/app.py b/WebApp/kredo/app.py index 715ffbe..de2e60b 100644 --- a/WebApp/kredo/app.py +++ b/WebApp/kredo/app.py @@ -1,11 +1,12 @@ from flask import Flask, render_template, request, flash, redirect, url_for, session, logging -from data import Blogs +#from data import Blogs from flask_mysqldb import MySQL from flask_wtf import Form from wtforms import Form, StringField, TextAreaField, PasswordField, validators from wtforms.validators import Required from passlib.hash import sha256_crypt +from functools import wraps app = Flask(__name__) @@ -18,23 +19,49 @@ # intialise MySQL mysql = MySQL(app) -Blogs = Blogs() +#Blogs = Blogs() @app.route('/') def home(): - return render_template('home.html') + return render_template('home.html') @app.route('/about') def about(): - return render_template('about.html') + return render_template('about.html') + +@app.route('/contact') +def contact(): + return render_template('contact.html') @app.route('/blogs') def blogs(): - return render_template( 'blogs.html', blogs = Blogs ) + # Create cursor + cur = mysql.connection.cursor() + + # Get articles + result = cur.execute("SELECT * FROM blogs") + + blogs = cur.fetchall() + + if result > 0: + return render_template('blogs.html', blogs=blogs) + else: + msg = 'No Blogs Found' + return render_template('blogs.html', msg=msg) + # Close connection + cur.close() @app.route('/blog//') def blog(id): - return render_template('blog.html', blog = blog ) + # Create cursor + cur = mysql.connection.cursor() + + # Get articles + result = cur.execute("SELECT * FROM blogs WHERE id = %s", [id]) + + blog = cur.fetchone() + + return render_template('blog.html', blog = blog ) class RegisterForm(Form): name = StringField('Name', [validators.Length(min = 1, max = 50)]) @@ -69,10 +96,175 @@ def register(): flash('You are now registered and can login', 'success') - return redirect(url_for('home')) + return redirect(url_for('login')) return render_template('register.html', form = form) +#User login +@app.route('/login', methods=['GET', 'POST']) +def login(): + if request.method == 'POST': + username = request.form['username'] + password_candidate = request.form['password'] + + #create Cursor + cur = mysql.connection.cursor() + + #Get user by username + result = cur.execute("SELECT * FROM users WHERE username = %s", [username]) + + if result > 0: + # Get started hash + data = cur.fetchone() + password = data['password'] + + # compare the password + if sha256_crypt.verify(password_candidate, password): + # Passed + session['logged_in'] = True + session['username'] = username + + + flash('You are now logged in', 'success') + return redirect(url_for('dashboard')) + else: + error = 'Invalid login' + return render_template('login.html', error=error) + #Closed connection + cur.close() + + else: + error = 'Username not found' + return render_template('login.html', error=error) + + return render_template('login.html') + +def is_logged_in(f): + @wraps(f) + def wrap(*args, **kwargs): + if 'logged_in' in session: + return f(*args, **kwargs) + else: + flash('Unauthorized, Please login', 'danger') + return redirect(url_for('login')) + return wrap + +@app.route('/logout') +@is_logged_in +def logout(): + session.clear() + flash('You are now logged out', 'success') + return redirect(url_for('login')) + +@app.route('/dashboard') +@is_logged_in +def dashboard(): + # Create cursor + cur = mysql.connection.cursor() + + # Get articles + result = cur.execute("SELECT * FROM blogs") + + blogs = cur.fetchall() + + if result > 0: + return render_template('dashboard.html', blogs=blogs) + else: + msg = 'No Blogs Found' + return render_template('dashboard.html', msg=msg) + # Close connection + cur.close() + +# Article Form Class +class BlogForm(Form): + title = StringField('Title', [validators.Length(min=1, max=200)]) + body = TextAreaField('Body', [validators.Length(min=30)]) + +# Add Article +@app.route('/add_blog', methods=['GET', 'POST']) +@is_logged_in +def add_blog(): + form = BlogForm(request.form) + if request.method == 'POST' and form.validate(): + title = form.title.data + body = form.body.data + + # Create Cursor + cur = mysql.connection.cursor() + + # Execute + cur.execute("INSERT INTO blogs(title, body, author) VALUES(%s, %s, %s)",(title, body, session['username'])) + + # Commit to DB + mysql.connection.commit() + + #Close connection + cur.close() + + flash('Blog is Created', 'success') + + return redirect(url_for('dashboard')) + + return render_template('add_blog.html', form=form) + +@app.route('/edit_blog/', methods=['GET', 'POST']) +@is_logged_in +def edit_blog(id): + # Create cursor + cur = mysql.connection.cursor() + + # Get article by id + result = cur.execute("SELECT * FROM blogs WHERE id = %s", [id]) + + blog = cur.fetchone() + cur.close() + # Get form + form = BlogForm(request.form) + + # Populate article form fields + form.title.data = blog['title'] + form.body.data = blog['body'] + + if request.method == 'POST' and form.validate(): + title = request.form['title'] + body = request.form['body'] + + # Create Cursor + cur = mysql.connection.cursor() + app.logger.info(title) + # Execute + cur.execute ("UPDATE blogs SET title=%s, body=%s WHERE id=%s",(title, body, id)) + # Commit to DB + mysql.connection.commit() + + #Close connection + cur.close() + + flash('Blog is Updated', 'success') + + return redirect(url_for('dashboard')) + + return render_template('edit_blog.html', form=form) + +# Delete Article +@app.route('/delete_blog/', methods=['POST']) +@is_logged_in +def delete_blog(id): + # Create cursor + cur = mysql.connection.cursor() + + # Execute + cur.execute("DELETE FROM blogs WHERE id = %s", [id]) + + # Commit to DB + mysql.connection.commit() + + #Close connection + cur.close() + + flash('Blog is Deleted', 'success') + + return redirect(url_for('dashboard')) if __name__ == '__main__': app.secret_key = 'secret123' diff --git a/WebApp/kredo/data.pyc b/WebApp/kredo/data.pyc index 59953f272a622c737d923c651301d8c2a45a8e6b..5055175d9f2e52ee654fba44b0388d425a7162ca 100644 GIT binary patch delta 70 zcmZ3)xrmdU`7Add Blog + {% from "includes/_formhelpers.html" import render_field %} +
+
+ {{ render_field(form.title, class_="form-control") }} +
+
+ {{ render_field(form.body, class_="form-control", id="editor") }} +
+

+

+{% endblock %} diff --git a/WebApp/kredo/templates/contact.html b/WebApp/kredo/templates/contact.html new file mode 100644 index 0000000..863ca7e --- /dev/null +++ b/WebApp/kredo/templates/contact.html @@ -0,0 +1,7 @@ +{% extends 'layout.html' %} + +{% block body %} +

Contact Us

+

Mail us at help@kredo.in

+ +{% endblock%} diff --git a/WebApp/kredo/templates/dashboard.html b/WebApp/kredo/templates/dashboard.html new file mode 100644 index 0000000..665116e --- /dev/null +++ b/WebApp/kredo/templates/dashboard.html @@ -0,0 +1,32 @@ +{% extends 'layout.html' %} + +{% block body %} +

Dashboard Welcome {{session.username}}

+ Add Blog +
+ + + + + + + + + + {% for blog in blogs %} + + + + + + + + + {% endfor %} +
IDTitleAuthorDate
{{blog.id}}{{blog.title}}{{blog.author}}{{blog.create_date}}Edit +
+ + +
+
+{% endblock %} diff --git a/WebApp/kredo/templates/edit_blog.html b/WebApp/kredo/templates/edit_blog.html new file mode 100644 index 0000000..0b98b6e --- /dev/null +++ b/WebApp/kredo/templates/edit_blog.html @@ -0,0 +1,15 @@ +{% extends 'layout.html' %} + +{% block body %} +

Edit Blog

+ {% from "includes/_formhelpers.html" import render_field %} +
+
+ {{ render_field(form.title, class_="form-control") }} +
+
+ {{ render_field(form.body, class_="form-control", id="editor") }} +
+

+

+{% endblock %} diff --git a/WebApp/kredo/templates/home.html b/WebApp/kredo/templates/home.html index 4d7c647..675c0cc 100644 --- a/WebApp/kredo/templates/home.html +++ b/WebApp/kredo/templates/home.html @@ -4,6 +4,25 @@

Welcome to KREDO

Wish to learn all that is currently hot in the tech industry? Need help training your students or workforce?

+

SignUp Today

+

Looking for some interesting hot topics, here you register NOW!! + + + {% if session.logged_in == NULL %} + Register + Login + {% endif %} + +

+
+
+ Kredo.ai + + About Us + Contact Us + +
+
{% endblock %} diff --git a/WebApp/kredo/templates/includes/_messages.html b/WebApp/kredo/templates/includes/_messages.html index 0588492..b5865e9 100644 --- a/WebApp/kredo/templates/includes/_messages.html +++ b/WebApp/kredo/templates/includes/_messages.html @@ -7,3 +7,11 @@ {% endif %} {% endwith %} + +{% if error %} +
{{error}}
+{% endif %} + +{% if msg %} +
{{msg}}
+{% endif %} diff --git a/WebApp/kredo/templates/includes/_navbar.html b/WebApp/kredo/templates/includes/_navbar.html index b2ca68e..c14c85b 100644 --- a/WebApp/kredo/templates/includes/_navbar.html +++ b/WebApp/kredo/templates/includes/_navbar.html @@ -1,26 +1,31 @@ + + + + diff --git a/WebApp/kredo/templates/layout.html b/WebApp/kredo/templates/layout.html index 6ec871a..b78ac8d 100644 --- a/WebApp/kredo/templates/layout.html +++ b/WebApp/kredo/templates/layout.html @@ -4,6 +4,8 @@ Kredo + + {% include 'includes/_navbar.html' %}
@@ -11,6 +13,10 @@ {% block body %}{% endblock %}
- + + + diff --git a/WebApp/kredo/templates/login.html b/WebApp/kredo/templates/login.html new file mode 100644 index 0000000..0ce9259 --- /dev/null +++ b/WebApp/kredo/templates/login.html @@ -0,0 +1,16 @@ +{% extends 'layout.html' %} + +{% block body %} +

Login

+
+
+ + +
+
+ + +
+ +
+{% endblock %}