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


Python APIUtilities._api_success方法代码示例

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


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

示例1: delete_comment

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [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

示例2: get_favorite_state

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

示例3: cast_vote

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [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

示例4: reposition_cover_picture

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [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

示例5: get_resource_vote_count

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [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

示例6: get_standard

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [as 别名]
def get_standard(request, category_id):
    from meta.models import Category
    category = Category.objects.get(pk=category_id)

    # Get all categories in this standard.
    subjects = Category.objects.filter(parent=category)

    # Get all the subcategories in the currently selected subject.
    grades = Category.objects.filter(parent=subjects[0]).order_by('position')

    context = {
        'subjects': [],
        'grades': [],
    }

    for subject in subjects:
        context['subjects'].append({
            'title': subject.title,
            'id': subject.id
        })

    for grade in grades:
        context['grades'].append({
            'title': grade.title,
            'id': grade.id
        })

    return APIUtilities._api_success(context)
开发者ID:varunarora,项目名称:OC,代码行数:30,代码来源:views.py

示例7: load

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [as 别名]
    def load(self):
        category = Category.objects.get(pk=self.current_category_id)

        all_raw_resources = []
        all_resources = []

        category_resources = Resource.objects.filter(categories=category).order_by('-created')
        tagged_resources = Resource.objects.filter(tags__in=category.tags.all()).filter(
            tags__in=Tag.objects.filter(category=self.resource_type_tag_category)).order_by('-created')
        category_resource_count = category_resources.count()

        def categorize_resource(r):
            r.category = category
            return r

        if self.resource_count <= category_resource_count:
            all_raw_resources += list(OrderedDict.fromkeys(
                category_resources[(self.resource_count):] | map(categorize_resource, tagged_resources)))
        else:
            all_raw_resources += map(categorize_resource, tagged_resources[int(self.resource_count) - category_resource_count:])

        for resource in all_raw_resources:
            try:
                self.build_browse_resource(resource)
                all_resources.append(resource)
            except Tag.DoesNotExist:
                pass

        serialized_resources = serialize(all_resources)

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

示例8: get_child_tags_from_category

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [as 别名]
def get_child_tags_from_category(request, category_id):
    from meta.models import Category, TagCategory
    category = Category.objects.get(pk=category_id)

    # Get all tags in order that belong to this category.
    from meta.CategoryUtilities import TreeBuilder
    tag_category_builder = TreeBuilder('tag_category')
    (browse_tree, flattened_tree) = tag_category_builder.build_tree(
        {'root': [TagCategory.objects.get(title='Standards')]}, [])

    tags = category.tags.filter(category__in=flattened_tree).order_by('position')

    serialized_tags = {}
    for tag in tags:
        serialized_tags[tag.id] = {
            'id': tag.id,
            'title': tag.title,
            'description': tag.description,            
            'position': tag.position,
            'url': reverse(
                'meta:standard', kwargs={
                    'tag_title': tag.title
            })
        }

    context = {
        'tags': serialized_tags
    }
    return APIUtilities._api_success(context)
开发者ID:varunarora,项目名称:OC,代码行数:31,代码来源:views.py

示例9: get_standards

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [as 别名]
def get_standards(request):
    from meta.models import Category
    root_category = Category.objects.get(title='Standards')

    # Get all categories whose parent is root_category.
    child_categories = Category.objects.filter(parent=root_category).order_by(
        'position')

    serialized_standards = {}
    for standard in child_categories:
        serialized_standards[standard.id] = {
            'id': standard.id,
            'title': standard.title,
            'slug': standard.slug,
            'position': standard.position
        }
        subjects = Category.objects.filter(parent=standard).order_by('position')

        serialized_subjects = {}
        for subject in subjects:
            serialized_subjects[subject.id] = {
                'id': subject.id,
                'title': subject.title,
                'slug': subject.slug,
                'position': subject.position
            }
        serialized_standards[standard.id]['subjects'] = serialized_subjects

    context = {
        'standards': serialized_standards
    }
    return APIUtilities._api_success(context)
开发者ID:varunarora,项目名称:OC,代码行数:34,代码来源:views.py

示例10: request_invite

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [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

示例11: get_licenses

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [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

示例12: cast_resource_revision_vote

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [as 别名]
def cast_resource_revision_vote(request, resource_id, revision_id, positive):
    try:
        from oer.models import Resource, ResourceRevision
        Resource.objects.get(pk=resource_id)
        revision = ResourceRevision.objects.get(pk=revision_id)
    except:
        return APIUtilities._api_not_found()

    # Check if the vote already exists.
    try:
        from django.contrib.contenttypes.models import ContentType
        resource_revision_ct = ContentType.objects.get_for_model(ResourceRevision)

        existing_vote = Vote.objects.get(
            user=request.user, parent_type=resource_revision_ct, parent_id=revision.id,
            positive=positive)

        existing_vote.delete()

        context = {'action': 'unvote'}
        return APIUtilities._api_success(context)

    except Vote.DoesNotExist:
        try:
            # Create the vote and save it
            new_vote = Vote()
            new_vote.user = request.user
            new_vote.positive = positive
            new_vote.parent = revision
            new_vote.save()

            if positive and revision.user != request.user:
                Vote.resource_revision_vote_casted.send(sender="ResourceRevision", vote=new_vote)

            return APIUtilities._api_success()
        except:
            context = {
                'title': 'Unable to cast revision vote',
                'message': 'We apologize as we failed to cast your vote on this resource revision. '
                + 'Please contact us if the problem persists.'
            }
            return APIUtilities._api_failure(context)
开发者ID:varunarora,项目名称:OC,代码行数:44,代码来源:views.py

示例13: add_member

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [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

示例14: decline_request

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [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: remove_admin

# 需要导入模块: from oc_platform import APIUtilities [as 别名]
# 或者: from oc_platform.APIUtilities import _api_success [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


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