def sticky_content(request, class_id, item, item_id): r""" This view handles the sticking/unsticking of content from the content list with AJAX. The first agrument is the id of the current class. This view also takes in the item type ``item``. It takes in the item_id for whatever type of content we are going to sticky. It takes in the boolean stickied. If stickied then we will unsticky the object, otherwise we will sticky the object. """ if request.method != 'GET': return HttpResponseNotAllowed(['GET']) class_object = get_object_or_404(Class, id=class_id) if not (request.user.is_superuser or request.user == class_object.author or class_object.allowed_users.filter(id=request.user.id).exists()): return HttpResponseForbidden(json.dumps({ 'result': False, 'error': _('You are not authorized to perform this action') }), mimetype="application/json" ) # At this point we know they are authorized to stick/unstick topics if item == 'exposition': obj = get_object_or_404(Exposition, id=item_id) elif item == 'note': obj = get_object_or_404(Note, id=item_id) elif item == 'example': obj = get_object_or_404(Example, id=item_id) elif item == 'video': obj = get_object_or_404(Video, id=item_id) else: return HttpResponseBadRequest(json.dumps({ 'result': False, 'error': _('Bad item type.') }), mimetype="application/json" ) context = { 'result': True, 'item': item, 'id':obj.id, 'name':obj.__unicode__() } if obj.classes_stickied_in.filter(id=class_object.id).exists(): obj.classes_stickied_in.remove(class_object) context.update({'stickied':False}) else: obj.classes_stickied_in.add(class_object) context.update({'stickied':True}) return render_to_json_response(context)
def delete_content(request, item, pk): r""" This view handles the deletion of content from the content list with AJAX The first argument is the item type ``item``. The next argument is the pk for whatever type of content we are going to delete """ if request.method != 'GET': return HttpResponseNotAllowed(['GET']) # Get the correct object if item == 'exposition': obj = get_object_or_404(Exposition, pk=pk) elif item == 'note': obj = get_object_or_404(Note, pk=pk) elif item == 'example': obj = get_object_or_404(Example, pk=pk) elif item == 'video': obj = get_object_or_404(Video, pk=pk) else: return HttpResponseBadRequest(json.dumps({ 'result': False, 'error':_('Bad item type') }), mimetype="application/json" ) # Check that the user has permission to delete the object if not (request.user.is_superuser or request.user == obj.owner): return HttpResponseForbidden(json.dumps({ 'result': False, 'error': _('You are not authorized to perform this action') }), mimetype="application/json" ) # We are authorized to perform this action obj.delete() cur_user_rate = UserRating.objects.get(user=request.user).rating # Return and report a success with the correct response variables context = { 'result': True, 'item': item, 'id':pk, 'user_rating':cur_user_rate } return render_to_json_response(context)
def report(request): r"""View for processing the report form. It only accepts AJAX requests.""" context = {} if not request.is_ajax(): return HttpResponseBadRequest() if request.method == 'POST': form = ReportForm(request.POST) # Bind the form if form.is_valid(): form.submit(request.user) context.update({ # Add the success message to the context 'success': True, }) else: context.update(form.errors) # Add errors to be processed by AJAX return render_to_json_response(context) else: return HttpResponseBadRequest()
def delete_category(request, pk): r""" Deletes the category with pk=pk and returns html for an empty category form. .. warning:: This view is designed to only be used with AJAX """ if not request.is_ajax(): # If it is not an AJAX request return an error return HttpResponseBadRequest() category_object = get_object_or_404(ClassCategory, pk=pk) class_object = category_object.parent_class category_object.delete() category_form = CategoryForm(parent_class=class_object) template = loader.get_template('web/category_form_template.html') c = RequestContext(request, {'form':category_form}) form_html = template.render(c) context = { 'category_form': form_html, 'message': _('Successfully deleted category.') } return render_to_json_response(context)
def vote(request, atom_id, item, item_id, vote_type): r"""This voting system is a refactored version of the original voting system written by Taoran.""" atom_object = get_object_or_404(Atom, id=atom_id) if item == 'exposition': content_object = get_object_or_404(Exposition, id=item_id) elif item == 'note': content_object = get_object_or_404(Note, id=item_id) elif item == 'example': content_object = get_object_or_404(Example, id=item_id) elif item == 'video': content_object = get_object_or_404(Video, id=item_id) elif item == 'topic': content_object = get_object_or_404(Topic, id=item_id) else: raise Http404 vote = content_object.vote_set.get_or_create(atom=atom_object, user=request.user)[0] if (vote.vote > 0 and vote_type == 'up') or (vote.vote < 0 and vote_type == 'down'): context = {'result': False, 'alert':'You have already voted {}.'.format(vote_type)} return render_to_json_response(context) else: delta = 0 if vote_type == 'up': if vote.vote != 0: delta = -1*vote_down_delta_rating() vote.voteUp += vote_up_delta_rating() vote.voteDown += vote_down_delta_rating() else: vote.voteUp += vote_up_delta_rating() delta += vote_up_delta_rating() elif vote_type == 'down': if vote.vote != 0: delta = -1*vote_up_delta_rating() vote.voteDown -= vote_down_delta_rating() vote.voteUp -= vote_up_delta_rating() else: vote.voteDown -= vote_down_delta_rating() delta += vote_down_delta_rating() else: raise Http404 vote.vote += delta vote.save() if item != 'topic': user_rating = UserRating.objects.get(user=content_object.owner) else: user_rating = UserRating.objects.get(user=content_object.user) user_rating.VoteUp += vote_up_delta_rating() user_rating.VoteDown -= vote_down_delta_rating() user_rating.rating += delta user_rating.save() votes = [v.vote for v in content_object.vote_set.filter(atom=atom_object)] votesUp = [v.voteUp for v in content_object.vote_set.filter(atom=atom_object)] votesDown = [v.voteDown for v in content_object.vote_set.filter(atom=atom_object)] context = { 'result':True, 'votes':sum(votes), 'votesUp': sum(votesUp), 'votesDown': sum(votesDown), 'id':content_object.id, 'item':item } # update current user rating if user_rating.user == request.user: context.update({'user_rating': user_rating.rating}) return render_to_json_response(context)
def EditClassView(request, class_id, cat_id): r"""View for editing classes.""" context = {} # The context data class_object = get_object_or_404(Class, id=class_id) # The class instance class_form_kwargs = {'user':request.user, 'instance':class_object} category_form_kwargs = {'parent_class':class_object} if cat_id: # If we are editing a category category_object = get_object_or_404(ClassCategory, pk=cat_id) category_form_kwargs.update({'instance':category_object}) if request.method == 'POST': if u'class-form' in request.POST: # class submit class_form = ClassForm(request.POST, **class_form_kwargs) context.update( process_forms( request=request, class_object=class_object, class_form=class_form ) ) elif u'category-form' in request.POST: # category submit category_form = CategoryForm(request.POST, **category_form_kwargs) context.update( process_forms( request=request, class_object=class_object, category_form=category_form ) ) else: # submit all class_form = ClassForm(request.POST, **class_form_kwargs) category_form = CategoryForm(request.POST, **category_form_kwargs) context.update( process_forms( request=request, class_object=class_object, class_form=class_form, category_form=category_form ) ) if request.is_ajax(): return render_to_json_response(context) # Only need part of context elif cat_id is not None: # Non AJAX requests aren't allowed if cat_id return HttpResponseRedirect(reverse('edit_class', args=[class_id])) else: # GET if request.is_ajax(): category_form = CategoryForm(**category_form_kwargs) # Get form template = loader.get_template('web/category_form_template.html') c = RequestContext(request, {'form':category_form}) form_html = template.render(c) # HTML for the form context.update({ 'category_form':form_html # New form html will replace the old form }) return render_to_json_response(context) # html for ne wform elif cat_id is not None: # We don't allow non ajax requests if cat_id return HttpResponseRedirect(reverse('edit_class', args=[class_id])) context.update({ # Add forms to context if not post 'class_form':ClassForm(**class_form_kwargs), 'category_form':CategoryForm(**category_form_kwargs) }) context.update( get_navbar_context() ) context.update( get_breadcrumbs(request.path, web_breadcrumb_dict) ) context.update({ 'pk':class_object.id, }) return render(request, 'web/class_edit_form.html', context)