login: make login required and add a login html

This commit is contained in:
Gesche 2021-05-04 23:13:35 +02:00
parent cc93abc752
commit 1527f07fa3
5 changed files with 49 additions and 8 deletions

View File

@ -119,3 +119,5 @@ USE_TZ = True
# https://docs.djangoproject.com/en/3.1/howto/static-files/ # https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/static/'
LOGIN_URL = '/accounts/login/'

View File

@ -18,5 +18,6 @@ from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('writing/', include('writingtogether.urls')), path('writing/', include('writingtogether.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]

View File

@ -0,0 +1,36 @@
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login">
<input type="hidden" name="next" value="{{ next }}">
</form>
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
{% endblock %}

View File

@ -9,8 +9,9 @@ User = get_user_model()
class TestViews(TestCase): class TestViews(TestCase):
def setUp(self) -> None: def setUp(self) -> None:
self.user1 = User.objects.create(username='player1') self.user1 = User.objects.create_user(username='player1', password='12345')
self.user2 = User.objects.create(username='player2') self.user2 = User.objects.create_user(username='player2')
self.client.login(username='player1', password='12345')
def test_create_story_round_two_rounds(self): def test_create_story_round_two_rounds(self):

View File

@ -1,4 +1,5 @@
from django import forms from django import forms
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
@ -10,7 +11,7 @@ from django.views.generic import CreateView, UpdateView, RedirectView
from writingtogether.models import Story, StoryPart, StoryRound from writingtogether.models import Story, StoryPart, StoryRound
class IndexView(generic.ListView): class IndexView(LoginRequiredMixin, generic.ListView):
template_name = 'writingtogether/index.html' template_name = 'writingtogether/index.html'
context_object_name = 'open_story_round_list' context_object_name = 'open_story_round_list'
@ -19,12 +20,12 @@ class IndexView(generic.ListView):
return StoryRound.objects.order_by('-created')[:5] return StoryRound.objects.order_by('-created')[:5]
class DetailView(generic.DetailView): class DetailView(LoginRequiredMixin, generic.DetailView):
model = StoryRound model = StoryRound
template_name = 'writingtogether/detail.html' template_name = 'writingtogether/detail.html'
class StoryRoundCreate(CreateView): class StoryRoundCreate(LoginRequiredMixin, CreateView):
model = StoryRound model = StoryRound
fields = ['name', 'participants', 'number_of_rounds'] fields = ['name', 'participants', 'number_of_rounds']
success_url = reverse_lazy('writing:index') success_url = reverse_lazy('writing:index')
@ -53,12 +54,12 @@ class StoryRoundCreate(CreateView):
return HttpResponseRedirect(self.get_success_url()) return HttpResponseRedirect(self.get_success_url())
class StoryUpdate(UpdateView): class StoryUpdate(LoginRequiredMixin, UpdateView):
model = Story model = Story
fields = ['name'] fields = ['name']
class StoryPartUpdate(UpdateView): class StoryPartUpdate(LoginRequiredMixin, UpdateView):
model = StoryPart model = StoryPart
fields = ['text'] fields = ['text']
template_name = 'writingtogether/story_part.html' template_name = 'writingtogether/story_part.html'
@ -70,7 +71,7 @@ class StoryPartUpdate(UpdateView):
# return super().form_valid(form) # return super().form_valid(form)
class RedirectToNextOpenPart(RedirectView): class RedirectToNextOpenPart(LoginRequiredMixin, RedirectView):
permanent = False permanent = False
query_string = True query_string = True
pattern_name = 'writing:update_story_part' pattern_name = 'writing:update_story_part'