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)
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)
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)
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")
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()
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()
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()
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()
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)
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)
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()
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()
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)
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)
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()
def delete_category(request, project_id, category_id): try: category = GroupCategory.objects.get(pk=category_id) project = Project.objects.get(pk=project_id) except: return APIUtilities._api_not_found() if not request.user.is_authenticated() or request.user not in project.admins.all(): context = { 'title': 'You are not authorized to delete the category', 'message': 'You need to be logged in as an administrator of the group ' + 'to remove a member from the group.' } return APIUtilities._api_unauthorized_failure(context) group_categories = GroupCategory.objects.filter(parent=project).count() if group_categories == 1: context = { 'title': 'Could not delete the only category.', 'message': 'We failed to delete the category you marked for deletion ' + 'as this is the only category in the group and you need to have atleast ' + 'one category in a group. Create another category before you delete this one.' } return APIUtilities._api_failure(context) try: # Delete all the posts in this category. Comment.objects.filter(category=category).delete() category.delete() return APIUtilities._api_success() except: context = { 'title': 'Could not delete the category.', 'message': 'We failed to delete the category of this group ' + 'due to an internal problem. Please contact us if this problem persists.' } return APIUtilities._api_failure(context)
def add_item_to_section(request): section_id = request.POST.get('id', None) item_id = request.POST.get('item_id', None) try: section = Section.objects.get(pk=section_id) item = SectionItem.objects.get(pk=item_id) except: return APIUtilities._api_not_found() try: section.items.add(item) return APIUtilities.success() except: return APIUtilities._api_failure()
def accept_request(request, request_id): try: membership_request = Membership.objects.get(pk=request_id) membership_request.confirmed = True membership_request.save() # Generate notification for user just accepted into the project to notify. Membership.invite_request_accepted.send( sender="Projects", membership_id=membership_request.id, request=request) return APIUtilities._api_success() except: context = { 'title': 'Could not accept the request.', 'message': 'We failed to make a accept the invite request into the group ' + 'due to an internal problem. Please contact us if this problem persists.' } return APIUtilities._api_failure(context)
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)
def get_project_visibility(request, project_id): try: project = Project.objects.get(pk=project_id) if not request.user: return APIUtilities._api_unauthorized_failure() context = { 'visibility': project.visibility } return APIUtilities._api_success(context) except: context = { 'title': 'Could not get the visibility of this project.', 'message': 'We failed to retrieve the visibility of ' + 'this project. Please contact us if the problem persists.' } return APIUtilities._api_failure(context)
def get_standards_tree(request): try: standards_category = Category.objects.get(title='Standards') from meta.CategoryUtilities import TreeBuilder category_builder = TreeBuilder('category_tags') (browse_tree, flattened_tree) = category_builder.build_tree( {'root': [standards_category]}, []) tree = build_standards_navigation(browse_tree) context = { 'tree': tree } return APIUtilities._api_success(context) except: context = { 'title': 'Could not load the standards list', 'message': 'We failed to load the list of standards for you. ' + 'Please contact us if the problem persists.' } return APIUtilities._api_failure(context)
def review_resource(request): if request.method == "POST": from django.core.urlresolvers import reverse from interactions.models import Review try: modified_request = request.POST.copy() modified_request.setdefault('user', request.user.id) comment = create_comment(request, modified_request) review = Review( comment=comment, rating=int(request.POST.get('stars', None)) ) review.save() import datetime serialized_review = { 'user_url': reverse('user:user_profile', kwargs={ 'username': comment.user.username }), '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, 'stars': review.rating } context = { 'review': serialized_review } return APIUtilities._api_success(context) except: context = { 'title': 'Unable to post your review', 'message': 'We apologize as we failed to post your highly useful review. ' + 'Please contact us if the problem persists.' } return APIUtilities._api_failure(context) else: return APIUtilities._api_not_found()
def copy_curriculum(request): curriculum_id = request.POST.get('curriculum_id', None) title = request.POST.get('title', None) sync = request.POST.get('sync', None) if not request.user.is_authenticated(): return APIUtilities._api_failure() from django.contrib.contenttypes.models import ContentType curriculum_content_type = ContentType.objects.get_for_model(Curriculum) unit_content_type = ContentType.objects.get_for_model(Unit) section_content_type = ContentType.objects.get_for_model(Section) sectionitem_content_type = ContentType.objects.get_for_model(SectionItem) sectionitemresources_content_type = ContentType.objects.get_for_model(SectionItemResources) resource_content_type = ContentType.objects.get_for_model(Resource) standardcategory_content_type = ContentType.objects.get_for_model(StandardCategory) try: curriculum = Curriculum.objects.get(pk=curriculum_id) except: return APIUtilities._api_not_found() def add_sections_to(parent, sections): for section in sections: new_section = Section( position=section.position, title=section.title, settings=section.settings ) new_section.save() parent.sections.add(new_section) # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=section_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=section.id, target_id=new_section.id ).save() for item in section.items.all(): new_item = SectionItem( content_type=item.content_type, content_id=item.content_id, description=item.description, meta=item.meta, position=item.position ) new_item.save() new_section.items.add(new_item) # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=sectionitem_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=item.id, target_id=new_item.id ).save() for resource_set in item.resource_sets.all(): new_item_resources = SectionItemResources( title=resource_set.title, position=resource_set.position ) new_item_resources.save() new_item.resource_sets.add(new_item_resources) # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=sectionitemresources_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=resource_set.id, target_id=new_item_resources.id ).save() for resource in resource_set.resources.all(): new_curriculum_resource = Resource( resource=resource.resource, notes=resource.notes ) new_curriculum_resource.save() new_item_resources.resources.add(new_curriculum_resource) # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=resource_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=resource.id, target_id=new_curriculum_resource.id ).save() def make_standard_category_copies(parent, original_standard_category): if original_standard_category.standard_categories.count() == 0: return else: for standard_category in original_standard_category.standard_categories.all(): new_standard_category = StandardCategory( title=standard_category.title, category=standard_category.category, ) new_standard_category.save() parent.standard_categories.add(new_standard_category) # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=standardcategory_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=standard_category.id, target_id=new_standard_category.id ).save() add_sections_to(new_standard_category, standard_category.sections.all()) return make_standard_category_copies(new_standard_category, standard_category) new_curriculum = Curriculum( title=title, user=request.user, description=curriculum.description, visibility=curriculum.visibility, grade=curriculum.grade, subject=curriculum.subject, settings=curriculum.settings, slug=curriculum.slug, synced_to=curriculum ) new_curriculum.save() # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=curriculum_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=curriculum.id, target_id=new_curriculum.id, ).save() # Add references to EXISTING textbooks. for textbook in curriculum.textbooks.all(): new_curriculum.textbooks.add(textbook) # Make a deep copy of the units. for unit in curriculum.units.all(): new_unit = Unit( title=unit.title, period=unit.period ) new_unit.save() new_curriculum.units.add(new_unit) # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=unit_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=unit.id, target_id=new_unit.id ).save() add_sections_to(new_unit, unit.sections.all()) for textbook in new_curriculum.textbooks.all(): if unit in textbook.units.all(): textbook.units.add(new_unit) # Make a deep copy of the standard categories. make_standard_category_copies(new_curriculum, curriculum) return APIUtilities.success({'url': reverse( 'curriculum:curriculum', kwargs={ 'organization_slug': request.organization.slug, 'username': new_curriculum.user.username, 'curriculum_slug': new_curriculum.slug } )})
def add_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 add an administrator to ' + '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 add an' + '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.members.all(): context = { 'title': user.username + ' is not a member', 'message': ('The user who you wish to make an administrator is not' + 'member. In order to make %s an administrator, the user must' + 'first be added to the project as a member') % user.username } return APIUtilities._api_failure(context) try: # Add the user to the project members. project.admins.add(user) project.save() # Notify the member about being assigned an admin. Membership.member_turned_admin.send( sender="Projects", project=project, user=user, request=request) # Prepare (serialize) user object be sent through the response. context = { 'user': { 'id': user.id, 'name': user.get_full_name(), 'username': user.username, 'profile_pic': user.get_profile().profile_pic.name } } return APIUtilities._api_success(context) except: context = { 'title': 'Cannot add new administrator', 'message': 'We failed to add this administrator to this group. ' + 'We apologize for the inconvenience. Visit our Help center to ' + 'look for a solution.' } return APIUtilities._api_failure(context)
def create_update_issue(request): section_item_id = request.POST.get('host_id', None) issue_state = request.POST.get('ready', None) message = request.POST.get('message', None) issue_id = request.POST.get('id', None) from django.contrib.contenttypes.models import ContentType section_item_content_type = ContentType.objects.get_for_model(SectionItem) if issue_state == 'false': try: section_item = SectionItem.objects.get(pk=section_item_id) except: return APIUtilities._api_not_found() try: # Temp. associate all bugs with me. from django.contrib.auth.models import User reporter = User.objects.get(username='******') issue = Issue( host=section_item, reporter=reporter ) issue.save() context = { 'issue': { 'id': issue.id, 'host_id': section_item.id, 'message': issue.message, } } return APIUtilities.success(context) except: return APIUtilities._api_failure() elif message: issue = Issue.objects.get(pk=issue_id) issue.message = message issue.save() context = { 'issue': { 'message': issue.message, } } return APIUtilities.success(context) else: issue = Issue.objects.get(host_id=section_item_id, host_type=section_item_content_type) issue.delete() context = { 'issue': { 'id': None, 'host_id': None, 'message': None, } } return APIUtilities.success(context)