Initial plant reference and basic login
This commit is contained in:
parent
a2dba58fe4
commit
cada3c7357
31 changed files with 637 additions and 0 deletions
0
planner/__init__.py
Normal file
0
planner/__init__.py
Normal file
3
planner/admin.py
Normal file
3
planner/admin.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
planner/apps.py
Normal file
6
planner/apps.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class PlannerConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'planner'
|
||||
14
planner/forms.py
Normal file
14
planner/forms.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from django import forms
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth.forms import UserCreationForm
|
||||
|
||||
|
||||
class LoginForm(forms.Form):
|
||||
username = forms.CharField(max_length=65)
|
||||
password = forms.CharField(max_length=128, widget=forms.PasswordInput)
|
||||
|
||||
|
||||
class RegisterForm(UserCreationForm):
|
||||
class Meta:
|
||||
model=User
|
||||
fields = ['username','email','password1','password2']
|
||||
0
planner/migrations/__init__.py
Normal file
0
planner/migrations/__init__.py
Normal file
3
planner/models.py
Normal file
3
planner/models.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
13
planner/templates/planner/index.html
Normal file
13
planner/templates/planner/index.html
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
{% extends 'base.html' %}
|
||||
|
||||
{% block head_title %}Verdure: My Garden Plans{% endblock head_title %}
|
||||
{%block head_content%}<h1>Garden Planner</h1>{%endblock head_content%}
|
||||
|
||||
|
||||
|
||||
{% block content %}
|
||||
|
||||
<p>Green Planner</p>
|
||||
<p>TODO</p>
|
||||
|
||||
{% endblock content %}
|
||||
17
planner/templates/planner/login.html
Normal file
17
planner/templates/planner/login.html
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
{% extends 'base.html' %}
|
||||
|
||||
{% block head_title %}Verdure: Login{% endblock head_title %}
|
||||
{%block head_content%}<h1>Login</h1>{%endblock head_content%}
|
||||
|
||||
|
||||
|
||||
{% block content %}
|
||||
<form method="POST" novalidate>
|
||||
{% csrf_token %}
|
||||
<h2>Login</h2>
|
||||
{{form.as_p}}
|
||||
<input type="submit" value="Login" />
|
||||
</form>
|
||||
|
||||
{% endblock content%}
|
||||
|
||||
16
planner/templates/planner/register.html
Normal file
16
planner/templates/planner/register.html
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
{% extends 'base.html' %}
|
||||
|
||||
{% block head_title %}Verdure: Sign Up{% endblock head_title %}
|
||||
{%block head_content%}<h1>Sign Up</h1>{%endblock head_content%}
|
||||
|
||||
|
||||
|
||||
{% block content %}
|
||||
|
||||
<form method="POST" novalidate>
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<input type="submit" value="Register" />
|
||||
</form>
|
||||
|
||||
{% endblock content%}
|
||||
3
planner/tests.py
Normal file
3
planner/tests.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
12
planner/urls.py
Normal file
12
planner/urls.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
app_name = "planner"
|
||||
urlpatterns = [
|
||||
path("", views.index, name="index"),
|
||||
path("login/", views.sign_in, name="login"),
|
||||
path("logout/", views.sign_out, name="logout"),
|
||||
path("register/", views.sign_up, name="register"),
|
||||
]
|
||||
|
||||
72
planner/views.py
Normal file
72
planner/views.py
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
from django.shortcuts import render, redirect
|
||||
from django.http import HttpResponse, Http404
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth import login, authenticate, logout
|
||||
from .forms import LoginForm, RegisterForm
|
||||
import logging
|
||||
|
||||
def index(request):
|
||||
# from django.shortcuts import render
|
||||
# context = { "latest_plant_list": latest_plant_list }
|
||||
# return render(request, "planterteque/index.html", context)
|
||||
#template = loader.get_template("planner/index.html")
|
||||
#context = { }
|
||||
#return HttpResponse( template.render( context, request ) )
|
||||
return render(request, 'planner/index.html')
|
||||
|
||||
## Form action to log in
|
||||
def sign_in(request):
|
||||
if request.user.is_authenticated and request.method == 'GET':
|
||||
return redirect('planner:index')
|
||||
elif request.method == 'GET':
|
||||
form = LoginForm()
|
||||
return render(request, 'planner/login.html', {'form': form})
|
||||
elif request.method == 'POST':
|
||||
form = LoginForm(request.POST)
|
||||
|
||||
if form.is_valid():
|
||||
username = form.cleaned_data['username']
|
||||
password = form.cleaned_data['password']
|
||||
user = authenticate(request,username=username,password=password)
|
||||
if user:
|
||||
logging.warning('User %s is valid', username.title())
|
||||
login(request, user)
|
||||
messages.success(request,f'Hi {username.title()}, welcome back!')
|
||||
return redirect('planner:index')
|
||||
#return index(request)
|
||||
else:
|
||||
logging.warning('User login failed')
|
||||
else:
|
||||
logging.warning('User form invalid')
|
||||
|
||||
# form is not valid or user is not authenticated
|
||||
messages.error(request,f'Invalid username or password')
|
||||
return render(request,'planner/login.html',{'form': form})
|
||||
return redirect('planner:index')
|
||||
|
||||
## Form action to log out
|
||||
def sign_out(request):
|
||||
if (request.user.is_authenticated):
|
||||
logout(request)
|
||||
messages.success(request,f'You have been logged out.')
|
||||
return render(request, 'planner/index.html', {})
|
||||
return redirect('planner:index')
|
||||
|
||||
def sign_up(request):
|
||||
if request.method == 'GET':
|
||||
form = RegisterForm()
|
||||
return render(request, 'planner/register.html', { 'form': form})
|
||||
if request.method == 'POST':
|
||||
form = RegisterForm(request.POST)
|
||||
if form.is_valid():
|
||||
user = form.save(commit=False)
|
||||
user.username = user.username.lower()
|
||||
user.save()
|
||||
messages.success(request, 'You have signed up successfully.')
|
||||
login(request, user)
|
||||
return redirect('planner:index')
|
||||
else:
|
||||
return render(request, 'planner/register.html', {'form': form})
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue