Exemple #1
0
 def post(self, request):
     if not request.FILES:
         message = "Select a correct HTML file"
         response = json.dumps({"False": True, "message": message})
         return HttpResponse(response)
     f = request.FILES["file"]
     html = ""
     for chunk in f.chunks():
         html += str(chunk)
     bookmarks = HtmlBookmarks(html)
     for bookmark in bookmarks.parse():
         folder = None
         if bookmark.folder:
             try:
                 folder = Folder.objects.get(name=bookmark.folder,
                                             owner=self.request.user)
             except Folder.DoesNotExist:
                 folder = Folder(name=bookmark.folder,
                                 owner=self.request.user)
                 folder.save()
         mark = BookmarkModel(
             url=bookmark.url,
             title=bookmark.title or bookmark.url,
             owner=self.request.user,
             folder=folder,
         )
         try:
             mark.save()
         except DatabaseError:
             message = "Opps, Something fail."
             response = json.dumps({"False": True, "message": message})
             return HttpResponse(response)
     message = "Bookmarks saved successfully"
     response = json.dumps({"status": True, "message": message})
     return HttpResponse(response)
Exemple #2
0
 def test_get_others_bookmark(self):
     bookmark = Bookmark(user=self.user, title="you cannot edit me", url="http://google.com", domain="google.com")
     bookmark.save()
     self.client.login(username="******", password="******")
     response = self.client.get('/api/v1/bookmark/'+ str(bookmark.id), content_type="application/json")
     print response.status_code
     self.assertTrue(response.status_code == 401)
Exemple #3
0
def postdata(request):
    if request.method == 'POST':
        b_title = request.POST.get('title')
        b_url = request.POST.get('url')
        sdata = Bookmark(title=b_title, url=b_url)
        sdata.save()

    return render(request, 'postdata.html')
Exemple #4
0
 def test_change_bookmark_list_that_is_not_mine(self):
     l = List(name="My List", user=self.john)
     l.save()
     bookmark = Bookmark(user=self.user, title="you cannot edit me", url="http://google.com", domain="google.com")
     bookmark.save()
     self.client.login(username="******", password="******")
     put_data = {'title': 'awesome website', 'url': 'http://google.com', 'list': '/api/v1/list/' + str(l.id) , 'tags': 'searchengine google', 'created_time': "2013-2-12"}
     response = self.client.put('/api/v1/bookmark/'+ str(bookmark.id), json.dumps(put_data), content_type="application/json")
     self.assertTrue(response.status_code == 401)
Exemple #5
0
    def get(self, request, url):
        url = request.get_full_path()[1:]
        title = url
        bookmark = BookmarkModel(url=url,
                                 title=title[:255],
                                 owner=self.request.user)
        bookmark.save()

        return redirect(settings.LOGIN_REDIRECT_URL)
Exemple #6
0
 def test_edit_bookmark(self):
     bookmark = Bookmark(user=self.user, title="you cannot edit me", url="http://google.com", domain="google.com")
     bookmark.save()
     self.client.login(username="******", password="******")
     put_data = {'title': 'awesome website', 'url': 'http://google.com', 'tags': 'searchengine google', 'created_time': "2013-2-12"}
     response = self.client.put('/api/v1/bookmark/'+ str(bookmark.id), json.dumps(put_data), content_type="application/json")
     print response.status_code
     self.assertTrue(response.status_code == 202)
     bookmark = Bookmark.objects.get(id=bookmark.id)
     self.assertTrue(bookmark.title == "awesome website")
Exemple #7
0
 def test_get_others_bookmarks(self):
     bookmark = Bookmark(user=self.user, title="you cannot edit me", url="http://google.com", domain="google.com")
     bookmark.save()
     l = List.objects.get(name="Inbox", user=self.user)
     self.client.login(username="******", password="******")
     response = self.client.get('/api/v1/list/' + str(l.id) + '/bookmarks', content_type="application/json")
     print response
     print response.status_code
     data = json.loads(response.content)
     self.assertTrue(len(data['objects']) == 0)
