Exemplo n.º 1
0
def add(request):
    if request.method == "POST":
        bookmark_form = BookmarkInstanceForm(request.user, request.POST)
        if bookmark_form.is_valid():
            bookmark_instance = bookmark_form.save(commit=False)
            bookmark_instance.user = request.user
            bookmark_instance.save()
            bookmark = bookmark_instance.bookmark
            
            try:
                headers = {
                    "Accept" : "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
                    "Accept-Language" : "en-us,en;q=0.5",
                    "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                    "Connection" : "close",
                    ##"User-Agent": settings.URL_VALIDATOR_USER_AGENT
                    }
                req = urllib2.Request(bookmark.get_favicon_url(force=True), None, headers)
                urllib2.urlopen(req)
                has_favicon = True
            except:
                has_favicon = False
            
            bookmark.has_favicon = has_favicon
            bookmark.favicon_checked = datetime.datetime.now() 
            bookmark.save()
            
            if bookmark_form.should_redirect():
                return HttpResponseRedirect(bookmark.url)
            else:
                request.user.message_set.create(
                    message=_("You have saved bookmark '%(description)s'") % {
                        "description": bookmark_instance.description
                    }
                )
                return HttpResponseRedirect(reverse("all_bookmarks"))
    else:
        initial = {}
        if "url" in request.GET:
            initial["url"] = request.GET["url"]
        if "description" in request.GET:
            initial["description"] = request.GET["description"].strip()
        if "redirect" in request.GET:
            initial["redirect"] = request.GET["redirect"]
        
        if initial:
            bookmark_form = BookmarkInstanceForm(initial=initial)
        else:
            bookmark_form = BookmarkInstanceForm()
    
    bookmarks_add_url = "http://" + Site.objects.get_current().domain + reverse("add_bookmark")
    bookmarklet = "javascript:location.href='%s?url='+encodeURIComponent(location.href)+';description='+encodeURIComponent(document.title)+';redirect=on'" % bookmarks_add_url
    
    return render_to_response("bookmarks/add.html", {
        "bookmarklet": bookmarklet,
        "bookmark_form": bookmark_form,
    }, context_instance=RequestContext(request))
Exemplo n.º 2
0
def add(request):
    if request.method == "POST":
        print "LOGS: ADD BOOKMARKS BY POST"
        bookmark_form = BookmarkInstanceForm(request.user, request.POST)
        if bookmark_form.is_valid():
            bookmark_instance = bookmark_form.save(commit=False)
            bookmark_instance.user = request.user
            bookmark_instance.save(bookmark_form.clean()['url'])
            bookmark_instance.tags.add(*bookmark_form.cleaned_data['tags'])
            messages.success(request, _("You have saved bookmark '%(title)s'") % {
                    "title": bookmark_instance.title
                })
            return HttpResponseRedirect(reverse("bookmarks:all_bookmarks"))
    else:
        initial = {}
        if request.method == "GET":
            if "url" in request.GET:
                print "LOGS: ADD BOOKMARKS BY GET URL %(url)s" % {'url':request.GET["url"]}
                initial["url"] = request.GET["url"]
                bookmark_instance = get_user_bookmark(initial["url"], request.user)
                if bookmark_instance:
                    initial['image_list'] = [bookmark_instance.image_url]
                    initial['title'] = bookmark_instance.title
                    initial['description'] = bookmark_instance.description
                else:
                    bookmark_instance = get_bookmark(initial["url"])
                    if bookmark_instance:
                        initial['image_list'] = [bookmark_instance.image_url]
                        initial['title'] = bookmark_instance.title
                        initial['description'] = bookmark_instance.description
                    else:
                        initial = utils.fetch_bookmark(initial["url"])
                        if initial:
                            initial['message'] = "fetching successful!!!"
                            initial['message_type'] = "success" 
                            return HttpResponse(json.dumps(initial), content_type="application/json")
                        else:
                            initial = {}
                            initial['message'] = "Error in fetching !!!!"
                            initial['message_type'] = "danger"
                            return HttpResponse(json.dumps(initial), content_type="application/json")
            else:
                bookmark_form = BookmarkInstanceForm(request.user)
   
    return render_to_response("bookmarks/add.html", {
        "bookmark_form": bookmark_form,
    }, context_instance=RequestContext(request))
Exemplo n.º 3
0
def add(request):
    if request.method == "POST":
        bookmark_form = BookmarkInstanceForm(request.user, request.POST)
        if bookmark_form.is_valid():
            bookmark_instance = bookmark_form.save(commit=False)
            bookmark_instance.user = request.user
            bookmark_instance.save()
            bookmark = bookmark_instance.bookmark

            bookmark.has_favicon = bookmark.favicon_available()
            bookmark.favicon_checked = datetime.datetime.now()
            bookmark.save()

            if bookmark_form.should_redirect():
                return HttpResponseRedirect(bookmark.url)
            else:
                request.user.message_set.create(
                    message=_("You have saved bookmark '%(description)s'") % {
                        "description": bookmark_instance.description
                    }
                )
                return HttpResponseRedirect(reverse("all_bookmarks"))
    else:
        initial = {}
        if "url" in request.GET:
            initial["url"] = request.GET["url"]
        if "description" in request.GET:
            initial["description"] = request.GET["description"].strip()
        if "redirect" in request.GET:
            initial["redirect"] = request.GET["redirect"]

        if initial:
            bookmark_form = BookmarkInstanceForm(initial=initial)
        else:
            bookmark_form = BookmarkInstanceForm()

    bookmarks_add_url = "http://" + Site.objects.get_current().domain + reverse("add_bookmark")
    bookmarklet = ("javascript:location.href='%s?url='+encodeURIComponent(location.href)+';"
                   "description='+encodeURIComponent(document.title)+';redirect=on'"
                   % bookmarks_add_url)

    return render_to_response("bookmarks/add.html", {
        "bookmarklet": bookmarklet,
        "bookmark_form": bookmark_form,
    }, context_instance=RequestContext(request))
