本文整理汇总了Python中sefaria.client.util.jsonResponse函数的典型用法代码示例。如果您正苦于以下问题:Python jsonResponse函数的具体用法?Python jsonResponse怎么用?Python jsonResponse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了jsonResponse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: groups_invite_api
def groups_invite_api(request, group_name, uid_or_email, uninvite=False):
"""
API for adding or removing group members, or group invitations
"""
if request.method != "POST":
return jsonResponse({"error": "Unsupported HTTP method."})
group = Group().load({"name": group_name})
if not group:
return jsonResponse({"error": "No group named %s." % group_name})
if request.user.id not in group.admins:
return jsonResponse({"error": "You must be a group admin to invite new members."})
user = UserProfile(email=uid_or_email)
if not user.exists():
if uninvite:
group.remove_invitation(uid_or_email)
message = "Invitation removed."
else:
group.invite_member(uid_or_email, request.user.id)
message = "Invitation sent."
else:
is_new_member = not group.is_member(user.id)
if is_new_member:
group.add_member(user.id)
from sefaria.model.notification import Notification
notification = Notification({"uid": user.id})
notification.make_group_add(adder_id=request.user.id, group_name=group_name)
notification.save()
message = "Group member added."
else:
message = "%s is already a member of this group." % user.full_name
group_content = group.contents(with_content=True, authenticated=True)
return jsonResponse({"group": group_content, "message": message})
示例2: export_to_drive
def export_to_drive(request, credential, sheet_id):
"""
Export a sheet to Google Drive.
"""
http = credential.authorize(httplib2.Http())
service = build('drive', 'v3', http=http)
sheet = get_sheet(sheet_id)
if 'error' in sheet:
return jsonResponse({'error': {'message': sheet["error"]}})
file_metadata = {
'name': strip_tags(sheet['title'].strip()),
'mimeType': 'application/vnd.google-apps.document'
}
html_string = sheet_to_html_string(sheet)
media = MediaIoBaseUpload(
StringIO(html_string),
mimetype='text/html',
resumable=True)
new_file = service.files().create(body=file_metadata,
media_body=media,
fields='webViewLink').execute()
return jsonResponse(new_file)
示例3: sheet_node_api
def sheet_node_api(request, sheet_id, node_id):
if request.method == "GET":
sheet_node = get_sheet_node(int(sheet_id),int(node_id))
return jsonResponse(sheet_node, callback=request.GET.get("callback", None))
if request.method == "POST":
return jsonResponse({"error": "Unsupported HTTP method."})
示例4: user_sheet_list_api_with_sort
def user_sheet_list_api_with_sort(request, user_id, sort_by="date", limiter=0, offset=0):
limiter = int(limiter)
offset = int(offset)
if int(user_id) != request.user.id:
return jsonResponse({"error": "You are not authorized to view that."})
return jsonResponse(user_sheets(user_id, sort_by, limit=limiter, skip=offset), callback=request.GET.get("callback", None))
示例5: file_upload
def file_upload(request, resize_image=True):
from PIL import Image
from tempfile import NamedTemporaryFile
from sefaria.s3 import HostedFile
if request.method == "POST":
MAX_FILE_MB = 2
MAX_FILE_SIZE = MAX_FILE_MB * 1024 * 1024
MAX_FILE_DIMENSIONS = (1048, 1048)
uploaded_file = request.FILES['file']
if uploaded_file.size > MAX_FILE_SIZE:
return jsonResponse({"error": "Uploaded files must be smaller than %dMB." % MAX_FILE_MB})
name, extension = os.path.splitext(uploaded_file.name)
with NamedTemporaryFile(suffix=extension) as temp_uploaded_file:
temp_uploaded_file.write(uploaded_file.read())
with NamedTemporaryFile(suffix=extension) as temp_resized_file:
image = Image.open(temp_uploaded_file)
if resize_image:
image.thumbnail(MAX_FILE_DIMENSIONS, Image.ANTIALIAS)
image.save(temp_resized_file, optimize=True, quality=70)
name, extension = os.path.splitext(temp_resized_file.name)
hosted_file = HostedFile(filepath=temp_resized_file.name, content_type=uploaded_file.content_type)
try:
url = hosted_file.upload()
return jsonResponse({"status": "success", "url": url})
except:
return jsonResponse({"error": "There was an error uploading your file."})
else:
return jsonResponse({"error": "Unsupported HTTP method."})
示例6: sheet_list_api
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)
示例7: generate_feedback
def generate_feedback(request):
data = json.loads(request.POST.get('json', {}))
fb_type = data.get('type', None)
refs = data.get('refs', None)
url = data.get('url', None)
versions = data.get('currVersions', None)
uid = data.get('uid', None)
from_email = data.get('email', None)
msg = data.get('msg', None)
if not from_email:
from_email = model.user_profile.UserProfile(id=uid).email
if fb_type == "content_issue":
to_email = "[email protected]"
subject = "Correction from website - " + ' / '.join(refs)
message_html = msg + "\n\n" + "refs: " + ' / '.join(refs) + "\n" + "versions: " + str(versions) + "\n\n" + "URL: " + url
else:
to_email = "[email protected]"
subject = "Feedback from website - " + fb_type.replace("_"," ")
message_html = msg + "\n\n" + "URL: " + url
try:
send_email(subject, message_html, from_email, to_email)
return jsonResponse({"status": "ok"})
except:
return jsonResponse({"error": _("Sorry, there was an error.")})
示例8: user_sheet_list_api
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(user_sheets(user_id), callback=request.GET.get("callback", None))
示例9: add_source_to_sheet_api
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))
示例10: add_ref_to_sheet_api
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))
示例11: add_source_to_sheet_api
def add_source_to_sheet_api(request, sheet_id):
"""
API to add a fully formed source (posted as JSON) to sheet_id.
The contents of the "source" field will be a dictionary.
The input format is similar to, but differs slightly from, the internal format for sources on source sheets.
This method reformats the source to the format expected by add_source_to_sheet().
Fields of input dictionary:
either `refs` - an array of string refs, indicating a range
or `ref` - a string ref
or `outsideText` - a string
or `outsideBiText` - a dictionary with string fields "he" and "en"
or `comment` - a string
or `media` - a URL string
If the `ref` or `refs` fields are present, the `version`, `he` or `en` fields
can further specify the origin or content of text for that ref.
"""
source = json.loads(request.POST.get("source"))
if not source:
return jsonResponse({"error": "No source to copy given."})
if "refs" in source and source["refs"]:
ref = Ref(source["refs"][0]).to(Ref(source["refs"][-1]))
source["ref"] = ref.normal()
del source["refs"]
if "ref" in source and source["ref"]:
ref = Ref(source["ref"])
source["heRef"] = ref.he_normal()
if "version" in source or "en" in source or "he" in source:
text = {}
if "en" in source:
text["en"] = source["en"]
tc = TextChunk(ref, "he", source["version"]) if source.get("versionLanguage") == "he" else TextChunk(ref, "he")
text["he"] = tc.ja().flatten_to_string()
del source["en"]
elif "he" in source:
text["he"] = source["he"]
tc = TextChunk(ref, "en", source["version"]) if source.get("versionLanguage") == "en" else TextChunk(ref, "en")
text["en"] = tc.ja().flatten_to_string()
del source["he"]
else: # "version" in source
text[source["versionLanguage"]] = TextChunk(ref, source["versionLanguage"], source["version"]).ja().flatten_to_string()
other = "he" if source["versionLanguage"] == "en" else "en"
text[other] = TextChunk(ref, other).ja().flatten_to_string()
source.pop("version", None)
source.pop("versionLanguage", None)
source["text"] = text
note = request.POST.get("note", None)
response = add_source_to_sheet(int(sheet_id), source, note=note)
return jsonResponse(response)
示例12: private_sheet_list_api
def private_sheet_list_api(request, group):
group = group.replace("-", " ").replace("_", " ")
group = Group().load({"name": group})
if not group:
raise Http404
if request.user.is_authenticated and group.is_member(request.user.id):
return jsonResponse(group_sheets(group, True), callback=request.GET.get("callback", None))
else:
return jsonResponse(group_sheets(group, False), callback=request.GET.get("callback", None))
示例13: copy_source_to_sheet_api
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)))
示例14: private_sheet_list_api
def private_sheet_list_api(request, partner):
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()]:
return jsonResponse(partner_sheets(partner, True), callback=request.GET.get("callback", None))
else:
return jsonResponse(partner_sheets(partner, False), callback=request.GET.get("callback", None))
示例15: sheet_api
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"})