From b5eb32c8435851558a226503c77346c9fbc175a4 Mon Sep 17 00:00:00 2001 From: Esther Kleinhenz Date: Thu, 28 Jun 2018 12:33:19 +0200 Subject: [PATCH] added medinf login and fixed navlogin issues --- application/forms.py | 9 ++ application/migrations/0001_initial.py | 29 ++++++ application/static/css/application.css | 46 ++++++++++ application/templates/base.html | 85 +++++++++++++++++ application/templates/index.html | 23 +++++ application/templates/post_detail.html | 19 ++++ application/templates/post_draft_list.html | 11 +++ application/templates/post_edit.html | 9 ++ application/templates/post_list.html | 11 +++ application/templates/registration/login.html | 27 ++++++ application/urls.py | 12 +++ application/views.py | 91 ++++++++++++++++++- mysite/urls.py | 13 ++- 13 files changed, 382 insertions(+), 3 deletions(-) create mode 100644 application/forms.py create mode 100644 application/migrations/0001_initial.py create mode 100644 application/static/css/application.css create mode 100644 application/templates/base.html create mode 100644 application/templates/index.html create mode 100644 application/templates/post_detail.html create mode 100644 application/templates/post_draft_list.html create mode 100644 application/templates/post_edit.html create mode 100644 application/templates/post_list.html create mode 100644 application/templates/registration/login.html create mode 100644 application/urls.py diff --git a/application/forms.py b/application/forms.py new file mode 100644 index 0000000..8cc1754 --- /dev/null +++ b/application/forms.py @@ -0,0 +1,9 @@ +from django import forms + +from .models import Post + +class PostForm(forms.ModelForm): + + class Meta: + model = Post + fields = ('title', 'text') \ No newline at end of file diff --git a/application/migrations/0001_initial.py b/application/migrations/0001_initial.py new file mode 100644 index 0000000..1f762db --- /dev/null +++ b/application/migrations/0001_initial.py @@ -0,0 +1,29 @@ +# Generated by Django 2.0.6 on 2018-06-28 09:13 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Post', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=200)), + ('text', models.TextField()), + ('created_date', models.DateTimeField(default=django.utils.timezone.now)), + ('published_date', models.DateTimeField(blank=True, null=True)), + ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/application/static/css/application.css b/application/static/css/application.css new file mode 100644 index 0000000..e948e71 --- /dev/null +++ b/application/static/css/application.css @@ -0,0 +1,46 @@ +#navbar-efi { + background-color: #ffe240; + margin-bottom: 20px; +} + +/* Login Dropdown */ + +#login-dp{ + min-width: 250px; + padding: 14px 14px 0; + overflow:hidden; + background-color:rgba(255,255,255,.8); +} +#login-dp .bottom{ + background-color:rgba(255,255,255,.8); + border-top:1px solid #ddd; + clear:both; + padding:14px; +} +#login-dp .form-group { + margin-bottom: 10px; +} + +#login-button { + text-align: right; + min-width: 250px; +} + + +#login-button:focus { + border-color: rgba(0, 0, 0, 0.8); + box-shadow: 0 10px 10px rgba(0, 0, 0, 0.075) inset, 0 0 8px rgba(126, 239, 104, 0.6); + outline: 0 none; +} + + +@media(max-width:768px){ + #login-dp{ + background-color: inherit; + color: #fff; + } + #login-dp .bottom{ + background-color: inherit; + border-top:0 none; + } +} \ No newline at end of file diff --git a/application/templates/base.html b/application/templates/base.html new file mode 100644 index 0000000..90d5f06 --- /dev/null +++ b/application/templates/base.html @@ -0,0 +1,85 @@ + + + {% load static %} + + + + {% block title %}Seitenname{% endblock %} + + + + + + + + + + {% block content %} {% endblock %} + + + + + \ No newline at end of file diff --git a/application/templates/index.html b/application/templates/index.html new file mode 100644 index 0000000..ca0d197 --- /dev/null +++ b/application/templates/index.html @@ -0,0 +1,23 @@ +{% extends "base.html" %} + +{% block content %} + +
+
+

efi Medizintechnik Informationssystem

+ {% if user.is_authenticated %} +

Herzlich Willkommen!

+ {% else %} +

Bitte melden Sie sich mit Ihrer Domänenkennung an.

+ {% endif %} +
+ + {% if error %} +
+ {{ error }} +
+ {% endif %} + +
+ +{% endblock %} \ No newline at end of file diff --git a/application/templates/post_detail.html b/application/templates/post_detail.html new file mode 100644 index 0000000..7accf31 --- /dev/null +++ b/application/templates/post_detail.html @@ -0,0 +1,19 @@ +{% extends 'base.html' %} {% block content %} +
+ {% if post.published_date %} +
+ {{ post.published_date }} +
+ {% else %} + Publish + {% endif %} + + + + + + +

{{ post.title }}

+

{{ post.text|linebreaksbr }}

+
+{% endblock %} \ No newline at end of file diff --git a/application/templates/post_draft_list.html b/application/templates/post_draft_list.html new file mode 100644 index 0000000..61613ee --- /dev/null +++ b/application/templates/post_draft_list.html @@ -0,0 +1,11 @@ +{% extends 'base.html' %} + +{% block content %} + {% for post in posts %} +
+

created: {{ post.created_date|date:'d-m-Y' }}

+

{{ post.title }}

+

{{ post.text|truncatechars:200 }}

+
+ {% endfor %} +{% endblock %} \ No newline at end of file diff --git a/application/templates/post_edit.html b/application/templates/post_edit.html new file mode 100644 index 0000000..94ba17d --- /dev/null +++ b/application/templates/post_edit.html @@ -0,0 +1,9 @@ +{% extends 'base.html' %} + +{% block content %} +

