Skip to content
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
72 changes: 72 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import sqlite3
from contextlib import closing
from flask import Flask, render_template, g, Response, request,json

#config
DATABASE = 'db/app.db'
DEBUG = False
SECRET_KEY = 'hellothisisanewapp'
USERNAME = 'admin'
PASSWORD = 'default'

app = Flask(__name__)
app.config.from_object(__name__)

#database methods
def connect_db():
return sqlite3.connect(app.config['DATABASE'])

def init_db():
with closing(connect_db()) as db:
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()

@app.before_request
def before_request():
g.db = connect_db()

@app.teardown_request
def teardown_request(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()

#View functions:
@app.route('/')
def show_questions():
query = g.db.execute('select * from questions order by id asc')
questions = [dict(id=row[0], author=row[1], text=row[2]) for row in query.fetchall()]
return render_template('main.html',questions=questions)

@app.route('/view',methods=['GET'])
def show_answers():
qn = request.args.get('q')
query = g.db.execute('select * from answers where qn = ?',[qn])
answers = [dict(id=row[0], qn=row[1], written_by=row[2], text=row[3]) for row in query.fetchall()]

query = g.db.execute('select id,text from questions where id=?',[qn])
question = [dict(id=row[0],text=row[1]) for row in query.fetchall()][0]
return render_template('question.html',answers=answers,question=question)

@app.route('/addQ',methods=['POST'])
def add_question():
data = request.json
query = g.db.execute('insert into questions(author, text) values ( ?, ?)',[data['asker'],data['question']])
g.db.commit()
query = g.db.execute('select * from questions order by id asc')
questions = [dict(id=row[0], author=row[1], text=row[2]) for row in query.fetchall()]
return render_template('question_list.html',questions=questions)

@app.route('/addA',methods=['POST'])
def add_answer():
data = request.json
query = g.db.execute('insert into answers(qn, written_by,text) values ( ?,?,?)',[data['qn'],data['written_by'],data['text']])
g.db.commit()
query = g.db.execute('select * from answers where qn = ?',[data['qn']])
answers = [dict(id=row[0], qn=row[1], written_by=row[2], text=row[3]) for row in query.fetchall()]
return render_template('answer_list.html',answers=answers)

if __name__ == '__main__':
app.run()

13 changes: 13 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
drop table if exists questions;
create table questions (
id integer primary key autoincrement,
author text not null,
text text not null
);
drop table if exists answers;
create table answers (
id integer primary key autoincrement,
qn integer not null,
written_by text not null,
text text not null
);
54 changes: 54 additions & 0 deletions static/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
$(document).ready(function(){
$('#question-form').on('submit', function(e){
e.preventDefault();
if (!$('#asker').val())
return;
if (!$('#question').val())
return;

dataToSend = {
asker : $('#asker').val(),
question : $('#question').val()
}
var POSTdata = JSON.stringify(dataToSend);
$.ajax({
type :"POST",
url :"/addQ",
contentType:"application/json",
data :POSTdata,
success : function(data){
$('#question-list').html(data);
},
error : function(data){
console.log(data);
}
});
});

$('#answer-form').on('submit', function(e){
e.preventDefault();
if (!$('#answered_by').val())
return;
if (!$('#answer').val())
return;

dataToSend = {
qn : $('#question').val(),
written_by : $('#answered_by').val(),
text : $('#answer').val()
}
var POSTdata = JSON.stringify(dataToSend);
$.ajax({
type :"POST",
url :"/addA",
contentType:"application/json",
data :POSTdata,
success : function(data){
$('#answer-list').html(data);
},
error : function(data){
console.log(data);
}
});
});
});
6 changes: 6 additions & 0 deletions templates/answer_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% for answer in answers %}
<tr>
<td> {{answer.text}} </td>
<td> {{answer.written_by}} </td>
</tr>
{% endfor %}
17 changes: 17 additions & 0 deletions templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html>
<head>
<title>Questions and Answers</title>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script type='text/javascript' src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type='text/javascript' src="{{ url_for('static', filename='app.js') }}"></script>
</head>
<body>
<div class="container">
<div class="col-md-6 col-md-offset-3">
<h1 class="center page-header">QUESTIONS AND ANSWERS</h1>
{% block content%}
{% endblock %}
</div>
</div>
</body>
</html>
26 changes: 26 additions & 0 deletions templates/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends "layout.html" %}
{% block content %}
<form id="question-form" class="form-horizontal">
<h3>Ask a Question: </h3>
<div class="form-group">
<label for="asker" class="control-label">Asker</label>
<input id="asker" class="form-control" type="text" name="asker">
</div>
<div class="form-group">
<label for="question" class="control-label">Question</label>
<input id="question" class="form-control" type="text" name="question">
</div>
<button type="submit" class="btn btn-primary col-md-2 col-md-offset-5" value="Submit">Submit</button>
</form>

<table class="table table-striped table-bordered" style="text-align:center">
<thead>
<th>Question</th>
<th>Asker</th>
</thead>
<tbody id="question-list">
{% include 'question_list.html' %}
</tbody>
</table>

{% endblock %}
27 changes: 27 additions & 0 deletions templates/question.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% extends "layout.html" %}
{% block content %}
{% include 'question_box.html' %}
<form id="answer-form" class="form-horizontal">
<div class="form-group">
<label for="answered_by" class="control-label">Name</label>
<input id="answered_by" class="form-control" type="text" name="answered_by">
</div>
<div class="form-group">
<label for="answer" class="control-label">Answer</label>
<input id="answer" class="form-control" type="text" name="answer">
</div>
<input id="question" type="hidden" value="{{question.id}}"/>
<button type="submit" class="btn btn-primary col-md-2 col-md-offset-5" value="Submit">Submit</button>
</form>

<table class="table table-striped table-bordered" style="text-align:center">
<thead>
<th>Answer</th>
<th>By</th>
</thead>
<tbody id="answer-list">
{% include 'answer_list.html' %}
</tbody>
</table>
<span class="col-md-3 col-md-offset-9"><a href="/">Back to main</a></span>
{% endblock %}
6 changes: 6 additions & 0 deletions templates/question_box.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="panel panel-default">
<div class="panel-heading">QUESTION</div>
<div class="panel-body">
{{ question.text }}
</div>
</div>
8 changes: 8 additions & 0 deletions templates/question_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% block question_list %}
{% for question in questions %}
<tr>
<td> <a href ="/view?q={{question.id}}">{{ question.text }}</a> </td>
<td> {{ question.author }} </td>
</tr>
{% endfor %}
{% endblock %}