From 2d7af0e65f9220cd41bdd7ae54202c374fd90279 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 4 Dec 2023 13:09:31 +0100 Subject: [PATCH] Init --- .gitignore | 4 + manage.py | 22 ++++++ mysite/__init__.py | 0 mysite/asgi.py | 16 ++++ mysite/settings.py | 124 ++++++++++++++++++++++++++++++ mysite/urls.py | 27 +++++++ mysite/wsgi.py | 16 ++++ polls/__init__.py | 0 polls/admin.py | 5 ++ polls/apps.py | 6 ++ polls/forms.py | 14 ++++ polls/migrations/0001_initial.py | 26 +++++++ polls/migrations/__init__.py | 0 polls/models.py | 9 +++ polls/tests.py | 3 + polls/urls.py | 10 +++ polls/views.py | 41 ++++++++++ requirements.txt | 1 + templates/base.html | 68 ++++++++++++++++ templates/polls/edit.html | 18 +++++ templates/polls/index.html | 38 +++++++++ templates/registration/login.html | 40 ++++++++++ 22 files changed, 488 insertions(+) create mode 100644 .gitignore create mode 100755 manage.py create mode 100644 mysite/__init__.py create mode 100644 mysite/asgi.py create mode 100644 mysite/settings.py create mode 100644 mysite/urls.py create mode 100644 mysite/wsgi.py create mode 100644 polls/__init__.py create mode 100644 polls/admin.py create mode 100644 polls/apps.py create mode 100644 polls/forms.py create mode 100644 polls/migrations/0001_initial.py create mode 100644 polls/migrations/__init__.py create mode 100644 polls/models.py create mode 100644 polls/tests.py create mode 100644 polls/urls.py create mode 100644 polls/views.py create mode 100644 requirements.txt create mode 100644 templates/base.html create mode 100644 templates/polls/edit.html create mode 100644 templates/polls/index.html create mode 100644 templates/registration/login.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0b57a4c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/venv/ +/db.sqlite3 +/playground.py +/playground2.py diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..a7da667 --- /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', 'mysite.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/mysite/__init__.py b/mysite/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mysite/asgi.py b/mysite/asgi.py new file mode 100644 index 0000000..cce50dc --- /dev/null +++ b/mysite/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for mysite 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/4.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + +application = get_asgi_application() diff --git a/mysite/settings.py b/mysite/settings.py new file mode 100644 index 0000000..427c2dd --- /dev/null +++ b/mysite/settings.py @@ -0,0 +1,124 @@ +""" +Django settings for mysite project. + +Generated by 'django-admin startproject' using Django 4.2.6. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.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/4.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-p0yr76f^19@hi$n)u3)w1lk35#ijlq8(@ulv#*0o8^x32b^1h&' + +# 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', + 'polls.apps.PollsConfig', +] + +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 = 'mysite.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [BASE_DIR / 'templates'] + , + '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 = 'mysite.wsgi.application' + +# Database +# https://docs.djangoproject.com/en/4.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + +# Password validation +# https://docs.djangoproject.com/en/4.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/4.2/topics/i18n/ + +LANGUAGE_CODE = 'de-de' + +TIME_ZONE = 'Europe/Berlin' + +USE_I18N = True + +USE_TZ = True + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.2/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +LOGIN_REDIRECT_URL = '/polls/' +LOGOUT_REDIRECT_URL = '/polls/' + + diff --git a/mysite/urls.py b/mysite/urls.py new file mode 100644 index 0000000..837ac68 --- /dev/null +++ b/mysite/urls.py @@ -0,0 +1,27 @@ +""" +URL configuration for mysite project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.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('polls/', include('polls.urls')), + # ... + path('admin/', admin.site.urls), + path('accounts/', include('django.contrib.auth.urls')), +] + + diff --git a/mysite/wsgi.py b/mysite/wsgi.py new file mode 100644 index 0000000..4ce2cc5 --- /dev/null +++ b/mysite/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for mysite 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/4.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + +application = get_wsgi_application() diff --git a/polls/__init__.py b/polls/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/polls/admin.py b/polls/admin.py new file mode 100644 index 0000000..3211894 --- /dev/null +++ b/polls/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Notice + +admin.site.register(Notice) + diff --git a/polls/apps.py b/polls/apps.py new file mode 100644 index 0000000..5a5f94c --- /dev/null +++ b/polls/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class PollsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'polls' diff --git a/polls/forms.py b/polls/forms.py new file mode 100644 index 0000000..158d2e8 --- /dev/null +++ b/polls/forms.py @@ -0,0 +1,14 @@ +from django import forms +import datetime + + +class NoticeForm(forms.Form): + date_formats = ['%d.%m.%Y', '%d.%m.%y'] + notice_title = forms.CharField(label='Titel', max_length=80) + notice_text = forms.CharField(label='Text', max_length=400) + pub_start = forms.DateField(label='Von', + input_formats=date_formats, + initial=datetime.date.today) + pub_end = forms.DateField(label='Bis', + input_formats=date_formats, + initial=datetime.date.today) diff --git a/polls/migrations/0001_initial.py b/polls/migrations/0001_initial.py new file mode 100644 index 0000000..a20d7f1 --- /dev/null +++ b/polls/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# Generated by Django 4.2.6 on 2023-11-12 18:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Notice', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('notice_title', models.CharField(max_length=80)), + ('notice_text', models.CharField(max_length=400)), + ('pub_start', models.DateTimeField()), + ('pub_end', models.DateTimeField()), + ], + ), + ] + + diff --git a/polls/migrations/__init__.py b/polls/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/polls/models.py b/polls/models.py new file mode 100644 index 0000000..f1c3226 --- /dev/null +++ b/polls/models.py @@ -0,0 +1,9 @@ +from django.db import models + + +class Notice(models.Model): + notice_title = models.CharField(max_length=80) + notice_text = models.CharField(max_length=400) + pub_start = models.DateTimeField() + pub_end = models.DateTimeField() + diff --git a/polls/tests.py b/polls/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/polls/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/polls/urls.py b/polls/urls.py new file mode 100644 index 0000000..8a3876b --- /dev/null +++ b/polls/urls.py @@ -0,0 +1,10 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('', views.index, name='index'), + path('new', views.new, name='new'), + path('delete/', views.delete, name='delete'), +] + + diff --git a/polls/views.py b/polls/views.py new file mode 100644 index 0000000..bfe5b2c --- /dev/null +++ b/polls/views.py @@ -0,0 +1,41 @@ + +from django.shortcuts import render, redirect +from .models import Notice +from django.utils import timezone +from .forms import NoticeForm +from django.contrib.auth.decorators import login_required + + +@login_required +def new(request): + if request.method == 'POST': + form = NoticeForm(request.POST) + if form.is_valid(): + notice = Notice() + notice.notice_title = form.cleaned_data['notice_title'] + notice.notice_text = form.cleaned_data['notice_text'] + notice.pub_start = form.cleaned_data['pub_start'] + notice.pub_end = form.cleaned_data['pub_end'] + notice.save() + return redirect('index') + context = {'form': NoticeForm()} + return render(request, 'polls/edit.html', context) + +@login_required +def delete(request, deleteId=None): + if deleteId is not None: + try: + notice = Notice.objects.get(pk=deleteId) + if request.user.id == notice.user_id: + notice.delete() + except: + pass + return redirect('index') + +def index(request): + notices = Notice.objects.all() + notices = notices.filter(pub_start__lte=timezone.now()) + notices = notices.filter(pub_end__gte=timezone.now()) + context = {'notices': notices} + return render(request, 'polls/index.html', context) + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..41f0147 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Django>=4.2.6 diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..c85e851 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,68 @@ + + + + + + {% block title %} Platzhalter {% endblock %} + + + + + + +
+ {% block content %} Platzhalter {% endblock %} + + {% if user.is_authenticated %} + Abmelden + {% endif %} + + {% if not user.is_authenticated %} + Anmelden + {% endif %} + +
+ + + + + + + + + diff --git a/templates/polls/edit.html b/templates/polls/edit.html new file mode 100644 index 0000000..a65c4e6 --- /dev/null +++ b/templates/polls/edit.html @@ -0,0 +1,18 @@ +{% extends "base.html" %} + +{% block title %}Edit{% endblock %} + +{% block content %} +