Exemple #8
0
 def test_delete_bookmark_by_other_user(self):
     bookmark = Bookmark(user=self.user, title="you cannot edit me", url="http://google.com", domain="google.com")
     bookmark.save()
     self.client.login(username="******", password="******")
     response = self.client.delete('/api/v1/bookmark/'+ str(bookmark.id), content_type="application/json")
     print response, response.status_code
     self.assertTrue(response.status_code == 401)
     try:
         bookmark = Bookmark.objects.get(id=bookmark.id)
     except:
         self.fail()
Exemple #9
0
 def test_get_others_bookmark(self):
     bookmark = Bookmark(user=self.user,
                         title="you cannot edit me",
                         url="http://google.com",
                         domain="google.com")
     bookmark.save()
     self.client.login(username="******", password="******")
     response = self.client.get('/api/v1/bookmark/' + str(bookmark.id),
                                content_type="application/json")
     print response.status_code
     self.assertTrue(response.status_code == 401)
Exemple #10
0
 def test_get_others_bookmarks(self):
     bookmark = Bookmark(user=self.user,
                         title="you cannot edit me",
                         url="http://google.com",
                         domain="google.com")
     bookmark.save()
     l = List.objects.get(name="Inbox", user=self.user)
     self.client.login(username="******", password="******")
     response = self.client.get('/api/v1/list/' + str(l.id) + '/bookmarks',
                                content_type="application/json")
     print response
     print response.status_code
     data = json.loads(response.content)
     self.assertTrue(len(data['objects']) == 0)
Exemple #11
0
 def test_delete_bookmark_by_other_user(self):
     bookmark = Bookmark(user=self.user,
                         title="you cannot edit me",
                         url="http://google.com",
                         domain="google.com")
     bookmark.save()
     self.client.login(username="******", password="******")
     response = self.client.delete('/api/v1/bookmark/' + str(bookmark.id),
                                   content_type="application/json")
     print response, response.status_code
     self.assertTrue(response.status_code == 401)
     try:
         bookmark = Bookmark.objects.get(id=bookmark.id)
     except:
         self.fail()
Exemple #12
0
def scrape(collectionId):
    """ Bookmark scrape method """
    form = ScrapeForm()
    if request.args.get('error'):
        error = request.args.get('error')
    else:
        error = None

    if request.args.get('bookmark_url'):
        url_to_scrape = request.args.get('bookmark_url')
        meta = article_meta_scrape(current_user, url_to_scrape)
    elif form.validate_on_submit():
        url_to_scrape = form.url.data
        meta = article_meta_scrape(current_user, url_to_scrape)
    else:
        url_to_scrape = None
        meta = None

    bookmark = None

    if meta:
        # set up temp category for bookmark
        new_category = Category('temp')
        db.session.add(new_category)
        db.session.flush()
        category = new_category

        # get library
        collection = Collection.query.filter_by(id=collectionId).first()

        # increment num_bookmarks for that collection
        collection.num_bookmarks += 1
        db.session.flush()

        if collection:
            # add bookmark to db
            title = meta['title']
            description = meta['description']
            url = meta['url']
            image = meta['image']
            source = meta['source']
            slug = slugify(title)
            published_on = datetime.utcnow()

            bookmark = Bookmark(collection, current_user, category, slug, url,
                                title, source, published_on, description,
                                image, None, None)
            db.session.add(bookmark)
            db.session.commit()
            return redirect(url_for('collection', collectionId=collectionId))
        else:
            error = 'Please try a different URL...'

    else:
        error = 'Please try a different URL...'

    return redirect(
        url_for('collection', collectionId=collectionId, error=error))
