diff --git a/app.py b/app.py new file mode 100644 index 0000000000000000000000000000000000000000..2c4e1d6a31cc03d072a0fcff41d8bd4551db1575 --- /dev/null +++ b/app.py @@ -0,0 +1,63 @@ +from flask import Flask, render_template, request, redirect, url_for +from flask_sqlalchemy import SQLAlchemy +import os + +app = Flask(__name__) + +DB_URI = os.environ.get('DATABASE_URI') + +if DB_URI is None: + raise ValueError('No database URI set in environment variable DATABASE_URI') + +app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI + +db = SQLAlchemy(app) + +class Questions(db.Model): + id = db.Column(db.Integer, primary_key=True) + question = db.Column(db.String(255), nullable=False) + option1 = db.Column(db.String(255), nullable=False) + option2 = db.Column(db.String(255), nullable=False) + option3 = db.Column(db.String(255), nullable=False) + option4 = db.Column(db.String(255), nullable=False) + answer = db.Column(db.String(255), nullable=False) + feedback = db.Column(db.String(255)) + + def delete(self): + db.session.delete(self) + db.session.commit() + +@app.route('/') +def index(): + return render_template("index.html") + +@app.route('/questions') +def questions(): + questions = Questions.query.all() + return render_template("questions.html", questions=questions) + +@app.route('/add_question', methods=['GET', 'POST']) +def add_question(): + if request.method == 'POST': + question = request.form['question'] + option1 = request.form['option1'] + option2 = request.form['option2'] + option3 = request.form['option3'] + option4 = request.form['option4'] + answer = request.form['answer'] + feedback = request.form['feedback'] + new_question = Questions(question=question, option1=option1, option2=option2, option3=option3, option4=option4, answer=answer, feedback=feedback) + db.session.add(new_question) + db.session.commit() + return redirect(url_for('questions')) + return render_template("add_question.html") + +@app.route('/delete_question/<int:id>', methods=['POST']) +def delete_question(id): + question = Questions.query.get(id) + question.delete() + return redirect(url_for('questions')) + + +if __name__ == '__main__': + app.run(debug=True) diff --git a/templates/add_question.html b/templates/add_question.html new file mode 100644 index 0000000000000000000000000000000000000000..e5ca0f114e9b9131168939fca9cd1f59077dee13 --- /dev/null +++ b/templates/add_question.html @@ -0,0 +1,37 @@ +{% extends "layout.html" %} +{% block content %} +<div class="container"> + <h1 class="mt-3">Add Question</h1> + <form method="POST" action="{{ url_for('add_question') }}" class="mt-3"> + <div class="form-group"> + <label for="question">Question:</label> + <input type="text" name="question" class="form-control" required> + </div> + <div class="form-group"> + <label for="option1">Option 1:</label> + <input type="text" name="option1" class="form-control" required> + </div> + <div class="form-group"> + <label for="option2">Option 2:</label> + <input type="text" name="option2" class="form-control" required> + </div> + <div class="form-group"> + <label for="option3">Option 3:</label> + <input type="text" name="option3" class="form-control" required> + </div> + <div class="form-group"> + <label for="option4">Option 4:</label> + <input type="text" name="option4" class="form-control" required> + </div> + <div class="form-group"> + <label for="answer">Answer:</label> + <input type="text" name="answer" class="form-control" required> + </div> + <div class="form-group"> + <label for="feedback">Feedback:</label> + <input type="text" name="feedback" class="form-control"> + </div> + <button type="submit" class="btn btn-primary">Add Question</button> + </form> +</div> +{% endblock content %} \ No newline at end of file diff --git a/templates/edit_question.html b/templates/edit_question.html new file mode 100644 index 0000000000000000000000000000000000000000..70b07e3424388dc22457b61f478322aba4ae5f74 --- /dev/null +++ b/templates/edit_question.html @@ -0,0 +1,34 @@ +<!DOCTYPE html> +<html> + <head> + <title>Edit Question</title> + <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}"> + </head> + <body> + <h1>Edit Question</h1> + <form method="POST" action="{{ url_for('edit_question', question_id=question.id) }}"> + <label for="question">Question:</label> + <input type="text" name="question" value="{{ question.question }}" required> + <br> + <label for="option1">Option 1:</label> + <input type="text" name="option1" value="{{ question.option1 }}" required> + <br> + <label for="option2">Option 2:</label> + <input type="text" name="option2" value="{{ question.option2 }}" required> + <br> + <label for="option3">Option 3:</label> + <input type="text" name="option3" value="{{ question.option3 }}" required> + <br> + <label for="option4">Option 4:</label> + <input type="text" name="option4" value="{{ question.option4 }}" required> + <br> + <label for="answer">Answer:</label> + <input type="text" name="answer" value="{{ question.answer }}" required> + <br> + <label for="feedback">Feedback:</label> + <input type="text" name="feedback" value="{{ question.feedback }}"> + <br> + <button type="submit">Update Question</button> + </form> + </body> +</html> diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000000000000000000000000000000000000..ea08f8690b556b0c886eb652d024eb77c7ee4235 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,4 @@ +{% extends "layout.html" %} +{% block content %} + +{% endblock content %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000000000000000000000000000000000000..b99a9974c166829825f00102da2f3dc0f67c0fff --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,36 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <link + rel="stylesheet" + href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" + integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" + crossorigin="anonymous" + /> + <link + rel="stylesheet" + href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" + crossorigin="anonymous" + /> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Document</title> +</head> +<body> + <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> + + <div class="collapse navbar-collapse" id="navbar"> + <div class="navbar-nav"> + <a href="{{ url_for('index') }}" class="nav-item nav-link">Home</a> + <a href="{{ url_for('questions') }}" class="nav-item nav-link">Delete question</a> + <a href="{{ url_for('add_question') }}" class="nav-item nav-link">Add question</a> +</div> + </div> + </nav> + <div id="content"> + {% block content %} + {% endblock %} + </div> +</body> +</html> \ No newline at end of file diff --git a/templates/questions.html b/templates/questions.html new file mode 100644 index 0000000000000000000000000000000000000000..e86a34c4e9a8465f3f94368a16e42ae7ba3bb245 --- /dev/null +++ b/templates/questions.html @@ -0,0 +1,35 @@ +{% extends "layout.html" %} +{% block content %} +<div class="container"> + <table class="table table-bordered"> + <thead> + <tr> + <th>Question</th> + <th>Option 1</th> + <th>Option 2</th> + <th>Option 3</th> + <th>Option 4</th> + <th>Answer</th> + <th>Action</th> + </tr> + </thead> + <tbody> + {% for question in questions %} + <tr> + <td>{{ question.question }}</td> + <td>{{ question.option1 }}</td> + <td>{{ question.option2 }}</td> + <td>{{ question.option3 }}</td> + <td>{{ question.option4 }}</td> + <td>{{ question.answer }}</td> + <td> + <form method="POST" action="{{ url_for('delete_question', id=question.id) }}"> + <button class="btn btn-danger" type="submit">Delete</button> + </form> + </td> + </tr> + {% endfor %} + </tbody> + </table> +</div> +{% endblock content %} \ No newline at end of file