Neue Nachricht

+
+ {% csrf_token %} + {{ form.as_p }} + +
+{% endblock %} + + + + diff --git a/templates/polls/index.html b/templates/polls/index.html new file mode 100644 index 0000000..319e140 --- /dev/null +++ b/templates/polls/index.html @@ -0,0 +1,38 @@ +{% extends "base.html" %} + +{% block title %}News{% endblock %} + +{% block content %} +
+

News

+
+ + {% for notice in notices %} +
+

{{ notice.notice_title }}

+

{{ notice.notice_text }}

+ + {% if user.is_staff %} +

+ + Meldung löschen + +

+ {% endif %} + + {% endfor %} + +
+ +

+ + Neue Meldung erstellen + +

+ + +{% endblock %} + + diff --git a/templates/registration/login.html b/templates/registration/login.html new file mode 100644 index 0000000..c977a8c --- /dev/null +++ b/templates/registration/login.html @@ -0,0 +1,40 @@ +{% extends "base.html" %} + +{% block title %}Anmeldung{% endblock %} + +{% block content %} + + {% if form.errors %} +

Benutzername oder Passwort falsch.

+ {% endif %} + + {% if next %} + {% if user.is_authenticated %} +

Sie haben nicht die hierfür notwendige Berechtigung.

+ {% else %} +

Bitte melden Sie sich an.

+ {% endif %} + {% endif %} + +
+ {% csrf_token %} + + + + + + + + + +
{{ form.username.label_tag }}{{ form.username }}
{{ form.password.label_tag }}{{ form.password }}
+ + + +
+ +

+ Passwort vergessen? +

+ +{% endblock %} \ No newline at end of file