|
1 |
| -from django.shortcuts import render |
| 1 | +from django.shortcuts import render, redirect |
| 2 | +from django.contrib.auth.models import User |
| 3 | +from django.contrib.auth.decorators import login_required |
| 4 | +from django.contrib.auth import authenticate, login, logout |
2 | 5 | from missionboard.forms import RegisterForm, SigninForm
|
3 | 6 |
|
4 |
| -import random |
5 |
| -# from collections import namedtuple |
6 |
| -# nt_category = namedtuple('Category', 'name, description') |
7 |
| - |
8 | 7 |
|
9 | 8 | def signin(request):
|
| 9 | + if request.user.is_authenticated: |
| 10 | + return redirect('missionboard_index') |
| 11 | + |
10 | 12 | if request.method == 'GET':
|
11 | 13 | form = SigninForm()
|
12 | 14 | context = {'form': form}
|
13 | 15 | return render(request, 'signin.html', context)
|
14 | 16 | elif request.method == 'POST':
|
15 |
| - return render(request, 'signin.html') |
| 17 | + form = SigninForm(request.POST) |
| 18 | + if form.is_valid(): |
| 19 | + username = form.cleaned_data['name'] |
| 20 | + password = form.cleaned_data['password'] |
| 21 | + u = authenticate(request, username=username, password=password) |
| 22 | + login(request, u) |
| 23 | + return redirect('missionboard_index') |
| 24 | + else: |
| 25 | + return render(request, 'signin.html', {'form': form}) |
| 26 | + |
| 27 | +@login_required |
| 28 | +def signout(request): |
| 29 | + logout(request) |
| 30 | + return redirect('missionboard_index') |
16 | 31 |
|
17 | 32 |
|
18 | 33 | def register(request):
|
19 | 34 | if request.method == 'GET':
|
20 | 35 | form = RegisterForm()
|
21 |
| - context = {'form': form} |
22 |
| - |
23 |
| - return render(request, 'register.html', context) |
| 36 | + return render(request, 'register.html', {'form': form}) |
24 | 37 |
|
25 | 38 | elif request.method == 'POST':
|
26 | 39 | form = RegisterForm(request.POST)
|
27 | 40 | if form.is_valid():
|
28 |
| - return render(request, 'register.html') |
| 41 | + username = form.cleaned_data['name'] |
| 42 | + email = form.cleaned_data['email'] |
| 43 | + password = form.cleaned_data['password'] |
| 44 | + password2 = form.cleaned_data['password2'] |
| 45 | + User.objects.create_user(username, email, password) |
| 46 | + return redirect('signin.html') |
29 | 47 | else:
|
30 | 48 | return render(request, 'register.html', {'form': form})
|
0 commit comments