Exemple #13
0
def import_to_lianpeng(file_name):
    f = open(file_name)
    xml = f.read()
    f.close()
    soup = BeautifulSoup(xml)
    posts = soup.findAll('post')
    #import pdb;pdb.set_trace()
    h = HTMLParser.HTMLParser()
    for post in posts:
        stamp = float(post.createtime.string) / 1000
        created_time = datetime.datetime.utcfromtimestamp(stamp).replace(
            tzinfo=utc)
        tags = " ".join([tag.string for tag in post.findAll('tag')])
        title = post.find('title').string
        if not title:
            title = "No Title"
        print created_time
        print title
        note = post.find('text')
        if note and note.string:
            note = h.unescape(note.string)
        else:
            note = ""
        bookmark = Bookmark(list_id=1397,
                            user_id=3,
                            title=title,
                            note=note,
                            created_time=created_time,
                            domain='note.lianpeng.me',
                            tags=tags)
        bookmark.save()
        bookmark = Bookmark.objects.get(id=bookmark.id)
        bookmark.url = 'http://lianpeng.me/note/{}/'.format(bookmark.id)
        bookmark.save()
Exemple #14
0
def _sync_github(user, default_list, state):
    website = 'github'
    user_social_auth = user.social_auth.get(provider=website)
    github_username = user_social_auth.extra_data['login']
    page = 1
    while True:
        resp = urllib2.urlopen('https://api.github.com/users/' + github_username + '/starred?page=' + str(page))
        data = resp.read()
        items = json.loads(data)
        for item in items:
            url = item['html_url']
            desc = item['description']
            language = item.get('language', '')
            title = item['full_name']
            try:
                #: if one of the github project url is saved before, stop iterating and return.
                Bookmark.objects.get(list=default_list, url=url, user=user)
                return
            except Bookmark.DoesNotExist:
                bookmark = Bookmark(url=url, user=user, note=desc)
                bookmark.domain = urlparse(url).netloc
                bookmark.title = title
                bookmark.list = default_list
                if language:
                    bookmark.tags = language.lower()
                bookmark.save()
        #: if less than 30, there will no more items, stop fetching data from Github
        if len(items) < 30:
            break
        page += 1

    if state.state != 1:
        state.state = 1
        state.list = default_list
        state.save()
Exemple #15
0
 def test_change_bookmark_list_that_is_not_mine(self):
     l = List(name="My List", user=self.john)
     l.save()
     bookmark = Bookmark(user=self.user,
                         title="you cannot edit me",
                         url="http://google.com",
                         domain="google.com")
     bookmark.save()
     self.client.login(username="******", password="******")
     put_data = {
         'title': 'awesome website',
         'url': 'http://google.com',
         'list': '/api/v1/list/' + str(l.id),
         'tags': 'searchengine google',
         'created_time': "2013-2-12"
     }
     response = self.client.put('/api/v1/bookmark/' + str(bookmark.id),
                                json.dumps(put_data),
                                content_type="application/json")
     self.assertTrue(response.status_code == 401)
Exemple #16
0
 def test_edit_bookmark(self):
     bookmark = Bookmark(user=self.user,
                         title="you cannot edit me",
                         url="http://google.com",
                         domain="google.com")
     bookmark.save()
     self.client.login(username="******", password="******")
     put_data = {
         'title': 'awesome website',
         'url': 'http://google.com',
         'tags': 'searchengine google',
         'created_time': "2013-2-12"
     }
     response = self.client.put('/api/v1/bookmark/' + str(bookmark.id),
                                json.dumps(put_data),
                                content_type="application/json")
     print response.status_code
     self.assertTrue(response.status_code == 202)
     bookmark = Bookmark.objects.get(id=bookmark.id)
     self.assertTrue(bookmark.title == "awesome website")
Exemple #17
0
def import_to_lianpeng(file_name):
    f = open(file_name)
    xml = f.read()
    f.close()
    soup = BeautifulSoup(xml)
    posts = soup.findAll('post')
    #import pdb;pdb.set_trace()
    h = HTMLParser.HTMLParser()
    for post in posts:
        stamp = float(post.createtime.string) / 1000
        created_time = datetime.datetime.utcfromtimestamp(stamp).replace(tzinfo=utc)
        tags = " ".join([tag.string for tag in post.findAll('tag')])
        title = post.find('title').string
        if not title:
            title = "No Title"
        print created_time
        print title
        note = post.find('text')
        if note and note.string:
            note = h.unescape(note.string)
        else:
            note = ""
        bookmark = Bookmark(list_id=1397, user_id=3, title=title, note=note, created_time=created_time, domain='note.lianpeng.me', tags=tags)
        bookmark.save()
        bookmark = Bookmark.objects.get(id=bookmark.id)
        bookmark.url = 'http://lianpeng.me/note/{}/'.format(bookmark.id)
        bookmark.save()
