From 05062d251dfe096e5e54aba912d5744d2695d164 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Marek=20St=C4=99pniowski?=
Date: Sat, 16 Aug 2008 19:05:58 +0200
Subject: [PATCH 1/1] Finished logging in/registration.
---
catalogue/templatetags/catalogue.py | 27 ++++++++++++++
catalogue/views.py | 55 +++++++++++++++++++++++------
media/css/master.css | 9 +++++
templates/base.html | 21 +++++------
templates/catalogue/book_sets.html | 4 ++-
urls.py | 5 +--
6 files changed, 95 insertions(+), 26 deletions(-)
diff --git a/catalogue/templatetags/catalogue.py b/catalogue/templatetags/catalogue.py
index 1bf250c18..f2f35ddae 100644
--- a/catalogue/templatetags/catalogue.py
+++ b/catalogue/templatetags/catalogue.py
@@ -3,11 +3,24 @@ from django import template
from django.template import Node, Variable
from django.utils.encoding import smart_str
from django.core.urlresolvers import reverse
+from django.contrib.auth import forms
register = template.Library()
+class RegistrationForm(forms.UserCreationForm):
+ def as_ul(self):
+ "Returns this form rendered as HTML s -- excluding the ."
+ return self._html_output(u'%(errors)s%(label)s %(field)s%(help_text)s', u'%s', '', u' %s', False)
+
+
+class LoginForm(forms.AuthenticationForm):
+ def as_ul(self):
+ "Returns this form rendered as HTML s -- excluding the ."
+ return self._html_output(u'%(errors)s%(label)s %(field)s%(help_text)s', u'%s', '', u' %s', False)
+
+
def iterable(obj):
try:
iter(obj)
@@ -40,6 +53,10 @@ def title_from_tags(tags):
title = u''
+ # Specjalny przypadek oglÄ
dania wszystkich lektur w danym zestawie
+ if len(self) == 1 and 'set' in self:
+ return u'Zestaw %s' % self['set']
+
# Specjalny przypadek "TwórczoÅÄ w pozytywizmie", wtedy gdy tylko epoka
# jest wybrana przez użytkownika
if 'epoch' in self and len(self) == 1:
@@ -80,6 +97,16 @@ def title_from_tags(tags):
return capfirst(title)
+@register.simple_tag
+def user_creation_form():
+ return RegistrationForm(prefix='registration').as_ul()
+
+
+@register.simple_tag
+def authentication_form():
+ return LoginForm(prefix='login').as_ul()
+
+
@register.tag
def catalogue_url(parser, token):
bits = token.split_contents()
diff --git a/catalogue/views.py b/catalogue/views.py
index dc2e7b107..2f4b30ff5 100644
--- a/catalogue/views.py
+++ b/catalogue/views.py
@@ -7,6 +7,18 @@ from django.db.models import Q
from django.contrib.auth.decorators import login_required
from django.utils.datastructures import SortedDict
from django.views.decorators.http import require_GET, require_POST
+from django.contrib import auth
+from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
+from django.utils import simplejson
+from django.utils.functional import Promise
+from django.utils.encoding import force_unicode
+
+class LazyEncoder(simplejson.JSONEncoder):
+ def default(self, obj):
+ if isinstance(obj, Promise):
+ return force_unicode(obj)
+ return obj
+
from catalogue import models
from catalogue import forms
@@ -28,16 +40,7 @@ def catalogue_redirect(request, tags=''):
else:
return HttpResponseRedirect(reverse('catalogue.views.main_page'))
-#
-# @require_GET
-# def tag_search(request):
-# search_form = forms.SearchForm(request.GET)
-# if search_form.is_valid():
-# query = search_form.cleaned_data['q']
-# tags = search_form.cleaned_data['tags']
-#
-
-
+
def tags_starting_with(request):
try:
prefix = request.GET['q']
@@ -131,6 +134,38 @@ def book_detail(request, slug):
context_instance=RequestContext(request))
+def logout_then_redirect(request):
+ auth.logout(request)
+ return HttpResponseRedirect(request.GET.get('next', '/'))
+
+
+@require_POST
+def register(request):
+ registration_form = UserCreationForm(request.POST, prefix='registration')
+ if registration_form.is_valid():
+ user = registration_form.save()
+ user = auth.authenticate(
+ username=registration_form.cleaned_data['username'],
+ password=registration_form.cleaned_data['password1']
+ )
+ auth.login(request, user)
+ response_data = {'success': True, 'errors': {}}
+ else:
+ response_data = {'success': False, 'errors': registration_form.errors}
+ return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
+
+
+@require_POST
+def login(request):
+ form = AuthenticationForm(data=request.POST, prefix='login')
+ if form.is_valid():
+ auth.login(request, form.get_user())
+ response_data = {'success': True, 'errors': {}}
+ else:
+ response_data = {'success': False, 'errors': form.errors}
+ return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
+
+
@login_required
def book_sets(request, slug):
book = get_object_or_404(models.Book, slug=slug)
diff --git a/media/css/master.css b/media/css/master.css
index 942111ce9..41c40c0f5 100644
--- a/media/css/master.css
+++ b/media/css/master.css
@@ -312,6 +312,15 @@ em {
display: block;
}
+.cuteform span.help-text {
+ display: block;
+ font-size: 0.8em;
+ color: #999;
+}
+
+.cuteform .error {
+ color: #BF3024;
+}
/* ================ */
/* = Modal Window = */
diff --git a/templates/base.html b/templates/base.html
index 78bb6b5a2..03f8e198a 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -1,4 +1,4 @@
-{% load chunks compressed %}
+{% load chunks compressed catalogue %}
@@ -23,7 +23,7 @@
{% if user.is_staff %}
| Administracja
{% endif %}
- | Wyloguj
+ | Wyloguj
{% else %}
Zaloguj siÄ / ZaÅóż konto
@@ -67,26 +67,21 @@
diff --git a/templates/catalogue/book_sets.html b/templates/catalogue/book_sets.html
index 7e7b3d36e..1b87c12af 100644
--- a/templates/catalogue/book_sets.html
+++ b/templates/catalogue/book_sets.html
@@ -9,5 +9,7 @@
{% endif %}
\ No newline at end of file
diff --git a/urls.py b/urls.py
index c4185d6e2..104899f67 100644
--- a/urls.py
+++ b/urls.py
@@ -15,8 +15,9 @@ urlpatterns = patterns('',
url(r'^admin/(.*)$', admin.site.root),
# Użytkownicy
- url(r'^uzytkownicy/zaloguj/$', 'django.contrib.auth.views.login', {'template_name': 'auth/login.html'}, name='login'),
- url(r'^uzytkownicy/wyloguj/$', 'django.contrib.auth.views.logout_then_login', name='logout'),
+ url(r'^uzytkownicy/zaloguj/$', 'catalogue.views.login', name='login'),
+ url(r'^uzytkownicy/wyloguj/$', 'catalogue.views.logout_then_redirect', name='logout'),
+ url(r'^uzytkownicy/utworz/$', 'catalogue.views.register', name='register'),
# Pliki statyczne
url(r'^media/(?P.*)$', 'django.views.static.serve',
--
2.20.1