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):
15 if not request.user.is_authenticated:
16 return JsonResponse({})
18 slug = request.headers['Referer'].rsplit('.', 1)[0].rsplit('/', 1)[-1]
20 slug = 'w-80-dni-dookola-swiata'
23 book = catalogue.models.Book.objects.get(slug=slug)
24 except catalogue.models.Book.DoesNotExist:
27 if request.method == 'POST':
29 bm, created = models.Bookmark.objects.update_or_create(
32 anchor=request.POST.get('anchor', ''),
34 'note': request.POST.get('note', ''),
37 return JsonResponse(bm.get_for_json())
40 bm.anchor: bm.get_for_json()
41 for bm in models.Bookmark.objects.filter(
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', '')
53 return JsonResponse(bm.get_for_json())
56 def bookmark_delete(request, uuid):
57 models.Bookmark.objects.filter(user=request.user, uuid=uuid).delete()
58 return JsonResponse({})
66 slug = request.headers['Referer'].rsplit('.', 1)[0].rsplit('/', 1)[-1]
68 slug = 'w-80-dni-dookola-swiata'
71 book = catalogue.models.Book.objects.get(slug=slug)
72 except catalogue.models.Book.DoesNotExist:
75 if request.method == 'POST':
77 # ensure unique? or no?
79 text = request.POST.get('text', '')
82 stext = re.sub(r'\s+', ' ', text)
88 with book.html_file.open('r') as f:
90 tree = html.fromstring(ht)
92 for sel in ('.//a[@class="theme-begin"]',
93 './/a[@class="anchor"]',
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)
103 if stext not in htext:
109 q = models.Quote.objects.create(
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),
118 return JsonResponse(q.get_for_json())
120 return JsonResponse({
121 q.start_elem: q.get_for_json()
122 for q in models.Quote.objects.filter(
130 def quote(request, uuid):
131 q = get_object_or_404(models.Quote, user=request.user, uuid=uuid)
133 return JsonResponse(q.get_for_json())
135 return render(request, 'bookmarks/quote_detail.html', {