Author SHA1 Message Date
zeitlersa64582 6a55fa7394 Solution of 08 2018-12-17 22:27:24 +01:00
zeitlersa64582 5d99369516 Solution of 07 2018-12-12 21:13:07 +01:00
zeitlersa64582 b4ac907a6a Solution of 06 2018-12-12 20:14:16 +01:00
zeitlersa64582 fccc0f39d6 Solution of 05 2018-11-23 12:38:10 +01:00
zeitlersa64582 1d65d9c9a3 Solution of 04 2018-11-23 12:21:19 +01:00
15 changed files with 337 additions and 10 deletions
+93
View File
@@ -0,0 +1,93 @@
### OSX ###
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear on external disk
.Spotlight-V100
.Trashes
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml
# Translations
*.mo
*.pot
# Sphinx documentation
docs/_build/
# PyBuilder
target/
### Django ###
*.log
*.pot
*.pyc
**__pycache__/
local_settings.py
db.sqlite3
.env
**/db.sqlite3
# PyCharm
**/.idea/
+4 -1
View File
@@ -37,6 +37,9 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'posts.apps.PostsConfig', #you could also add 'posts' to register everything in your app
'rest_framework'
]
MIDDLEWARE = [
@@ -104,7 +107,7 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'de-de'
TIME_ZONE = 'UTC'
+4 -2
View File
@@ -15,9 +15,11 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import include, path
import posts
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('posts/', include('posts.urls'), name='posts')
path('', views.index),
path('posts/', include('posts.urls')),
path('accounts/', include('django.contrib.auth.urls'))
]
+5
View File
@@ -0,0 +1,5 @@
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, 'index.html')
BIN
View File
Binary file not shown.
+9
View File
@@ -0,0 +1,9 @@
from django import forms
import datetime
class NoticeForm(forms.Form):
date_formats = ['%d.%m.%Y', '%d.%m.%y']
title = forms.CharField(label='Titel', max_length=80)
text = forms.CharField(label='Text', max_length=400)
start = forms.DateField(label='Von', input_formats=date_formats, initial=datetime.date.today)
end = forms.DateField(label='Bis', input_formats=date_formats, initial=datetime.date.today)
+24
View File
@@ -0,0 +1,24 @@
# Generated by Django 2.1.3 on 2018-11-23 11:25
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Notice',
fields=[
('id', models.AutoField(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()),
],
),
]
+5 -1
View File
@@ -1,3 +1,7 @@
from django.db import models
# Create your models here.
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()
+7
View File
@@ -0,0 +1,7 @@
from rest_framework import serializers
from .models import Notice
class NoticeSerializer(serializers.ModelSerializer):
class Meta:
model = Notice
fields = ('id', 'notice_title', 'notice_text', 'pub_start', 'pub_end')
+5 -2
View File
@@ -3,6 +3,9 @@ from django.urls import path, re_path
urlpatterns = [
# use re_path for regex (.*) - this regex accepts anything - see https://docs.python.org/3/library/re.html for more
path('', views.home, name=''),
re_path('(?P<value>.*)/$', views.home, name='textwiedergabe')
path('', views.home, name='index'),
path('delete/<int:value>', views.delete, name='delete'),
path('new', views.new, name='new'),
path('notices/', views.notice_list, name='index'),
path('notices/<int:id>', views.notice_detail, name='delete'),
]
+86 -4
View File
@@ -1,6 +1,88 @@
from django.http import HttpResponse
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render, redirect
from datetime import timedelta
from django.utils import timezone
from rest_framework.parsers import JSONParser
from django.views.decorators.csrf import csrf_exempt
from posts.models import Notice
from posts.forms import NoticeForm
from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
from posts.serializers import NoticeSerializer
# Create your views here.
# if no value is given use the default ''
def home(request, value=''):
return HttpResponse(value)
#Titel und die Texte aller Meldungen, deren Veröffentlichungsdatum vor und der Endedatum nach dem aktuellen Datum liegt.
def home(request, value=""):
now = timezone.now()
notices = Notice.objects.all()
display_notices = list()
for notice in notices:
if notice.pub_start < now and notice.pub_end > now:
display_notices.append(notice)
context = {
"title": "Beboop",
"notices": display_notices
}
return render(request, 'index.html', context)
@login_required
def new(request):
if request.method == "POST":
form = NoticeForm(request.POST)
if form.is_valid():
newNotice = Notice(notice_title=form.cleaned_data['title'],
notice_text=form.cleaned_data['text'],
pub_start=form.cleaned_data['start'],
pub_end=form.cleaned_data['end'])
newNotice.save()
return redirect('index')
context = {'form': NoticeForm()}
return render(request, 'edit.html', context)
@staff_member_required
def delete(request, value):
notice = Notice.objects.get(id = value)
notice.delete()
return redirect('index')
@csrf_exempt
def notice_list(request):
if request.method == "GET":
notices = Notice.objects.all()
serializer = NoticeSerializer(notices, many=True)
return JsonResponse(serializer.data, safe=False)
elif request.method == "POST":
data = JSONParser().parse(request)
serializer = NoticeSerializer(data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
return HttpResponse(status=405)
@csrf_exempt
def notice_detail(request, id):
try:
notice = Notice.objects.get(id=id)
except:
return HttpResponse(status=404)
if request.method == 'GET':
serializer = NoticeSerializer(notice)
return JsonResponse(serializer.data)
elif request.method == 'PUT':
data = JSONParser().parse(request)
serializer = NoticeSerializer(notice, data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data)
return JsonResponse(serializer.errors, status=400)
elif request.method == 'DELETE':
notice.delete()
return HttpResponse(status=204)
return HttpResponse(status=405)
+53
View File
@@ -0,0 +1,53 @@
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>{% block title %} {% endblock %}</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand">MEIM</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<li class="nav-item active">
<a class="nav-link" href="/posts">Posts</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://www.th-nuernberg.de/fakultaeten/efi/">Efi</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://www.google.com/">Google</a>
</li>
</ul>
<ul class="navbar-nav">
{% if request.user.is_authenticated %}
<span class="navbar-text"> Willkommen {{ request.user.username }}! </span>
<li class="nav-item">
<a class="nav-link" href="{% url 'logout' %}?next=/posts">Abmelden</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{% url 'login' %}?next=/posts">Anmelden</a>
</li>
{% endif %}
</ul>
</div>
</nav>
{% block content %} {% endblock %}
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>
+12
View File
@@ -0,0 +1,12 @@
{% extends "base.html" %}
{% block title %} {{ title }} {% endblock title %}
{% block content %}
<h1>Neue Nachricht</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Speichern</button>
</form>
{% endblock content %}
+18
View File
@@ -0,0 +1,18 @@
{% extends "base.html" %}
{% block title %} {{ title }} {% endblock title %}
{% block content %}
{% for notice in notices %}
<div class="jumbotron">
<h1 class="display-6">{{ notice.notice_title }}</h1>
<p class="lead">{{notice.notice_text}}</p>
{% if user.is_staff %}
<p><a href="{% url 'delete' notice.id %}" class="btn btn-info" role="button">Nachricht löschen</a></p>
{% endif %}
</div>
{% endfor %}
{% if user.is_authenticated %}
<p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p>
{% endif %}
{% endblock content %}
+12
View File
@@ -0,0 +1,12 @@
{% extends 'base.html' %}
{% block title %} Login {% endblock %}
{% block content %}
<div class="container"><form method="post" action="{% url 'login' %}">
{% csrf_token %}
Benutzername: {{ form.username }}
<br>Passwort: {{ form.password }} <br>
<button type="submit" class="btn btn-default">Anmelden</button>
<input type="hidden" name="next" value="{{ next }}" />
</form>
</div>
{% endblock %}