-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
133 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,6 @@ | ||
from flask import ( | ||
abort, Flask, typing as ft, | ||
render_template, send_from_directory, | ||
# request, | ||
) | ||
from flask.views import View | ||
from typing import Any | ||
|
||
from zluuba_art.constants import PET_PROJECTS | ||
from zluuba_art.error_handlers import handle_404, handle_generic_exception | ||
# from zluuba_art.logger.logger import log | ||
|
||
|
||
class BaseView(View): | ||
methods = ['GET'] | ||
init_every_request = False | ||
|
||
|
||
class HomeView(BaseView): | ||
def dispatch_request(self) -> ft.ResponseReturnValue: | ||
""" | ||
Handles the main page route, logs user statistics, | ||
and renders the main page. | ||
:return: The rendered 'index.html' template. | ||
""" | ||
|
||
# log('users_statistic', request) | ||
return render_template('index.html') | ||
|
||
|
||
class CVPageView(BaseView): | ||
def dispatch_request(self) -> ft.ResponseReturnValue: | ||
""" | ||
Renders the cv page. | ||
:return: The rendered 'cv.html' template. | ||
""" | ||
|
||
return render_template('cv.html') | ||
|
||
|
||
class ProjectsPageView(BaseView): | ||
def dispatch_request(self) -> ft.ResponseReturnValue: | ||
""" | ||
Renders the pet projects page. | ||
:return: The rendered 'pet_projects.html' template. | ||
""" | ||
|
||
return render_template('pet_projects.html') | ||
|
||
|
||
class ProjectPageView(BaseView): | ||
def dispatch_request(self, *args: Any, **kwargs: Any | ||
) -> ft.ResponseReturnValue: | ||
""" | ||
Renders the specified pet project page. | ||
:param project: Name of the pet project. | ||
:return: The rendered project template. | ||
""" | ||
project = kwargs['project'] | ||
project_html_page = PET_PROJECTS.get(project) | ||
|
||
if not project_html_page: | ||
abort(404) | ||
|
||
return render_template(f'projects/{project_html_page}') | ||
|
||
|
||
class LinksPageView(BaseView): | ||
def dispatch_request(self) -> ft.ResponseReturnValue: | ||
""" | ||
Renders the links page. | ||
:return: The rendered 'links.html' template. | ||
""" | ||
|
||
return render_template('links.html') | ||
|
||
|
||
class RobotsPageView(BaseView): | ||
def dispatch_request(self) -> ft.ResponseReturnValue: | ||
""" | ||
Serves the robots.txt file. | ||
:return: The robots.txt file. | ||
""" | ||
|
||
static_folder = app.static_folder | ||
|
||
if static_folder is None: | ||
abort(404) | ||
|
||
return send_from_directory(static_folder, 'robots.txt') | ||
|
||
|
||
def register_api(app): | ||
app.add_url_rule( | ||
'/', | ||
view_func=HomeView.as_view('main_page'), | ||
) | ||
app.add_url_rule( | ||
'/cv', | ||
view_func=CVPageView.as_view('cv_page'), | ||
) | ||
app.add_url_rule( | ||
'/projects', | ||
view_func=ProjectsPageView.as_view('projects_page') | ||
) | ||
app.add_url_rule( | ||
'/projects/<project>', | ||
view_func=ProjectPageView.as_view('project_page') | ||
) | ||
app.add_url_rule( | ||
'/links', | ||
view_func=LinksPageView.as_view('links_page') | ||
) | ||
app.add_url_rule( | ||
'/robots.txt', | ||
view_func=RobotsPageView.as_view('robots_page') | ||
) | ||
|
||
app.register_error_handler(404, handle_404) | ||
app.register_error_handler(Exception, handle_generic_exception) | ||
from flask import Flask | ||
from .urls import register_app | ||
|
||
|
||
app = Flask(__name__) | ||
register_api(app) | ||
register_app(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from .error_handlers import ( | ||
handle_404, handle_generic_exception, | ||
) | ||
from .views import ( | ||
HomeView, CVPageView, ProjectsPageView, | ||
ProjectPageView, LinksPageView, RobotsPageView, | ||
) | ||
|
||
|
||
def register_app(app): | ||
app.add_url_rule( | ||
'/', | ||
view_func=HomeView.as_view('main_page'), | ||
) | ||
app.add_url_rule( | ||
'/cv', | ||
view_func=CVPageView.as_view('cv_page'), | ||
) | ||
app.add_url_rule( | ||
'/projects', | ||
view_func=ProjectsPageView.as_view('projects_page') | ||
) | ||
app.add_url_rule( | ||
'/projects/<project>', | ||
view_func=ProjectPageView.as_view('project_page') | ||
) | ||
app.add_url_rule( | ||
'/links', | ||
view_func=LinksPageView.as_view('links_page') | ||
) | ||
app.add_url_rule( | ||
'/robots.txt', | ||
view_func=RobotsPageView.as_view('robots_page') | ||
) | ||
|
||
app.register_error_handler(404, handle_404) | ||
app.register_error_handler(Exception, handle_generic_exception) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from flask import ( | ||
abort, current_app, render_template, | ||
send_from_directory, typing as ft, | ||
) | ||
from flask.views import View | ||
from typing import Any | ||
|
||
from .constants import PET_PROJECTS | ||
|
||
|
||
class BaseView(View): | ||
methods = ['GET'] | ||
init_every_request = False | ||
|
||
|
||
class HomeView(BaseView): | ||
def dispatch_request(self) -> ft.ResponseReturnValue: | ||
""" | ||
Handles the main page route, logs user statistics, | ||
and renders the main page. | ||
:return: The rendered 'index.html' template. | ||
""" | ||
|
||
# log('users_statistic', request) | ||
return render_template('index.html') | ||
|
||
|
||
class CVPageView(BaseView): | ||
def dispatch_request(self) -> ft.ResponseReturnValue: | ||
""" | ||
Renders the cv page. | ||
:return: The rendered 'cv.html' template. | ||
""" | ||
|
||
return render_template('cv.html') | ||
|
||
|
||
class ProjectsPageView(BaseView): | ||
def dispatch_request(self) -> ft.ResponseReturnValue: | ||
""" | ||
Renders the pet projects page. | ||
:return: The rendered 'pet_projects.html' template. | ||
""" | ||
|
||
return render_template('pet_projects.html') | ||
|
||
|
||
class ProjectPageView(BaseView): | ||
def dispatch_request(self, *args: Any, **kwargs: Any | ||
) -> ft.ResponseReturnValue: | ||
""" | ||
Renders the specified pet project page. | ||
:param project: Name of the pet project. | ||
:return: The rendered project template. | ||
""" | ||
project = kwargs['project'] | ||
project_html_page = PET_PROJECTS.get(project) | ||
|
||
if not project_html_page: | ||
abort(404) | ||
|
||
return render_template(f'projects/{project_html_page}') | ||
|
||
|
||
class LinksPageView(BaseView): | ||
def dispatch_request(self) -> ft.ResponseReturnValue: | ||
""" | ||
Renders the links page. | ||
:return: The rendered 'links.html' template. | ||
""" | ||
|
||
return render_template('links.html') | ||
|
||
|
||
class RobotsPageView(BaseView): | ||
def dispatch_request(self) -> ft.ResponseReturnValue: | ||
""" | ||
Serves the robots.txt file. | ||
:return: The robots.txt file. | ||
""" | ||
|
||
static_folder = current_app.static_folder | ||
|
||
if static_folder is None: | ||
abort(404) | ||
|
||
return send_from_directory(static_folder, 'robots.txt') |