本文整理汇总了Python中oc_platform.APIUtilities类的典型用法代码示例。如果您正苦于以下问题:Python APIUtilities类的具体用法?Python APIUtilities怎么用?Python APIUtilities使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了APIUtilities类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_mapping
def create_mapping(request):
from_id = request.POST.get('from', None)
to_id = request.POST.get('to', None)
standards_category = TagCategory.objects.get(title='Standards')
try:
from_node = Tag.objects.get(pk=from_id, category=standards_category)
to_node = Tag.objects.get(pk=to_id, category=standards_category)
except:
return APIUtilities._api_not_found()
from meta.models import TagMapping
mapping = TagMapping(from_node=from_node, to_node=to_node)
mapping.save()
serialize_mapping = {
'id': mapping.id,
'from_id': from_node.id,
'from_title': from_node.title,
'to_id': to_node.id,
'to_title': to_node.title,
}
return APIUtilities.success(serialize_mapping)
示例2: get_resource_vote_count
def get_resource_vote_count(request, resource_id):
try:
from oer.models import Resource
resource = Resource.objects.get(pk=resource_id)
except:
return APIUtilities._api_not_found()
try:
from django.contrib.contenttypes.models import ContentType
resource_ct = ContentType.objects.get_for_model(Resource)
upvotes = Vote.objects.filter(
parent_id=resource.id, parent_type=resource_ct, positive=1)
downvotes = Vote.objects.filter(
parent_id=resource.id, parent_type=resource_ct, positive=0)
user_upvoted = False
user_downvoted = False
if (request.user.is_authenticated()):
if upvotes.filter(user=request.user).count() > 0:
user_upvoted = True
if downvotes.filter(user=request.user).count() > 0:
user_downvoted = True
context = {
'upvote_count': upvotes.count(),
'downvote_count': downvotes.count(),
'user_upvoted': 'true' if user_upvoted else 'false',
'user_downvoted': 'true' if user_downvoted else 'false'
}
return APIUtilities._api_success(context)
except:
return APIUtilities._api_failure()
示例3: get_licenses
def get_licenses(request):
try:
licenses = License.objects.all()
serialized_licenses = []
for license in licenses:
serialized_license = {
'id': license.id,
'title': license.title,
'description': license.description
}
serialized_licenses.append(serialized_license)
context = {
'licenses': serialized_licenses
}
return APIUtilities._api_success(context)
except:
context = {
'title': 'Could not load the licenses list',
'message': 'We failed to load the list of standards for you. '
+ 'Please contact us if the problem persists.'
}
return APIUtilities._api_failure(context)
示例4: reposition_cover_picture
def reposition_cover_picture(request, project_id):
try:
project = Project.objects.get(pk=project_id)
except:
raise Http404
if request.user not in project.admins.all():
return APIUtilities._api_unauthorized_failure()
left = request.GET.get('left', None)
top = request.GET.get('top', None)
try:
project.cover_pic_position.top = int(top)
project.cover_pic_position.left = int(left)
project.cover_pic_position.save()
return APIUtilities._api_success()
except:
context = {
'title': 'Cannot reposition project cover picture.',
'message': 'We failed to reposition the project cover picture. Please ' +
'contact us if this problem persists.'
}
return APIUtilities._api_failure(context)
示例5: cast_vote
def cast_vote(request, comment_id, positive):
comment_ct = ContentType.objects.get_for_model(Comment)
# Check if the comment already exists
try:
existing_vote = Vote.objects.get(
parent_id=comment_id, parent_type=comment_ct,
positive=positive, user=request.user
)
existing_vote.delete()
response = {'status': 'unvote success'}
return HttpResponse(
json.dumps(response), 200, content_type="application/json")
except Vote.DoesNotExist:
try:
comment = Comment.objects.get(pk=comment_id)
# Create the vote and save it
new_vote = Vote()
new_vote.user = request.user
new_vote.positive = positive
new_vote.parent = comment
new_vote.save()
if positive:
# Send out a notification to the person who originally wrote
# the comment, if a positive vote is casted.
Vote.vote_casted.send(sender="Comments", vote=new_vote)
return APIUtilities._api_success()
except:
return APIUtilities._api_failure()
示例6: delete_comment
def delete_comment(request, comment_id):
try:
comment = Comment.objects.get(pk=comment_id)
# TODO(Varun): Same logic as duplicated from template generation to show
# delete button
from interactions.CommentUtilities import CommentUtilities
(host_type, host, root) = CommentUtilities.get_comment_root(comment)
if host_type.name == 'project':
if request.user == comment.user or request.user in host.admins.all():
# Delete all descendant elements of this comment.
delete_comment_tree(comment)
return APIUtilities._api_success()
else:
return APIUtilities._api_unauthorized_failure()
elif host_type.name == 'resource':
if request.user == comment.user or (
request.user in host.collaborators.all() or
request.user == host.creator):
delete_comment_tree(comment)
return APIUtilities._api_success()
return APIUtilities._api_failure()
except Comment.DoesNotExist:
return APIUtilities._api_not_found()
示例7: link_objective_to_standard
def link_objective_to_standard(request):
objective_id = request.POST.get('objective_id', None)
standard_id = request.POST.get('standard_id', None)
remove_objective_id = request.POST.get('remove_objective_id', None)
standards_categories = TagCategory.objects.filter(title__in=['Standards', 'Objectives'])
try:
from curriculum.models import Objective
objective = Objective.objects.get(pk=objective_id)
previous_objective = Objective.objects.get(pk=remove_objective_id)
standard = Tag.objects.get(pk=standard_id, category__in=standards_categories)
except:
return APIUtilities._api_not_found()
if objective.parent:
return APIUtilities.failure({
'standard_id': objective.parent.id,
'standard_title': objective.parent.title
})
else:
previous_objective.parent = None
previous_objective.save()
objective.parent = standard
objective.save()
objective.parent = standard
objective.save()
return APIUtilities.success()
示例8: create_section
def create_section(request):
parent_id = request.POST.get('parent_id', None)
is_unit = request.POST.get('is_unit', None)
title = request.POST.get('title', None)
section_type = request.POST.get('section_type', None)
position = request.POST.get('position', None)
settings = {
'type': section_type
}
try:
if is_unit:
parent = Unit.objects.get(pk=parent_id)
else:
parent = StandardCategory.objects.get(pk=parent_id)
except:
return APIUtilities._api_not_found()
new_section = Section(position=position, title=title, settings=settings)
new_section.save()
parent.sections.add(new_section)
context = {
'id': new_section.id
}
return APIUtilities.success(context)
示例9: add_url_to_section_item_resources
def add_url_to_section_item_resources(request):
section_item_resources_id = request.POST.get('section_item_resources_id', None)
try:
section_item_resources = SectionItemResources.objects.get(pk=section_item_resources_id)
except:
section_item_resources = create_resource_set(request, section_item_resources)
if not section_item_resources:
return APIUtilities._api_not_found()
from oer.views import new_url_from_form
new_resource = new_url_from_form(
request.user, request.POST.get('title', None),
request.POST.get('url', None))
new_objective_resource = Resource(resource=new_resource)
new_objective_resource.save()
section_item_resources.resources.add(new_objective_resource)
context = {
'resource': {
'id': new_objective_resource.id,
'url': new_resource.revision.content.url,
'title': new_resource.title,
'thumbnail': settings.MEDIA_URL + new_resource.image.name if new_resource.image else '',
}
}
return APIUtilities.success(context)
示例10: get_mappings
def get_mappings(request, standard_id):
from meta.models import TagMapping
standards_category = TagCategory.objects.get(title='Standards')
try:
standard = Tag.objects.get(pk=standard_id, category=standards_category)
except:
return APIUtilities._api_not_found()
tag_mappings = TagMapping.objects.filter(from_node=standard)
serialized_tag_mappings = []
for tag_mapping in tag_mappings:
serialized_tag_mappings.append({
'id': tag_mapping.id,
'standard': tag_mapping.to_node.id,
'standardTitle': tag_mapping.to_node.title,
'notes': tag_mapping.deviation,
})
context = {
'mappings': serialized_tag_mappings
}
return APIUtilities.success(context)
示例11: get_favorite_state
def get_favorite_state(request, favorite_type, parent_id):
try:
from oer.models import Resource, Collection
from django.contrib.contenttypes.models import ContentType
resource_ct = ContentType.objects.get_for_model(Resource)
collection_ct = ContentType.objects.get_for_model(Collection)
favorite = Favorite.objects.get(
user=request.user, parent_id=parent_id, parent_type=(
resource_ct if favorite_type == 'resource' else collection_ct))
context = {
'favorite': {
'state': 'true',
'created': str(favorite.created)
}
}
return APIUtilities._api_success(context)
except:
context = {
'favorite': {
'state': 'false'
}
}
return APIUtilities._api_success(context)
示例12: request_invite
def request_invite(request, project_id):
project = Project.objects.get(pk=project_id)
try:
# Check to see if the user already exists as a member.
Membership.objects.get(
user=request.user, project=project)
context = {
'title': 'Cannot complete the request invite.',
'message': 'We failed to make a request invite on your behalf '
+ 'because you either have already sent an invite request or are a '
+ 'current member.'
}
return APIUtilities._api_failure(context)
except Membership.DoesNotExist:
if project.visibility != 'public':
new_membership = Membership(
user=request.user, project=project, confirmed=False)
new_membership.save()
# Create a notification for the admins about this request.
Membership.new_invite_request.send(
sender="Projects", membership_id=new_membership.id, request=request)
else:
new_membership = Membership(
user=request.user, project=project, confirmed=True)
new_membership.save()
# Create a notification for the admins about this membership.
Membership.new_member.send(
sender="Projects", membership_id=new_membership.id, request=request)
return APIUtilities._api_success()
示例13: get_standard
def get_standard(request, standard_id):
try:
standard = StandardCategory.objects.get(pk=standard_id)
except:
return APIUtilities._api_not_found()
return APIUtilities.success(get_serialized_sections(standard))
示例14: get_sections
def get_sections(request, unit_id):
try:
unit = Unit.objects.get(pk=unit_id)
except:
return APIUtilities._api_not_found()
return APIUtilities.success(get_serialized_sections(unit))
示例15: update_item
def update_item(request):
item_id = request.POST.get('id', None)
description = request.POST.get('description', None)
try:
item = SectionItem.objects.get(pk=item_id)
except:
return APIUtilities._api_not_found()
try:
if description:
item.description = description
item.save()
context = { 'item': {
'id': item.id,
'description': item.description
}}
else:
new_metas = []
import re
for post_key, post_value in request.POST.items():
if 'meta' in post_key:
# Loop through the json objects.
match = re.match('meta\[(?P<num>\d+)\]\[(?P<key>.+)\]', post_key)
try:
new_metas[int(match.group('num'))][match.group('key')] = post_value
except:
new_metas.append({match.group('key'): post_value})
for new_meta in new_metas:
# If the meta object exists, update it.
try:
next(meta_item for meta_item in item.meta if meta_item['slug'] == new_meta['slug'])['body'] = new_meta['body']
except:
# If not, create it.
if not item.meta:
item.meta = []
item.meta.append({
'slug': new_meta['slug'],
'body': new_meta['body'],
'title': new_meta['title'],
'position': new_meta['position']
})
item.save()
context = {
'id': item.id,
'meta': item.meta
}
return APIUtilities.success(context)
except:
return APIUtilities._api_failure()