Exemplo n.º 4
0
    def _syncPost(self, post_elem, user):
        time_lst = time.strptime(post_elem.attrib['time'], "%Y-%m-%dT%H:%M:%SZ")
        time_obj = datetime.datetime(*time_lst[0:7])
        tags = self.clean_tags(post_elem.attrib['tag'])

        try:
            extended = post_elem.attrib['extended']
        except KeyError:
            extended = ''
        default_dict = {
            'description': post_elem.attrib['description'],
            'tags': tags,
            'url': post_elem.attrib['href'],
            # Is post_hash attrib unique to the post/URL or post/username ?!
            'post_hash': post_hash,
            'saved_date': time_obj,
            'extended_info': extended,
        }

        # Save only shared bookmarks
#        try:
#            is_shared = post_elem.attrib['shared'] # Only set, when it isn't shared
#        except KeyError:
#            obj, created = Bookmark.objects.get_or_create(
#                post_hash=post_hash, defaults=default_dict)
#            return obj
#        return None

        # to save pinax Bookmark
        try:
            unicode(default_dict['description'].decode('latin-1'))
        except:
            default_dict['description'] = ''
        print default_dict['description']
        bookmark_instance_form = BookmarkInstanceForm(user,default_dict)
        if bookmark_instance_form.is_valid():
            bookmark_instance = bookmark_instance_form.save(commit=False)
            bookmark_instance.user = user
            bookmark_instance.save()
            print bookmark_instance
            bookmark = bookmark_instance.bookmark
            try:
                headers = {
                    "Accept" : "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
                    "Accept-Language" : "en-us,en;q=0.5",
                    "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                    "Connection" : "close",
                    ##"User-Agent": settings.URL_VALIDATOR_USER_AGENT
                    }
                req = urllib2.Request(bookmark.get_favicon_url(force=True), None, headers)
                u = urllib2.urlopen(req)
                has_favicon = True
            except:
                has_favicon = False
            bookmark.has_favicon = has_favicon
            bookmark.favicon_checked = datetime.datetime.now() 
#                    bookmark.added = bookmark['add_date']
            bookmark.save()
#                    print bookmark
        else:
            print "bookmark_instance_form no es valido"            

        return
Exemplo n.º 5
0
def add(request):

    if request.method == "POST":
        bookmark_form = BookmarkInstanceForm(request.user, request.POST)
        if bookmark_form.is_valid():
            bookmark_instance = bookmark_form.save(commit=False)
            bookmark_instance.user = request.user
            bookmark_instance.save()
            bookmark = bookmark_instance.bookmark

            try:
                headers = {
                    "Accept":
                    "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
                    "Accept-Language": "en-us,en;q=0.5",
                    "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
                    "Connection": "close",
                    ##"User-Agent": settings.URL_VALIDATOR_USER_AGENT
                }
                req = urllib2.Request(bookmark.get_favicon_url(force=True),
                                      None, headers)
                u = urllib2.urlopen(req)
                has_favicon = True
            except:
                has_favicon = False

            bookmark.has_favicon = has_favicon
            bookmark.favicon_checked = datetime.now()
            bookmark.save()

            if bookmark_form.should_redirect():
                return HttpResponseRedirect(bookmark.url)
            else:
                request.user.message_set.create(
                    message=_("You have saved bookmark '%(description)s'") %
                    {'description': bookmark_instance.description})
                return HttpResponseRedirect(
                    reverse("bookmarks.views.bookmarks"))
    else:
        initial = {}
        if "url" in request.GET:
            initial["url"] = request.GET["url"]
        if "description" in request.GET:
            initial["description"] = request.GET["description"]
        if "redirect" in request.GET:
            initial["redirect"] = request.GET["redirect"]

        if initial:
            bookmark_form = BookmarkInstanceForm(initial=initial)
        else:
            bookmark_form = BookmarkInstanceForm()

    bookmarks_add_url = "http://" + Site.objects.get_current(
    ).domain + reverse(add)
    bookmarklet = "javascript:location.href='%s?url='+encodeURIComponent(location.href)+';description='+encodeURIComponent(document.title)+';redirect=on'" % bookmarks_add_url

    return render_to_response("bookmarks/add.html", {
        "bookmarklet": bookmarklet,
        "bookmark_form": bookmark_form,
    },
                              context_instance=RequestContext(request))