Exemple #18
0
def bookmark(request):
    try:
        email = request.session['login_id']
    except KeyError as e:
        return render(request, 'login_form.html')

    all_bookmark = None
    if request.method == 'POST':
        bookmark = Bookmark(
            bookmark_name=request.POST["bookmark_name"], bookmark_url=request.POST["bookmark_url"],
            bookmark_desc=request.POST["bookmark_desc"]
        )
        bookmark.save()
        result = {"login_id": email,
                  "bookmark_name": request.POST["bookmark_name"],
                  "bookmark_url": request.POST["bookmark_url"]}

        return render(request, 'bookmark.html', result)
    else:
        all_bookmark = Bookmark.objects.all()
        result = {"login_id": email,
                  "all_bookmark": all_bookmark}
        return render(request, 'bookmark.html', result)
Exemple #19
0
    def test_award_points(self):
        l = self.user.list_set.all()[0] #: Inbox
        bookmark = Bookmark(user=self.user, title="test page", url="http://example.me/", domain='example.com', list=l)
        bookmark.save()
        self.assertTrue(self.user.targetstat_targets.points == 1)

        bookmark = Bookmark(user=self.user, title="test page", url="http://example.com/", domain='example.me', list=l)
        bookmark.save()
        self.assertTrue(self.user.targetstat_targets.points == 1)
Exemple #20
0
    def test_award_points(self):
        l = self.user.list_set.all()[0]  #: Inbox
        bookmark = Bookmark(user=self.user,
                            title="test page",
                            url="http://example.me/",
                            domain='example.com',
                            list=l)
        bookmark.save()
        self.assertTrue(self.user.targetstat_targets.points == 1)

        bookmark = Bookmark(user=self.user,
                            title="test page",
                            url="http://example.com/",
                            domain='example.me',
                            list=l)
        bookmark.save()
        self.assertTrue(self.user.targetstat_targets.points == 1)
Exemple #21
0
def index(request):
    l, created = List.objects.get_or_create(kind=LIST_KIND_CHOICES.NOTE,
            user=request.user, defaults={"name": _("Note")})
    site = Site.objects.get_current()
    bookmark = Bookmark(url="http://{}/note/empty/".format(site.domain),
            domain="note.lianpeng.me", title="输入标题", note="",
            user=request.user, charset="UTF-8", list=l)
    bookmark.save()
    bookmark.url = 'http://{}{}'.format(site.domain, reverse('note_detail', args=(bookmark.id, )))
    bookmark.save()
    return redirect('note_edit', id=bookmark.id)
Exemple #22
0
def index(request):
    l, created = List.objects.get_or_create(kind=LIST_KIND_CHOICES.NOTE,
                                            user=request.user,
                                            defaults={"name": _("Note")})
    site = Site.objects.get_current()
    bookmark = Bookmark(url="http://{}/note/empty/".format(site.domain),
                        domain="note.lianpeng.me",
                        title="输入标题",
                        note="",
                        user=request.user,
                        charset="UTF-8",
                        list=l)
    bookmark.save()
    bookmark = Bookmark.objects.get(id=bookmark.id)
    bookmark.url = 'http://{}{}'.format(
        site.domain, reverse('note_detail', args=(bookmark.id, )))
    bookmark.save()
    return redirect('note_edit', id=bookmark.id)
Exemple #23
0
 def post(self, request):
     form = BookmarkForm(request.POST)
     if form.is_valid():
         bookmark = Bookmark()
         bookmark.title = form.cleaned_data.get("title")
         bookmark.url = form.cleaned_data.get("url")
         bookmark.save()
         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)
     return HttpResponseRedirect(reverse('bookmark:home'))
