Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Implemented login and logout views

Roland Geiderffac429ae1c0
diff --git a/manager/templates/login.html b/manager/templates/login.html
new file mode 100644
index 000000000..f8022023e
--- /dev/null
+++ b/manager/templates/login.html
@@ -0,0 +1,23 @@
+{% extends "base.html" %}
+{% load i18n %}
+
+{% block title %} {% trans "Login" %}{% endblock %}
+
+
+{% block content %}
+
+<form action="{% url manager.views.login %}"
+ method="post"
+ class="ym-form">
+ {% csrf_token %}
+ {{ form.non_field_errors }}
+<table>
+{% include "form_snippet.html" with field=form.username %}
+{% include "form_snippet.html" with field=form.password %}
+ <tr>
+ <td><input type="submit" value='{% trans "Login" %}' /></td>
+ <td></td>
+ </tr>
+</table>
+</form>
+{% endblock %}
diff --git a/manager/views.py b/manager/views.py
index 249d076b5..a0e6f9ba9 100644
--- a/manager/views.py
+++ b/manager/views.py
@@ -22,10 +22,13 @@ from django.http import Http404
from django.forms import ModelForm
from django.forms.models import modelformset_factory
from django.core.context_processors import csrf
-from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import permission_required, login_required
from django.utils.translation import ugettext as _
+from django.contrib.auth import authenticate
+from django.contrib.auth import login as django_login
+from django.contrib.auth import logout as django_logout
+from django.contrib.auth.forms import AuthenticationForm
from manager.models import TrainingSchedule
from manager.models import Day
@@ -53,15 +56,44 @@ def index(request):
latest_trainings = TrainingSchedule.objects.all().order_by('-creation_date')[:5]
return render_to_response('index.html', {'latest_workouts_list': latest_trainings})
+
def login(request):
"""Login the user and redirect it
"""
- pass
+ template_data = {}
+ template_data.update(csrf(request))
+
+ template_data['form'] = AuthenticationForm()
+
+ if request.method == 'POST':
+ authentication_form = AuthenticationForm(data=request.POST)
+
+ # If the data is valid, log in and redirect
+ if authentication_form.is_valid():
+ username = authentication_form.cleaned_data['username']
+ password = authentication_form.cleaned_data['password']
+
+ user = authenticate(username=username, password=password)
+ if user is not None:
+ if user.is_active:
+ django_login(request, user)
+
+ # Redirect to the start page.
+ return HttpResponseRedirect('/')
+ else:
+ # Return a disabled account error message
+ pass
+ else:
+ # Return an invalid login error message.
+ pass
+
+ return render_to_response('login.html', template_data)
def logout(request):
- """Logout the
+ """Logout the user
"""
- logout(request)
+ django_logout(request)
+ return HttpResponseRedirect('/login')
# ************************
# Workout functions