forked from Sefaria/Sefaria-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
680 lines (546 loc) · 20.3 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
import json
from bson.son import SON
from datetime import datetime, timedelta
from django.template import RequestContext
from django.shortcuts import render_to_response, redirect
from django.http import Http404
# noinspection PyUnresolvedReferences
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import ensure_csrf_cookie
from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
# noinspection PyUnresolvedReferences
from django.contrib.auth.models import User
from django.contrib.auth.models import Group as DjangoGroup
from reader.views import s2_sheets, s2_sheets_by_tag
# noinspection PyUnresolvedReferences
from sefaria.client.util import jsonResponse, HttpResponse
from sefaria.sheets import *
from sefaria.model.user_profile import *
from sefaria.model.group import Group, GroupSet
from sefaria.utils.users import user_link
from sefaria.system.exceptions import InputError
# sefaria.model.dependencies makes sure that model listeners are loaded.
# noinspection PyUnresolvedReferences
import sefaria.model.dependencies
def annotate_user_links(sources):
"""
Search a sheet for any addedBy fields (containg a UID) and add corresponding user links.
"""
for source in sources:
if "addedBy" in source:
source["userLink"] = user_link(source["addedBy"])
if "subsources" in source:
source["subsources"] = annotate_user_links(source["subsources"])
return sources
@ensure_csrf_cookie
def new_sheet(request):
"""
View an new, empty source sheet.
"""
owner_groups = get_user_groups(request.user)
query = {"owner": request.user.id or -1 }
hide_video = db.sheets.find(query).count() > 2
return render_to_response('sheets.html', {"can_edit": True,
"new_sheet": True,
"is_owner": True,
"hide_video": hide_video,
"owner_groups": owner_groups,
"current_url": request.get_full_path,
},
RequestContext(request))
def can_edit(user, sheet):
"""
Returns true if user can edit sheet.
"""
if sheet["owner"] == user.id:
return True
if "collaboration" not in sheet["options"]:
return False
if sheet["options"]["collaboration"] == "anyone-can-edit":
return True
if sheet["options"]["collaboration"] == "group-can-edit":
if "group" in sheet:
if sheet["group"] in [group.name for group in user.groups.all()]:
return True
return False
def can_add(user, sheet):
"""
Returns true if user has adding persmission on sheet.
Returns false if user has the higher permission "can_edit"
"""
if not user.is_authenticated():
return False
if can_edit(user, sheet):
return False
if "collaboration" not in sheet["options"]:
return False
if sheet["options"]["collaboration"] == "anyone-can-add":
return True
if sheet["options"]["collaboration"] == "group-can-add":
if "group" in sheet:
if sheet["group"] in [group.name for group in user.groups.all()]:
return True
return False
def get_user_groups(user):
"""
Returns a list of Groups that user belongs to.
"""
groups = [g.name for g in user.groups.all()]
return GroupSet({"name": {"$in": groups }}, sort=[["name", 1]])
def make_sheet_class_string(sheet):
"""
Returns a string of class names corresponding to the options of sheet.
"""
o = sheet["options"]
classes = []
classes.append(o.get("language", "bilingual"))
classes.append(o.get("layout", "sideBySide"))
classes.append(o.get("langLayout", "heRight"))
if o.get("numbered", False): classes.append("numbered")
if o.get("boxed", False): classes.append("boxed")
if sheet["status"] == "public":
classes.append("public")
return " ".join(classes)
@ensure_csrf_cookie
def view_sheet(request, sheet_id):
"""
View the sheet with sheet_id.
"""
sheet = get_sheet(sheet_id)
if "error" in sheet:
return HttpResponse(sheet["error"])
sheet["sources"] = annotate_user_links(sheet["sources"])
# Count this as a view
db.sheets.update({"id": int(sheet_id)}, {"$inc": {"views": 1}})
try:
owner = User.objects.get(id=sheet["owner"])
author = owner.first_name + " " + owner.last_name
owner_groups = get_user_groups(request.user) if sheet["owner"] == request.user.id else None
except User.DoesNotExist:
author = "Someone Mysterious"
owner_groups = None
sheet_class = make_sheet_class_string(sheet)
can_edit_flag = can_edit(request.user, sheet)
can_add_flag = can_add(request.user, sheet)
sheet_group = Group().load({"name": sheet["group"]}) if "group" in sheet and sheet["group"] != "None" else None
embed_flag = "embed" in request.GET
likes = sheet.get("likes", [])
like_count = len(likes)
viewer_is_liker = request.user.id in likes
return render_to_response('sheets.html', {"sheetJSON": json.dumps(sheet),
"sheet": sheet,
"sheet_class": sheet_class,
"can_edit": can_edit_flag,
"can_add": can_add_flag,
"title": sheet["title"],
"author": author,
"is_owner": request.user.id == sheet["owner"],
"is_public": sheet["status"] == "public",
"owner_groups": owner_groups,
"sheet_group": sheet_group,
"like_count": like_count,
"viewer_is_liker": viewer_is_liker,
"current_url": request.get_full_path,
}, RequestContext(request))
def view_visual_sheet(request, sheet_id):
"""
View the sheet with sheet_id.
"""
sheet = get_sheet(sheet_id)
if "error" in sheet:
return HttpResponse(sheet["error"])
sheet["sources"] = annotate_user_links(sheet["sources"])
# Count this as a view
db.sheets.update({"id": int(sheet_id)}, {"$inc": {"views": 1}})
try:
owner = User.objects.get(id=sheet["owner"])
author = owner.first_name + " " + owner.last_name
owner_groups = get_user_groups(request.user) if sheet["owner"] == request.user.id else None
except User.DoesNotExist:
author = "Someone Mysterious"
owner_groups = None
sheet_class = make_sheet_class_string(sheet)
can_edit_flag = can_edit(request.user, sheet)
can_add_flag = can_add(request.user, sheet)
sheet_group = Group().load({"name": sheet["group"]}) if "group" in sheet and sheet["group"] != "None" else None
embed_flag = "embed" in request.GET
likes = sheet.get("likes", [])
like_count = len(likes)
viewer_is_liker = request.user.id in likes
return render_to_response('sheets_visual.html',{"sheetJSON": json.dumps(sheet),
"sheet": sheet,
"sheet_class": sheet_class,
"can_edit": can_edit_flag,
"can_add": can_add_flag,
"title": sheet["title"],
"author": author,
"is_owner": request.user.id == sheet["owner"],
"is_public": sheet["status"] == "public",
"owner_groups": owner_groups,
"sheet_group": sheet_group,
"like_count": like_count,
"viewer_is_liker": viewer_is_liker,
"current_url": request.get_full_path,
}, RequestContext(request))
def delete_sheet_api(request, sheet_id):
"""
Deletes sheet with id, only if the requester is the sheet owner.
"""
id = int(sheet_id)
sheet = db.sheets.find_one({"id": id})
if not sheet:
return jsonResponse({"error": "Sheet %d not found." % id})
if request.user.id != sheet["owner"]:
return jsonResponse({"error": "Only the sheet owner may delete a sheet."})
db.sheets.remove({"id": id})
return jsonResponse({"status": "ok"})
def sheet_tag_counts(query):
"""
Returns tags ordered by count for sheets matching query.
"""
tags = db.sheets.aggregate([
{"$match": query },
{"$unwind": "$tags"},
{"$group": {"_id": "$tags", "count": {"$sum": 1}}},
{"$sort": SON([("count", -1), ("_id", -1)])},
{"$project": { "_id": 0, "tag": "$_id", "count": "$count" }}
])
return tags["result"]
def order_tags_for_user(tag_counts, uid):
"""
Returns of list of tag/count dicts order according to user's preference,
Adds empty tags if any appear in user's sort list but not in tags passed in
"""
profile = UserProfile(id=uid)
tag_order = getattr(profile, "tag_order", None)
if tag_order:
empty_tags = tag_order[:]
tags = [tag_count["tag"] for tag_count in tag_counts]
empty_tags = [tag for tag in tag_order if tag not in tags]
for tag in empty_tags:
tag_counts.append({"tag": tag, "count": 0})
try:
tag_counts = sorted(tag_counts, key=lambda x: tag_order.index(x["tag"]))
except:
pass
return tag_counts
def recent_public_tags(days=14, ntags=10):
"""
Returns list of tag/counts on public sheets modified in the last 'days'.
"""
cutoff = datetime.now() - timedelta(days=days)
query = {"status": "public", "dateModified": { "$gt": cutoff.isoformat() } }
tags = sheet_tag_counts(query)[:ntags]
return tags
def sheets_list(request, type=None):
"""
List of all public/your/all sheets
either as a full page or as an HTML fragment
"""
if not type:
# Sheet Splash page
if request.flavour == "mobile":
return s2_sheets(request)
query = {"status": "public"}
public = db.sheets.find(query).sort([["dateModified", -1]]).limit(32)
public_tags = recent_public_tags()
if request.user.is_authenticated():
query = {"owner": request.user.id}
your = db.sheets.find(query).sort([["dateModified", -1]]).limit(3)
your_tags = sheet_tag_counts(query)
your_tags = order_tags_for_user(your_tags, request.user.id)
collapse = your.count() > 3
else:
your = your_tags = collapse = None
return render_to_response('sheets_splash.html',
{
"public_sheets": public,
"public_tags": public_tags,
"your_sheets": your,
"your_tags": your_tags,
"collapse_private": collapse,
"groups": get_user_groups(request.user)
},
RequestContext(request))
response = { "status": 0 }
if type == "public":
query = {"status": "public"}
response["title"] = "Public Source Sheets"
response["public"] = True
tags = recent_public_tags()
elif type == "private":
query = {"owner": request.user.id or -1 }
response["title"] = "Your Source Sheets"
response["groups"] = get_user_groups(request.user)
tags = sheet_tag_counts(query)
tags = order_tags_for_user(tags, request.user.id)
elif type == "allz":
query = {}
response["title"] = "All Source Sheets"
response["public"] = True
tags = []
sheets = db.sheets.find(query).sort([["dateModified", -1]])
if "fragment" in request.GET:
return render_to_response('elements/sheet_table.html', {"sheets": sheets})
response["sheets"] = sheets
response["tags"] = tags
return render_to_response('sheets_list.html', response, RequestContext(request))
def partner_page(request, partner):
"""
Views the partner page for 'partner' which lists sheets in the partner group.
"""
partner = partner.replace("-", " ").replace("_", " ")
group = Group().load({"name": partner})
if not group:
raise Http404
if request.user.is_authenticated() and group.name in [g.name for g in request.user.groups.all()]:
in_group = True
query = {"status": {"$in": ["unlisted","public"]}, "group": group.name}
else:
in_group = False
query = {"status": "public", "group": group.name}
sheets = db.sheets.find(query).sort([["title", 1]])
tags = sheet_tag_counts(query)
return render_to_response('sheets_list.html', {"sheets": sheets,
"tags": tags,
"status": "unlisted",
"group": group,
"in_group": in_group,
"title": "%s on Sefaria" % group.name,
}, RequestContext(request))
def groups_page(request):
groups = GroupSet(sort=[["name", 1]])
return render_to_response("groups.html",
{"groups": groups},
RequestContext(request))
@staff_member_required
def groups_api(request):
j = request.POST.get("json")
if not j:
return jsonResponse({"error": "No JSON given in post data."})
group = json.loads(j)
if request.method == "POST":
existing = GroupSet({"name": group["name"]})
if len(existing):
existing.update(group)
existing.save()
else:
Group(group).save()
DjangoGroup.objects.create(name=group["name"])
return jsonResponse({"status": "ok"})
elif request.method == "DELETE":
GroupSet(group).delete()
return jsonResponse({"status": "ok"})
else:
return jsonResponse({"error": "Unsupported HTTP method."})
def sheet_stats(request):
pass
def sheets_tags_list(request):
"""
View public sheets organized by tags.
"""
tags_list = make_tag_list()
return render_to_response('sheet_tags.html', {"tags_list": tags_list, }, RequestContext(request))
def sheets_tag(request, tag, public=True, group=None):
"""
View sheets for a particular tag.
"""
if public:
if request.flavour == "mobile":
return s2_sheets_by_tag(request, tag)
sheets = get_sheets_by_tag(tag)
elif group:
sheets = get_sheets_by_tag(tag, group=group)
else:
sheets = get_sheets_by_tag(tag, uid=request.user.id)
in_group = request.user.is_authenticated() and group in [g.name for g in request.user.groups.all()]
groupCover = Group().load({"name": group}).coverUrl if Group().load({"name": group}) else None
return render_to_response('tag.html', {
"tag": tag,
"sheets": sheets,
"public": public,
"group": group,
"groupCover": groupCover,
"in_group": in_group,
}, RequestContext(request))
return render_to_response('sheet_tags.html', {"tags_list": tags_list, }, RequestContext(request))
@login_required
def private_sheets_tag(request, tag):
"""
Wrapper for sheet_tag for user tags
"""
return sheets_tag(request, tag, public=False)
def partner_sheets_tag(request, partner, tag):
"""
Wrapper for sheet_tag for partner tags
"""
group = partner.replace("_", " ")
return sheets_tag(request, tag, public=False, group=group)
def sheet_list_api(request):
"""
API for listing available sheets
"""
if request.method == "GET":
return jsonResponse(sheet_list(), callback=request.GET.get("callback", None))
# Save a sheet
if request.method == "POST":
if not request.user.is_authenticated():
return jsonResponse({"error": "You must be logged in to save."})
j = request.POST.get("json")
if not j:
return jsonResponse({"error": "No JSON given in post data."})
sheet = json.loads(j)
if "id" in sheet:
existing = get_sheet(sheet["id"])
if "error" not in existing and \
not can_edit(request.user, existing) and \
not can_add(request.user, existing):
return jsonResponse({"error": "You don't have permission to edit this sheet."})
responseSheet = save_sheet(sheet, request.user.id)
if "rebuild" in responseSheet and responseSheet["rebuild"]:
# Don't bother adding user links if this data won't be used to rebuild the sheet
responseSheet["sources"] = annotate_user_links(responseSheet["sources"])
return jsonResponse(responseSheet)
def user_sheet_list_api(request, user_id):
"""
API for listing the sheets that belong to user_id.
"""
if int(user_id) != request.user.id:
return jsonResponse({"error": "You are not authorized to view that."})
return jsonResponse(sheet_list(user_id), callback=request.GET.get("callback", None))
def sheet_api(request, sheet_id):
"""
API for accessing and individual sheet.
"""
if request.method == "GET":
sheet = get_sheet(int(sheet_id))
return jsonResponse(sheet, callback=request.GET.get("callback", None))
if request.method == "POST":
return jsonResponse({"error": "TODO - save to sheet by id"})
def check_sheet_modified_api(request, sheet_id, timestamp):
"""
Check if sheet_id has been modified since timestamp.
If modified, return the new sheet.
"""
sheet_id = int(sheet_id)
callback=request.GET.get("callback", None)
last_mod = get_last_updated_time(sheet_id)
if not last_mod:
return jsonResponse({"error": "Couldn't find last modified time."}, callback)
if timestamp >= last_mod:
return jsonResponse({"modified": False}, callback)
sheet = get_sheet(sheet_id)
if "error" in sheet:
return jsonResponse(sheet, callback)
sheet["modified"] = True
sheet["sources"] = annotate_user_links(sheet["sources"])
return jsonResponse(sheet, callback)
def add_source_to_sheet_api(request, sheet_id):
"""
API to add a fully formed source (posted as JSON) to sheet_id.
"""
source = json.loads(request.POST.get("source"))
if not source:
return jsonResponse({"error": "No source to copy given."})
return jsonResponse(add_source_to_sheet(int(sheet_id), source))
def copy_source_to_sheet_api(request, sheet_id):
"""
API to copy a source from one sheet to another.
"""
copy_sheet = request.POST.get("sheet")
copy_source = request.POST.get("source")
if not copy_sheet and copy_source:
return jsonResponse({"error": "Need both a sheet and source number to copy."})
return jsonResponse(copy_source_to_sheet(int(sheet_id), int(copy_sheet), int(copy_source)))
def add_ref_to_sheet_api(request, sheet_id):
"""
API to add a source to a sheet using only a ref.
"""
ref = request.POST.get("ref")
if not ref:
return jsonResponse({"error": "No ref given in post data."})
return jsonResponse(add_ref_to_sheet(int(sheet_id), ref))
@login_required
def update_sheet_tags_api(request, sheet_id):
"""
API to update tags for sheet_id.
"""
tags = json.loads(request.POST.get("tags"))
return jsonResponse(update_sheet_tags(int(sheet_id), tags))
def visual_sheet_api(request, sheet_id):
"""
API for visual source sheet layout
"""
if not request.user.is_authenticated():
return {"error": "You must be logged in to save a sheet layout."}
if request.method != "POST":
return jsonResponse({"error": "Unsupported HTTP method."})
visualNodes = json.loads(request.POST.get("visualNodes"))
zoomLevel = json.loads(request.POST.get("zoom"))
add_visual_data(int(sheet_id), visualNodes, zoomLevel)
return jsonResponse({"status": "ok"})
def like_sheet_api(request, sheet_id):
"""
API to like sheet_id.
"""
if not request.user.is_authenticated():
return {"error": "You must be logged in to like sheets."}
if request.method != "POST":
return jsonResponse({"error": "Unsupported HTTP method."})
add_like_to_sheet(int(sheet_id), request.user.id)
return jsonResponse({"status": "ok"})
def unlike_sheet_api(request, sheet_id):
"""
API to unlike sheet_id.
"""
if not request.user.is_authenticated():
return jsonResponse({"error": "You must be logged in to like sheets."})
if request.method != "POST":
return jsonResponse({"error": "Unsupported HTTP method."})
remove_like_from_sheet(int(sheet_id), request.user.id)
return jsonResponse({"status": "ok"})
def sheet_likers_api(request, sheet_id):
"""
API to retrieve the list of people who like sheet_id.
"""
response = {"likers": likers_list_for_sheet(sheet_id)}
return jsonResponse(response, callback=request.GET.get("callback", None))
def tag_list_api(request):
"""
API to retrieve the list of public tags ordered by count.
"""
response = sheet_tag_counts({"status": "public"})
response = jsonResponse(response, callback=request.GET.get("callback", None))
response["Cache-Control"] = "max-age=3600"
return response
def trending_tags_api(request):
"""
API to retrieve the list of peopke who like sheet_id.
"""
response = recent_public_tags(days=14)
response = jsonResponse(response, callback=request.GET.get("callback", None))
response["Cache-Control"] = "max-age=3600"
return response
def sheets_by_tag_api(request, tag):
"""
API to retrieve the list of peopke who like sheet_id.
"""
sheets = get_sheets_by_tag(tag, public=True)
sheets = [{"title": s["title"], "id": s["id"], "owner": s["owner"], "views": s["views"]} for s in sheets]
for sheet in sheets:
profile = UserProfile(id=sheet["owner"])
sheet["ownerName"] = profile.full_name
sheet["ownerImageUrl"] = profile.gravatar_url_small
response = {"tag": tag, "sheets": sheets}
response = jsonResponse(response, callback=request.GET.get("callback", None))
response["Cache-Control"] = "max-age=3600"
return response
@login_required
def make_sheet_from_text_api(request, ref, sources=None):
"""
API to generate a sheet from a ref with optional sources.
"""
sources = sources.replace("_", " ").split("+") if sources else None
sheet = make_sheet_from_text(ref, sources=sources, uid=request.user.id, generatedBy=None, title=None)
return redirect("/sheets/%d" % sheet["id"])