当前位置: 首页>>代码示例>>Python>>正文


Python APIUtilities._api_failure方法代码示例

本文整理汇总了Python中oc_platform.APIUtilities._api_failure方法的典型用法代码示例。如果您正苦于以下问题:Python APIUtilities._api_failure方法的具体用法?Python APIUtilities._api_failure怎么用?Python APIUtilities._api_failure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在oc_platform.APIUtilities的用法示例。


在下文中一共展示了APIUtilities._api_failure方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: suggest_resources

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
def suggest_resources(request, section_item_id):
    try:
        section_item = SectionItem.objects.get(pk=section_item_id)
    except:
        return APIUtilities._api_not_found()

    if section_item.content:
        if not section_item.content.parent:
            context = {
                'message': 'To suggest resources, the objective needs to be linked ' +
                    'a standard.'
            }
            return APIUtilities._api_failure(context)
    else:
        context = {
            'message': 'To suggest resources, this item needs to be an objective.'
        }
        return APIUtilities._api_failure(context)

    from meta.models import TagMapping
    mappings = TagMapping.objects.filter(from_node=section_item.content.parent)

    from oer.models import Resource as OEResource

    #resources = OEResource.objects.none()
    tags = []
    for mapping in mappings:
        tags.append(mapping.to_node)

    resources = OEResource.objects.filter(tags__in=tags)

    serialized_resources = {}
    for resource in resources:
        serialized_resources[resource.id] = {
            'id': resource.id,
            'url': reverse(
                'read', kwargs={
                    'resource_id': resource.id,
                    'resource_slug': resource.slug
                }
            ),
            'title': resource.title,
            'user': resource.user.get_full_name(),
            'user_url': reverse('user:user_profile', kwargs={
                'username': resource.user.username }),
            'description': resource.description,
            'thumbnail': settings.MEDIA_URL + resource.image.name,
       }

    context = {
        'resources': serialized_resources
    }
    return APIUtilities.success(context)
开发者ID:varunarora,项目名称:OC,代码行数:55,代码来源:views.py

示例2: add_member

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
def add_member(request, project_id, user_id):
    if not request.user.is_authenticated():
        context = {
            'title': 'You are not logged in',
            'message': 'You need to be logged in to add a member to the' +
                       'project.'
        }
        return APIUtilities._api_failure(context)

    project = Project.objects.get(pk=project_id)

    if request.user not in project.admins.all():
        context = {
            'title': 'You are not the administrator',
            'message': 'You must be an administrator of the project to add a'
            + 'member'
        }
        return APIUtilities._api_failure(context)

    from django.contrib.auth.models import User
    user = User.objects.get(pk=user_id)

    if Membership.objects.filter(user=user, project=project).count() != 0:
        context = {
            'title': 'Member is already added',
            'message': 'This member is already a part of the project.'
        }
        return APIUtilities._api_failure(context)        

    try:
        add_user_to_project(user, project)

        # Prepare (serialize) user object be sent through the response.
        context = {
            'user': {
                'id': user.id,
                'name': user.get_full_name(),
                'username': user.username,
                'profile_pic': settings.MEDIA_URL + user.get_profile().profile_pic.name,
                'project_id': project.id
            }
        }

        return APIUtilities._api_success(context)

    except:
        context = {
            'title': 'Cannot add new member',
            'message': 'We failed to add this member to this project. We '
            + 'apologize for the inconvenience. Visit our Help center to look '
            + 'for a solution.'
        }
        return APIUtilities._api_failure(context)
开发者ID:varunarora,项目名称:OC,代码行数:55,代码来源:views.py

