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
14 def bookmarks(request):
16 slug = request.headers['Referer'].rsplit('.', 1)[0].rsplit('/', 1)[-1]
18 slug = 'w-80-dni-dookola-swiata'
21 book = catalogue.models.Book.objects.get(slug=slug)
22 except catalogue.models.Book.DoesNotExist:
25 if request.method == 'POST':
27 bm, created = models.Bookmark.objects.update_or_create(
30 anchor=request.POST.get('anchor', ''),
32 'note': request.POST.get('note', ''),
35 return JsonResponse(bm.get_for_json())
38 bm.anchor: bm.get_for_json()
39 for bm in models.Bookmark.objects.filter(
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', '')
51 return JsonResponse(bm.get_for_json())
54 def bookmark_delete(request, uuid):
55 models.Bookmark.objects.filter(user=request.user, uuid=uuid).delete()
56 return JsonResponse({})
64 slug = request.headers['Referer'].rsplit('.', 1)[0].rsplit('/', 1)[-1]
66 slug = 'w-80-dni-dookola-swiata'
69 book = catalogue.models.Book.objects.get(slug=slug)
70 except catalogue.models.Book.DoesNotExist:
73 if request.method == 'POST':
75 # ensure unique? or no?
77 text = request.POST.get('text', '')
80 stext = re.sub(r'\s+', ' ', text)
86 with book.html_file.open('r') as f:
88 tree = html.fromstring(ht)
90 for sel in ('.//a[@class="theme-begin"]',
91 './/a[@class="anchor"]',
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)
101 if stext not in htext:
107 q = models.Quote.objects.create(
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),
116 return JsonResponse(q.get_for_json())
118 return JsonResponse({
119 q.start_elem: q.get_for_json()
120 for q in models.Quote.objects.filter(
128 def quote(request, uuid):
129 q = get_object_or_404(models.Quote, user=request.user, uuid=uuid)
131 return JsonResponse(q.get_for_json())
133 return render(request, 'bookmarks/quote_detail.html', {