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)
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 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 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 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 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 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()
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)
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)
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)
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)
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 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))
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))
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 create_unit(request): title = request.POST.get('title', None) textbook_id = request.POST.get('textbook_id', None) curriculum_id = request.POST.get('curriculum_id', None) # Period properties. period_type = request.POST.get('type', None) unit = request.POST.get('unit', None) begin = request.POST.get('begin', None) end = request.POST.get('end', None) period_from = request.POST.get('from', None) period_to = request.POST.get('to', None) position = request.POST.get('position', None) parent = request.POST.get('parent', None) if begin: period = { 'type': period_type, 'unit': unit, 'begin': int(begin), 'end': int(end), 'from': period_from, 'to': period_to } else: period = { 'type': period_type, 'unit': unit, 'position': int(position), 'parent': int(parent) } try: curriculum = Curriculum.objects.get(pk=curriculum_id) except: return APIUtilities._api_not_found() if textbook_id: try: textbook = Textbook.objects.get(pk=textbook_id) except: return APIUtilities._api_not_found() else: textbook = None new_unit = Unit(title=title, period=period) new_unit.save() if textbook: textbook.units.add(new_unit) curriculum.units.add(new_unit) context = { 'id': new_unit.id } return APIUtilities.success(context)
def get_curriculum(request, curriculum_id): try: curriculum = Curriculum.objects.get(pk=curriculum_id) except: return APIUtilities._api_not_found() def get_serialized_standard_children(standard): sub_standards = [] for sub_standard in standard.standard_categories.all(): sub_standards.append( get_serialized_standard_children(sub_standard)) return { 'id': standard.id, 'title': standard.title, 'standards': sub_standards } serialized_textbooks = [] serialized_units = [] serialized_standards = [] for textbook in curriculum.textbooks.all(): serialized_textbook_units = [] for unit in curriculum.units.all(): if unit in textbook.units.all(): serialized_textbook_units.append({ 'id': unit.id, 'textbook_id': textbook.id }) serialized_textbooks.append({ 'id': textbook.id, 'title': textbook.title, 'description': textbook.description, 'thumbnail': settings.MEDIA_URL + textbook.thumbnail.name, 'units': serialized_textbook_units }) for unit in curriculum.units.all(): serialized_units.append({ 'id': unit.id, 'title': unit.title, 'period': unit.period }) for standard_category in curriculum.standard_categories.all(): serialized_standards.append( get_serialized_standard_children(standard_category)) return APIUtilities.success({ 'textbooks': serialized_textbooks, 'units': serialized_units, 'standards': serialized_standards })
def delete_event(request, event_id): try: event = Event.objects.get(pk=event_id) except: return APIUtilities._api_not_found() if request.user != event.user: return APIUtilities._api_unauthorized_failure() event.delete() return APIUtilities.success()
def delete_curriculum(request, curriculum_id): try: curriculum = Curriculum.objects.get(pk=curriculum_id) except: return APIUtilities._api_not_found() if request.user != curriculum.user: return APIUtilities._api_unauthorized_failure() delete_individual_curriculum(curriculum) return APIUtilities.success()
def delete_item(request, section_item_id): try: item = SectionItem.objects.get(pk=section_item_id) except: return APIUtilities._api_not_found() if not request.user: return APIUtilities._api_unauthorized_failure() delete_individual_item(item) return APIUtilities.success()
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 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 delete_item_meta(request, section_item_id, position): try: item = SectionItem.objects.get(pk=section_item_id) except: return APIUtilities._api_not_found() meta = item.meta del meta[int(position)] item.meta = meta item.save() return APIUtilities.success()
def get_reference(request, resource_id): try: resource = Resource.objects.get(pk=resource_id) except: return APIUtilities._api_not_found() reference = resource.resource.revision.content context = dict({ 'textbook_title': reference.textbook.title, 'thumbnail': settings.MEDIA_URL + reference.textbook.thumbnail.name, }.items() + reference.scope.items()) return APIUtilities.success(context)
def delete_mapping(request): mapping_id = request.POST.get('id', None) try: from meta.models import TagMapping mapping = TagMapping.objects.get(pk=mapping_id) except: return APIUtilities._api_not_found() mapping.delete() return APIUtilities.success()
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 remove_item_from_event(request): section_item_id = request.POST.get('item_id') event_id = request.POST.get('event_id') try: from curriculum.models import SectionItem section_item = SectionItem.objects.get(pk=section_item_id) event = Event.objects.get(pk=event_id) except: return APIUtilities._api_not_found() event.items.remove(section_item) return APIUtilities.success()
def delete_section_item_resources(request, section_item_resources_id): try: section_item_resources = SectionItemResources.objects.get(pk=section_item_resources_id) except: return APIUtilities._api_not_found() for resource in section_item_resources.resources.all(): resource.delete() sections_with_resources = SectionItem.objects.get(resource_sets=section_item_resources) sections_with_resources.resource_sets.remove(section_item_resources) section_item_resources.delete() return APIUtilities.success()
def delete_section(request, section_id): try: section = Section.objects.get(pk=section_id) except: return APIUtilities._api_not_found() if not request.user: return APIUtilities._api_unauthorized_failure() for item in section.items.all(): delete_individual_item(item) section.delete() return APIUtilities.success()
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)