Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some refactoring #24

Merged
merged 12 commits into from
Sep 11, 2023
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
42 changes: 17 additions & 25 deletions candle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
from flask_wtf.csrf import CSRFProtect
from candle.config import Config

from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.wrappers import Response


login_manager = LoginManager() # keeps session data
login_manager.login_view = 'auth.login'
# login_manager.login_message_category = 'info' # flash message category (not yet implemented)
Expand All @@ -25,46 +21,42 @@

def create_app(config_class=Config):
app = Flask(__name__)
app.url_map.strict_slashes = False
app.config.from_object(config_class)

# Use middleware for 2016 path-prefix. Used for testing. Source: https://dlukes.github.io/flask-wsgi-url-prefix.html#mwe
if app.config['ENV'] == "development":
app.wsgi_app = DispatcherMiddleware(
Response('Not Found', status=404),
{'/2016-2017-zima': app.wsgi_app}
)

init_extensions(app)
register_blueprints(app)
return app


def register_blueprints(app):
from candle.main.main import main
from candle.api.api import api
from candle.auth.auth import auth
from candle.timetable.timetable import timetable
from candle.my_timetable.my_timetable import my_timetable
from candle.entities.room.room import room
from candle.entities.student_group.student_group import student_group
from candle.entities.teacher.teacher import teacher
from candle.entities.subject.subject import subject
from candle.panel.panel import panel
from candle.search.search import search
from candle.errors.errors import errors

app.register_blueprint(main)
app.register_blueprint(api)
from candle.api import create_api
from candle.common.frontend import common
from candle.rooms.frontend import rooms
from candle.groups.frontend import groups
from candle.subjects.frontend import subjects
from candle.teachers.frontend import teachers

app.register_blueprint(auth)
app.register_blueprint(timetable)
app.register_blueprint(my_timetable)
app.register_blueprint(room)
app.register_blueprint(student_group)
app.register_blueprint(teacher)
app.register_blueprint(subject)
app.register_blueprint(panel)
app.register_blueprint(search)
app.register_blueprint(errors)

api = create_api()
app.register_blueprint(api)

app.register_blueprint(common)
app.register_blueprint(rooms)
app.register_blueprint(groups)
app.register_blueprint(subjects)
app.register_blueprint(teachers)


def init_extensions(app):
Expand Down
29 changes: 29 additions & 0 deletions candle/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from flask import Blueprint


def get_error_handler(code, message):
def fn(error):
return {"error": message}, code

return fn


def create_api():
api = Blueprint('api', __name__, url_prefix="/api")

from candle.teachers.api import teachers
from candle.subjects.api import subjects
from candle.rooms.api import rooms
from candle.groups.api import groups

api.register_blueprint(teachers)
api.register_blueprint(subjects)
api.register_blueprint(rooms)
api.register_blueprint(groups)

api.register_error_handler(404, get_error_handler(404, "Not found."))
api.register_error_handler(403, get_error_handler(403, "Forbidden."))
api.register_error_handler(500, get_error_handler(500, "Server error."))

return api

78 changes: 0 additions & 78 deletions candle/api/api.py

This file was deleted.

12 changes: 6 additions & 6 deletions candle/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
def require_remote_user(func):
@wraps(func)
def wrapper(*args, **kwargs):
if current_app.config['ENV'] == 'production':
if current_app.config['DEBUG']:
request.environ.setdefault("REMOTE_USER", "svttest")
else:
if request.environ.get('REMOTE_USER') is None:
flash('User not logged in', 'error')
return redirect(url_for('main.home'))
else:
request.environ.setdefault("REMOTE_USER", "svttest")
return redirect(url_for('common.home'))
return func(*args, **kwargs)
return wrapper

Expand All @@ -39,11 +39,11 @@ def login():
# flash('Prihlasenie bolo neuspesne.')

login_user(user, remember=True)
return redirect(url_for("main.home"))
return redirect(url_for("common.home"))


@auth.route('/odhlasit')
@require_remote_user
def logout():
logout_user()
return redirect(url_for('main.home'))
return redirect(url_for('common.home'))
File renamed without changes.
67 changes: 67 additions & 0 deletions candle/common/frontend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from flask import Blueprint, render_template, redirect, url_for, request
from flask_login import current_user

from candle import db
from candle.groups.search import search_groups
from candle.models import UserTimetable
from flask_wtf.csrf import CSRFError

from candle.rooms.search import search_rooms
from candle.subjects.search import search_subjects
from candle.teachers.search import search_teachers

common = Blueprint('common', __name__, template_folder="templates",
static_folder='static',
static_url_path='/common/static')


