|
1 | | -import os |
2 | | - |
3 | 1 | from pyechonext.apidoc_ui import APIDocUI, APIDocumentation |
4 | 2 | from pyechonext.app import ApplicationType, EchoNext |
5 | 3 | from pyechonext.config import SettingsConfigType, SettingsLoader |
6 | 4 | from pyechonext.middleware import middlewares |
7 | | -from pyechonext.response import Response |
| 5 | +from pyechonext.mvc.controllers import PageController |
8 | 6 | from pyechonext.static import StaticFile |
9 | 7 | from pyechonext.template_engine.jinja import render_template |
10 | | -from pyechonext.urls import URL, IndexView |
| 8 | +from pyechonext.urls import URL |
11 | 9 | from pyechonext.utils.exceptions import MethodNotAllow |
12 | | -from pyechonext.views import View |
13 | 10 |
|
14 | 11 |
|
15 | | -class UsersView(View): |
16 | | - def get(self, request, response, **kwargs): |
17 | | - return render_template( |
18 | | - request, |
19 | | - "index.html", |
20 | | - user_name="User", |
21 | | - session_id=request.session_id, |
22 | | - friends=["Bob", "Anna", "John"], |
23 | | - ) |
| 12 | +class UsersView(PageController): |
| 13 | + def get(self, request, response, *args, **kwargs): |
| 14 | + return render_template( |
| 15 | + request, |
| 16 | + "index.html", |
| 17 | + user_name="User", |
| 18 | + session_id=request.session_id, |
| 19 | + friends=["Bob", "Anna", "John"], |
| 20 | + ) |
24 | 21 |
|
25 | | - def post(self, request, response, **kwargs): |
26 | | - raise MethodNotAllow(f"Request {request.path}: method not allow") |
| 22 | + def post(self, request, response, *args, **kwargs): |
| 23 | + raise MethodNotAllow(f"Request {request.path}: method not allow") |
27 | 24 |
|
28 | 25 |
|
29 | | -url_patterns = [URL(url="/", view=IndexView), URL(url="/users", view=UsersView)] |
| 26 | +url_patterns = [URL(path="/users", controller=UsersView)] |
30 | 27 | config_loader = SettingsLoader(SettingsConfigType.PYMODULE, "el_config.py") |
31 | 28 | settings = config_loader.get_settings() |
32 | 29 | static_files = [StaticFile(settings, "styles.css")] |
33 | 30 | echonext = EchoNext( |
34 | | - __name__, |
35 | | - settings, |
36 | | - middlewares, |
37 | | - urls=url_patterns, |
38 | | - application_type=ApplicationType.HTML, |
39 | | - static_files=static_files, |
| 31 | + __name__, |
| 32 | + settings, |
| 33 | + middlewares, |
| 34 | + urls=url_patterns, |
| 35 | + application_type=ApplicationType.HTML, |
| 36 | + static_files=static_files, |
40 | 37 | ) |
41 | 38 | apidoc = APIDocumentation(echonext) |
42 | 39 |
|
43 | 40 |
|
44 | 41 | @echonext.route_page("/api-docs") |
45 | 42 | def api_docs(request, response): |
46 | | - ui = APIDocUI(apidoc.generate_spec()) |
47 | | - return ui.generate_html_page() |
| 43 | + ui = APIDocUI(apidoc.generate_spec()) |
| 44 | + return ui.generate_html_page() |
48 | 45 |
|
49 | 46 |
|
50 | 47 | @echonext.route_page("/book") |
51 | | -class BooksResource(View): |
52 | | - """ |
53 | | - This class describes a books resource. |
54 | | - """ |
55 | | - |
56 | | - def get(self, request, response, **kwargs): |
57 | | - """ |
58 | | - get queries |
59 | | -
|
60 | | - :param request: The request |
61 | | - :type request: Request |
62 | | - :param response: The response |
63 | | - :type response: Response |
64 | | - :param kwargs: The keywords arguments |
65 | | - :type kwargs: dictionary |
66 | | -
|
67 | | - :returns: result |
68 | | - :rtype: str |
69 | | - """ |
70 | | - return echonext.i18n_loader.get_string("title %{name}", name=str(request.GET)) |
71 | | - |
72 | | - def post(self, request, response, **kwargs): |
73 | | - """ |
74 | | - post queries |
75 | | -
|
76 | | - :param request: The request |
77 | | - :type request: Request |
78 | | - :param response: The response |
79 | | - :type response: Response |
80 | | - :param kwargs: The keywords arguments |
81 | | - :type kwargs: dictionary |
82 | | -
|
83 | | - :returns: result |
84 | | - :rtype: str |
85 | | - """ |
86 | | - return echonext.l10n_loader.format_currency(1305.50) |
| 48 | +class BooksResource(PageController): |
| 49 | + """ |
| 50 | + This class describes a books resource. |
| 51 | + """ |
| 52 | + |
| 53 | + def get(self, request, response, **kwargs): |
| 54 | + """ |
| 55 | + get queries |
| 56 | +
|
| 57 | + :param request: The request |
| 58 | + :type request: Request |
| 59 | + :param response: The response |
| 60 | + :type response: Response |
| 61 | + :param kwargs: The keywords arguments |
| 62 | + :type kwargs: dictionary |
| 63 | +
|
| 64 | + :returns: result |
| 65 | + :rtype: str |
| 66 | + """ |
| 67 | + return echonext.i18n_loader.get_string("title %{name}", name=str(request.GET)) |
| 68 | + |
| 69 | + def post(self, request, response, **kwargs): |
| 70 | + """ |
| 71 | + post queries |
| 72 | +
|
| 73 | + :param request: The request |
| 74 | + :type request: Request |
| 75 | + :param response: The response |
| 76 | + :type response: Response |
| 77 | + :param kwargs: The keywords arguments |
| 78 | + :type kwargs: dictionary |
| 79 | +
|
| 80 | + :returns: result |
| 81 | + :rtype: str |
| 82 | + """ |
| 83 | + return echonext.l10n_loader.format_currency(1305.50) |
0 commit comments