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 <li>s -- excluding the <ul></ul>."
+ return self._html_output(u'<li>%(errors)s%(label)s %(field)s<span class="help-text">%(help_text)s</span></li>', u'<li>%s</li>', '</li>', u' %s', False)
+
+
+class LoginForm(forms.AuthenticationForm):
+ def as_ul(self):
+ "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
+ return self._html_output(u'<li>%(errors)s%(label)s %(field)s<span class="help-text">%(help_text)s</span></li>', u'<li>%s</li>', '</li>', u' %s', False)
+
+
def iterable(obj):
try:
iter(obj)
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:
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()
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
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']
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)
display: block;
}
+.cuteform span.help-text {
+ display: block;
+ font-size: 0.8em;
+ color: #999;
+}
+
+.cuteform .error {
+ color: #BF3024;
+}
/* ================ */
/* = Modal Window = */
-{% load chunks compressed %}
+{% load chunks compressed catalogue %}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
{% if user.is_staff %}
| <a href="/admin/">Administracja</a>
{% endif %}
- | <a href="{% url logout %}">Wyloguj</a>
+ | <a href="{% url logout %}?next={{ request.get_full_path }}">Wyloguj</a>
</p>
{% else %}
<p><a href="{% url login %}" id="login-register-link">Zaloguj się / Załóż konto</a></p>
<div id="login-register-window">
<div class="header"><a href="#" class="jqmClose">Zamknij</a></div>
<div class="target">
- <form method="post" action="{% url login %}" id="login-form" class="cuteform" style="width: 18em;">
+ <form method="post" action="{% url login %}" id="login-form" class="cuteform">
<h2>Zaloguj się / <a href="#" id="show-registration-form" style="font-size: 0.85em; font-weight: normal">Załóż konto</a></h2>
+ <p><span id="id_login-__all__" /></p>
<ol>
- <li><label for="id_username" title="np. Anna Kowalska">Nazwa użytkownika:</label> <input id="id_username" type="text" name="username" maxlength="30" /></li>
- <li><label for="id_password">Hasło:</label> <input type="password" name="password" id="id_password" /></li>
+ {% authentication_form %}
<li><input type="submit" value="Zaloguj się" /></li>
</ol>
- <p style="display:none;"><input type="hidden" name="next" value="{{ request.get_full_path }}" /></p>
</form>
- <form method="post" action="{% url login %}" id="registration-form" class="cuteform" style="display: none; width: 18em;">
+ <form method="post" action="{% url register %}" id="registration-form" class="cuteform" style="display: none;">
<h2><a href="#" id="show-login-form" style="font-size: 0.85em; font-weight: normal">Zaloguj się</a> / Załóż konto</h2>
+ <p><span id="id_registration-__all__" /></p>
<ol>
- <li><label for="id_username" title="np. Anna Kowalska">Nazwa użytkownika:</label> <input id="id_username" type="text" name="username" maxlength="30" /></li>
- <li><label for="id_email">Adres e-mail:</label> <input id="id_email" type="text" name="email" maxlength="80" /></li>
- <li><label for="id_password">Hasło:</label> <input type="password" name="password" id="id_password" /></li>
- <li><label for="id_password2">Powtórz hasło:</label> <input type="password" name="password2" id="id_password2" /></li>
-
+ {% user_creation_form %}
<li><input type="submit" value="Załóż konto" /></li>
</ol>
- <p style="display:none;"><input type="hidden" name="next" value="{{ request.get_full_path }}" /></p>
</form>
</div>
</div>
{% endif %}
<hr />
<form action="{% url catalogue.views.new_set %}" method="POST" accept-charset="utf-8" class="cuteform">
- <p>{{ new_set_form.name }} <input type="submit" value="Dodaj zestaw"/></p>
+<ol>
+ <li>{{ new_set_form.name }} <input type="submit" value="Dodaj zestaw"/></li>
+</ol>
</form>
\ No newline at end of file
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<path>.*)$', 'django.views.static.serve',