コード例 #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']
    })