示例3: remove_admin

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
def remove_admin(request, project_id, user_id):
    if not request.user.is_authenticated():
        context = {
            'title': 'You are not logged in',
            'message': 'You need to be logged in to remove an administrator ' +
                       'from the group.'
        }
        return APIUtilities._api_failure(context)

    project = Project.objects.get(pk=project_id)

    if request.user not in project.admins.all():
        context = {
            'title': 'You are not the administrator',
            'message': 'You must be an administrator of the group to remove '
            + 'another administrator'
        }
        return APIUtilities._api_failure(context)

    from django.contrib.auth.models import User
    user = User.objects.get(pk=user_id)

    # Check if the user being requested to be added is a current member.
    if user not in project.admins.all():
        context = {
            'title': user.username + ' is not an administrator',
            'message': ('The user who you wish to remove as an administrator '
            + 'is not an administrator. In order to remove %s as an '
            + 'administrator, the user must first be be assigned as an '
            + 'administrator') % user.username
        }
        return APIUtilities._api_failure(context)

    try:
        # Add the user to the project members.
        project.admins.remove(user)
        project.save()

        return APIUtilities._api_success()

    except:
        context = {
            'title': 'Cannot remove the administrator',
            'message': 'We failed to remove this administrator from this '
            + 'project. We apologize for the inconvenience. Visit our Help '
            + 'center to look for a solution.'
        }
        return APIUtilities._api_failure(context)
开发者ID:varunarora,项目名称:OC,代码行数:50,代码来源:views.py

示例4: post_comment

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
def post_comment(request):
    response = {'status': 'error'}

    if request.method == "POST":
        try:
            comment = create_comment(request, request.POST)
            if comment:
                comment_ct = ContentType.objects.get_for_model(Comment)

                import datetime
                serialized_comment = {
                    'user_id': comment.user.id,
                    'name': str(comment.user.get_full_name()),
                    'username': comment.user.username,
                    'created': datetime.datetime.strftime(comment.created, '%b. %d, %Y, %I:%M %P'),
                    'profile_pic': settings.MEDIA_URL + comment.user.get_profile().profile_pic.name,
                    'body': comment.body_markdown_html,
                    'content_type': comment_ct.id,
                    'id': comment.id
                }

                response['status'] = 'success'
                response['message'] = serialized_comment

                return HttpResponse(
                    json.dumps(response), 200, content_type="application/json")
        except:
            return APIUtilities._api_failure()

    return HttpResponse(
        json.dumps(response), 401, content_type="application/json")
开发者ID:varunarora,项目名称:OC,代码行数:33,代码来源:views.py

示例5: request_invite

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
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()
开发者ID:varunarora,项目名称:OC,代码行数:37,代码来源:views.py

示例6: cast_vote

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
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()
开发者ID:varunarora,项目名称:OC,代码行数:36,代码来源:views.py

示例7: get_resource_vote_count

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
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()
开发者ID:varunarora,项目名称:OC,代码行数:37,代码来源:views.py

示例8: delete_comment

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
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()    
开发者ID:varunarora,项目名称:OC,代码行数:31,代码来源:views.py

示例9: reposition_cover_picture

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
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)    
开发者ID:varunarora,项目名称:OC,代码行数:27,代码来源:views.py

示例10: get_licenses

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
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)        
开发者ID:varunarora,项目名称:OC,代码行数:27,代码来源:views.py

示例11: update_item

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
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()
开发者ID:varunarora,项目名称:OC,代码行数:60,代码来源:views.py

示例12: post_comment_reference

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
def post_comment_reference(request):
    if request.method == "POST":
        try:
            from interactions.models import CommentReference
            from django.contrib.contenttypes.models import ContentType

            # Get comment reference owner.
            owner_content_type = ContentType.objects.get(pk=request.POST.get('owner_type'))

            # Create a comment reference to no comment.
            new_comment_reference = CommentReference(
                reference=request.POST.get('reference'),
                owner_id=request.POST.get('owner_id'),
                owner_type=owner_content_type
            )
            new_comment_reference.save()

            # Create a new comment.
            comment_reference_content_type = ContentType.objects.get_for_model(CommentReference)

            modified_request = request.POST.copy()
            modified_request.setdefault('parent_type', comment_reference_content_type.id)
            modified_request.setdefault('parent_id', new_comment_reference.id)

            new_comment = create_comment(request, modified_request)

            # Map the comment to the reference.
            new_comment_reference.comment = new_comment
            new_comment_reference.save()

            import datetime
            context = {
                'username': new_comment.user.username,
                'name': new_comment.user.get_full_name(),
                'created': datetime.datetime.strftime(new_comment.created, '%b. %d, %Y, %I:%M %P'),
                'profile_pic': settings.MEDIA_URL + new_comment.user.get_profile().profile_pic.name,
                'body': new_comment.body_markdown_html,
                'id': new_comment.id        
            }
            return APIUtilities._api_success(context)

        except:
            return APIUtilities._api_failure()
    else:
        return APIUtilities._api_failure()
