def update_note(request, space_url): """ Updated the current note with the POST data. UpdateNoteForm is an incomplete form that doesn't handle some properties, only the important for the note editing. """ if request.method == "GET" and request.is_ajax: note = get_object_or_404(Note, pk=request.GET['noteid']) response_data = {} response_data['title'] = note.title response_data['message'] = note.message return HttpResponse(json.dumps(response_data), mimetype="application/json") if request.method == "POST" and request.is_ajax: note = get_object_or_404(Note, pk=request.POST['noteid']) note_form = UpdateNoteForm(request.POST or None, instance=note) if note_form.is_valid(): note_form_uncommited = note_form.save(commit=False) note_form_uncommited.title = request.POST['title'] note_form_uncommited.message = request.POST['message'] note_form_uncommited.last_mod_author = request.user note_form_uncommited.save() msg = "The note has been updated." else: msg = "The form is not valid, check field(s): " + note_form.errors else: msg = "There was some error in the petition." return HttpResponse(msg)
def update_note(request, space_url): """ Updated the current note with the POST data. UpdateNoteForm is an incomplete form that doesn't handle some properties, only the important for the note editing. """ place = get_object_or_404(Space, url=space_url) if request.method == "GET" and request.is_ajax: note = get_object_or_404(Note, pk=request.GET['noteid']) ctype = ContentType.objects.get_for_model(Note) latest_comments = Comment.objects.filter(is_public=True, is_removed=False, content_type=ctype, object_pk=note.id) \ .order_by('-submit_date')[:5] form = CommentForm(target_object=note) response_data = {} response_data['title'] = note.title response_data['message'] = note.message response_data['author'] = {'name': note.author.username} response_data['comments'] = [{'username': c.user.username, 'comment': c.comment, 'submit_date': c.submit_date} for c in latest_comments] response_data["form_html"] = form.as_p() return HttpResponse(json.dumps(response_data, cls=DjangoJSONEncoder), mimetype="application/json") if request.method == "POST" and request.is_ajax: if has_operation_permission(request.user, place, 'note.change_note', allow=['admins', 'mods']) or request.user == note.author: note = get_object_or_404(Note, pk=request.POST['noteid']) note_form = UpdateNoteForm(request.POST or None, instance=note) if note_form.is_valid(): note_form_uncommited = note_form.save(commit=False) note_form_uncommited.title = request.POST['title'] note_form_uncommited.message = request.POST['message'] note_form_uncommited.last_mod_author = request.user note_form_uncommited.save() msg = "The note has been updated." else: msg = "The form is not valid, check field(s): " + note_form.errors return HttpResponse(msg) else: msg = "There was some error in the petition." return HttpResponse(msg)
def update_note(request, space_url): """ Updated the current note with the POST data. UpdateNoteForm is an incomplete form that doesn't handle some properties, only the important for the note editing. """ if request.method == "GET" and request.is_ajax: note = get_object_or_404(Note, pk=request.GET['noteid']) ctype = ContentType.objects.get_for_model(Note) latest_comments = Comment.objects.filter( is_public=True, is_removed=False, content_type=ctype, object_pk=note.id).order_by('-submit_date')[:5] form = CommentForm(target_object=note) response_data = {} response_data['title'] = note.title response_data['message'] = note.message response_data['comments'] = [{ 'username': c.user.username, 'comment': c.comment, 'submit_date': c.submit_date } for c in latest_comments] response_data["form_html"] = form.as_p() return HttpResponse(json.dumps(response_data, cls=DjangoJSONEncoder), mimetype="application/json") if request.method == "POST" and request.is_ajax: note = get_object_or_404(Note, pk=request.POST['noteid']) note_form = UpdateNoteForm(request.POST or None, instance=note) if note_form.is_valid(): note_form_uncommited = note_form.save(commit=False) note_form_uncommited.title = request.POST['title'] note_form_uncommited.message = request.POST['message'] note_form_uncommited.last_mod_author = request.user note_form_uncommited.save() msg = "The note has been updated." else: msg = "The form is not valid, check field(s): " + note_form.errors else: msg = "There was some error in the petition." return HttpResponse(msg)
def update_note(request, space_url): """ Updated the current note with the POST data. UpdateNoteForm is an incomplete form that doesn't handle some properties, only the important for the note editing. """ if request.method == "GET" and request.is_ajax: note = get_object_or_404(Note, pk=request.GET["noteid"]) ctype = ContentType.objects.get_for_model(Note) latest_comments = Comment.objects.filter( is_public=True, is_removed=False, content_type=ctype, object_pk=note.id ).order_by("-submit_date")[:5] form = CommentForm(target_object=note) response_data = {} response_data["title"] = note.title response_data["message"] = note.message response_data["comments"] = [ {"username": c.user.username, "comment": c.comment, "submit_date": c.submit_date} for c in latest_comments ] response_data["form_html"] = form.as_p() return HttpResponse(json.dumps(response_data, cls=DjangoJSONEncoder), mimetype="application/json") if request.method == "POST" and request.is_ajax: note = get_object_or_404(Note, pk=request.POST["noteid"]) note_form = UpdateNoteForm(request.POST or None, instance=note) if note_form.is_valid(): note_form_uncommited = note_form.save(commit=False) note_form_uncommited.title = request.POST["title"] note_form_uncommited.message = request.POST["message"] note_form_uncommited.last_mod_author = request.user note_form_uncommited.save() msg = "The note has been updated." else: msg = "The form is not valid, check field(s): " + note_form.errors else: msg = "There was some error in the petition." return HttpResponse(msg)
def update_note(request, space_url): """ Updated the current note with the POST data. UpdateNoteForm is an incomplete form that doesn't handle some properties, only the important for the note editing. """ # Shit double validation here due to the fact that we can't get the note ID # until the JS code sends us the GET or POST signals place = get_object_or_404(Space, url=space_url) if request.method == "GET" and request.is_ajax(): note = get_object_or_404(Note, pk=request.GET['noteid']) debate = get_object_or_404(Debate, pk=note.debate.id) if (request.user.has_perm('admin_space', place) or request.user.has_perm('mod_space', place) or request.user.has_perm('admin_debate', debate) or request.user.has_perm('mod_debate', debate) or request.user == note.author): ctype = ContentType.objects.get_for_model(Note) latest_comments = Comment.objects.filter(is_public=True, is_removed=False, content_type=ctype, object_pk=note.id) \ .order_by('-submit_date')[:5] form = CommentForm(target_object=note) response_data = {} response_data['title'] = note.title response_data['message'] = note.message response_data['author'] = {'name': note.author.username} response_data['comments'] = [{'username': c.user.username, 'comment': c.comment, 'submit_date': c.submit_date} for c in latest_comments] response_data["form_html"] = form.as_p() return HttpResponse(json.dumps(response_data, cls=DjangoJSONEncoder), mimetype="application/json") else: raise PermissionDenied if request.method == "POST" and request.is_ajax: note = get_object_or_404(Note, pk=request.POST['noteid']) debate = get_object_or_404(Debate, pk=note.debate.id) if (request.user.has_perm('admin_space', place) or request.user.has_perm('mod_space', place) or request.user.has_perm('admin_debate', debate) or request.user.has_perm('mod_debate', debate) or request.user == note.author): note_form = UpdateNoteForm(request.POST or None, instance=note) if note_form.is_valid(): note_form_uncommited = note_form.save(commit=False) note_form_uncommited.title = request.POST['title'] note_form_uncommited.message = request.POST['message'] note_form_uncommited.last_mod_author = request.user note_form_uncommited.save() else: return HttpResponseBadRequest(_("The form is not valid, check field(s): ") + note_form.errors) else: raise PermissionDenied return HttpResponseBadRequest(_("Bad request"))
def update_note(request, space_url): """ Updated the current note with the POST data. UpdateNoteForm is an incomplete form that doesn't handle some properties, only the important for the note editing. """ # Shit double validation here due to the fact that we can't get the note ID # until the JS code sends us the GET or POST signals place = get_object_or_404(Space, url=space_url) if request.method == "GET" and request.is_ajax(): note = get_object_or_404(Note, pk=request.GET['noteid']) debate = get_object_or_404(Debate, pk=note.debate.id) if (request.user.has_perm('admin_space', place) or request.user.has_perm('mod_space', place) or request.user.has_perm('admin_debate', debate) or request.user.has_perm('mod_debate', debate) or request.user == note.author): ctype = ContentType.objects.get_for_model(Note) latest_comments = Comment.objects.filter(is_public=True, is_removed=False, content_type=ctype, object_pk=note.id) \ .order_by('-submit_date')[:5] form = CommentForm(target_object=note) response_data = {} response_data['title'] = note.title response_data['message'] = note.message response_data['author'] = {'name': note.author.username} response_data['comments'] = [{ 'username': c.user.username, 'comment': c.comment, 'submit_date': c.submit_date } for c in latest_comments] response_data["form_html"] = form.as_p() return HttpResponse(json.dumps(response_data, cls=DjangoJSONEncoder), mimetype="application/json") else: raise PermissionDenied elif request.method == "POST" and request.is_ajax(): note = get_object_or_404(Note, pk=request.POST['noteid']) debate = get_object_or_404(Debate, pk=note.debate.id) if (request.user.has_perm('admin_space', place) or request.user.has_perm('mod_space', place) or request.user.has_perm('admin_debate', debate) or request.user.has_perm('mod_debate', debate) or request.user == note.author): note_form = UpdateNoteForm(request.POST or None, instance=note) if note_form.is_valid(): note_form_uncommited = note_form.save(commit=False) note_form_uncommited.title = request.POST['title'] note_form_uncommited.message = request.POST['message'] note_form_uncommited.last_mod_author = request.user note_form_uncommited.save() return HttpResponse(_("Note saved")) else: return HttpResponseBadRequest( _("The form is not valid, check field(s): ") + note_form.errors) else: raise PermissionDenied else: return HttpResponseBadRequest(_("Bad request"))