Exemple #24
0
def _sync_github(user, default_list, state):
    website = 'github'
    user_social_auth = user.social_auth.get(provider=website)
    github_username = user_social_auth.extra_data['login']
    page = 1
    while True:
        resp = urllib2.urlopen('https://api.github.com/users/' +
                               github_username + '/starred?page=' + str(page))
        data = resp.read()
        items = json.loads(data)
        for item in items:
            url = item['html_url']
            desc = item['description']
            language = item.get('language', '')
            title = item['full_name']
            try:
                #: if one of the github project url is saved before, stop iterating and return.
                Bookmark.objects.get(list=default_list, url=url, user=user)
                return
            except Bookmark.DoesNotExist:
                bookmark = Bookmark(url=url, user=user, note=desc)
                bookmark.domain = urlparse(url).netloc
                bookmark.title = title
                bookmark.list = default_list
                if language:
                    bookmark.tags = language.lower()
                bookmark.save()
        #: if less than 30, there will no more items, stop fetching data from Github
        if len(items) < 30:
            break
        page += 1

    if state.state != 1:
        state.state = 1
        state.list = default_list
        state.save()
Exemple #25
0
def social_post():
    """ Social post method to make a post """
    form = PostForm()
    bookmark = None

    if form.validate_on_submit():
        form_url = form.url.data
        form_post = form.post.data

        # check if article is already a bookmark
        if Bookmark.query.filter_by(url=form_url).first():
            # if it is, grab relevant info to add to post
            bookmark = Bookmark.query.filter_by(url=form_url).first()
        else:
            # if it isn't, create new PostArticle
            url_to_scrape = form_url
            meta = article_meta_scrape(current_user, url_to_scrape)

            if meta:
                # find or create 'Posts' collection
                if Collection.query.filter_by(user_id=current_user.id,
                                              name='Posts').first():
                    collection = Collection.query.filter_by(
                        user_id=current_user.id, name='Posts').first()
                else:
                    collection = Collection(
                        'Posts', current_user.id, 0,
                        'https://dummyimage.com/1920x1280/000/fff&text=Posts',
                        '', 'Posts')
                    db.session.add(collection)
                    db.session.flush()

                if collection:
                    collection.num_bookmarks += 1
                    db.session.flush()

                    # add bookmark to db
                    title = meta['title']
                    description = meta['description']
                    # description = 'this is a test'
                    url = meta['url']
                    image = meta['image']
                    source = meta['source']
                    slug = slugify(title)
                    published_on = datetime.utcnow()
                    if Category.query.filter_by(name='Posts').first():
                        category = Category.query.filter_by(
                            name='Posts').first()
                    else:
                        category = Category('Posts')
                        db.session.add(category)
                        db.session.flush()

                    bookmark = Bookmark(collection, current_user, category,
                                        slug, url, title, source, published_on,
                                        description, image, None, None)
                    db.session.add(bookmark)
                    db.session.flush()

        # create post
        if bookmark:
            post = Post(current_user.id, bookmark.collection_id, bookmark.id,
                        form_url, bookmark.title, bookmark.description,
                        bookmark.text, bookmark.image, bookmark.tags,
                        bookmark.published_on, bookmark.source, form_post,
                        datetime.utcnow(), 0, 0, 0)
            db.session.add(post)
            db.session.commit()

    return redirect(url_for('social'))
