mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 01:26:51 +03:00
Update django example
This commit is contained in:
parent
196d86f4b3
commit
023d766267
|
@ -105,9 +105,9 @@ The output should be something like:
|
||||||
githubnavigator/wsgi.py 4 4 0%
|
githubnavigator/wsgi.py 4 4 0%
|
||||||
manage.py 12 2 83%
|
manage.py 12 2 83%
|
||||||
web/__init__.py 0 0 100%
|
web/__init__.py 0 0 100%
|
||||||
web/apps.py 7 0 100%
|
web/apps.py 6 0 100%
|
||||||
web/tests.py 28 0 100%
|
web/tests.py 28 0 100%
|
||||||
web/urls.py 3 0 100%
|
web/urls.py 3 0 100%
|
||||||
web/views.py 12 0 100%
|
web/views.py 12 0 100%
|
||||||
---------------------------------------------------
|
---------------------------------------------------
|
||||||
TOTAL 121 10 92%
|
TOTAL 120 10 92%
|
||||||
|
|
|
@ -10,6 +10,6 @@ import os
|
||||||
|
|
||||||
from django.core.asgi import get_asgi_application
|
from django.core.asgi import get_asgi_application
|
||||||
|
|
||||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'githubnavigator.settings')
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "githubnavigator.settings")
|
||||||
|
|
||||||
application = get_asgi_application()
|
application = get_asgi_application()
|
||||||
|
|
|
@ -15,7 +15,7 @@ class SearchService:
|
||||||
"""Search for repositories and return formatted data."""
|
"""Search for repositories and return formatted data."""
|
||||||
repositories = self._github_client.search_repositories(
|
repositories = self._github_client.search_repositories(
|
||||||
query=query,
|
query=query,
|
||||||
**{'in': 'name'},
|
**{"in": "name"},
|
||||||
)
|
)
|
||||||
return [
|
return [
|
||||||
self._format_repo(repository)
|
self._format_repo(repository)
|
||||||
|
@ -25,20 +25,20 @@ class SearchService:
|
||||||
def _format_repo(self, repository: Repository):
|
def _format_repo(self, repository: Repository):
|
||||||
commits = repository.get_commits()
|
commits = repository.get_commits()
|
||||||
return {
|
return {
|
||||||
'url': repository.html_url,
|
"url": repository.html_url,
|
||||||
'name': repository.name,
|
"name": repository.name,
|
||||||
'owner': {
|
"owner": {
|
||||||
'login': repository.owner.login,
|
"login": repository.owner.login,
|
||||||
'url': repository.owner.html_url,
|
"url": repository.owner.html_url,
|
||||||
'avatar_url': repository.owner.avatar_url,
|
"avatar_url": repository.owner.avatar_url,
|
||||||
},
|
},
|
||||||
'latest_commit': self._format_commit(commits[0]) if commits else {},
|
"latest_commit": self._format_commit(commits[0]) if commits else {},
|
||||||
}
|
}
|
||||||
|
|
||||||
def _format_commit(self, commit: Commit):
|
def _format_commit(self, commit: Commit):
|
||||||
return {
|
return {
|
||||||
'sha': commit.sha,
|
"sha": commit.sha,
|
||||||
'url': commit.html_url,
|
"url": commit.html_url,
|
||||||
'message': commit.commit.message,
|
"message": commit.commit.message,
|
||||||
'author_name': commit.commit.author.name,
|
"author_name": commit.commit.author.name,
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
|
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
|
||||||
|
|
||||||
# SECURITY WARNING: keep the secret key used in production secret!
|
# SECURITY WARNING: keep the secret key used in production secret!
|
||||||
SECRET_KEY = ')6*iyg26c9l!fvyvwd&3+vyf-dcw)e=5x2t(j)(*c29z@ykhi0'
|
SECRET_KEY = ")6*iyg26c9l!fvyvwd&3+vyf-dcw)e=5x2t(j)(*c29z@ykhi0"
|
||||||
|
|
||||||
# SECURITY WARNING: don't run with debug turned on in production!
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
|
@ -31,54 +31,54 @@ ALLOWED_HOSTS = []
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
'web.apps.WebConfig',
|
"web.apps.WebConfig",
|
||||||
'bootstrap4',
|
"bootstrap4",
|
||||||
'django.contrib.admin',
|
"django.contrib.admin",
|
||||||
'django.contrib.auth',
|
"django.contrib.auth",
|
||||||
'django.contrib.contenttypes',
|
"django.contrib.contenttypes",
|
||||||
'django.contrib.sessions',
|
"django.contrib.sessions",
|
||||||
'django.contrib.messages',
|
"django.contrib.messages",
|
||||||
'django.contrib.staticfiles',
|
"django.contrib.staticfiles",
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
'django.middleware.security.SecurityMiddleware',
|
"django.middleware.security.SecurityMiddleware",
|
||||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||||
'django.middleware.common.CommonMiddleware',
|
"django.middleware.common.CommonMiddleware",
|
||||||
'django.middleware.csrf.CsrfViewMiddleware',
|
"django.middleware.csrf.CsrfViewMiddleware",
|
||||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
"django.contrib.messages.middleware.MessageMiddleware",
|
||||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||||
]
|
]
|
||||||
|
|
||||||
ROOT_URLCONF = 'githubnavigator.urls'
|
ROOT_URLCONF = "githubnavigator.urls"
|
||||||
|
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
||||||
'DIRS': [],
|
"DIRS": [],
|
||||||
'APP_DIRS': True,
|
"APP_DIRS": True,
|
||||||
'OPTIONS': {
|
"OPTIONS": {
|
||||||
'context_processors': [
|
"context_processors": [
|
||||||
'django.template.context_processors.debug',
|
"django.template.context_processors.debug",
|
||||||
'django.template.context_processors.request',
|
"django.template.context_processors.request",
|
||||||
'django.contrib.auth.context_processors.auth',
|
"django.contrib.auth.context_processors.auth",
|
||||||
'django.contrib.messages.context_processors.messages',
|
"django.contrib.messages.context_processors.messages",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
WSGI_APPLICATION = 'githubnavigator.wsgi.application'
|
WSGI_APPLICATION = "githubnavigator.wsgi.application"
|
||||||
|
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
|
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
|
||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
"default": {
|
||||||
'ENGINE': 'django.db.backends.sqlite3',
|
"ENGINE": "django.db.backends.sqlite3",
|
||||||
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
|
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,16 +88,16 @@ DATABASES = {
|
||||||
|
|
||||||
AUTH_PASSWORD_VALIDATORS = [
|
AUTH_PASSWORD_VALIDATORS = [
|
||||||
{
|
{
|
||||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -105,9 +105,9 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||||
# Internationalization
|
# Internationalization
|
||||||
# https://docs.djangoproject.com/en/3.0/topics/i18n/
|
# https://docs.djangoproject.com/en/3.0/topics/i18n/
|
||||||
|
|
||||||
LANGUAGE_CODE = 'en-us'
|
LANGUAGE_CODE = "en-us"
|
||||||
|
|
||||||
TIME_ZONE = 'UTC'
|
TIME_ZONE = "UTC"
|
||||||
|
|
||||||
USE_I18N = True
|
USE_I18N = True
|
||||||
|
|
||||||
|
@ -119,13 +119,13 @@ USE_TZ = True
|
||||||
# Static files (CSS, JavaScript, Images)
|
# Static files (CSS, JavaScript, Images)
|
||||||
# https://docs.djangoproject.com/en/3.0/howto/static-files/
|
# https://docs.djangoproject.com/en/3.0/howto/static-files/
|
||||||
|
|
||||||
STATIC_URL = '/static/'
|
STATIC_URL = "/static/"
|
||||||
|
|
||||||
# Github client settings
|
# Github client settings
|
||||||
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
|
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
|
||||||
GITHUB_REQUEST_TIMEOUT = 10
|
GITHUB_REQUEST_TIMEOUT = 10
|
||||||
|
|
||||||
# Search settings
|
# Search settings
|
||||||
DEFAULT_LIMIT = 5
|
DEFAULT_LIMIT = 5
|
||||||
DEFAULT_QUERY = 'Dependency Injector'
|
DEFAULT_QUERY = "Dependency Injector"
|
||||||
LIMIT_OPTIONS = [5, 10, 20]
|
LIMIT_OPTIONS = [5, 10, 20]
|
||||||
|
|
|
@ -17,6 +17,6 @@ from django.contrib import admin
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', include('web.urls')),
|
path("", include("web.urls")),
|
||||||
path('admin/', admin.site.urls),
|
path("admin/", admin.site.urls),
|
||||||
]
|
]
|
||||||
|
|
|
@ -11,6 +11,6 @@ import os
|
||||||
|
|
||||||
from django.core.wsgi import get_wsgi_application
|
from django.core.wsgi import get_wsgi_application
|
||||||
|
|
||||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'githubnavigator.settings')
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "githubnavigator.settings")
|
||||||
|
|
||||||
application = get_wsgi_application()
|
application = get_wsgi_application()
|
||||||
|
|
|
@ -5,7 +5,7 @@ import sys
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'githubnavigator.settings')
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "githubnavigator.settings")
|
||||||
try:
|
try:
|
||||||
from django.core.management import execute_from_command_line
|
from django.core.management import execute_from_command_line
|
||||||
except ImportError as exc:
|
except ImportError as exc:
|
||||||
|
@ -17,5 +17,5 @@ def main():
|
||||||
execute_from_command_line(sys.argv)
|
execute_from_command_line(sys.argv)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -3,11 +3,10 @@
|
||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
|
|
||||||
from githubnavigator import container
|
from githubnavigator import container
|
||||||
from . import views
|
|
||||||
|
|
||||||
|
|
||||||
class WebConfig(AppConfig):
|
class WebConfig(AppConfig):
|
||||||
name = 'web'
|
name = "web"
|
||||||
|
|
||||||
def ready(self):
|
def ready(self):
|
||||||
container.wire(modules=[views])
|
container.wire(modules=[".views"])
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
{% extends 'bootstrap4/bootstrap4.html' %}
|
{% extends "bootstrap4/bootstrap4.html" %}
|
||||||
|
|
||||||
{% load bootstrap4 %}
|
{% load bootstrap4 %}
|
||||||
|
|
||||||
|
|
|
@ -15,49 +15,49 @@ class IndexTests(TestCase):
|
||||||
github_client_mock = mock.Mock(spec=Github)
|
github_client_mock = mock.Mock(spec=Github)
|
||||||
github_client_mock.search_repositories.return_value = [
|
github_client_mock.search_repositories.return_value = [
|
||||||
mock.Mock(
|
mock.Mock(
|
||||||
html_url='repo1-url',
|
html_url="repo1-url",
|
||||||
name='repo1-name',
|
name="repo1-name",
|
||||||
owner=mock.Mock(
|
owner=mock.Mock(
|
||||||
login='owner1-login',
|
login="owner1-login",
|
||||||
html_url='owner1-url',
|
html_url="owner1-url",
|
||||||
avatar_url='owner1-avatar-url',
|
avatar_url="owner1-avatar-url",
|
||||||
),
|
),
|
||||||
get_commits=mock.Mock(return_value=[mock.Mock()]),
|
get_commits=mock.Mock(return_value=[mock.Mock()]),
|
||||||
),
|
),
|
||||||
mock.Mock(
|
mock.Mock(
|
||||||
html_url='repo2-url',
|
html_url="repo2-url",
|
||||||
name='repo2-name',
|
name="repo2-name",
|
||||||
owner=mock.Mock(
|
owner=mock.Mock(
|
||||||
login='owner2-login',
|
login="owner2-login",
|
||||||
html_url='owner2-url',
|
html_url="owner2-url",
|
||||||
avatar_url='owner2-avatar-url',
|
avatar_url="owner2-avatar-url",
|
||||||
),
|
),
|
||||||
get_commits=mock.Mock(return_value=[mock.Mock()]),
|
get_commits=mock.Mock(return_value=[mock.Mock()]),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
with container.github_client.override(github_client_mock):
|
with container.github_client.override(github_client_mock):
|
||||||
response = self.client.get(reverse('index'))
|
response = self.client.get(reverse("index"))
|
||||||
|
|
||||||
self.assertContains(response, 'Results found: 2')
|
self.assertContains(response, "Results found: 2")
|
||||||
|
|
||||||
self.assertContains(response, 'repo1-url')
|
self.assertContains(response, "repo1-url")
|
||||||
self.assertContains(response, 'repo1-name')
|
self.assertContains(response, "repo1-name")
|
||||||
self.assertContains(response, 'owner1-login')
|
self.assertContains(response, "owner1-login")
|
||||||
self.assertContains(response, 'owner1-url')
|
self.assertContains(response, "owner1-url")
|
||||||
self.assertContains(response, 'owner1-avatar-url')
|
self.assertContains(response, "owner1-avatar-url")
|
||||||
|
|
||||||
self.assertContains(response, 'repo2-url')
|
self.assertContains(response, "repo2-url")
|
||||||
self.assertContains(response, 'repo2-name')
|
self.assertContains(response, "repo2-name")
|
||||||
self.assertContains(response, 'owner2-login')
|
self.assertContains(response, "owner2-login")
|
||||||
self.assertContains(response, 'owner2-url')
|
self.assertContains(response, "owner2-url")
|
||||||
self.assertContains(response, 'owner2-avatar-url')
|
self.assertContains(response, "owner2-avatar-url")
|
||||||
|
|
||||||
def test_index_no_results(self):
|
def test_index_no_results(self):
|
||||||
github_client_mock = mock.Mock(spec=Github)
|
github_client_mock = mock.Mock(spec=Github)
|
||||||
github_client_mock.search_repositories.return_value = []
|
github_client_mock.search_repositories.return_value = []
|
||||||
|
|
||||||
with container.github_client.override(github_client_mock):
|
with container.github_client.override(github_client_mock):
|
||||||
response = self.client.get(reverse('index'))
|
response = self.client.get(reverse("index"))
|
||||||
|
|
||||||
self.assertContains(response, 'Results found: 0')
|
self.assertContains(response, "Results found: 0")
|
||||||
|
|
|
@ -5,5 +5,5 @@ from django.urls import path
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.index, name='index'),
|
path("", views.index, name="index"),
|
||||||
]
|
]
|
||||||
|
|
|
@ -18,18 +18,18 @@ def index(
|
||||||
default_limit: int = Provide[Container.config.DEFAULT_LIMIT.as_int()],
|
default_limit: int = Provide[Container.config.DEFAULT_LIMIT.as_int()],
|
||||||
limit_options: List[int] = Provide[Container.config.LIMIT_OPTIONS],
|
limit_options: List[int] = Provide[Container.config.LIMIT_OPTIONS],
|
||||||
) -> HttpResponse:
|
) -> HttpResponse:
|
||||||
query = request.GET.get('query', default_query)
|
query = request.GET.get("query", default_query)
|
||||||
limit = int(request.GET.get('limit', default_limit))
|
limit = int(request.GET.get("limit", default_limit))
|
||||||
|
|
||||||
repositories = search_service.search_repositories(query, limit)
|
repositories = search_service.search_repositories(query, limit)
|
||||||
|
|
||||||
return render(
|
return render(
|
||||||
request,
|
request,
|
||||||
template_name='index.html',
|
template_name="index.html",
|
||||||
context={
|
context={
|
||||||
'query': query,
|
"query": query,
|
||||||
'limit': limit,
|
"limit": limit,
|
||||||
'limit_options': limit_options,
|
"limit_options": limit_options,
|
||||||
'repositories': repositories,
|
"repositories": repositories,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user