@common.route('/')
def home():
if current_user.is_authenticated:
my_timetables = current_user.timetables
# if the user doesn't have any timetable:
if my_timetables.first() is None:
# create a new one:
ut = UserTimetable(name='Rozvrh', user_id=current_user.id)
db.session.add(ut)
db.session.commit()
else:
# select the latest one (with the highest id):
ut = my_timetables.order_by(UserTimetable.id_)[-1]
# redirect to user's timetable view:
return redirect(url_for('my_timetable.show_timetable', id_=ut.id_) )
else: # user is logged out, show welcome-info:
return render_template('timetable/timetable.html', title='Rozvrh', show_welcome=True)


@common.route('/search/')
def search():
query = request.args.get("q")
if query:
return render_template("search.html", title="Vyhľadávanie",
subjects=search_subjects(query).limit(20).all(),
groups=search_groups(query).limit(20).all(),
rooms=search_rooms(query).limit(20).all(),
teachers=search_teachers(query).limit(20).all(),
)
return render_template('search.html', title='Vyhľadávanie')


@common.app_errorhandler(404)
def error_404(error):
return render_template('errors/404.html'), 404


@common.app_errorhandler(403)
def error_403(error):
return render_template('errors/403.html'), 403


@common.app_errorhandler(500)
def error_500(error):
return render_template('errors/500.html'), 500


@common.app_errorhandler(CSRFError)
def csrf_error(reason):
return render_template('errors/csrf_error.html', reason=reason.description), 400
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,4 @@ ul.quickswitch li.active a {

#web_header ul.quickswitch li.active a {
color: black;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -823,4 +823,4 @@ ul.quickswitch li a {

#welcome {
font-size: 1.25em;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<body>
<div id="vrch">
<div id="vrch_logo"><a href="{{ url_for('main.home') }}">Rozvrh</a></div>
<div id="vrch_logo"><a href="{{ url_for('common.home') }}">Rozvrh</a></div>
</div>

{% include('main/first_row.html') %}
Expand Down Expand Up @@ -43,7 +43,7 @@ <h1>{{ web_header }}</h1>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>

<script src={{ url_for('main.static', filename='js/csrf_header.js') }}></script>
<script src={{ url_for('common.static', filename='js/csrf_header.js') }}></script>
{% endblock %}
</body>
</html>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h2 class="pristupnost">Menu používateľa</h2>
<div id="vrch_riadok1_vlavo">
<h2 class="pristupnost">Linky</h2>
<ul>
<li><a class="selected" href="{{ url_for('main.home') }}">Rozvrh</a></li>
<li><a class="selected" href="{{ url_for('common.home') }}">Rozvrh</a></li>
{# <li><a href="https://github.com/fmfi-svt/candle">Zoznam chýb</a></li>#}
{# <li><a href="https://github.com/fmfi-svt/candle/wiki">Dokumentácia</a></li>#}
{# <li><a href="https://github.com/fmfi-svt/candle/wiki/FAQ">FAQ</a></li>#}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
</script>

<title>{% include('main/title.html') %}</title>
<link rel="shortcut icon" href="{{ url_for('main.static', filename='images/favicon.ico') }}">
<link rel="shortcut icon" href="{{ url_for('common.static', filename='images/favicon.ico') }}">

{# Candle CSS #}
<link rel="stylesheet" type="text/css" media="screen" href="{{ url_for('main.static', filename='css/main.css') }}" />
<link rel="stylesheet" type="text/css" media="screen" href="{{ url_for('main.static', filename='css/dark.css') }}" title="Tmavé farby" />
<link rel="stylesheet" type="text/css" media="screen" href="{{ url_for('common.static', filename='css/main.css') }}" />
<link rel="stylesheet" type="text/css" media="screen" href="{{ url_for('common.static', filename='css/dark.css') }}" title="Tmavé farby" />

{# CSS for JQUERY-UI #}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
Expand Down
45 changes: 45 additions & 0 deletions candle/common/templates/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{% extends "main/base.html" %}

{% block obsah %}
<form>
<input type="text" name="q" value="{{ request.args.q }}">
<button type="submit">Vyhľadať</button>
</form>

{% if subjects %}
<h2>Predmety</h2>
<ul>
{% for subject in subjects %}
<li><a href="{{ url_for("subjects.show_timetable", slug=subject.url_id) }}">{{ subject }}</a></li>
{% endfor %}
</ul>
{% endif %}

{% if groups %}
<h2>Krúžky</h2>
<ul>
{% for group in groups %}
<li><a href="{{ url_for("groups.show_timetable", slug=group.url_id) }}">{{ group }}</a></li>
{% endfor %}
</ul>
{% endif %}

{% if teachers %}
<h2>Učitelia</h2>
<ul>
{% for teacher in teachers %}
<li><a href="{{ url_for("teachers.show_timetable", slug=teacher.url_id) }}">{{ teacher }}</a></li>
{% endfor %}
</ul>
{% endif %}

{% if rooms %}
<h2>Miestnosti</h2>
<ul>
{% for room in rooms %}
<li><a href="{{ url_for("rooms.show_timetable", slug=room.url_id) }}">{{ room }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}

Loading
Loading