From 72960be4282cf0c098a7829bf98158f4531bbb02 Mon Sep 17 00:00:00 2001 From: Mukunda Date: Sat, 21 Jul 2018 21:26:00 +0530 Subject: [PATCH 1/2] Here is a simple project on Web Application, we are using python and few database of sql. --- WebApp/kredo/.gitattributes | 2 + WebApp/kredo/FLUSH | 0 WebApp/kredo/README.md | 1 + WebApp/kredo/SET | 226 ++++++++++++++++++ WebApp/kredo/app.py | 79 ++++++ WebApp/kredo/data.py | 32 +++ WebApp/kredo/data.pyc | Bin 0 -> 1186 bytes WebApp/kredo/templates/about.html | 7 + WebApp/kredo/templates/blog.html | 11 + WebApp/kredo/templates/blogs.html | 10 + WebApp/kredo/templates/home.html | 9 + .../templates/includes/_formhelpers.html | 9 + .../kredo/templates/includes/_messages.html | 9 + WebApp/kredo/templates/includes/_navbar.html | 26 ++ WebApp/kredo/templates/layout.html | 16 ++ WebApp/kredo/templates/register.html | 24 ++ 16 files changed, 461 insertions(+) create mode 100644 WebApp/kredo/.gitattributes create mode 100644 WebApp/kredo/FLUSH create mode 100644 WebApp/kredo/README.md create mode 100644 WebApp/kredo/SET create mode 100644 WebApp/kredo/app.py create mode 100644 WebApp/kredo/data.py create mode 100644 WebApp/kredo/data.pyc create mode 100644 WebApp/kredo/templates/about.html create mode 100644 WebApp/kredo/templates/blog.html create mode 100644 WebApp/kredo/templates/blogs.html create mode 100644 WebApp/kredo/templates/home.html create mode 100644 WebApp/kredo/templates/includes/_formhelpers.html create mode 100644 WebApp/kredo/templates/includes/_messages.html create mode 100644 WebApp/kredo/templates/includes/_navbar.html create mode 100644 WebApp/kredo/templates/layout.html create mode 100644 WebApp/kredo/templates/register.html diff --git a/WebApp/kredo/.gitattributes b/WebApp/kredo/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/WebApp/kredo/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/WebApp/kredo/FLUSH b/WebApp/kredo/FLUSH new file mode 100644 index 0000000..e69de29 diff --git a/WebApp/kredo/README.md b/WebApp/kredo/README.md new file mode 100644 index 0000000..9fc9ab3 --- /dev/null +++ b/WebApp/kredo/README.md @@ -0,0 +1 @@ +# kredo diff --git a/WebApp/kredo/SET b/WebApp/kredo/SET new file mode 100644 index 0000000..6359a64 --- /dev/null +++ b/WebApp/kredo/SET @@ -0,0 +1,226 @@ +mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper +Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. + +Oracle is a registered trademark of Oracle Corporation and/or its +affiliates. Other names may be trademarks of their respective +owners. + +Usage: mysql [OPTIONS] [database] + -?, --help Display this help and exit. + -I, --help Synonym for -? + --auto-rehash Enable automatic rehashing. One doesn't need to use + 'rehash' to get table and field completion, but startup + and reconnecting may take a longer time. Disable with + --disable-auto-rehash. + (Defaults to on; use --skip-auto-rehash to disable.) + -A, --no-auto-rehash + No automatic rehashing. One has to use 'rehash' to get + table and field completion. This gives a quicker start of + mysql and disables rehashing on reconnect. + --auto-vertical-output + Automatically switch to vertical output mode if the + result is wider than the terminal width. + -B, --batch Don't use history file. Disable interactive behavior. + (Enables --silent.) + --bind-address=name IP address to bind to. + -b, --binary-as-hex Print binary data as hex + --character-sets-dir=name + Directory for character set files. + --column-type-info Display column type information. + -c, --comments Preserve comments. Send comments to the server. The + default is --skip-comments (discard comments), enable + with --comments. + -C, --compress Use compression in server/client protocol. + -#, --debug[=#] This is a non-debug version. Catch this and exit. + --debug-check This is a non-debug version. Catch this and exit. + -T, --debug-info This is a non-debug version. Catch this and exit. + -D, --database=name Database to use. + --default-character-set=name + Set the default character set. + --delimiter=name Delimiter to be used. + --enable-cleartext-plugin + Enable/disable the clear text authentication plugin. + -e, --execute=name Execute command and quit. (Disables --force and history + file.) + -E, --vertical Print the output of a query (rows) vertically. + -f, --force Continue even if we get an SQL error. + --histignore=name A colon-separated list of patterns to keep statements + from getting logged into syslog and mysql history. + -G, --named-commands + Enable named commands. Named commands mean this program's + internal commands; see mysql> help . When enabled, the + named commands can be used from any line of the query, + otherwise only from the first line, before an enter. + Disable with --disable-named-commands. This option is + disabled by default. + -i, --ignore-spaces Ignore space after function names. + --init-command=name SQL Command to execute when connecting to MySQL server. + Will automatically be re-executed when reconnecting. + --local-infile Enable/disable LOAD DATA LOCAL INFILE. + -b, --no-beep Turn off beep on error. + -h, --host=name Connect to host. + -H, --html Produce HTML output. + -X, --xml Produce XML output. + --line-numbers Write line numbers for errors. + (Defaults to on; use --skip-line-numbers to disable.) + -L, --skip-line-numbers + Don't write line number for errors. + -n, --unbuffered Flush buffer after each query. + --column-names Write column names in results. + (Defaults to on; use --skip-column-names to disable.) + -N, --skip-column-names + Don't write column names in results. + --sigint-ignore Ignore SIGINT (CTRL-C). + -o, --one-database Ignore statements except those that occur while the + default database is the one named at the command line. + --pager[=name] Pager to use to display results. If you don't supply an + option, the default pager is taken from your ENV variable + PAGER. Valid pagers are less, more, cat [> filename], + etc. See interactive help (\h) also. This option does not + work in batch mode. Disable with --disable-pager. This + option is disabled by default. + -p, --password[=name] + Password to use when connecting to server. If password is + not given it's asked from the tty. + -P, --port=# Port number to use for connection or 0 for default to, in + order of preference, my.cnf, $MYSQL_TCP_PORT, + /etc/services, built-in default (3306). + --prompt=name Set the mysql prompt to this value. + --protocol=name The protocol to use for connection (tcp, socket, pipe, + memory). + -q, --quick Don't cache result, print it row by row. This may slow + down the server if the output is suspended. Doesn't use + history file. + -r, --raw Write fields without conversion. Used with --batch. + --reconnect Reconnect if the connection is lost. Disable with + --disable-reconnect. This option is enabled by default. + (Defaults to on; use --skip-reconnect to disable.) + -s, --silent Be more silent. Print results with a tab as separator, + each row on new line. + -S, --socket=name The socket file to use for connection. + --ssl-mode=name SSL connection mode. + --ssl Deprecated. Use --ssl-mode instead. + (Defaults to on; use --skip-ssl to disable.) + --ssl-verify-server-cert + Deprecated. Use --ssl-mode=VERIFY_IDENTITY instead. + --ssl-ca=name CA file in PEM format. + --ssl-capath=name CA directory. + --ssl-cert=name X509 cert in PEM format. + --ssl-cipher=name SSL cipher to use. + --ssl-key=name X509 key in PEM format. + --ssl-crl=name Certificate revocation list. + --ssl-crlpath=name Certificate revocation list path. + --tls-version=name TLS version to use, permitted values are: TLSv1, TLSv1.1 + -t, --table Output in table format. + --tee=name Append everything into outfile. See interactive help (\h) + also. Does not work in batch mode. Disable with + --disable-tee. This option is disabled by default. + -u, --user=name User for login if not current user. + -U, --safe-updates Only allow UPDATE and DELETE that uses keys. + -U, --i-am-a-dummy Synonym for option --safe-updates, -U. + -v, --verbose Write more. (-v -v -v gives the table output format). + -V, --version Output version information and exit. + -w, --wait Wait and retry if connection is down. + --connect-timeout=# Number of seconds before connection timeout. + --max-allowed-packet=# + The maximum packet length to send to or receive from + server. + --net-buffer-length=# + The buffer size for TCP/IP and socket communication. + --select-limit=# Automatic limit for SELECT when using --safe-updates. + --max-join-size=# Automatic limit for rows in a join when using + --safe-updates. + --secure-auth Refuse client connecting to server if it uses old + (pre-4.1.1) protocol. Deprecated. Always TRUE + --server-arg=name Send embedded server this as a parameter. + --show-warnings Show warnings after every statement. + -j, --syslog Log filtered interactive commands to syslog. Filtering of + commands depends on the patterns supplied via histignore + option besides the default patterns. + --plugin-dir=name Directory for client-side plugins. + --default-auth=name Default authentication client-side plugin to use. + --binary-mode By default, ASCII '\0' is disallowed and '\r\n' is + translated to '\n'. This switch turns off both features, + and also turns off parsing of all clientcommands except + \C and DELIMITER, in non-interactive mode (for input + piped to mysql or loaded using the 'source' command). + This is necessary when processing output from mysqlbinlog + that may contain blobs. + --connect-expired-password + Notify the server that this client is prepared to handle + expired password sandbox mode. + +Default options are read from the following files in the given order: +/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf +The following groups are read: mysql client +The following options may be given as the first argument: +--print-defaults Print the program argument list and exit. +--no-defaults Don't read default options from any option file, + except for login file. +--defaults-file=# Only read default options from the given file #. +--defaults-extra-file=# Read this file after the global files are read. +--defaults-group-suffix=# + Also read groups with concat(group, suffix) +--login-path=# Read this path from the login file. + +Variables (--variable-name=value) +and boolean options {FALSE|TRUE} Value (after reading options) +--------------------------------- ---------------------------------------- +auto-rehash TRUE +auto-vertical-output FALSE +bind-address (No default value) +binary-as-hex FALSE +character-sets-dir (No default value) +column-type-info FALSE +comments FALSE +compress FALSE +database (No default value) +default-character-set auto +delimiter ; +enable-cleartext-plugin FALSE +vertical FALSE +force FALSE +histignore (No default value) +named-commands FALSE +ignore-spaces FALSE +init-command (No default value) +local-infile FALSE +no-beep FALSE +host (No default value) +html FALSE +xml FALSE +line-numbers TRUE +unbuffered FALSE +column-names TRUE +sigint-ignore FALSE +port 0 +prompt mysql> +quick FALSE +raw FALSE +reconnect FALSE +socket (No default value) +ssl TRUE +ssl-verify-server-cert FALSE +ssl-ca (No default value) +ssl-capath (No default value) +ssl-cert (No default value) +ssl-cipher (No default value) +ssl-key (No default value) +ssl-crl (No default value) +ssl-crlpath (No default value) +tls-version (No default value) +table FALSE +user (No default value) +safe-updates FALSE +i-am-a-dummy FALSE +connect-timeout 0 +max-allowed-packet 16777216 +net-buffer-length 16384 +select-limit 1000 +max-join-size 1000000 +secure-auth TRUE +show-warnings FALSE +plugin-dir (No default value) +default-auth (No default value) +binary-mode FALSE +connect-expired-password FALSE diff --git a/WebApp/kredo/app.py b/WebApp/kredo/app.py new file mode 100644 index 0000000..715ffbe --- /dev/null +++ b/WebApp/kredo/app.py @@ -0,0 +1,79 @@ +from flask import Flask, render_template, request, flash, redirect, url_for, session, logging +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 + +app = Flask(__name__) + +# config MySQL +app.config['MYSQL_HOST'] = 'localhost' +app.config['MYSQL_USER'] = 'root' +app.config['MYSQL_PASSWORD'] = '' +app.config['MYSQL_DB'] = 'kredo' +app.config['MYSQL_CURSORCLASS'] = 'DictCursor' +# intialise MySQL +mysql = MySQL(app) + +Blogs = Blogs() + +@app.route('/') +def home(): + return render_template('home.html') + +@app.route('/about') +def about(): + return render_template('about.html') + +@app.route('/blogs') +def blogs(): + return render_template( 'blogs.html', blogs = Blogs ) + +@app.route('/blog//') +def blog(id): + return render_template('blog.html', blog = blog ) + +class RegisterForm(Form): + name = StringField('Name', [validators.Length(min = 1, max = 50)]) + username = StringField('Username', [validators.Length(min = 4, max = 25)]) + email = StringField('Email', [validators.Length(min = 6, max = 50)]) + password = PasswordField('Password', [ + validators.DataRequired(), + validators.EqualTo('confirm', message = 'Passwords do not match') + ]) + confirm = PasswordField('Confirm Password') + +@app.route('/register', methods = ['GET', 'POST']) +def register(): + form = RegisterForm(request.form) + if request.method == 'POST' and form.validate(): + name = form.name.data + email = form.email.data + username = form.username.data + password = sha256_crypt.encrypt(str(form.password.data)) + + # create Cursor + cur = mysql.connection.cursor() + + # Execute query + cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)", (name, email, username, password)) + + # Commit to DB + mysql.connection.commit() + + # Close connection + cur.close() + + flash('You are now registered and can login', 'success') + + return redirect(url_for('home')) + return render_template('register.html', form = form) + + + +if __name__ == '__main__': + app.secret_key = 'secret123' + app.run(debug = True) diff --git a/WebApp/kredo/data.py b/WebApp/kredo/data.py new file mode 100644 index 0000000..56582da --- /dev/null +++ b/WebApp/kredo/data.py @@ -0,0 +1,32 @@ +def Blogs(): + Blogs = [ + { + 'id' : 'KB1', + 'title' : 'Blog 1', + 'body' : 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + 'author' : 'Kartic Rao', + 'create date' : '10-07-2018' + }, + { + 'id' : 'KB2', + 'title' : 'Blog 2', + 'body' : 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + 'author' : 'Abraar Syed', + 'create date' : '11-07-2018' + }, + { + 'id' : 'KB3', + 'title' : 'Blog 3', + 'body' : 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + 'author' : 'Sandeep', + 'create date' : '12-07-2018' + }, + { + 'id' : 'KB4', + 'title' : 'Blog 4', + 'body' : 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', + 'author' : 'Mukund', + 'create date' : '13-07-2018' + } + ] + return Blogs diff --git a/WebApp/kredo/data.pyc b/WebApp/kredo/data.pyc new file mode 100644 index 0000000000000000000000000000000000000000..59953f272a622c737d923c651301d8c2a45a8e6b GIT binary patch literal 1186 zcmbVLO;b}b6b%#vi}?Mqy-OC*(w0FNj-!YxlyP9(=qe>h3k_UW>Ya1NERq zJ-kLeYEh4Gpq{j-rwwYnB)$vrO|bZ9Eb}mTxIfq%KDq}7Yq^3ZL6uB{=Azn?Wc?TesWW9!rHt=q8iZtos zGL#HPo-jXPZn+Vx96Jfp&_$6w_>=)1StbWdwYcO*X*c1bPy**ujzt4rVN{f5Chwpa zBdzi+a|N-edQwA4fx&>1m0Q;2TL*kp?!kVglqDljEUc+DOAaE2h-c_DMt05i7GM4W+FN}^(%DuJ|QR<`9J zL{A!BO|d4$hpJ+NtiHjYo^s3*G$xF7)p}E9$QlrF9BG3F?+%Fk_hR z7|nN#@(Pn4OO6mjiyr=k-kGC!8gU=Wd2v(f(G0x7G-9KEbj>ZT7dEwXbs>$o>z{dx zecSAoslSiuJp0fO6ZYEV`ju~N|2(4h{hD_#Opah;cHqo`q- Xi)pI=Rg_m*$4kDU#o(((SrzsN9Ahn& literal 0 HcmV?d00001 diff --git a/WebApp/kredo/templates/about.html b/WebApp/kredo/templates/about.html new file mode 100644 index 0000000..ddcc4df --- /dev/null +++ b/WebApp/kredo/templates/about.html @@ -0,0 +1,7 @@ +{% extends 'layout.html' %} + +{% block body %} +

