def editNote(request, binder_id, note_id): binder = get_object_or_404(Binder, pk=binder_id) note = get_object_or_404(Note, pk=note_id) if request.method == 'POST': form = NoteEditForm(request.POST, instance=note) if form.is_valid(): note = form.save() currTags = note.getTags() oldTags = note.tag_set.all() for tag in oldTags: if (tag.name not in currTags): tag.notes.remove(note) if tag.notes.all().count() == 0: tag.delete() for tag in currTags: if Tag.objects.filter(name=str(slugify(tag))).exists(): old = Tag.objects.get(name=str(slugify(tag))) if not old.tagged(note): old.notes.add(note) old.save() else: new = Tag(name=tag) new.save() new.notes.add(note) new.save() return HttpResponseRedirect('/smartnote/'+str(binder_id)) else: form = NoteEditForm({'text': note.text,}, instance=note) return render(request, 'smartnote/editNote.html', {'form': form, 'binder': binder, 'note': note,})
def addNote(request, binder_id): binder = get_object_or_404(Binder, pk=binder_id) if request.method == 'POST': form = NoteForm(request.POST) if form.is_valid(): note = form.save(commit=False) note.binder = binder note.slug = slugify(note.title) note.number = note.pageNumber() note.save() tags = note.getTags() for tag in tags: if Tag.objects.filter(name=str(slugify(tag))).exists(): old = Tag.objects.get(name=slugify(tag)) old.notes.add(note) old.save() else: new = Tag(name=tag) new.save() new.notes.add(note) new.save() return HttpResponseRedirect('/smartnote/'+binder_id) else: form = NoteForm() return render(request, 'smartnote/addNote.html', {'form': form, 'binder': binder,})