예제 #1
0
파일: views.py 프로젝트: heldergg/dre
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
예제 #2
0
파일: views.py 프로젝트: scofield1991/notes
def add_note(request):
    if request.method=='POST':

        note_form=NoteForm(data=request.POST)

        if note_form.is_valid():
            note_form.instance.user = request.user
            note_form.save()

            return HttpResponseRedirect('/notes/')
    else:
        note_form = NoteForm()


    return render(request,
        'notesapp/add_note.html',
        {'note_form': note_form, 'user_id':request.session['user_id'] } )
예제 #3
0
파일: views.py 프로젝트: scofield1991/notes
def add_note(request):
    if request.method == 'POST':

        note_form = NoteForm(data=request.POST)

        if note_form.is_valid():
            note_form.instance.user = request.user
            note_form.save()

            return HttpResponseRedirect('/notes/')
    else:
        note_form = NoteForm()

    return render(request, 'notesapp/add_note.html', {
        'note_form': note_form,
        'user_id': request.session['user_id']
    })
예제 #4
0
파일: views.py 프로젝트: scofield1991/notes
def edit_note(request, note_id):
    if request.method == 'GET':
        note_obj = Note.objects.get(id=note_id)
        note_form = NoteForm(instance=note_obj)
        #note_obj=Note.objects,filter(id=request.POST.get('id')).update(note_obj=request.POST.get('note_body'))
        return render(request, 'notesapp/edit_note.html', {'note': note_form})
    else:
        note_obj = Note.objects.filter(id=note_id)
        note_obj.update(note_name=request.POST.get('note_name'))
        note_obj.update(note_body=request.POST.get('note_body'))
        note_obj.update(color=request.POST.get('color'))
        note_obj.update(text=request.POST.get('text'))
        #note_obj=Note.objects.filter(id=note_id)
        note_obj[0].labels = []

        for label in request.POST.getlist('labels'):
            note_obj[0].labels.add(Label.objects.get(id=label))

        for category in request.POST.getlist('category'):
            note_obj[0].category.add(Category.objects.get(id=category))

        return HttpResponseRedirect('/notes/')