Exemple #26
0
def handle_imported_file(data, user, site, list_name):
    soup = BeautifulSoup(data)
    entries = soup.findAll('dt')

    default_list, created = List.objects.get_or_create(name=list_name,
                                                       user=user)

    for entry in entries:
        link = entry.find('a')
        bookmark = Bookmark()
        l = None
        date = None
        if not link:
            continue
        for attr in link.attrs:
            if attr[0] == 'href':
                bookmark.url = attr[1]
            if attr[0] == 'add_date':
                stamp = int(attr[1])
                scale = len(attr[1]) - 10
                stamp = stamp / pow(10, scale)
                date = datetime.datetime.utcfromtimestamp(stamp).replace(
                    tzinfo=utc)
                bookmark.created_time = date
            if attr[0] == 'tags':
                tags = attr[1].replace(',', ' ')
                bookmark.tags = tags
            if attr[0] == 'list':
                name = attr[1]
                l, created = List.objects.get_or_create(name=name, user=user)

        if site == 'kippt':
            dd = entry.findNext('dd')
            if dd:
                tags = dd.string
                if tags:
                    tags = re.findall('#([^ ]+)', tags)
                    tags = ' '.join(tags)
                    bookmark.tags = tags
        if site == 'google':
            tag_ele = entry.find('h3')
            if tag_ele:
                tag = tag_ele.string
                continue
            else:
                try:
                    existed_bookmark = Bookmark.objects.get(url=bookmark.url,
                                                            user=user)
                    existed_bookmark.tags = existed_bookmark.tags + " " + tag
                    existed_bookmark.save()
                    continue
                except Bookmark.DoesNotExist:
                    try:
                        bookmark.tags = bookmark.tags + " " + tag
                    except UnboundLocalError:
                        pass
                except:
                    continue

        bookmark.domain = urlparse(bookmark.url).netloc
        if l:
            bookmark.list = l
        else:
            bookmark.list = default_list
        bookmark.title = link.string
        bookmark.user = user
        # we can not have two bookmark with the same url in the same list
        try:
            Bookmark.objects.get(url=bookmark.url, list=bookmark.list)
            continue
        except Bookmark.DoesNotExist:
            pass
        try:
            bookmark.save()
        except:
            continue
Exemple #27
0
def deactivate_bookmark(request, pk):
    Bookmark.deactivate(pk=pk)
    next = request.GET.get("next", "/")
    return http.HttpResponseRedirect(next)
Exemple #28
0
def handle_imported_file(data, user, site, list_name):
    soup = BeautifulSoup(data)
    entries = soup.findAll('dt')

    default_list, created = List.objects.get_or_create(name=list_name, user=user)

    for entry in entries:
        link = entry.find('a')
        bookmark = Bookmark()
        l = None
        date = None
        if not link:
            continue
        for attr in link.attrs:
            if attr[0] == 'href':
                bookmark.url = attr[1]
            if attr[0] == 'add_date':
                stamp = int(attr[1])
                scale = len(attr[1]) - 10
                stamp = stamp / pow(10, scale)
                date = datetime.datetime.utcfromtimestamp(stamp).replace(tzinfo=utc)
                bookmark.created_time = date
            if attr[0] == 'tags':
                tags = attr[1].replace(',', ' ')
                bookmark.tags = tags
            if attr[0] == 'list':
                name = attr[1]
                l, created = List.objects.get_or_create(name=name, user=user)

        if site == 'kippt':
            dd = entry.findNext('dd')
            if dd:
                tags = dd.string
                if tags:
                    tags = re.findall('#([^ ]+)', tags)
                    tags = ' '.join(tags)
                    bookmark.tags = tags
        if site == 'google':
            tag_ele = entry.find('h3')
            if tag_ele:
                tag = tag_ele.string
                continue
            else:
                try:
                    existed_bookmark = Bookmark.objects.get(url=bookmark.url, user=user)
                    existed_bookmark.tags = existed_bookmark.tags + " " + tag
                    existed_bookmark.save()
                    continue
                except Bookmark.DoesNotExist:
                    try:
                        bookmark.tags = bookmark.tags + " " + tag
                    except UnboundLocalError:
                        pass
                except:
                    continue
            

        bookmark.domain = urlparse(bookmark.url).netloc
        if l:
            bookmark.list = l
        else:
            bookmark.list = default_list
        bookmark.title = link.string 
        bookmark.user = user
        # we can not have two bookmark with the same url in the same list
        try:
            Bookmark.objects.get(url=bookmark.url, list=bookmark.list)
            continue
        except Bookmark.DoesNotExist:
            pass
        try:
            bookmark.save()
        except:
            continue