Skip to content
Snippets Groups Projects
Commit edbcfa09 authored by Liv.Dornfest's avatar Liv.Dornfest
Browse files

final refactoring

parent bebb94f3
No related branches found
No related tags found
No related merge requests found
Pipeline #164168 passed
......@@ -58,9 +58,9 @@ def create_app():
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
# blueprint for non-auth parts of app
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
# blueprint for api parts of app
from .api import api as api_blueprint
app.register_blueprint(api_blueprint)
# careful! debug mode activated is security flaw
app.debug = False
......
from flask import Blueprint
api = Blueprint('api',__name__)
from . import main
\ No newline at end of file
from flask import Blueprint, render_template, request, make_response, jsonify
from flask import render_template, request, make_response, jsonify
import flask
from flask_login import login_required, current_user
from . import db, admin_required
from .models import Post,Announcement
from .forms import MessageForm
from .. import db, admin_required
from ..models import Post,Announcement
from ..forms import MessageForm
from . import api
main = Blueprint('main', __name__)
@main.route('/')
@main.route('/index.html')
@api.route('/')
@api.route('/index.html')
def index_html():
response = flask.make_response(render_template('./index.html',mimetype='text/html'))
return response
@main.get('/show_all')
# show all public announcements from the feed
@api.get('/show_all')
def show_all():
result = db.session.query(Announcement).all()
output = [[r.author, r.timestamp, r.body] for r in result]
response = make_response(jsonify(output))
return response
@main.get('/show_all_posts')
# show all private conversations
# only logged in moderators get this option
@api.get('/show_all_posts')
@login_required
@admin_required
def show_all_posts():
result = db.session.query(Post).all()
output = [[r.author, r.timestamp, r.body, r.recipient] for r in result]
response = make_response(jsonify(output))
return response
@main.route('/show_user_history', methods=['GET', 'POST'])
@login_required # must be logged in
@admin_required # must be moderator
# show all of a user's sent messages (private)
# only logged in moderators get this option
@api.route('/show_user_history', methods=['GET', 'POST'])
@login_required
@admin_required
def show_user_history():
user = request.args.get('q')
all_messages = db.session.query(Post).all()
......@@ -46,8 +47,10 @@ def show_user_history():
response = make_response(jsonify(output))
return response
@main.route('/show_conv', methods=['GET', 'POST'])
# show the current user their conversation/private messages
# with another user
# must be logged in
@api.route('/show_conv', methods=['GET', 'POST'])
@login_required
def show_conv():
user = request.args.get('q') or request.form.get('q')
......@@ -60,8 +63,9 @@ def show_conv():
response = make_response(jsonify(output))
return response
@main.route('/send', methods=['GET', 'POST'])
# publish an announcement for all to see
# available for all, including guests/anonymous users
@api.route('/send', methods=['GET', 'POST'])
def send():
message = request.args.get('message')
if current_user == "Guest":
......@@ -76,8 +80,9 @@ def send():
response = make_response(jsonify(author = announcement.author, message = announcement.body))
return response
@main.route('/new_post', methods=['GET', 'POST'])
# create a new private message
# must be logged in
@api.route('/new_post', methods=['GET', 'POST'])
@login_required
def new_post():
form = MessageForm()
......@@ -102,25 +107,30 @@ def new_post():
response = make_response(render_template('./post.html', message_form = form))
return response
# get your private inbox
# must be logged in
@login_required
@main.route('/get_inbox', methods=['GET', 'POST'])
@api.route('/get_inbox', methods=['GET', 'POST'])
def get_inbox():
all_messages = db.session.query(Post).filter(Post.recipient == current_user.username)
response = make_response(render_template('./inbox.html', posts=all_messages))
response.headers['Content-Type'] = 'text/html'
return response
# get your private carbon copies
# must be logged in
@login_required
@main.route('/get_copy', methods=['GET', 'POST'])
@api.route('/get_copy', methods=['GET', 'POST'])
def get_copy():
all_messages = db.session.query(Post).filter(Post.copy == current_user.username)
response = make_response(render_template('./copy.html', posts=all_messages))
response.headers['Content-Type'] = 'text/html'
return response
# get your sent private messages
# must be logged in
@login_required
@main.route('/get_outbox', methods=['GET', 'POST'])
@api.route('/get_outbox', methods=['GET', 'POST'])
def get_outbox():
all_messages = db.session.query(Post).filter(Post.author == current_user.username)
response = make_response(render_template('./outbox.html', posts=all_messages))
......
......@@ -12,9 +12,10 @@ from werkzeug.security import generate_password_hash, check_password_hash
# Log in as guest
# You can write announcements as guest
# And write in the feed
@auth.route('/guest', methods=['GET','POST'])
@auth.get('/guest')
def guest():
response = redirect(url_for('main.index_html'))
response = redirect(url_for('api.index_html'))
return response
# Register as a user
......@@ -48,7 +49,7 @@ def register():
@auth.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated: # if you are logged in, you cannot log in again
response = redirect(url_for('main.index_html'))
response = redirect(url_for('api.index_html'))
return response
form = LoginForm()
......@@ -59,7 +60,7 @@ def login():
login_user(user)
response = redirect(url_for('main.index_html'))
response = redirect(url_for('api.index_html'))
return response
response = make_response(render_template('./login.html', form = form))
......
......@@ -7,7 +7,7 @@
{% endif %}
<div id="link_pane">
<a href="{{ url_for('main.index_html') }}">Back to main page</a>
<a href="{{ url_for('api.index_html') }}">Back to main page</a>
</div>
<div id="header_big">
<dt> Your carbon copies: </dt>
......@@ -29,7 +29,7 @@
{% endfor %}
<a href="{{ url_for('main.index_html') }}">Back to main page</a>
<a href="{{ url_for('api.index_html') }}">Back to main page</a>
</div>
......
......@@ -6,7 +6,7 @@
{% endif %}
<div id="link_pane">
<a href="{{ url_for('main.index_html') }}">Back to main page</a>
<a href="{{ url_for('api.index_html') }}">Back to main page</a>
</div>
<div id="header_big">
<dt> Your received messages: </dt>
......@@ -30,7 +30,7 @@
{% endfor %}
<br>
<a href="{{ url_for('main.index_html') }}">Back to main page</a>
<a href="{{ url_for('api.index_html') }}">Back to main page</a>
</div>
......
......@@ -16,9 +16,9 @@
<div id="link_pane">
{% if current_user.is_authenticated %}
<a href="{{ url_for('main.get_inbox') }}">Show inbox </a>
<a href="{{ url_for('main.get_outbox') }}">Show outbox </a>
<a href="{{ url_for('main.get_copy' )}}">Show CC </a>
<a href="{{ url_for('api.get_inbox') }}">Show inbox </a>
<a href="{{ url_for('api.get_outbox') }}">Show outbox </a>
<a href="{{ url_for('api.get_copy' )}}">Show CC </a>
<a href="{{ url_for('auth.logout') }}">Logout</a>
{% endif %}
</div>
......@@ -40,7 +40,7 @@
<button type="button" id="sendBtn">Publish</button>
{% if current_user.is_authenticated %}
<form action="{{ url_for('main.new_post') }}">
<form action="{{ url_for('api.new_post') }}">
<input type="submit" value="Send PM" />
</form>
{% endif %}
......
......@@ -7,7 +7,7 @@
{% endif %}
<div id="link_pane">
<a href="{{ url_for('main.index_html') }}">Back to main page</a>
<a href="{{ url_for('api.index_html') }}">Back to main page</a>
</div>
<div id="header_big">
<dt> Your sent messages: </dt>
......@@ -30,7 +30,7 @@
{% endfor %}
<a href="{{ url_for('main.index_html') }}">Back to main page</a>
<a href="{{ url_for('api.index_html') }}">Back to main page</a>
</div>
......
......@@ -29,7 +29,7 @@
<input type=submit value=Send>
</form>
<a href="{{ url_for('main.index_html') }}">Back to main page</a>
<a href="{{ url_for('api.index_html') }}">Back to main page</a>
</div>
</body>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment