Skip to content

Commit

Permalink
Merge pull request #23 from l0rb/master
Browse files Browse the repository at this point in the history
login/logout/signup
  • Loading branch information
interrogator committed Nov 28, 2019
2 parents 9a93932 + 07fc220 commit e2fa2d9
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,6 @@ csv/
corpora.json
uploads/
db.sqlite3

# vim
*.swp
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
FROM python:3.7-buster
FROM python:3.7-slim-buster

RUN apt-get update && \
apt-get install -y git build-essential

WORKDIR /

Expand Down
Empty file added accounts/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions accounts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions accounts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class AccountsConfig(AppConfig):
name = 'accounts'
6 changes: 6 additions & 0 deletions accounts/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib.auth.forms import AuthenticationForm

def forms(request):
return {
'login_form': AuthenticationForm()
}
Empty file added accounts/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions accounts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions accounts/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
27 changes: 27 additions & 0 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.shortcuts import render, redirect
from django.contrib.auth import login, logout, authenticate
from django.contrib.auth.forms import UserCreationForm

def signup(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect('/')
else:
form = UserCreationForm()
return render(request, 'accounts/signup.html', {'form': form})

def logout_view(request):
logout(request)
return redirect('/')

def login_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user:
login(request, user)
return redirect('/')

3 changes: 3 additions & 0 deletions buzzword/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'accounts',
]

STATICFILES_FINDERS = [
Expand Down Expand Up @@ -100,6 +102,7 @@
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'accounts.context_processors.forms',
],
},
},
Expand Down
12 changes: 12 additions & 0 deletions buzzword/templates/accounts/signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

{% block body %}
<div class="container">
<h2>Register</h2>
<form method="post" novalidate>
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary">Create an account</button>
</form>
</div

{% endblock %}
2 changes: 2 additions & 0 deletions buzzword/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
<link rel="stylesheet" href="{{ STATIC_URL }}static/buzzword/bootstrap.css">
</head>
<body>
{% block body %}
{% include "nav.html" %}
<div class="container">
{% block content %}
{% endblock %}
</div>
{% endblock body %}
</body>
</html>
16 changes: 5 additions & 11 deletions buzzword/templates/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</li>
{% if not user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="yyy">Register</a>
<a class="nav-link" href="{% url 'signup' %}">Register</a>
</li>
{% endif %}
</ul>
Expand All @@ -25,19 +25,13 @@
<li class="nav-item">
<a class="nav-link">Logged in as {{ user.username }}</a>
</li>
<form class="form-inline my-2 my-lg-0" action="logout" method="POST">
{% csrf_token %}
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Logout</button>
</form>
<a class="btn btn-default" href="{% url 'logout' %}">Logout</a>
</ul>
{% else %}
<form class="form-inline my-2 my-lg-0" action="login" method="POST">
<form class="form-inline my-2 my-lg-0" action="{% url 'login' %}" method="POST">
{% csrf_token %}
<input class="form-control mr-sm-2" type="text" placeholder="Username" aria-label="Username"
name="username" id="id_username">
<input class="form-control mr-sm-2" type="password" placeholder="Password" aria-label="Password"
name="password" id="id_password">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Sign in</button>
{{ login_form.as_p }}
<button type="submit" class="btn btn-primary">Login</button>
</form>
{% endif %}
</div>
Expand Down
5 changes: 5 additions & 0 deletions buzzword/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
from django.conf import settings
from django.conf.urls.static import static

from accounts import views as accounts_views

urlpatterns = [
path('', include("start.urls")),
path('signup/', accounts_views.signup, name='signup'),
path('logout/', accounts_views.logout_view, name='logout'),
path('login/', accounts_views.login_view, name='login'),
path('explore/', include('explore.urls')),
path('admin/', admin.site.urls),
path('django_plotly_dash/', include('django_plotly_dash.urls')),
Expand Down

0 comments on commit e2fa2d9

Please sign in to comment.