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