Add a minimalistic app + allow for dev env

This commit is contained in:
The Dod 2021-01-11 21:09:22 +02:00
parent aaa86031e0
commit 8e5de872ac
18 changed files with 162 additions and 12 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ project.env
venv
__pycache__
*.pyc
*.sqlite3

18
README.md Normal file
View File

@ -0,0 +1,18 @@
Based on example at https://blog.bitsacm.in/django-on-docker/
(django+gunicorn+nginx+postgresql on docker)
### To install local dev server (sqlite3, and DEBUG):
* `virtualenv -p python3 venv`
* `. venv/bin/activate`
* `pip install -r djangoproject/requirements.txt`
#### To run local dev server
* First time in a shell, run `. venv/bin/activate`, and `. dev.env`
* `cd djangoproject`
* `./manage.py runserver` and browse to http://localhost:8000
### To deploy dockers locally
* You should copy `project.env.example` to `project.env` and edit it.
* `docker-compose up` would then run the server on http://localhost:8888

2
dev.env Normal file
View File

@ -0,0 +1,2 @@
export ENVIRONMENT=dev
export SECRET_KEY=nevermindthisisdev

View File

View File

@ -0,0 +1,10 @@
from django.contrib import admin
from .models import Item
class ItemAdmin(admin.ModelAdmin):
list_display = ['title', 'link', 'modified']
readonly_fields = ['modified']
admin.site.register(Item, ItemAdmin)

View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class BillboardConfig(AppConfig):
name = 'billboard'

View File

@ -0,0 +1,25 @@
# Generated by Django 3.1.5 on 2021-01-11 18:16
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Item',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=64, verbose_name='Title')),
('link', models.URLField(blank=True, verbose_name='link')),
('description', models.TextField(verbose_name='Description')),
('modified', models.DateTimeField(default=django.utils.timezone.now, verbose_name='Modified')),
],
),
]

View File

@ -0,0 +1,17 @@
# Generated by Django 3.1.5 on 2021-01-11 19:12
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('billboard', '0001_initial'),
]
operations = [
migrations.AlterModelOptions(
name='item',
options={'ordering': ['-modified'], 'verbose_name': 'Item', 'verbose_name_plural': 'Items'},
),
]

View File

@ -0,0 +1,25 @@
from django.db import models
from django.utils import timezone
# Maybe one day we'd want to localize this
def _(s): return s
class Item(models.Model):
title = models.CharField(_("Title"), max_length=64)
link = models.URLField(_("link"), blank=True)
description = models.TextField(_("Description"))
modified = models.DateTimeField(_("Modified"), default=timezone.now)
def save(self, *args, **kwargs):
self.modified = timezone.now()
super().save(*args, **kwargs) # Call the "real" save() method.
def __str__(self):
return self.title
class Meta:
verbose_name = _('Item')
verbose_name_plural = _('Items')
ordering = [ '-modified' ]

View File

@ -0,0 +1,16 @@
<html>
<head>
<title>My recommendations</title>
</head>
<body>
<h3>My recommendations</h3>
<ul>
{% for item in items %}
<li>
{% if item.link %}<a href="{{ item.link }}">{% endif %}<strong>{{ item.title }}</strong>{% if item.link %}</a>{% endif %}:
{{ item.description }}
</li>
{% endfor %}
</ul>
</body>
</html>

View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@ -0,0 +1,7 @@
from django.urls import path, re_path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]

View File

@ -0,0 +1,7 @@
from django.shortcuts import render
from .models import Item
def index(request):
return render(request, 'billboard/index.html', {
'items': Item.objects.all()})

View File

@ -10,4 +10,4 @@ class Database:
class Secrets:
SECRET_KEY = 'ladm3zbrp7y$c9h$-jz+(@0)d*zpppges+to67z-pg$wbzcqm='
SECRET_KEY = os.getenv('SECRET_KEY')

View File

@ -16,6 +16,8 @@ from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
ENVIRONMENT = os.getenv('ENVIRONMENT', 'production')
from djangoproject.local_settings import Database, Secrets
@ -27,7 +29,7 @@ from djangoproject.local_settings import Database, Secrets
SECRET_KEY = Secrets.SECRET_KEY
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = ENVIRONMENT.lower()=='dev'
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "0.0.0.0"]
@ -40,6 +42,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'billboard'
]
MIDDLEWARE = [
@ -75,16 +78,24 @@ WSGI_APPLICATION = 'djangoproject.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
if DEBUG:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'db.sqlite3',
}
}
else:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": Database.NAME,
"USER": Database.USER,
"PASSWORD": Database.PASSWORD,
"HOST": Database.HOST,
"PORT": Database.PORT,
}
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": Database.NAME,
"USER": Database.USER,
"PASSWORD": Database.PASSWORD,
"HOST": Database.HOST,
"PORT": Database.PORT,
}
}

View File

@ -14,8 +14,9 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
urlpatterns = [
path('', include('billboard.urls')),
path('admin/', admin.site.urls),
]

View File

@ -1,3 +1,5 @@
ENVIRONMENT=production
SECRET_KEY=verystrongsecretkey
POSTGRES_USER=dbadmin
POSTGRES_PASSWORD=verysecretdbpassword
POSTGRES_DB=project_db