About Us

+

Kredo is a free portfolio WebApp that works on creative thinking in students to identify their strengths in AI or Datascience. And not only allows you to learn but identify the you in you, work on this global-app and discover the real you.

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

{{blog.title}}

+ Written by {{blog.author}} on {{blog.create_date}} +
+
+ {{blog.body | safe}} +
+ +{% endblock %} diff --git a/WebApp/kredo/templates/blogs.html b/WebApp/kredo/templates/blogs.html new file mode 100644 index 0000000..d22a863 --- /dev/null +++ b/WebApp/kredo/templates/blogs.html @@ -0,0 +1,10 @@ +{% extends 'layout.html' %} + +{% block body %} +

Blogs

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

Welcome to KREDO

+

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

+
+ +{% endblock %} diff --git a/WebApp/kredo/templates/includes/_formhelpers.html b/WebApp/kredo/templates/includes/_formhelpers.html new file mode 100644 index 0000000..d22dd27 --- /dev/null +++ b/WebApp/kredo/templates/includes/_formhelpers.html @@ -0,0 +1,9 @@ +{% macro render_field(field) %} + {{ field.label }} + {{ field(**kwargs)|safe }} + {% if field.errors %} + {% for error in field.errors %} + {{ error }} + {% endfor %} + {% endif %} +{% endmacro %} diff --git a/WebApp/kredo/templates/includes/_messages.html b/WebApp/kredo/templates/includes/_messages.html new file mode 100644 index 0000000..0588492 --- /dev/null +++ b/WebApp/kredo/templates/includes/_messages.html @@ -0,0 +1,9 @@ +{% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} + + {% for category, message in messages %} +
{{ message }}
+ {% endfor %} + + {% endif %} +{% endwith %} diff --git a/WebApp/kredo/templates/includes/_navbar.html b/WebApp/kredo/templates/includes/_navbar.html new file mode 100644 index 0000000..b2ca68e --- /dev/null +++ b/WebApp/kredo/templates/includes/_navbar.html @@ -0,0 +1,26 @@ + diff --git a/WebApp/kredo/templates/layout.html b/WebApp/kredo/templates/layout.html new file mode 100644 index 0000000..6ec871a --- /dev/null +++ b/WebApp/kredo/templates/layout.html @@ -0,0 +1,16 @@ + + + + + Kredo + + + {% include 'includes/_navbar.html' %} +
+ {% include 'includes/_messages.html' %} + {% block body %}{% endblock %} +
+ + + + diff --git a/WebApp/kredo/templates/register.html b/WebApp/kredo/templates/register.html new file mode 100644 index 0000000..600f744 --- /dev/null +++ b/WebApp/kredo/templates/register.html @@ -0,0 +1,24 @@ +{% extends 'layout.html' %} + +{% block body %} +

Register

+ {% from "includes/_formhelpers.html" import render_field %} +
+
+ {{render_field(form.name, class_ ="form-control")}} +
+
+ {{render_field(form.email, class_ ="form-control")}} +
+
+ {{render_field(form.username, class_ ="form-control")}} +
+
+ {{render_field(form.password, class_ ="form-control")}} +
+
+ {{render_field(form.confirm, class_ ="form-control")}} +
+

+
+{% endblock %} From 700e36286c763a87ab0a3dfac0681fd8eadba8f1 Mon Sep 17 00:00:00 2001 From: Mukunda Selvaraj Date: Fri, 3 Aug 2018 00:01:59 +0530 Subject: [PATCH 2/2] 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 %}