From: Patrick Canterino Date: Thu, 20 Jan 2022 19:38:50 +0000 (+0100) Subject: First commit of my notes Django application X-Git-Url: https://git.p6c8.net/pcdenotes.git/commitdiff_plain/3fb7b9dea69252f64ee82967dfca97baf1b02df7 First commit of my notes Django application It's mainly a very simple blog without comments and other stuff. Main purpose is to post short articles, called "notes". It's loosely based on some Django tutorials. This is the very first commit. The application is able to list the notes and to display a single note. --- 3fb7b9dea69252f64ee82967dfca97baf1b02df7 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..19418ac --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +db.sqlite3 diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..e2961c3 --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pcdenotes.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/notes/__init__.py b/notes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/notes/admin.py b/notes/admin.py new file mode 100644 index 0000000..c2c09e5 --- /dev/null +++ b/notes/admin.py @@ -0,0 +1,12 @@ +from django.contrib import admin +from .models import Note + +# Register your models here. + +class NoteAdmin(admin.ModelAdmin): + list_display = ('title', 'status',) + list_filter = ('status',) + search_fields = ('title', 'content',) + + +admin.site.register(Note, NoteAdmin) \ No newline at end of file diff --git a/notes/apps.py b/notes/apps.py new file mode 100644 index 0000000..832dd3f --- /dev/null +++ b/notes/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class NotesConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'notes' diff --git a/notes/migrations/0001_initial.py b/notes/migrations/0001_initial.py new file mode 100644 index 0000000..0f70787 --- /dev/null +++ b/notes/migrations/0001_initial.py @@ -0,0 +1,33 @@ +# Generated by Django 3.2.11 on 2022-01-19 20:18 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Note', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=250, unique=True)), + ('slug', models.SlugField(max_length=250, unique=True)), + ('content', models.TextField()), + ('status', models.IntegerField(choices=[(0, 'Draft'), (1, 'Published')], default=0)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notes_posted', to=settings.AUTH_USER_MODEL)), + ], + options={ + 'ordering': ['-created_at'], + }, + ), + ] diff --git a/notes/migrations/__init__.py b/notes/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/notes/models.py b/notes/models.py new file mode 100644 index 0000000..3584c26 --- /dev/null +++ b/notes/models.py @@ -0,0 +1,29 @@ +from django.db import models +from django.contrib.auth.models import User +from django.urls import reverse + +# Create your models here. + +NOTE_STATUS = ((0, "Draft"), + (1, "Published")) + +class Note(models.Model): + title = models.CharField(max_length=250, unique=True) + slug = models.SlugField(max_length=250, unique=True) + author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='notes_posted') + content = models.TextField() + status = models.IntegerField(choices=NOTE_STATUS, default=0) + + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + class Meta: + ordering = ['-created_at'] + + def __str__(self): + return self.title + + def get_absolute_url(self): + return reverse("notes:note_detail", kwargs={"note_slug": self.slug}) + #return "/notes/%s" % (self.slug) + diff --git a/notes/templates/note_detail.html b/notes/templates/note_detail.html new file mode 100644 index 0000000..0862956 --- /dev/null +++ b/notes/templates/note_detail.html @@ -0,0 +1,19 @@ +{% load markdownify %} + + + + + {{ note.title }} + + + + + +

{{ note.title }}

+ +
{{ note.content|linebreaksbr|markdownify }}
+ +

Date: {{ note.created_at|date:"Y-m-d H:i" }}

+ + + diff --git a/notes/templates/note_list.html b/notes/templates/note_list.html new file mode 100644 index 0000000..fb2da33 --- /dev/null +++ b/notes/templates/note_list.html @@ -0,0 +1,26 @@ +{% load markdownify %} + + + + + Notes + + + + + +

Notes

+ {% for note in notes %} +

{{ note.title }}

+

ID: {{ note.id }}

+
{{ note.content|linebreaksbr|markdownify }}
+

Link: View

+

Date: {{ note.created_at|date:"Y-m-d H:i" }}

+ {% empty %} +

No notes

+ {% endfor %} + +

Number of notes: {{ notes_count }}

+ + + diff --git a/notes/tests.py b/notes/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/notes/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/notes/urls.py b/notes/urls.py new file mode 100644 index 0000000..503c5b4 --- /dev/null +++ b/notes/urls.py @@ -0,0 +1,9 @@ +from django.urls import path +from . import views + +app_name = 'notes' + +urlpatterns=[ + path('', views.note_list, name="note_list"), + path('notes/', views.note_detail, name="note_detail"), +] \ No newline at end of file diff --git a/notes/views.py b/notes/views.py new file mode 100644 index 0000000..4ebc94b --- /dev/null +++ b/notes/views.py @@ -0,0 +1,13 @@ +from django.shortcuts import render, get_object_or_404 +from .models import Note + +# Create your views here. + +def note_list(request): + notes = Note.objects.filter(status=1) + notes_count = Note.objects.filter(status=1).count() + return render(request, 'note_list.html', {'notes': notes, 'notes_count': notes_count}) + +def note_detail(request, note_slug): + note = get_object_or_404(Note, slug=note_slug, status=1) + return render(request, 'note_detail.html', {'note': note}) \ No newline at end of file diff --git a/pcdenotes/__init__.py b/pcdenotes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pcdenotes/asgi.py b/pcdenotes/asgi.py new file mode 100644 index 0000000..f86bd98 --- /dev/null +++ b/pcdenotes/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for pcdenotes project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pcdenotes.settings') + +application = get_asgi_application() diff --git a/pcdenotes/settings.py b/pcdenotes/settings.py new file mode 100644 index 0000000..7c51c45 --- /dev/null +++ b/pcdenotes/settings.py @@ -0,0 +1,134 @@ +""" +Django settings for pcdenotes project. + +Generated by 'django-admin startproject' using Django 3.2.11. + +For more information on this file, see +https://docs.djangoproject.com/en/3.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.2/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-(bf$#80p=*wkj(yh$)3oec0nn#46k$8(m#hx4yx8d02kcog3$q' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'markdownify.apps.MarkdownifyConfig', + 'notes', +] + +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', +] + +ROOT_URLCONF = 'pcdenotes.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', + ], + }, + }, +] + +WSGI_APPLICATION = 'pcdenotes.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.2/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.2/howto/static-files/ + +STATIC_URL = '/static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +MARKDOWNIFY = { + "default": { + #"STRIP": False, + "BLEACH": False + } +} \ No newline at end of file diff --git a/pcdenotes/urls.py b/pcdenotes/urls.py new file mode 100644 index 0000000..4832ebe --- /dev/null +++ b/pcdenotes/urls.py @@ -0,0 +1,22 @@ +"""pcdenotes URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('notes.urls')), +] diff --git a/pcdenotes/wsgi.py b/pcdenotes/wsgi.py new file mode 100644 index 0000000..923fb73 --- /dev/null +++ b/pcdenotes/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for pcdenotes project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pcdenotes.settings') + +application = get_wsgi_application() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..cdc394b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,15 @@ +asgiref==3.4.1 +bleach==4.1.0 +Django==3.2.11 +django-markdownify==0.9.0 +importlib-metadata==4.8.3 +Markdown==3.3.6 +packaging==21.3 +pkg-resources==0.0.0 +pyparsing==3.0.6 +pytz==2021.3 +six==1.16.0 +sqlparse==0.4.2 +typing-extensions==4.0.1 +webencodings==0.5.1 +zipp==3.6.0