7d83818c11ba49ae3cb8e459840d7a6acef774cf
[wolnelektury.git] / src / bookmarks / views.py
1 from django.http import Http404, JsonResponse
2 from django.shortcuts import render, get_object_or_404
3 from django.views.decorators import cache
4 import catalogue.models
5 from wolnelektury.utils import is_ajax
6 from . import models
7 from lxml import html
8 import re
9
10
11 # login required
12
13 @cache.never_cache
14 def bookmarks(request):
15     try:
16         slug = request.headers['Referer'].rsplit('.', 1)[0].rsplit('/', 1)[-1]
17     except:
18         slug = 'w-80-dni-dookola-swiata'
19 #        raise Http404()            
20     try:
21         book = catalogue.models.Book.objects.get(slug=slug)
22     except catalogue.models.Book.DoesNotExist:
23         raise Http404()
24
25     if request.method == 'POST':
26         # TODO test
27         bm, created = models.Bookmark.objects.update_or_create(
28             user=request.user,
29             book=book,
30             anchor=request.POST.get('anchor', ''),
31             defaults={
32                 'note': request.POST.get('note', ''),
33             }
34         )
35         return JsonResponse(bm.get_for_json())
36     else:
37         return JsonResponse({
38             bm.anchor: bm.get_for_json()
39             for bm in models.Bookmark.objects.filter(
40                     user=request.user,
41                     book=book,
42             )
43         })
44
45
46 def bookmark(request, uuid):
47     bm = get_object_or_404(models.Bookmark, user=request.user, uuid=uuid)
48     if request.method == 'POST':
49         bm.note = request.POST.get('note', '')
50         bm.save()
51     return JsonResponse(bm.get_for_json())
52
53
54 def bookmark_delete(request, uuid):
55     models.Bookmark.objects.filter(user=request.user, uuid=uuid).delete()
56     return JsonResponse({})
57
58
59
60
61 @cache.never_cache
62 def quotes(request):
63     try:
64         slug = request.headers['Referer'].rsplit('.', 1)[0].rsplit('/', 1)[-1]
65     except:
66         slug = 'w-80-dni-dookola-swiata'
67 #        raise Http404()            
68     try:
69         book = catalogue.models.Book.objects.get(slug=slug)
70     except catalogue.models.Book.DoesNotExist:
71         raise Http404()
72
73     if request.method == 'POST':
74         # TODO test
75         # ensure unique? or no?
76
77         text = request.POST.get('text', '')
78         text = text.strip()
79
80         stext = re.sub(r'\s+', ' ', text)
81         ## verify
82         print(text)
83         
84
85         # find out
86         with book.html_file.open('r') as f:
87             ht = f.read()
88         tree = html.fromstring(ht)
89         # TODO: clear
90         for sel in ('.//a[@class="theme-begin"]',
91                     './/a[@class="anchor"]',
92                     ):
93             for e in tree.xpath(sel):
94                 e.clear(keep_tail=True)
95         htext = html.tostring(tree, encoding='unicode', method='text')
96         htext = re.sub(r'\s+', ' ', htext)
97
98         print(htext)
99
100         otext = stext
101         if stext not in htext:
102             # raise 401
103             raise Http404()            
104
105         # paths?
106         # start elem?
107         q = models.Quote.objects.create(
108             user=request.user,
109             book=book,
110             start_elem=request.POST.get('startElem', ''),
111             end_elem=request.POST.get('startElem', ''),
112             start_offset=request.POST.get('startOffset', None),
113             end_offset=request.POST.get('startOffset', None),
114             text=text,
115         )
116         return JsonResponse(q.get_for_json())
117     else:
118         return JsonResponse({
119             q.start_elem: q.get_for_json()
120             for q in models.Quote.objects.filter(
121                     user=request.user,
122                     book=book,
123             )
124         })
125
126
127
128 def quote(request, uuid):
129     q = get_object_or_404(models.Quote, user=request.user, uuid=uuid)
130     if is_ajax(request):
131         return JsonResponse(q.get_for_json())
132     else:
133         return render(request, 'bookmarks/quote_detail.html', {
134             'object': q,
135         })
136
137