def NoteShow(request, uuid): note = Note.objects.get(note_uuid=uuid) form = NoteForm(request.POST or None, instance=note) if form.is_valid(): note.save() return HttpResponseRedirect('/') return render(request, 'note_create.html', {'form': form})
def add(request): if request.method == 'POST': form = NoteForm(request.POST) if form.is_valid(): form.save() return redirect('/') else: form = NoteForm() return render_to_response('add.html', {'form' : form}, RequestContext(request, processors=[count_notes]))
def NoteUpdate(request): noteid = request.GET['id'] note = Note.objects.get(id=noteid) form = NoteForm(request.POST or None, instance=note) if request.method == 'POST': if form.is_valid(): note.save() # return redirect('/') return HttpResponseRedirect('/') else: return render(request, 'note_create.html', {'form': form})
def NoteCreate(request): if request.method == 'POST': form = NoteForm(request.POST) if form.is_valid(): note = form.save(commit=False) note.created_by = request.user note.save() # return redirect('/') return HttpResponseRedirect('/') else: form = NoteForm() return render(request, 'note_create.html', {'form': form})
def project_notes(self): """Get notes and note form for the project.""" notes = self.object.notes.all().order_by('-creation_date') form = NoteForm() return {'notes': notes, 'form': form}
def post(self, request): form = NoteForm(request.POST) if form.is_valid(): text = form.cleaned_data['post'] print("Add new note:", text) note = Note() note.text_message = text note.save() form = NoteForm() # Clear input field return render( request, self.tamplate_name, { 'notes': Note.objects.all().order_by("-unique_word_count"), 'form': form })
def get(self, request, *args, **kwards): form = NoteForm() return render( request, self.tamplate_name, { 'notes': Note.objects.all().order_by("-unique_word_count"), 'form': form })
def create(request): if request.method == "POST": note = Note() form = NoteForm(request.POST, instance=note) if form.is_valid(): # if note get the authorID then set the ID is -1. because at the # first step. we didn't hava build UC system. if note.authorID == None: note.authorID = 0 # if user forget to set note's type. we set it as default value 0 if note.noteType == None: note.noteType = 0 if note.color == None: note.color = "#000000" # set createtime note.createTime = timezone.now() try: note.save() tags = form.cleaned_data["tags"] # TODO: not support chinese character tags = tags.replace(",", "|").replace(";", "|").replace(" ", "").replace("||", "|") tagArr = tags.split("|") # 下面虽然实现了tags的插入,但是效率不高 # for tag in tagArr: # tag_record=Tag(tagName=tag,noteID=note.id) # tag_record.save() # Optimizaition way insert_list = [] for tag in tagArr: # 此处逻辑不严,如果tag为空,还会插入的,这样没有意义 if tag != "": insert_list.append(Tag(tagName=tag, noteID=note)) # print insert_list Tag.objects.bulk_create(insert_list) return HttpResponseRedirect("/view?id=" + str(note.id)) except Exception, e: # return HttpResponseRedirect('/view?id='+str(note.id return HttpResponse("data is valid and we store it in the database failed. ") finally:
def get_context_data(self, pk): project = get_object_or_404(Project, pk=pk) form = NoteForm() notes = project.notes.all().order_by('-creation_date') return { 'project': project, 'form': form, 'notes': notes, }
def get_context_data(self, pk): story = get_object_or_404(Story, pk=pk) form = NoteForm() notes = story.notes.all().order_by('-creation_date') return { 'story': story, 'form': form, 'notes': notes, }
def edit(request, note_id): if (not request.user.is_authenticated()): return redirect('/auth/login') else: args = {} form = NoteForm(instance=Note.objects.get(id = note_id)) args.update(csrf(request)) args['form'] = form args['username'] = auth.get_user(request).username if (request.method == 'GET'): return render_to_response('edit.html',args) else: if (request.POST): form = NoteForm(request.POST) if form.is_valid(): note = form.save(commit=False) note.date = datetime.now() note.user = request.user note.save() return redirect('/note/all') else: args['errors'] = form.errors return render_to_response('edit.html',args)
def update(request): # 现在这个是要进行更新文章的入口 # 主要的逻辑处理都在这个里面 # OK id = request.GET.get("id", "1") note = Note.objects.get(pk=id) # 获得本文的所有的标签,因为无论是用户GET还是POST请求都会用到它,所以把它放到前面 tags = Tag.objects.filter(noteID=note) if request.method == "POST": # 如果用户要保存数据 # note = Note() form = NoteForm(request.POST, instance=note) if form.is_valid(): # if note get the authorID then set the ID is -1. because at the # first step. we didn't hava build UC system. # if user forget to set note's type. we set it as default value 0 if note.noteType == None: note.noteType = 0 if note.color == None: note.color = "#000000" # set createtime note.createTime = timezone.now() try: note.save() # 获得用户提交的新标签 newTags = form.cleaned_data["tags"] # TODO: not support chinese character newTags = newTags.replace(",", "|").replace(";", "|").replace(" ", "").replace("||", "|") newTags = newTags.split("|") oldTags = [] for _tag in tags: oldTags.append(_tag.tagName) # 如果想让一片文章所有的标签tags都变成最新的。需要先求一次 # oldTags与newTags的差集,然后删除差集中的所有元素 # 这样就可以删除所有旧的标签 # 第二步:newTags 和oldTags进行求差集。然后把这些集合插入到数据库 # 这样两步就可以实现了标签的更改。 # Start! # first step print oldTags print newTags print 156 oldDiff = list(set(oldTags).difference(set(newTags))) # oldTags中有而newTags中没有的 # 批量删除 # TODO 可能有删除性能瓶颈,查查批量删除 for tag in oldDiff: tagRecorder = Tag.objects.get(tagName=tag, noteID=note) tagRecorder.delete() print 165 print newTags print oldTags newDiff = list(set(newTags).difference(set(oldTags))) # newTags中有而oldTags中没有的 print 169 # 批量插入操作 insert_list = [] for tag in newDiff: # 此处逻辑不严,如果tag为空,还会插入的,这样没有意义 if tag != "": insert_list.append(Tag(tagName=tag, noteID=note)) # print insert_list Tag.objects.bulk_create(insert_list) print 178 # return redirect(reverse('note.views.view', # args=['id',note.id])) return HttpResponseRedirect("/view?id=" + str(note.id)) # return HttpResponse(note.content) # return HttpResponse("data is valid and we store it in the # database. The id of this note is:"+str(note.id)) except Exception, e: # return HttpResponseRedirect('/view?id='+str(note.id return HttpResponse("data is valid and we store it in the database failed. %s " % e) finally: