def bookmark_save_page(request): ajax = 'ajax' in request.GET if request.method == 'POST': form = BookmarkSaveForm(request.POST) if form.is_valid(): bookmark = _bookmark_save(request, form) if ajax: variables = RequestContext(request, { 'bookmarks': [bookmark], 'show_edit': True, 'show_tags': True, }) return render(request, 'bookmark_list.html', variables) else: return HttpResponseRedirect( '/user/%s/' % request.user.username ) else: if ajax: return HttpResponse('failure') elif 'url' in request.GET: url = request.GET['url'] title = '' tags = '' try: link = Link.objects.get(url=url) bookmark = Bookmark.objects.get( link=link, user=request.user, ) title = bookmark.title tags = ' '.join( [tag.name for tag in bookmark.tag_set.all()] ) except ObjectDoesNotExist: pass form = BookmarkSaveForm({ 'url': url, 'title': title, 'tags': tags, }) else: form = BookmarkSaveForm() variables = RequestContext(request, { 'form': form }) if ajax: return render(request, 'bookmark_save_form.html', variables) else: return render(request, 'bookmark_save.html', variables)
def bookmark_save_page(request): """Сохранение закладки""" if request.method == "POST": # отправка данных form = BookmarkSaveForm(request.POST) if form.is_valid(): link, dummy = Link.objects.get_or_create(url=form.cleaned_data["url"]) # получаем или создаём bookmark, created = Bookmark.objects.get_or_create(user=request.user, link=link) # получаем или создаём bookmark.title = form.cleaned_data["title"] # обновляем название if not created: # если обновление - чистим список старых тегов bookmark.tag_set.clear() tag_names = form.cleaned_data["tags"].split() # список тегов for tag_name in tag_names: tag, dummy = Tag.objects.get_or_create(name=tag_name) bookmark.tag_set.add(tag) # Расшаривание if form.cleaned_data["share"]: shared, created = SharedBookmark.objects.get_or_create(bookmark=bookmark) if created: shared.users_voted.add(request.user) shared.save() bookmark.save() # сохраняем return HttpResponseRedirect("/bookmarks/user/%s/" % request.user.username) elif "url" in request.GET: url = request.GET["url"] title = "" tags = "" try: link = Link.objects.get(url=url) bookmark = Bookmark.objects.get(link=link, user=request.user) title = bookmark.title tags = " ".join(tag.name for tag in bookmark.tag_set.all()) except (Link.DoesNotExist, Bookmark.DoesNotExist): pass form = BookmarkSaveForm({"url": url, "title": title, "tags": tags}) else: form = BookmarkSaveForm() variables = RequestContext(request, {"form": form}) return render_to_response(u"bookmarks/bookmark_save_page.html", variables)
def bookmark_save_page(request): ajax = False if 'ajax' in request.GET.keys(): ajax = True if request.method == 'POST': form = BookmarkSaveForm(request.POST) if form.is_valid(): bookmark = _bookmark_save_page(request, form) if ajax: print('bookmark = ', bookmark) variables = RequestContext(request, { 'bookmarks' : [bookmark], 'show_edit': True, 'show_tags': True }) data = render_to_response('bookmark_list.html', variables) print(data) return data else: return HttpResponseRedirect('/user/{}/'.format(request.user.username)) else: if ajax: return HttpResponse('failure') elif 'url' in request.GET: url = request.GET['url'] title = '' tags = '' try: link = Link.objects.get(url=url) bookmark = Bookmark.objects.get(link=link, user=request.user) title = bookmark.title tags = ' '.join(tag.name for tag in bookmark.tag_set.all()) except ObjectDoesNotExist: pass form = BookmarkSaveForm({ 'url' : url, 'title' : title, 'tags' : tags }) else: form = BookmarkSaveForm() variables = RequestContext(request, {'form' : form}) if ajax: return render_to_response('bookmark_save_form.html', variables) else: print('Loading Page') return render_to_response('bookmark_save.html', variables)
def bookmark_save(request): ajax = 'ajax' in request.GET if request.method == 'POST': form = BookmarkSaveForm(request.POST) if form.is_valid(): bookmark = _save_bookmark(request, form) if ajax: context = { 'bookmarks': [bookmark], ###must be a iterable list 'show_edit': True, 'show_tags': True, } return render(request, 'bookmark_list.html', context) else: return redirect('/user/%s/' % request.user.username) else: if ajax: return HttpResponse(u'failure') elif 'url' in request.GET: url = request.GET['url'] title = tags = '' try: link = Link.objects.get(url=url) bookmark = Bookmark.objects.get( link=link, user=request.user ) title = bookmark.title tags = ' '.join( tag.name for tag in bookmark.tag_set.all() ) except (Link.DoesNotExist, Bookmark.DoesNotExist): pass form = BookmarkSaveForm({ 'url': url, 'title': title, 'tags': tags, }) else: form = BookmarkSaveForm() if ajax: return render(request, 'bookmark_save_form.html', {'form':form}) else: return render(request, 'bookmark_save.html', {'form':form})
def bookmark_save_page(request): ajax=request.GET.has_key('ajax') if request.method=='POST' : form=BookmarkSaveForm(request.POST) print request.POST if form.is_valid(): bookmark=_bookmark_save(request,form) if ajax: variables=RequestContext(request,{'bookmarks':[bookmark],'show_edit':True ,'show_tags':True}) return render_to_response('bookmarkList.html',variables) else : return HttpResponseRedirect("/user/%s/" % request.user.username) else : if ajax : return HttpResponse("failure") elif request.GET.has_key('url'): url=request.GET['url'] title='' tags='' try : link=Link.objects.get(url=url) bookmark=Bookmark.objects.get(link=link,user=request.user) title=bookmark.title tags=''.join(tag.name for tag in bookmark.tag_set.all()) except ObjectDoesNotExist : pass form=BookmarkSaveForm({'title':title,'tags':tags,'url':url}) else : form=BookmarkSaveForm() variables=RequestContext(request,{'form':form,'user':request.user}) return render_to_response("bookmark_save.html",variables)
def bookmark_save(request): if request.method == 'POST': form = BookmarkSaveForm(request.POST) if form.is_valid(): bookmark, bookmark_created = _bookmark_save(request, form) if not form.cleaned_data['personal']: # create a shared_bookmark for newly created public bookmark if bookmark_created: _save_sharedbookmark(request, bookmark) # This case is for a bookmark having been marked as private and # then made public else: try: SharedBookmark.objects.get(bookmark = bookmark) except SharedBookmark.DoesNotExist: _save_sharedbookmark(request, bookmark) bookmark.personal = False # bookmark is personal else: # check if a personal bookmark has an existing shared_bookmark # and remove it. This case is for a bookmark having been # marked as public and then made private if not bookmark_created: try: shared = SharedBookmark.objects.get(bookmark = bookmark) if shared.like_votes.count() < 4: shared.delete() # need to possibly add return value here to indicate # successful removal else: pass # need to possibly add return value here to indicate # that the bookmark has too many likes to remove except SharedBookmark.DoesNotExist: pass # else bookmark just created # no need to check for existing SharedBookmark, since # bookmark was just created bookmark.personal = True if request.is_ajax(): variables = { 'bookmarks': [bookmark], 'show_single_edit': True, 'show_tags': True } return render(request, 'bookmark_list.html', variables) else: return HttpResponseRedirect(reverse(user_page, args=[request.user.username])) else: if request.ajax(): return HttpResponse('failure') else: data = {} edit_bookmark = False if 'url' in request.GET or 'id' in request.GET: if 'url' in request.GET: edit_bookmark = True url = request.GET['url'] else: id = request.GET['id'] title = '' tags = '' features = '' try: if edit_bookmark: link = Link.objects.get(url=url) request.session['link'] = link bookmark = Bookmark.objects.get( link = link, user = request.user ) personal = bookmark.personal else: bookmark = Bookmark.objects.get(pk=id) request.session['link'] = bookmark.link personal = True title = bookmark.title tags = ','.join(tag.name for tag in bookmark.tags.all()) features = bookmark.features.values_list('id', flat=True) except (Link.DoesNotExist, Bookmark.DoesNotExist): pass data = { 'title': title, 'tags': tags, 'features': features, 'personal': personal } # elif 'id' in request.GET: # id = request.GET['id'] # title = '' # tags = '' # features = '' # try: # bookmark = Bookmark.objects.get(pk=id) # request.session['link'] = bookmark.link # title = bookmark.title # tags = ','.join(tag.name for tag in bookmark.tags.all()) # features = bookmark.features.values_list('id', flat=True) # personal = True # #assert False # except (Bookmark.DoesNotExist): # pass # data = { # 'title': title, # 'tags': tags, # 'features': features, # 'personal': personal # } else: link = request.session.get('link', None) if link: ################################################# # Look at possibly using phantomjs to get title ################################################## # driver = webdriver.Firefox() # driver.get(link.url) # title = driver.title # data = {'title': title} soup = BeautifulSoup(urllib.urlopen(link.url), "lxml") data = {'title': soup.title.string} form = BookmarkSaveForm(initial=data) variables = {'form': form} if request.is_ajax(): return render(request, 'bookmark_save_form.html', variables) else: return render(request, 'bookmark_save.html', variables)