from django.views.decorators.http import 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
from django.utils.http import urlquote_plus
from django.views.decorators import cache
from django.utils import translation
from django.utils.translation import ugettext as _
from django.views.generic.list_detail import object_list
+from ajaxable.utils import LazyEncoder, JSONResponse
from catalogue import models
from catalogue import forms
from catalogue.utils import split_tags, AttachmentHttpResponse, async_build_pdf
staff_required = user_passes_test(lambda user: user.is_staff)
-class LazyEncoder(simplejson.JSONEncoder):
- def default(self, obj):
- if isinstance(obj, Promise):
- return force_unicode(obj)
- return obj
-
-# shortcut for JSON reponses
-class JSONResponse(HttpResponse):
- def __init__(self, data={}, callback=None, **kwargs):
- # get rid of mimetype
- kwargs.pop('mimetype', None)
- data = simplejson.dumps(data)
- if callback:
- data = callback + "(" + data + ");"
- super(JSONResponse, self).__init__(data, mimetype="application/json", **kwargs)
-
-
def catalogue(request):
tags = models.Tag.objects.exclude(
category__in=('set', 'book')).exclude(book_count=0)
categories = split_tags(tags)
fragment_tags = categories.get('theme', [])
- form = forms.SearchForm()
return render_to_response('catalogue/catalogue.html', locals(),
context_instance=RequestContext(request))
def book_list(request, filter=None, template_name='catalogue/book_list.html'):
""" generates a listing of all books, optionally filtered with a test function """
- form = forms.SearchForm()
-
books_by_author, orphans, books_by_parent = models.Book.book_list(filter)
books_nav = SortedDict()
for tag in books_by_author:
theme = get_object_or_404(models.Tag, slug=theme_slug, category='theme')
fragments = models.Fragment.tagged.with_all([book_tag, theme])
- form = forms.SearchForm()
return render_to_response('catalogue/book_fragments.html', locals(),
context_instance=RequestContext(request))
projects.add((project, meta.get('funded_by', '')))
projects = sorted(projects)
- form = forms.SearchForm()
custom_pdf_form = forms.CustomPDFForm()
return render_to_response('catalogue/book_detail.html', locals(),
context_instance=RequestContext(request))
return HttpResponseRedirect('/')
-# ==================
-# = Authentication =
-# ==================
-@require_POST
-@cache.never_cache
-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))
-
-
-@require_POST
-@cache.never_cache
-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))
-
-
-@cache.never_cache
-def logout_then_redirect(request):
- auth.logout(request)
- return HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
-
-
-
# =========
# = Admin =
# =========
return HttpResponse(_("Error importing file: %r") % book_import_form.errors)
-
-def clock(request):
- """ Provides server time for jquery.countdown,
- in a format suitable for Date.parse()
- """
- return HttpResponse(datetime.now().strftime('%Y/%m/%d %H:%M:%S'))
-
-
# info views for API
def book_info(request, id, lang='pl'):