Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce whitenoise and restructure html a bit #1

Merged
merged 5 commits into from
Nov 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '^ar@p4e$sdqoa*=gd)*(e*(+&k3rv#x_*v5w*wb9+ef@097-fd'
SECRET_KEY = os.environ.get('SECRET_KEY', "a-not-so-secret-key")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

DEBUG = os.environ.get('DEBUG', True)
ALLOWED_HOSTS = ['*', ]


# Application definition

INSTALLED_APPS = [
Expand All @@ -48,6 +46,7 @@

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
Expand All @@ -74,9 +73,6 @@
},
]

WSGI_APPLICATION = 'app.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

Expand Down Expand Up @@ -115,24 +111,22 @@
TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
STATIC_ROOT = BASE_DIR / "static"

CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels.layers.InMemoryChannelLayer"
}
"default": {
# IMPORTANT, do not ever use this in a production environment
"BACKEND": "channels.layers.InMemoryChannelLayer"
}
}


ASGI_APPLICATION = 'sockpuppet.routing.application'

WSGI_APPLICATION = 'app.wsgi.application'
10 changes: 7 additions & 3 deletions core/templates/_add_sources.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{% load pygmentize %}
{% for file in files %}
{% for filetype, filenames in files.items %}
<details>
<summary>{{file.name}}</summary>
{{file.src|pygmentize:file.type}}
<summary>{{ filetype }}</summary>
{% for file in filenames %}
<br>
{{ file.filename }} (LOC {{ file.loc }})
{{ file.src|pygmentize:file.pygment_type }}
{% endfor %}
</details>
{% endfor %}
40 changes: 40 additions & 0 deletions core/templates/_book_search_demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% load humanize %}
<form data-controller="book-search" data-reflex-root="#morph">
<input type="text" placeholder="search for a book ..." data-target="book-search.query"
data-action="debounced:input->book-search#perform" />
<div id="morph">
<div>

<span data-target="book-search.activity" class="" hidden>
<i class="fas fa-spinner fa-spin"></i>
Searching for books...
</span>
<span data-target="book-search.count" class="">
<strong>{{count|default:0|intcomma}}</strong> books found
</span>
</div>

<table data-target="book-search.list" {% if not books %} hidden {% endif %}>
<thead>
<tr>
<th scope="col">Subject</th>
<th scope="col">Title</th>
<th scope="col">Author</th>
<th scope="col">Publish Date</th>
<th scope="col">ISBN</th>
</tr>
</thead>
<tbody>
{% for book in books|slice:'0:5' %}
<tr>
<td>{{book.subject|join:', '|truncatechars:10}} </td>
<td>{{book.title|truncatechars:30}}</td>
<td>{{book.author_name|join:', '|truncatechars:30}}</td>
<td>{{book.publish_year.0 }}</td>
<td>{{book.isbn.0|truncatechars:5}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</form>
10 changes: 10 additions & 0 deletions core/templates/_example_demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<section>
<a
href="#"
data-controller="example"
data-action="click->example#increment"
data-count="{{ count }}"
>
Increment {{ count }}
</a>
</section>
50 changes: 0 additions & 50 deletions core/templates/book_search.html

This file was deleted.

9 changes: 9 additions & 0 deletions core/templates/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends "base.html" %}
{% load humanize %}
{% block subtitle %}{{ subtitle }}{% endblock subtitle %}
{% block main %}
<div>
{% include demo_template %}
{% include "_add_sources.html" %}
</div>
{% endblock %}
15 changes: 0 additions & 15 deletions core/templates/example.html

This file was deleted.

11 changes: 6 additions & 5 deletions core/views/book_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@


class BookSearch(MixinBase, TemplateView):
template_name="book_search.html"
demo_template = "_book_search_demo.html"
subtitle = 'Search Book'
files = (
('core/reflexes/book_search_reflex.py', 'python3'),
('core/views/book_search.py', 'python3'),
('core/javascript/controllers/book_search_controller.js', 'typescript'),
('core/templates/book_search.html', 'htmldjango'),
('core/reflexes/book_search_reflex.py', 'python', 'python3'),
('core/views/book_search.py', 'python', 'python3'),
('core/javascript/controllers/book_search_controller.js', 'javascript', 'javascript'),
('core/templates/_book_search_demo.html', 'html', 'htmldjango'),
)

book_search = BookSearch.as_view()
12 changes: 6 additions & 6 deletions core/views/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from .mixins import MixinBase

class ExampleView(MixinBase, TemplateView):

template_name = 'example.html'
demo_template = '_example_demo.html'
subtitle = 'Increment'
files = (
('core/views/example.py', 'python3'),
('core/reflexes/example_reflex.py', 'python3'),
('core/javascript/controllers/example_controller.js', 'typescript'),
('core/templates/example.html', 'htmldjango'),
('core/views/example.py', 'python', 'python3'),
('core/reflexes/example_reflex.py', 'python', 'python3'),
('core/javascript/controllers/example_controller.js', 'javascript', 'javascript'),
('core/templates/_example_demo.html', 'html', 'htmldjango'),
)

def get_context_data(self, *args, **kwargs):
Expand Down
18 changes: 17 additions & 1 deletion core/views/mixins.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
from collections import defaultdict
from django.conf import settings
import os

BASE_PATH = settings.BASE_DIR


class MixinBase:
template_name="demo.html"
demo_template = None
subtitle = None

def get_files(self):
files = defaultdict(list)
path_ = lambda x: open(os.path.join(BASE_PATH, x)).read()
return [dict(src=path_(i[0]), type=i[1], name=i[0]) for i in self.files]
for filename, filetype, pygment_type in self.files:
filesrc = path_(filename)
files[filetype].append({
'src': filesrc,
'pygment_type': pygment_type,
'filename': filename,
'loc': len(filesrc.split('\n'))
})
return dict(files)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['files'] = self.get_files()
context['demo_template'] = self.demo_template
context['subtitle'] = self.subtitle
return context

Loading