Added book_text view and template to repository.
[wolnelektury.git] / apps / catalogue / views.py
1 # -*- coding: utf-8 -*-
2 from django.template import RequestContext
3 from django.shortcuts import render_to_response, get_object_or_404
4 from django.http import HttpResponse, HttpResponseRedirect, Http404
5 from django.core.urlresolvers import reverse
6 from django.db.models import Q
7 from django.contrib.auth.decorators import login_required
8 from django.utils.datastructures import SortedDict
9 from django.views.decorators.http import require_POST
10 from django.contrib import auth
11 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
12 from django.utils import simplejson
13 from django.utils.functional import Promise
14 from django.utils.encoding import force_unicode
15
16 from catalogue import models
17 from catalogue import forms
18 from catalogue.utils import split_tags
19 from newtagging import views as newtagging_views
20
21
22 class LazyEncoder(simplejson.JSONEncoder):
23     def default(self, obj):
24         if isinstance(obj, Promise):
25             return force_unicode(obj)
26         return obj
27
28
29 def search(request):
30     query = request.GET.get('q', '')
31     tags = request.GET.get('tags', '')
32     if tags == '':
33         tags = []
34     
35     try:
36         tag_list = models.Tag.get_tag_list(tags)
37         tag = models.Tag.objects.get(name=query)
38     except models.Tag.DoesNotExist:
39         try:
40             book = models.Book.objects.get(title=query)
41             return HttpResponseRedirect(book.get_absolute_url())
42         except models.Book.DoesNotExist:
43             return HttpResponseRedirect(reverse('catalogue.views.main_page'))
44     else:
45         tag_list.append(tag)
46         return HttpResponseRedirect(reverse('catalogue.views.tagged_object_list', 
47             kwargs={'tags': '/'.join(tag.slug for tag in tag_list)}
48         ))
49
50
51 def tags_starting_with(request):
52     try:
53         prefix = request.GET['q']
54         if len(prefix) < 2:
55             raise KeyError
56             
57         books = models.Book.objects.filter(title__icontains=prefix)
58         tags = models.Tag.objects.filter(name__icontains=prefix)
59         if request.user.is_authenticated():
60             tags = tags.filter(~Q(category='set') | Q(user=request.user))
61         else:
62             tags = tags.filter(~Q(category='set'))
63         
64         completions = [book.title for book in books] + [tag.name for tag in tags]
65
66         return HttpResponse('\n'.join(completions))    
67     
68     except KeyError:
69         return HttpResponse('')
70
71
72 def main_page(request):    
73     if request.user.is_authenticated():
74         shelves = models.Tag.objects.filter(category='set', user=request.user)
75         new_set_form = forms.NewSetForm()
76     extra_where = 'NOT catalogue_tag.category = "set"'
77     tags = models.Tag.objects.usage_for_model(models.Book, counts=True, extra={'where': [extra_where]})
78     fragment_tags = models.Tag.objects.usage_for_model(models.Fragment, counts=True,
79         extra={'where': ['catalogue_tag.category = "theme"'] + [extra_where]})
80     categories = split_tags(tags)
81     
82     form = forms.SearchForm()
83     return render_to_response('catalogue/main_page.html', locals(),
84         context_instance=RequestContext(request))
85
86
87 def book_list(request):
88     books = models.Book.objects.all()
89     form = forms.SearchForm()
90     
91     books_by_first_letter = SortedDict()
92     for book in books:
93         books_by_first_letter.setdefault(book.title[0], []).append(book)
94     
95     return render_to_response('catalogue/book_list.html', locals(),
96         context_instance=RequestContext(request))
97
98
99 def tagged_object_list(request, tags=''):
100     # Prevent DoS attacks on our database
101     if len(tags.split('/')) > 6:
102         raise Http404
103         
104     try:
105         tags = models.Tag.get_tag_list(tags)
106     except models.Tag.DoesNotExist:
107         raise Http404
108     
109     model = models.Book
110     shelf_is_set = (len(tags) == 1 and tags[0].category == 'set')
111     theme_is_set = any(tag.category == 'theme' for tag in tags)
112     if theme_is_set:
113         model = models.Fragment
114
115     extra_where = 'NOT catalogue_tag.category = "set"'
116     related_tags = models.Tag.objects.related_for_model(tags, model, counts=True, extra={'where': [extra_where]})
117     categories = split_tags(related_tags)
118
119     return newtagging_views.tagged_object_list(
120         request,
121         tag_model=models.Tag,
122         queryset_or_model=model,
123         tags=tags,
124         template_name='catalogue/tagged_object_list.html',
125         extra_context = {'categories': categories, 'shelf_is_set': shelf_is_set },
126     )
127
128
129 def book_detail(request, slug):
130     book = get_object_or_404(models.Book, slug=slug)
131     tags = list(book.tags.filter(~Q(category='set')))
132     categories = split_tags(tags)
133     
134     form = forms.SearchForm()
135     return render_to_response('catalogue/book_detail.html', locals(),
136         context_instance=RequestContext(request))
137
138
139 def book_text(request, slug):
140     book = get_object_or_404(models.Book, slug=slug)
141     
142     return render_to_response('catalogue/book_text.html', locals(),
143         context_instance=RequestContext(request))
144
145
146 def logout_then_redirect(request):
147     auth.logout(request)
148     return HttpResponseRedirect(request.GET.get('next', '/'))
149
150
151 @require_POST
152 def register(request):
153     registration_form = UserCreationForm(request.POST, prefix='registration')
154     if registration_form.is_valid():
155         user = registration_form.save()
156         user = auth.authenticate(
157             username=registration_form.cleaned_data['username'], 
158             password=registration_form.cleaned_data['password1']
159         )
160         auth.login(request, user)
161         response_data = {'success': True, 'errors': {}}
162     else:
163         response_data = {'success': False, 'errors': registration_form.errors}
164     return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
165
166
167 @require_POST
168 def login(request):
169     form = AuthenticationForm(data=request.POST, prefix='login')
170     if form.is_valid():
171         auth.login(request, form.get_user())
172         response_data = {'success': True, 'errors': {}}
173     else:
174         response_data = {'success': False, 'errors': form.errors}
175     return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
176
177
178 def book_sets(request, slug):
179     book = get_object_or_404(models.Book, slug=slug)
180     user_sets = models.Tag.objects.filter(category='set', user=request.user)
181     book_sets = book.tags.filter(category='set', user=request.user)
182     
183     if not request.user.is_authenticated():
184         return HttpResponse('<p>Aby zarządzać swoimi półkami, musisz się zalogować.</p>')
185     
186     if request.method == 'POST':
187         form = forms.ObjectSetsForm(book, request.user, request.POST)
188         if form.is_valid():
189             book.tags = ([models.Tag.objects.get(pk=id) for id in form.cleaned_data['set_ids']] +
190                 list(book.tags.filter(~Q(category='set') | ~Q(user=request.user))))
191             if request.is_ajax():
192                 return HttpResponse('<p>Półki zostały zapisane.</p>')
193             else:
194                 return HttpResponseRedirect('/')
195     else:
196         form = forms.ObjectSetsForm(book, request.user)
197         new_set_form = forms.NewSetForm()
198     
199     return render_to_response('catalogue/book_sets.html', locals(),
200         context_instance=RequestContext(request))
201
202
203 def fragment_sets(request, id):
204     fragment = get_object_or_404(models.Fragment, pk=id)
205     user_sets = models.Tag.objects.filter(category='set', user=request.user)
206     fragment_sets = fragment.tags.filter(category='set', user=request.user)
207
208     if not request.user.is_authenticated():
209         return HttpResponse('<p>Aby zarządzać swoimi półkami, musisz się zalogować.</p>')
210
211     if request.method == 'POST':
212         form = forms.ObjectSetsForm(fragment, request.user, request.POST)
213         if form.is_valid():
214             fragment.tags = ([models.Tag.objects.get(pk=id) for id in form.cleaned_data['set_ids']] +
215                 list(fragment.tags.filter(~Q(category='set') | ~Q(user=request.user))))
216             if request.is_ajax():
217                 return HttpResponse('<p>Półki zostały zapisane.</p>')
218             else:
219                 return HttpResponseRedirect('/')
220     else:
221         form = forms.ObjectSetsForm(fragment, request.user)
222         new_set_form = forms.NewSetForm()
223
224     return render_to_response('catalogue/fragment_sets.html', locals(),
225         context_instance=RequestContext(request))
226
227
228 @login_required
229 @require_POST
230 def new_set(request):
231     new_set_form = forms.NewSetForm(request.POST)
232     if new_set_form.is_valid():
233         new_set = new_set_form.save(request.user)
234         
235         if request.is_ajax():
236             return HttpResponse(u'<p>Półka <strong>%s</strong> została utworzona</p>' % new_set)
237         else:
238             return HttpResponseRedirect('/')
239     
240     return render_to_response('catalogue/book_sets.html', locals(),
241             context_instance=RequestContext(request))
242
243
244 @login_required
245 @require_POST
246 def delete_shelf(request, slug):
247     user_set = get_object_or_404(models.Tag, slug=slug, category='set', user=request.user)
248     user_set.delete()
249     
250     if request.is_ajax():
251         return HttpResponse(u'<p>Półka <strong>%s</strong> została usunięta</p>' % user_set.name)
252     else:
253         return HttpResponseRedirect('/')
254
255
256 @login_required
257 def user_shelves(request):
258     shelves = models.Tag.objects.filter(category='set', user=request.user)
259     new_set_form = forms.NewSetForm()
260     return render_to_response('catalogue/user_shelves.html', locals(),
261             context_instance=RequestContext(request))
262