New post

+
{% csrf_token %} + {{ form.as_p }} + +
+{% endblock %} \ No newline at end of file diff --git a/application/templates/post_list.html b/application/templates/post_list.html new file mode 100644 index 0000000..1693e88 --- /dev/null +++ b/application/templates/post_list.html @@ -0,0 +1,11 @@ +{% extends 'base.html' %} {% block content %} {% for post in posts %} +
+
+ {{ post.published_date }} +
+

+ {{ post.title }} +

+

{{ post.text|linebreaks }}

+
+{% endfor %} {% endblock %} \ No newline at end of file diff --git a/application/templates/registration/login.html b/application/templates/registration/login.html new file mode 100644 index 0000000..7c5e4e0 --- /dev/null +++ b/application/templates/registration/login.html @@ -0,0 +1,27 @@ +{% extends "base.html" %} {% block content %} {% if form.errors %} +

Your username and password didn't match. Please try again.

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

Your account doesn't have access to this page. To proceed, please login with an account that has access.

+{% else %} +

Please login to see this page.

+{% endif %} {% endif %} + +
+ {% csrf_token %} + +
+ {{ form.username.label_tag }} + {{ form.username }} +
+
+ {{ form.password.label_tag }} + {{ form.password }} +
+ +
+ + +
+
+ +{% endblock %} \ No newline at end of file diff --git a/application/urls.py b/application/urls.py new file mode 100644 index 0000000..baedc80 --- /dev/null +++ b/application/urls.py @@ -0,0 +1,12 @@ +from django.conf.urls import url +from . import views + +urlpatterns = [ + url(r'^$', views.post_list, name='post_list'), + url(r'^post/(?P\d+)/$', views.post_detail, name='post_detail'), + url(r'^post/new/$', views.post_new, name='post_new'), + url(r'^post/(?P\d+)/edit/$', views.post_edit, name='post_edit'), + url(r'^drafts/$', views.post_draft_list, name='post_draft_list'), + url(r'^post/(?P\d+)/publish/$', views.post_publish, name='post_publish'), + url(r'^post/(?P\d+)/remove/$', views.post_remove, name='post_remove'), +] \ No newline at end of file diff --git a/application/views.py b/application/views.py index 91ea44a..7c28bf0 100644 --- a/application/views.py +++ b/application/views.py @@ -1,3 +1,92 @@ -from django.shortcuts import render +from django.shortcuts import render, get_object_or_404 +from django.utils import timezone +from .models import Post +from .forms import PostForm +from django.shortcuts import redirect +from django.contrib.auth.decorators import login_required +from django.contrib.auth import authenticate, login, logout +import logging +import mysite.settings + # Create your views here. + + +def navlogin(request): + + log = logging.getLogger('medinf') + logout(request) + error = "" + if request.POST: + username = request.POST.get("username", "?") + password = request.POST.get("password", "?") + + user = authenticate(username=username, password=password) + if user is not None: + if user.is_active: + login(request, user) + return redirect(mysite.settings.LOGIN_REDIRECT_URL) + else: + log.info("Inactive user {} tried to login".format(username)) + error = "Ihre Benutzerkennung wurde deaktiviert." + else: + log.info("Login failed for {}".format(username)) + error = "Benutzername oder Kennwort falsch." + context = {'error': error} + return render(request, 'index.html', context) + + +def post_list(request): + posts = Post.objects.filter( + published_date__lte=timezone.now()).order_by('published_date') + return render(request, 'post_list.html', {'posts': posts}) + + +def post_detail(request, pk): + post = get_object_or_404(Post, pk=pk) + return render(request, 'post_detail.html', {'post': post}) + +@login_required +def post_new(request): + if request.method == "POST": + form = PostForm(request.POST) + if form.is_valid(): + post = form.save(commit=False) + post.author = request.user + post.save() + return redirect('post_detail', pk=post.pk) + else: + form = PostForm() + return render(request, 'post_edit.html', {'form': form}) + +@login_required +def post_edit(request, pk): + post = get_object_or_404(Post, pk=pk) + if request.method == "POST": + form = PostForm(request.POST, instance=post) + if form.is_valid(): + post = form.save(commit=False) + post.author = request.user + post.save() + return redirect('post_detail', pk=post.pk) + else: + form = PostForm(instance=post) + return render(request, 'post_edit.html', {'form': form}) + +@login_required +def post_draft_list(request): + posts = Post.objects.filter( + published_date__isnull=True).order_by('created_date') + return render(request, 'post_draft_list.html', {'posts': posts}) + +@login_required +def post_publish(request, pk): + post = get_object_or_404(Post, pk=pk) + post.publish() + return redirect('post_detail', pk=pk) + +@login_required +def post_remove(request, pk): + post = get_object_or_404(Post, pk=pk) + post.delete() + return redirect('post_list') diff --git a/mysite/urls.py b/mysite/urls.py index a86e4a5..300c7ff 100644 --- a/mysite/urls.py +++ b/mysite/urls.py @@ -14,8 +14,17 @@ 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.conf.urls import include, url + +from django.contrib.auth import views +import application.views + urlpatterns = [ - path('admin/', admin.site.urls), + url(r'^admin/', admin.site.urls), + url(r'^navlogin/', application.views.navlogin, name='navlogin'), + url(r'^accounts/login/$', views.login, name='login'), + url(r'^accounts/logout/$', views.logout, + name='logout', kwargs={'next_page': '/'}), + url(r'', include('application.urls')), ]