示例#1
0
def addnote(request):
    setting = Setting.objects.get(pk=1)
    if request.method == 'POST':
        form = NoteForm(request.POST, request.FILES)
        if form.is_valid():
            current_user = request.user
            data = Note()  # model ile bağlantı kur
            data.user_id = current_user.id
            data.category = form.cleaned_data['category']
            data.title = form.cleaned_data['title']
            data.keywords = form.cleaned_data['keywords']
            data.description = form.cleaned_data['description']
            data.image = form.cleaned_data['image']
            data.detail = form.cleaned_data['detail']
            data.slug = form.cleaned_data['slug']
            data.status = 'False'
            data.save()  # veritabanına kaydet
            messages.success(request, 'Your Content Insterted Successfuly')
            return HttpResponseRedirect('/user/notes')
        else:
            messages.success(request, 'Note Form Error:' + str(form.errors))
            return HttpResponseRedirect('/user/addnote')
    else:
        category = Category.objects.all()
        form = NoteForm()
        context = {
            'category': category,
            'form': form,
            'setting': setting,
        }
        return render(request, 'user_addnote.html', context)
示例#2
0
def add_note(request, **kwargs):
    user_id = request.user.id

    if request.is_ajax():
        new_note = Note()
        new_note.user_id = user_id
        new_note.name = request.POST.get('name', '')
        if new_note.name == '':
            new_note.name = '제목 없음'

        new_note.content = request.POST.get('content', '')
        if new_note.content == '':
            new_note.content = '내용 없음'

        if request.POST.get('isNotPublic') == 'true':
            new_note.isPublic = False

        new_note.save()

        tmp_topic = request.POST.get('topic', '')
        tmp_topic = tmp_topic.split(':$&*:')
        tmp_topic.pop()

        for t in tmp_topic:
            chk_t = Topic.objects.filter(name__exact=t)
            # 동일한 Topic이 아직 없다면
            if chk_t.count() == 0:
                topic = Topic()
                topic.name = t
                topic.save()
                new_note.topic.add(topic.id)

            # 동일한 Topic이 이미 있다면
            else:
                new_note.topic.add(chk_t[0].id)

        return to_json({'status': 'success', 'id': new_note.id})

    else:
        return to_json({'status': 'failed'})