Commits

Jernej Kos committed d5ae918

Added basic login system.

  • Participants
  • Parent commits 33126bf

Comments (0)

Files changed (11)

     'survey',
 )
 
+LOGIN_REDIRECT_URL = '/survey/'
+
 # A sample logging configuration. The only tangible logging
 # performed by this configuration is to send an email to
 # the site admins on every HTTP 500 error.

File survey/views.py

+from django.contrib.auth.decorators import login_required
 from django.shortcuts import get_object_or_404, render_to_response
 from django.template import RequestContext
 
 from . import models as survey_models
 
-def create_survey(request):
-  pass
+@login_required
+def list_surveys(request):
+  return render_to_response('survey/list_surveys.html', dict(
+    surveys = survey_models.Survey.objects.all()),
+    context_instance = RequestContext(request))
 
+@login_required
 def enter_survey_instance(request, survey_id):
   survey = get_object_or_404(survey_models.Survey, pk = survey_id)
   if request.method == 'POST':
   return render_to_response('survey/enter.html', dict(
     survey = survey), context_instance = RequestContext(request))
 
+@login_required
 def list_subjects(request, survey_id):
   survey = get_object_or_404(survey_models.Survey, pk = survey_id)
-  return render_to_response('survey/list.html', dict(
+  return render_to_response('survey/list_subjects.html', dict(
     survey = survey), context_instance = RequestContext(request))
 
+@login_required
 def edit_survey_instance(request, survey_id, subject_id):
   survey = get_object_or_404(survey_models.Survey, pk = survey_id)
   subject = get_object_or_404(survey_models.Subject, pk = subject_id)
     subject = subject,
     answers = answers), context_instance = RequestContext(request))
 
-def list_surveys(request):
-  pass

File templates/base.html

+
+Logged in as: <b>{{ user }}</b> (<a href="{% url logout %}">logout</a>)<br/>
+
+{% block content %}
+{% endblock %}
+

File templates/registration/login.html

+{% if form.errors %}
+<p>Your username and password didn't match. Please try again.</p>
+{% 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>
+

File templates/survey/edit.html

+{% extends "base.html" %}
 
+{% block content %}
 Survey: <b>{{ survey.title }}</b><br/>
 Subject#: <b>{{ subject.pk }}</b>
 
 <hr/>
 <input type="submit" value="Submit" />
 </form>
+{% endblock %}
 

File templates/survey/enter.html

+{% extends "base.html" %}
 
+{% block content %}
 Survey: <b>{{ survey.title }}</b>
 
 <form method="POST" action="{% url enter_survey survey.pk %}">
 <hr/>
 <input type="submit" value="Submit" />
 </form>
+{% endblock %}
 

File templates/survey/list.html

-Survey: <b>{{ survey.title }}</b><br/>
-<br/>
-Subjects:
-<hr/>
-{% for subject in survey.get_subjects %}
-<a href="{% url edit_survey survey.pk subject.pk %}">Subject {{ subject.pk }}</a> ({{ subject.created_at }})<br/>
-{% endfor %}
-<hr/>
-<a href="{% url enter_survey survey.pk %}">Enter new subject</a>

File templates/survey/list_subjects.html

+{% extends "base.html" %}
+
+{% block content %}
+Survey: <b>{{ survey.title }}</b><br/>
+<br/>
+Subjects:
+<hr/>
+{% for subject in survey.get_subjects %}
+<a href="{% url edit_survey survey.pk subject.pk %}">Subject {{ subject.pk }}</a> ({{ subject.created_at }})<br/>
+{% endfor %}
+<hr/>
+<a href="{% url enter_survey survey.pk %}">Enter new subject</a>
+{% endblock %}
+

File templates/survey/list_surveys.html

+{% extends "base.html" %}
+
+{% block content %}
+<br/>
+Surveys:
+<hr/>
+{% for survey in surveys %}
+<a href="{% url list_subjects survey.pk %}">{{ survey }}</a><br/>
+{% endfor %}
+<hr/>
+{% endblock %}
+

File templates/survey/submit_done.html

+{% extends "base.html" %}
+
+{% block content %}
 Survey: <b>{{ survey.title }}</b><br/>
 Subject#: <b>{{ subject.pk }}</b><br/>
 <br/>
 Answers recorded successfully.<br/>
 <br/>
 <a href="{% url list_subjects survey.pk %}">List of subjects</a>
+{% endblock %}
+
 admin.autodiscover()
 
 urlpatterns = patterns('',
+  url(r'^survey/?$', 'survey.views.list_surveys', name = 'list_surveys'),
   url(r'^survey/(?P<survey_id>\w+)$', 'survey.views.enter_survey_instance', name = 'enter_survey'),
   url(r'^survey/(?P<survey_id>\w+)/(?P<subject_id>\w+)$', 'survey.views.edit_survey_instance', name = 'edit_survey'),
   url(r'^survey/(?P<survey_id>\w+)/.subjects$', 'survey.views.list_subjects', name = 'list_subjects'),
 
+  url(r'^accounts/login/$', 'django.contrib.auth.views.login', name = 'login'),
+  url(r'^accounts/logout/$', 'django.contrib.auth.views.logout_then_login', name = 'logout'),
+  url(r'^$', 'django.contrib.auth.views.login'),
+
   url(r'^admin/', include(admin.site.urls)),
 )