def manage(request, ctype_id, object_id ): context = {} context['success'] = False # Get the object content_type = ContentType.objects.get(id=ctype_id) try: obj = content_type.get_object_for_this_type(id=object_id) except ObjectDoesNotExist: context['message'] = 'O objecto para aplicar a nota já não existe.' return context # Check if there's a note associated to the object try: note = Note.objects.get( user = request.user, content_type = content_type, object_id = obj.id ) except ObjectDoesNotExist: note = None # Process the form form = NoteForm(request.POST) if form.is_valid(): txt = form.cleaned_data['txt'] public = form.cleaned_data['public'] if not txt.strip() and not note: context['message'] = 'Nota vazia. Não vou criar uma nota vazia' return context elif not txt.strip() and note: # Delete the note: note.delete() context['success'] = True context['html'] = '' context['message'] = 'Nota apagada' return context if note: note.txt = txt note.public = public note.timestamp = datetime.datetime.now() context['message'] = 'Nota editada' else: note = Note( user = request.user, content_object = obj, txt = txt , public = public) context['message'] = 'Nota criada para o objecto' note.save() context['success'] = True context['html'] = note.html() return context context['message'] = 'Input inválido. Não vou criar a nota.' if note: context['html'] = note.html() return context
def update(self, pk): try: note = Note.objects.get(id=pk) except Note.DoesNotExist: note = Note() category_name = self.data.pop("category", None) if category_name: note.category = Category.objects.get(name=category_name) for field, value in self.data.items(): setattr(note, field, value) note.save() return note