开发者ID:varunarora,项目名称:OC,代码行数:47,代码来源:views.py

示例13: remove_member

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
def remove_member(request, project_id, user_id):
    if not request.user.is_authenticated():
        context = {
            'title': 'You are not logged in',
            'message': 'You need to be logged in to remove a member from the' +
                       'group.'
        }
        return APIUtilities._api_failure(context)

    project = Project.objects.get(pk=project_id)

    if request.user not in project.admins.all():
        context = {
            'title': 'You are not the administrator',
            'message': 'You must be an administrator of the group to remove a'
            + 'member'
        }
        return APIUtilities._api_failure(context)

    from django.contrib.auth.models import User
    user = User.objects.get(pk=user_id)

    try:
        # Remove the user from the project members and admins.
        member = Membership.objects.get(user=user, project=project)
        member.delete()

        project.admins.remove(user)
        project.save()

        return APIUtilities._api_success()

    except:
        context = {
            'title': 'Cannot remove the member',
            'message': 'We failed to remove the member from this project. We '
            + 'apologize for the inconvenience. Visit our Help center to look '
            + 'for a solution.'
        }
        return APIUtilities._api_failure(context)
开发者ID:varunarora,项目名称:OC,代码行数:42,代码来源:views.py

示例14: decline_request

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
def decline_request(request, request_id):
    try:
        membership_request = Membership.objects.get(pk=request_id)
        membership_request.delete()

        return APIUtilities._api_success()
    except:
        context = {
            'title': 'Could not accept the request.',
            'message': 'We failed to make a decline the invite request into the group '
            + 'due to an internal problem. Please contact us if this problem persists.'
        }
        return APIUtilities._api_failure(context)
开发者ID:varunarora,项目名称:OC,代码行数:15,代码来源:views.py

示例15: favorite_resource

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_failure [as 别名]
def favorite_resource(request, favorite_type, parent_id):
    # Check if the favorite already exists
    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)

        existing_favorite = Favorite.objects.get(
            user=request.user, parent_id=parent_id, parent_type=(
                resource_ct.id if favorite_type == 'resource' else collection_ct.id))

        existing_favorite.delete()

        response = {'status': 'unfavorite success'}
        return HttpResponse(
            json.dumps(response), 200, content_type="application/json")

    except Favorite.DoesNotExist:
        try:
            # Create the favorite and save it
            new_favorite = Favorite(user=request.user,
                parent_id=parent_id, parent_type=(
                    resource_ct if favorite_type == 'resource' else collection_ct))
            new_favorite.save()

            if (favorite_type == 'resource' and new_favorite.parent.user != request.user) or (
                favorite_type == 'collection' and new_favorite.parent.creator != request.user):
                # Send out a notification to the person who originally made
                # the resource.
                from interactions.tasks import favorite as favorite_task
                favoriting_task = favorite_task.delay(request.get_host(), new_favorite.id)
 
            return APIUtilities._api_success()
        except:
            return APIUtilities._api_failure()

    except TypeError:
        return APIUtilities._api_failure()
开发者ID:varunarora,项目名称:OC,代码行数:41,代码来源:views.py


注:本文中的oc_platform.APIUtilities._api_failure方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。