Update django example

This commit is contained in:
Roman Mogylatov 2021-09-30 19:17:30 -04:00
parent 196d86f4b3
commit 023d766267
12 changed files with 94 additions and 95 deletions

View File

@ -105,9 +105,9 @@ The output should be something like:
githubnavigator/wsgi.py 4 4 0%
manage.py 12 2 83%
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/urls.py 3 0 100%
web/views.py 12 0 100%
---------------------------------------------------
TOTAL 121 10 92%
TOTAL 120 10 92%

View File

@ -10,6 +10,6 @@ import os
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()

View File

@ -15,7 +15,7 @@ class SearchService:
"""Search for repositories and return formatted data."""
repositories = self._github_client.search_repositories(
query=query,
**{'in': 'name'},
**{"in": "name"},
)
return [
self._format_repo(repository)
@ -25,20 +25,20 @@ class SearchService:
def _format_repo(self, repository: Repository):
commits = repository.get_commits()
return {
'url': repository.html_url,
'name': repository.name,
'owner': {
'login': repository.owner.login,
'url': repository.owner.html_url,
'avatar_url': repository.owner.avatar_url,
"url": repository.html_url,
"name": repository.name,
"owner": {
"login": repository.owner.login,
"url": repository.owner.html_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):
return {
'sha': commit.sha,
'url': commit.html_url,
'message': commit.commit.message,
'author_name': commit.commit.author.name,
"sha": commit.sha,
"url": commit.html_url,
"message": commit.commit.message,
"author_name": commit.commit.author.name,
}

View File

@ -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/
# 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!
DEBUG = True
@ -31,54 +31,54 @@ ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'web.apps.WebConfig',
'bootstrap4',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"web.apps.WebConfig",
"bootstrap4",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = 'githubnavigator.urls'
ROOT_URLCONF = "githubnavigator.urls"
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
WSGI_APPLICATION = 'githubnavigator.wsgi.application'
WSGI_APPLICATION = "githubnavigator.wsgi.application"
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
}
}
@ -88,16 +88,16 @@ DATABASES = {
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
# 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
@ -119,13 +119,13 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
STATIC_URL = "/static/"
# Github client settings
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
GITHUB_REQUEST_TIMEOUT = 10
# Search settings
DEFAULT_LIMIT = 5
DEFAULT_QUERY = 'Dependency Injector'
DEFAULT_QUERY = "Dependency Injector"
LIMIT_OPTIONS = [5, 10, 20]

View File

@ -17,6 +17,6 @@ from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('web.urls')),
path('admin/', admin.site.urls),
path("", include("web.urls")),
path("admin/", admin.site.urls),
]

View File

@ -11,6 +11,6 @@ import os
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()

View File

@ -5,7 +5,7 @@ import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'githubnavigator.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "githubnavigator.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
@ -17,5 +17,5 @@ def main():
execute_from_command_line(sys.argv)
if __name__ == '__main__':
if __name__ == "__main__":
main()

View File

@ -3,11 +3,10 @@
from django.apps import AppConfig
from githubnavigator import container
from . import views
class WebConfig(AppConfig):
name = 'web'
name = "web"
def ready(self):
container.wire(modules=[views])
container.wire(modules=[".views"])

View File

@ -1,4 +1,4 @@
{% extends 'bootstrap4/bootstrap4.html' %}
{% extends "bootstrap4/bootstrap4.html" %}
{% load bootstrap4 %}

View File

@ -15,49 +15,49 @@ class IndexTests(TestCase):
github_client_mock = mock.Mock(spec=Github)
github_client_mock.search_repositories.return_value = [
mock.Mock(
html_url='repo1-url',
name='repo1-name',
html_url="repo1-url",
name="repo1-name",
owner=mock.Mock(
login='owner1-login',
html_url='owner1-url',
avatar_url='owner1-avatar-url',
login="owner1-login",
html_url="owner1-url",
avatar_url="owner1-avatar-url",
),
get_commits=mock.Mock(return_value=[mock.Mock()]),
),
mock.Mock(
html_url='repo2-url',
name='repo2-name',
html_url="repo2-url",
name="repo2-name",
owner=mock.Mock(
login='owner2-login',
html_url='owner2-url',
avatar_url='owner2-avatar-url',
login="owner2-login",
html_url="owner2-url",
avatar_url="owner2-avatar-url",
),
get_commits=mock.Mock(return_value=[mock.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-name')
self.assertContains(response, 'owner1-login')
self.assertContains(response, 'owner1-url')
self.assertContains(response, 'owner1-avatar-url')
self.assertContains(response, "repo1-url")
self.assertContains(response, "repo1-name")
self.assertContains(response, "owner1-login")
self.assertContains(response, "owner1-url")
self.assertContains(response, "owner1-avatar-url")
self.assertContains(response, 'repo2-url')
self.assertContains(response, 'repo2-name')
self.assertContains(response, 'owner2-login')
self.assertContains(response, 'owner2-url')
self.assertContains(response, 'owner2-avatar-url')
self.assertContains(response, "repo2-url")
self.assertContains(response, "repo2-name")
self.assertContains(response, "owner2-login")
self.assertContains(response, "owner2-url")
self.assertContains(response, "owner2-avatar-url")
def test_index_no_results(self):
github_client_mock = mock.Mock(spec=Github)
github_client_mock.search_repositories.return_value = []
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")

View File

@ -5,5 +5,5 @@ from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path("", views.index, name="index"),
]

View File

@ -18,18 +18,18 @@ def index(
default_limit: int = Provide[Container.config.DEFAULT_LIMIT.as_int()],
limit_options: List[int] = Provide[Container.config.LIMIT_OPTIONS],
) -> HttpResponse:
query = request.GET.get('query', default_query)
limit = int(request.GET.get('limit', default_limit))
query = request.GET.get("query", default_query)
limit = int(request.GET.get("limit", default_limit))
repositories = search_service.search_repositories(query, limit)
return render(
request,
template_name='index.html',
template_name="index.html",
context={
'query': query,
'limit': limit,
'limit_options': limit_options,
'repositories': repositories,
"query": query,
"limit": limit,
"limit_options": limit_options,
"repositories": repositories,
}
)