def create_bookmark(request): ''' View function which handles rendering the bookmark form and stores them if valid or re-renders ''' if request.method == "POST": if request.is_ajax(): data = simplejson.loads(request.POST.keys()[0]) else: data = request.POST bookmark_form = BookmarkForm(data=data) bookmark_id = data.get('bookmark_id','') if bookmark_id != '': bookmark_id = int(bookmark_id) if bookmark_form.is_valid(): bookmark_form_cleaned = bookmark_form.cleaned_data bookmark_json = store_bookmark(request, bookmark_form_cleaned,bookmark_id) return HttpResponse(bookmark_json, mimetype='application/json') bookmark_form.errors['status'] = 'failure' return HttpResponse(simplejson.dumps(bookmark_form.errors),mimetype='application/json') raise Http404()
def new(request): ctx = {} template_name = 'bookmark/form.html' form = BookmarkForm() if request.method == "POST": form = BookmarkForm(request.POST) if form.is_valid(): bookmark = form.save(created_by=request.user) return redirect('bookmark_last', bookmark.pk) ctx['form'] = form return render(request, template_name, ctx)
def post(self, request, bookmark_id): bookmark = Bookmark.objects.get(pk=bookmark_id) form = BookmarkForm(request.POST) if form.is_valid(): BookmarkTag.objects.filter(bookmark=bookmark).delete() new_tags = form.cleaned_data.get("tags") tags = [x.strip() for x in new_tags.split(',')] for t in tags: tag, created = Tag.objects.get_or_create(name=t) bookmark_tag, created = BookmarkTag.objects \ .get_or_create(bookmark=bookmark, tag=tag) bookmark.title = form.cleaned_data.get("title") bookmark.url = form.cleaned_data.get("url") bookmark.save() return HttpResponseRedirect(reverse('bookmark:home'))
def edit(request, pk): ctx = {} template_name = 'bookmark/form.html' bookmark = get_object_or_404(Bookmark, pk=pk) if not bookmark.created_by==request.user: return HttpResponse(u'您没有权限执行该操作') ctx['bml'] = bookmark form = BookmarkForm(instance=bookmark) if request.method == "POST": form = BookmarkForm(request.POST, instance=bookmark) if form.is_valid(): form.save() messages.info(request, u'成功编辑') return redirect('bookmark_detail', bookmark.pk) ctx['form'] = form ctx['bml'] = bookmark return render(request, template_name, ctx)
def get(self, request, bookmark_id): bookmark = Bookmark.objects.get(pk=bookmark_id) tags = Tag.objects.filter(bookmarktag__bookmark=bookmark) \ .values_list('name', flat=True) data = {} data['tags'] = ", ".join(tags) data['title'] = bookmark.title data['url'] = bookmark.url form = BookmarkForm(initial=data) return render(request, 'bookmark/form.html', {'form': form})
def update(self, request, bookmark_id): # Get bookmark first, if not return NOT_FOUND try: bookmark = Bookmark.objects.get(pk = int(bookmark_id), owner=request.user) oldUrl = bookmark.url form = BookmarkForm(request.PUT, instance=bookmark) bookmark = form.save() if not bookmark.title: bookmark.title = utils.get_title_by_url(bookmark.url) bookmark.save() # update screen shot if oldUrl != bookmark.url: utils.update_bk_screen_shot_async(bookmark) return bookmark except ObjectDoesNotExist: return rc.NOT_FOUND
def bookmark_create(request): if request.method == 'POST': form = BookmarkForm(request.POST) if form.is_valid(): form.save() return redirect('bookmark:list') else: form = BookmarkForm() return render(request, 'bookmark/bookmark_create.html', {'form': form})
def bookmark_update(request, pk): bookmark = get_object_or_404(Bookmark, pk=pk) if request.method == 'POST': form = BookmarkForm(request.POST, instance=bookmark) if form.is_valid(): form.save() return redirect('bookmark:detail', bookmark.pk) else: form = BookmarkForm(instance=bookmark) return render(request, 'bookmark/bookmark_update.html', { 'bookmark': bookmark, 'form': form, })
def get(self, request): form = BookmarkForm() return render(request, 'bookmark/form.html', {'form': form})