mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-09 15:42:23 +03:00
Make various fixes
This commit is contained in:
parent
b1b2379280
commit
458bea7b83
|
@ -333,26 +333,59 @@ Put next into the ``index.html``:
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1 class="mb-4">Github Navigator</h1>
|
<h1 class="mb-4">Github Navigator</h1>
|
||||||
|
|
||||||
<p class="mb-4">
|
<form>
|
||||||
<form>
|
<div class="form-group form-row">
|
||||||
<div class="form-group form-row">
|
<label for="search_term" class="col-form-label">Search for:</label>
|
||||||
<label for="search_term" class="col-form-label">Search for:</label>
|
<div class="col-10">
|
||||||
<div class="col-10">
|
<input class="form-control" type="text" id="search_term"
|
||||||
<input class="form-control" type="text" id="search_term"
|
placeholder="Type something to search on the GitHub"
|
||||||
placeholder="Type something to search on the GitHub"
|
name="search_term"
|
||||||
name="search_term"
|
value="{{ search_term if search_term }}">
|
||||||
value="{{ search_term if search_term }}">
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</form>
|
||||||
</form>
|
|
||||||
</p>
|
|
||||||
|
|
||||||
|
|
||||||
{% if repositories|length == 0 %}
|
{% if repositories|length == 0 %}
|
||||||
<p><small>No search results</small></p>
|
<small>No search results</small>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<small>Results found: {{ repositories|length }}</small>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<table class="table table-striped">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>#</th>
|
||||||
|
<th>Repository</th>
|
||||||
|
<th class="text-nowrap">Repository owner</th>
|
||||||
|
<th class="text-nowrap">Last commit</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for repository in repositories %} {{n}}
|
||||||
|
<tr>
|
||||||
|
<th>{{ loop.index }}</th>
|
||||||
|
<td><a href="{{ repository.url }}">
|
||||||
|
{{ repository.name }}</a>
|
||||||
|
</td>
|
||||||
|
<td><a href="{{ repository.owner.url }}">
|
||||||
|
<img src="{{ repository.owner.avatar_url }}"
|
||||||
|
alt="avatar" height="24" width="24"/></a>
|
||||||
|
<a href="{{ repository.owner.url }}">
|
||||||
|
{{ repository.owner.login }}</a>
|
||||||
|
</td>
|
||||||
|
<td><a href="{{ repository.latest_commit.url }}">
|
||||||
|
{{ repository.latest_commit.sha }}</a>
|
||||||
|
{{ repository.latest_commit.message }}
|
||||||
|
{{ repository.latest_commit.author_name }}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
Ok, almost there. The last step is to make ``index`` view to render the ``index.html`` template.
|
Ok, almost there. The last step is to make ``index`` view to render the ``index.html`` template.
|
||||||
|
@ -360,20 +393,25 @@ Ok, almost there. The last step is to make ``index`` view to render the ``index.
|
||||||
Edit ``views.py``:
|
Edit ``views.py``:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
:emphasize-lines: 3,7
|
|
||||||
|
|
||||||
"""Views module."""
|
"""Views module."""
|
||||||
|
|
||||||
from flask import render_template
|
from flask import request, render_template
|
||||||
|
|
||||||
|
|
||||||
def index():
|
def index():
|
||||||
return render_template('index.html')
|
search_term = request.args.get('search_term', 'Dependency Injector')
|
||||||
|
repositories = []
|
||||||
|
|
||||||
|
return render_template(
|
||||||
|
'index.html',
|
||||||
|
search_term=search_term,
|
||||||
|
repositories=repositories,
|
||||||
|
)
|
||||||
|
|
||||||
That's it.
|
That's it.
|
||||||
|
|
||||||
Make sure the app is running and open ``http://127.0.0.1:7000/``.
|
Make sure the app is running and open ``http://127.0.0.1:5000/``.
|
||||||
|
|
||||||
You should see:
|
You should see:
|
||||||
|
|
||||||
|
@ -667,6 +705,86 @@ Edit ``containers.py``:
|
||||||
|
|
||||||
index_view = flask.View(views.index)
|
index_view = flask.View(views.index)
|
||||||
|
|
||||||
|
Make the search
|
||||||
|
---------------
|
||||||
|
|
||||||
|
Now we are ready to make the search work.
|
||||||
|
|
||||||
|
Edit ``views.py``:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
:emphasize-lines: 5,8,11,16
|
||||||
|
|
||||||
|
"""Views module."""
|
||||||
|
|
||||||
|
from flask import request, render_template
|
||||||
|
|
||||||
|
from .services import SearchService
|
||||||
|
|
||||||
|
|
||||||
|
def index(search_service: SearchService):
|
||||||
|
search_term = request.args.get('search_term')
|
||||||
|
|
||||||
|
repositories = search_service.search_repositories(search_term)
|
||||||
|
|
||||||
|
return render_template(
|
||||||
|
'index.html',
|
||||||
|
search_term=search_term,
|
||||||
|
repositories=repositories,
|
||||||
|
)
|
||||||
|
|
||||||
|
Edit ``containers.py``:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
:emphasize-lines: 32-38
|
||||||
|
|
||||||
|
"""Application containers module."""
|
||||||
|
|
||||||
|
from dependency_injector import containers, providers
|
||||||
|
from dependency_injector.ext import flask
|
||||||
|
from flask import Flask
|
||||||
|
from flask_bootstrap import Bootstrap
|
||||||
|
from github import Github
|
||||||
|
|
||||||
|
from . import services, views
|
||||||
|
|
||||||
|
|
||||||
|
class ApplicationContainer(containers.DeclarativeContainer):
|
||||||
|
"""Application container."""
|
||||||
|
|
||||||
|
app = flask.Application(Flask, __name__)
|
||||||
|
|
||||||
|
bootstrap = flask.Extension(Bootstrap)
|
||||||
|
|
||||||
|
config = providers.Configuration()
|
||||||
|
|
||||||
|
github_client = providers.Factory(
|
||||||
|
Github,
|
||||||
|
login_or_token=config.github.auth_token,
|
||||||
|
timeout=config.github.request_timeout,
|
||||||
|
)
|
||||||
|
|
||||||
|
search_service = providers.Factory(
|
||||||
|
services.SearchService,
|
||||||
|
github_client=github_client,
|
||||||
|
)
|
||||||
|
|
||||||
|
index_view = flask.View(
|
||||||
|
views.index,
|
||||||
|
search_service=search_service,
|
||||||
|
)
|
||||||
|
|
||||||
|
Edit ``config.yml``:
|
||||||
|
|
||||||
|
.. code-block::
|
||||||
|
:emphasize-lines: 3-5
|
||||||
|
|
||||||
|
github:
|
||||||
|
request_timeout: 10
|
||||||
|
search:
|
||||||
|
default_term: "Dependency Injector"
|
||||||
|
default_limit: 5
|
||||||
|
|
||||||
Tests
|
Tests
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
|
|
@ -32,22 +32,29 @@
|
||||||
<th>#</th>
|
<th>#</th>
|
||||||
<th>Repository</th>
|
<th>Repository</th>
|
||||||
<th class="text-nowrap">Repository owner</th>
|
<th class="text-nowrap">Repository owner</th>
|
||||||
<!-- <th class="text-nowrap">Created at</th>-->
|
|
||||||
<th class="text-nowrap">Last commit</th>
|
<th class="text-nowrap">Last commit</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for repository in repositories %} {{n}}
|
{% for repository in repositories %} {{n}}
|
||||||
<tr>
|
<tr>
|
||||||
<th>{{ loop.index }}</th>
|
<th>{{ loop.index }}</th>
|
||||||
<td><a href="{{ repository.url }}">{{ repository.name }}</a></td>
|
<td><a href="{{ repository.url }}">
|
||||||
<td><a href="{{ repository.owner.url }}"><img src="{{ repository.owner.avatar_url }}" alt="avatar" height="24" width="24"/></a>
|
{{ repository.name }}</a>
|
||||||
<a href="{{ repository.owner.url }}">{{ repository.owner.login }}</a>
|
</td>
|
||||||
|
<td><a href="{{ repository.owner.url }}">
|
||||||
|
<img src="{{ repository.owner.avatar_url }}"
|
||||||
|
alt="avatar" height="24" width="24"/></a>
|
||||||
|
<a href="{{ repository.owner.url }}">
|
||||||
|
{{ repository.owner.login }}</a>
|
||||||
|
</td>
|
||||||
|
<td><a href="{{ repository.latest_commit.url }}">
|
||||||
|
{{ repository.latest_commit.sha }}</a>
|
||||||
|
{{ repository.latest_commit.message }}
|
||||||
|
{{ repository.latest_commit.author_name }}
|
||||||
</td>
|
</td>
|
||||||
<td><a href="{{ repository.latest_commit.url }}">{{ repository.latest_commit.sha }}</a> {{ repository.latest_commit['message'] }} {{ repository.latest_commit.author_name }}</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -5,15 +5,12 @@ from flask import request, render_template
|
||||||
from .services import SearchService
|
from .services import SearchService
|
||||||
|
|
||||||
|
|
||||||
def index(search_service: SearchService, default_search_term, default_search_limit):
|
def index(search_service: SearchService, default_search_term: str, default_search_limit: int):
|
||||||
search_term = request.args.get('search_term', default_search_term)
|
search_term = request.args.get('search_term', default_search_term)
|
||||||
limit = request.args.get('limit', default_search_limit, int)
|
repositories = search_service.search_repositories(search_term, default_search_limit)
|
||||||
|
|
||||||
repositories = search_service.search_repositories(search_term, limit)
|
|
||||||
|
|
||||||
return render_template(
|
return render_template(
|
||||||
'index.html',
|
'index.html',
|
||||||
search_term=search_term,
|
search_term=search_term,
|
||||||
limit=limit,
|
|
||||||
repositories=repositories,
|
repositories=repositories,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user