Skip to content
Merged
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
4 changes: 4 additions & 0 deletions WebApp/kredo/README.md
Original file line number Diff line number Diff line change
@@ -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.
206 changes: 199 additions & 7 deletions WebApp/kredo/app.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand All @@ -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/<string:id>/')
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)])
Expand Down Expand Up @@ -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/<string:id>', 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/<string:id>', 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'
Expand Down
Binary file modified WebApp/kredo/data.pyc
Binary file not shown.
15 changes: 15 additions & 0 deletions WebApp/kredo/templates/add_blog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'layout.html' %}

{% block body %}
<h1>Add Blog</h1>
{% from "includes/_formhelpers.html" import render_field %}
<form method="POST" action="">
<div class="form-group">
{{ render_field(form.title, class_="form-control") }}
</div>
<div class="form-group">
{{ render_field(form.body, class_="form-control", id="editor") }}
</div>
<p><input class="btn btn-primary" type="submit" value="Submit">
</form>
{% endblock %}
7 changes: 7 additions & 0 deletions WebApp/kredo/templates/contact.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends 'layout.html' %}

{% block body %}
<h1>Contact Us</h1>
<p class="lead">Mail us at help@kredo.in </p>

{% endblock%}
32 changes: 32 additions & 0 deletions WebApp/kredo/templates/dashboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% extends 'layout.html' %}

{% block body %}
<h1>Dashboard <small> Welcome {{session.username}}</small></h1>
<a class="btn btn-success" href="/add_blog"> Add Blog</a>
<hr>
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Date</th>
<th></th>
<th></th>
</tr>
{% for blog in blogs %}
<tr>
<td>{{blog.id}}</td>
<td>{{blog.title}}</td>
<td>{{blog.author}}</td>
<td>{{blog.create_date}}</td>
<td><a href="edit_blog/{{blog.id}}" class="btn btn-default pull-right">Edit</a></td>
<td>
<form action="{{url_for('delete_blog', id=blog.id)}}" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="Delete" class="btn btn-danger">
</form>
</td>
</tr>
{% endfor %}
</table>
{% endblock %}
15 changes: 15 additions & 0 deletions WebApp/kredo/templates/edit_blog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'layout.html' %}

{% block body %}
<h1>Edit Blog</h1>
{% from "includes/_formhelpers.html" import render_field %}
<form method="POST" action="">
<div class="form-group">
{{ render_field(form.title, class_="form-control") }}
</div>
<div class="form-group">
{{ render_field(form.body, class_="form-control", id="editor") }}
</div>
<p><input class="btn btn-primary" type="submit" value="Submit">
</form>
{% endblock %}
19 changes: 19 additions & 0 deletions WebApp/kredo/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@
<div class = "jumbotron text-center">
<h1>Welcome to KREDO</h1>
<p class="Lead"> Wish to learn all that is currently hot in the tech industry? Need help training your students or workforce?</p>
<h3 class="Lead">SignUp Today</h3>
<p>Looking for some interesting hot topics, here you register NOW!!</h1>


{% if session.logged_in == NULL %}
<a href="/register" class="btn btn-primary btn-lg">Register</a>
<a href="/login" class="btn btn-success btn-lg">Login</a>
{% endif %}

</div>
<div>
<div class="header">
<a href="/"><strong>Kredo.ai</strong></a>
<span class="left">
<a href="/about">About Us</a>
<a href="/contact">Contact Us</a>
</span>
<div class="clr"></div>
</div>
</div>

{% endblock %}
8 changes: 8 additions & 0 deletions WebApp/kredo/templates/includes/_messages.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@

{% endif %}
{% endwith %}

{% if error %}
<div class="alert alert-danger">{{error}}</div>
{% endif %}

{% if msg %}
<div class="alert alert-success">{{msg}}</div>
{% endif %}
Loading