def save_bookmark(self, url, desc, ext, tags, dt=None): """Save the bookmark to the db :param url: bookmark url :param desc: one line description :param ext: extended description/notes :param tags: The string of tags to store with this bmark :param mark: Instance of Bmark that we're storing to db """ # we should make sure that this url isn't already bookmarked before # adding it...if the hash matches, you must skip! check_hash = generate_hash(url) if check_hash not in self.hash_list: BmarkMgr.store(url, self.username, desc, ext, tags, dt=dt, inserted_by=IMPORTED) # add this hash to the list so that we can skip dupes in the same # import set self.hash_list.add(check_hash)
def posts_add(request): """Add a new bmark into the system given request params For example usage make sure to check out the unit tests in the test_delicious directory """ params = request.params request.response.content_type = 'text/xml' if 'url' in params and params['url']: # check if we already have this try: mark = BmarkMgr.get_by_url(params['url'], username=request.user.username) mark.description = params.get('description', mark.description) mark.extended = params.get('extended', mark.extended) new_tags = params.get('tags', None) if new_tags: mark.update_tags(new_tags) except NoResultFound: # then let's store this thing # if we have a dt param then set the date to be that manual # date if 'dt' in request.params: # date format by delapi specs: # CCYY-MM-DDThh:mm:ssZ fmt = "%Y-%m-%dT%H:%M:%SZ" stored_time = datetime.strptime(request.params['dt'], fmt) else: stored_time = None mark = BmarkMgr.store(params['url'], request.user.username, params.get('description', ''), params.get('extended', ''), params.get('tags', ''), dt=stored_time, inserted_by="DELICIOUS_API") # if we have content, stick it on the object here if 'content' in request.params: content = StringIO(request.params['content']) content.seek(0) parsed = ReadContent.parse(content, content_type="text/html", url=mark.hashed.url) mark.readable = Readable() mark.readable.content = parsed.content mark.readable.content_type = parsed.content_type mark.readable.status_code = parsed.status mark.readable.status_message = parsed.status_message return '<result code="done" />' else: return '<result code="Bad Request: missing url" />'
def edit_error(request): rdict = request.matchdict params = request.params post = request.POST with ReqAuthorize(request, username=rdict['username']): if 'new' in request.url: BmarkMgr.store(post['url'], request.user.username, post['description'], post['extended'], post['tags']) else: if 'hash_id' in rdict: hash_id = rdict['hash_id'] elif 'hash_id' in params: hash_id = params['hash_id'] bmark = BmarkMgr.get_by_hash(hash_id, request.user.username) if bmark is None: return HTTPNotFound() bmark.fromdict(post) bmark.update_tags(post['tags']) # if this is a new bookmark from a url, offer to go back to that url # for the user. if 'go_back' in params and params['comes_from'] != "": return HTTPFound(location=params['comes_from']) else: return HTTPFound(location=request.route_url( 'user_bmark_recent', username=request.user.username))
def edit_error(request): rdict = request.matchdict params = request.params post = request.POST with ReqAuthorize(request, username=rdict['username']): if 'new' in request.url: BmarkMgr.store(post['url'], request.user.username, post['description'], post['extended'], post['tags']) else: if 'hash_id' in rdict: hash_id = rdict['hash_id'] elif 'hash_id' in params: hash_id = params['hash_id'] bmark = BmarkMgr.get_by_hash(hash_id, request.user.username) if bmark is None: return HTTPNotFound() bmark.fromdict(post) bmark.update_tags(post['tags']) # if this is a new bookmark from a url, offer to go back to that url # for the user. if 'go_back' in params and params['comes_from'] != "": return HTTPFound(location=params['comes_from']) else: return HTTPFound( location=request.route_url('user_bmark_recent', username=request.user.username))
def edit(request): """Manual add a bookmark to the user account Can pass in params (say from a magic bookmarklet later) url description extended tags """ rdict = request.matchdict params = request.params new = False with ReqAuthorize(request, username=rdict['username']): if 'hash_id' in rdict: hash_id = rdict['hash_id'] elif 'hash_id' in params: hash_id = params['hash_id'] else: hash_id = None if hash_id: bmark = BmarkMgr.get_by_hash(hash_id, request.user.username) if bmark is None: return HTTPNotFound() else: # hash the url and make sure that it doesn't exist url = params.get('url', u"") if url != u"": new_url_hash = generate_hash(url) test_exists = BmarkMgr.get_by_hash(new_url_hash, request.user.username) if test_exists: location = request.route_url( 'user_bmark_edit', hash_id=new_url_hash, username=request.user.username) return HTTPFound(location) new = True desc = params.get('description', None) bmark = Bmark(url, request.user.username, desc=desc) tag_suggest = TagMgr.suggestions( url=bmark.hashed.url, username=request.user.username ) return { 'new': new, 'bmark': bmark, 'user': request.user, 'tag_suggest': tag_suggest, }
def count_rrd(): """Add these counts to the rrd graph""" rrd = SystemCounts( ini.get('rrd_data').format(here=HERE), ini.get('rrd_graphs').format(here=HERE)) rrd.mark(datetime.now(), BmarkMgr.count(), BmarkMgr.count(distinct=True), TagMgr.count()) rrd.update()
def posts_add(request): """Add a new bmark into the system given request params For example usage make sure to check out the unit tests in the test_delicious directory """ params = request.GET with Authorize(request.registry.settings.get('api_key', ''), params.get('api_key', None)): request.response_content_type = 'text/xml' if 'url' in params and params['url']: # check if we already have this try: mark = BmarkMgr.get_by_url(params['url']) mark.description = params.get('description', mark.description) mark.extended = params.get('extended', mark.extended) new_tags = params.get('tags', None) if new_tags: mark.update_tags(new_tags) except NoResultFound: # then let's store this thing # if we have a dt param then set the date to be that manual # date if 'dt' in request.params: # date format by delapi specs: # CCYY-MM-DDThh:mm:ssZ fmt = "%Y-%m-%dT%H:%M:%SZ" stored_time = datetime.strptime(request.params['dt'], fmt) else: stored_time = None # we want to store fulltext info so send that along to the # import processor conn_str = request.registry.settings.get( 'sqlalchemy.url', False) fulltext = get_fulltext_handler(conn_str) BmarkMgr.store( params['url'], params.get('description', ''), params.get('extended', ''), params.get('tags', ''), dt=stored_time, fulltext=fulltext, ) return '<result code="done" />' else: return '<result code="Bad Request: missing url" />'
def posts_add(request): """Add a new bmark into the system given request params For example usage make sure to check out the unit tests in the test_delicious directory """ params = request.GET with Authorize(request.registry.settings.get("api_key", ""), params.get("api_key", None)): request.response_content_type = "text/xml" if "url" in params and params["url"]: # check if we already have this try: mark = BmarkMgr.get_by_url(params["url"]) mark.description = params.get("description", mark.description) mark.extended = params.get("extended", mark.extended) new_tags = params.get("tags", None) if new_tags: mark.update_tags(new_tags) except NoResultFound: # then let's store this thing # if we have a dt param then set the date to be that manual # date if "dt" in request.params: # date format by delapi specs: # CCYY-MM-DDThh:mm:ssZ fmt = "%Y-%m-%dT%H:%M:%SZ" stored_time = datetime.strptime(request.params["dt"], fmt) else: stored_time = None # we want to store fulltext info so send that along to the # import processor conn_str = request.registry.settings.get("sqlalchemy.url", False) fulltext = get_fulltext_handler(conn_str) BmarkMgr.store( params["url"], params.get("description", ""), params.get("extended", ""), params.get("tags", ""), dt=stored_time, fulltext=fulltext, ) return '<result code="done" />' else: return '<result code="Bad Request: missing url" />'
def count_rrd(): """Add these counts to the rrd graph""" rrd = SystemCounts( ini.get('rrd_data').format(here=HERE), ini.get('rrd_graphs').format(here=HERE)) rrd.mark( datetime.now(), BmarkMgr.count(), BmarkMgr.count(distinct=True), TagMgr.count() ) rrd.update()
def save_bookmark(self, url, desc, ext, tags, dt=None, fulltext=None): """Save the bookmark to the db :param url: bookmark url :param desc: one line description :param ext: extended description/notes :param tags: The string of tags to store with this bmark :param mark: Instance of Bmark that we're storing to db :param fulltext: Fulltext handler instance used to store that info """ BmarkMgr.store(url, desc, ext, tags, dt=dt, fulltext=fulltext)
def edit_error(request): rdict = request.matchdict params = request.params post = request.POST with ReqAuthorize(request, username=rdict['username']): if 'new' in request.url: try: try: bmark = BmarkMgr.get_by_url(post['url']) except: bmark = None if bmark: return { 'new': False, 'bmark': bmark, 'message': "URL already Exists", 'user': request.user, } bmark = BmarkMgr.store( post['url'], request.user.username, post['description'], post['extended'], post['tags']) # Assign a task to fetch this pages content and parse it out # for storage and indexing. DBSession.flush() tasks.fetch_bmark_content.delay(bmark.bid) except InvalidBookmark, exc: # There was an issue using the supplied data to create a new # bookmark. Send the data back to the user with the error # message. bmark = Bmark( post['url'], request.user.username, desc=post['description'], ext=post['extended'], tags=post['tags']) return { 'new': True, 'bmark': bmark, 'message': exc.message, 'user': request.user, } else:
def posts_get(request): """Return one or more bmarks based on search criteria Supported criteria: - url TBI: - tag={TAG}+{TAG}+ - dt={CCYY-MM-DDThh:mm:ssZ} - hashes={MD5}+{MD5}+...+{MD5} """ params = request.GET request.response_content_type = 'text/xml' try: if 'url' in params and params['url']: url = request.GET['url'] bmark = BmarkMgr.get_by_url(url=url) if not bmark: return HTTPNotFound() # we need to escape any html entities in things return { 'datefound': bmark.stored.strftime('%Y-%m-%d'), 'posts': [bmark], 'escape': escape, } else: request.override_renderer = 'string' return '<result code="Not Found" />' except NoResultFound: request.override_renderer = 'string' return '<result code="Not Found" />'
def admin_bmark_remove(request): """Remove this bookmark from the system""" rdict = request.matchdict username = rdict.get('username') hash_id = rdict.get('hash_id') try: bmark = BmarkMgr.get_by_hash(hash_id, username=username) print bmark if bmark: DBSession.delete(bmark) return { 'message': "done", } else: return { 'error': 'Bookmark not found.', } except NoResultFound: request.response.status_code = 404 return { 'error': 'Bookmark with hash id {0} not found.'.format(rdict['hash_id']) }
def posts_get(request): """Return one or more bmarks based on search criteria Supported criteria: - url TBI: - tag={TAG}+{TAG}+ - dt={CCYY-MM-DDThh:mm:ssZ} - hashes={MD5}+{MD5}+...+{MD5} """ params = request.GET request.response_content_type = "text/xml" try: if "url" in params and params["url"]: url = request.GET["url"] bmark = BmarkMgr.get_by_url(url=url) if not bmark: return HTTPNotFound() # we need to escape any html entities in things return {"datefound": bmark.stored.strftime("%Y-%m-%d"), "posts": [bmark], "escape": escape} else: request.override_renderer = "string" return '<result code="Not Found" />' except NoResultFound: request.override_renderer = "string" return '<result code="Not Found" />'
def bmark_popular(request): """Get a list of the most popular bmarks for the api call""" rdict = request.matchdict params = request.params # check if we have a page count submitted page = int(params.get('page', '0')) count = int(params.get('count', RESULTS_MAX)) with_content = True if 'with_content' in params and params['with_content'] != "false" else False username = request.user.username # thou shalt not have more then the HARD MAX # @todo move this to the .ini as a setting if count > HARD_MAX: count = HARD_MAX # do we have any tags to filter upon tags = rdict.get('tags', None) if isinstance(tags, str): tags = [tags] # if we don't have tags, we might have them sent by a non-js browser as a # string in a query string if not tags and 'tag_filter' in params: tags = params.get('tag_filter').split() popular_list = BmarkMgr.find(limit=count, order_by=Bmark.clicks.desc(), page=page, tags=tags, username=username, with_content=with_content, with_tags=True, ) result_set = [] for res in popular_list: return_obj = dict(res) return_obj['tags'] = [dict(tag[1]) for tag in res.tags.items()] # the hashed object is there as well, we need to pull the url and # clicks from it as total_clicks return_obj['url'] = res.hashed.url return_obj['total_clicks'] = res.hashed.clicks if with_content: return_obj['readable'] = dict(res.hashed.readable) result_set.append(return_obj) return { 'bmarks': result_set, 'max_count': RESULTS_MAX, 'count': len(popular_list), 'page': page, 'tag_filter': tags, }
def posts_get(request): """Return one or more bmarks based on search criteria Supported criteria: - url TBI: - tag={TAG}+{TAG}+ - dt={CCYY-MM-DDThh:mm:ssZ} - hashes={MD5}+{MD5}+...+{MD5} """ params = request.params request.response.content_type = 'text/xml' LOG.debug(params) try: if 'url' in params and params['url']: url = request.params['url'] LOG.debug(url) bmark = BmarkMgr.get_by_url(url=url, username=request.user.username) if not bmark: return HTTPNotFound() # we need to escape any html entities in things return {'datefound': bmark.stored.strftime('%Y-%m-%d'), 'posts': [bmark], 'escape': escape, } else: request.override_renderer = 'string' return '<result code="Not Found" />' except NoResultFound: request.override_renderer = 'string' return '<result code="Not Found" />'
def admin_bmark_remove(request): """Remove this bookmark from the system""" rdict = request.matchdict username = rdict.get('username') hash_id = rdict.get('hash_id') try: bmark = BmarkMgr.get_by_hash(hash_id, username=username) print bmark if bmark: DBSession.delete(bmark) return _api_response(request, { 'message': "done", }) else: return _api_response(request, { 'error': 'Bookmark not found.', }) except NoResultFound: request.response.status_code = 404 return _api_response(request, { 'error': 'Bookmark with hash id {0} not found.'.format( rdict['hash_id']) })
def __init__(self, import_io, username=None): """work on getting an importer instance""" self.file_handle = import_io self.username = username # we need to get our list of hashes to make sure we check for dupes self.hash_list = set([b[0] for b in BmarkMgr.hash_list(username=username)])
def save_bookmark(self, url, desc, ext, tags, dt=None): """Save the bookmark to the db :param url: bookmark url :param desc: one line description :param ext: extended description/notes :param tags: The string of tags to store with this bmark :param mark: Instance of Bmark that we're storing to db """ # If a bookmark has the tag "private" then we ignore it to prevent # leaking user data. if tags and "private" in tags.lower().split(" "): return None check_hash = generate_hash(url) # We should make sure that this url isn't already bookmarked before # adding it...if the hash matches, you must skip! if check_hash not in self.hash_list: bmark = BmarkMgr.store(url, self.username, desc, ext, tags, dt=dt, inserted_by=IMPORTED) # Add this hash to the list so that we can skip dupes in the # same import set. self.hash_list.add(check_hash) return bmark # If we don't store a bookmark then just return None back to the # importer. return None
def bmark_list(request): """Display the list of bookmarks for this tag""" rdict = request.matchdict # check if we have a page count submitted tag = rdict.get('tag') page = int(rdict.get('page', 0)) # verify the tag exists before we go on # 404 if the tag isn't found exists = TagMgr.find(tags=[tag]) if not exists: raise HTTPNotFound() bmarks = BmarkMgr.by_tag(tag, limit=RESULTS_MAX, page=page) return {'tag': tag, 'bmark_list': bmarks, 'max_count': RESULTS_MAX, 'count': len(bmarks), 'page': page, 'allow_edit': access.edit_enabled(request.registry.settings), }
def test_unique_ct(self): """Verify that our unique count method is working""" ct = 5 common = 'testing.com' users = [] for i in range(ct): user = User() user.username = gen_random_word(10) DBSession.add(user) users.append(user) for i in range(ct - 2): b = Bmark( url=gen_random_word(12), username=users[i].username ) DBSession.add(b) # Add in our dupes c = Bmark( url=common, username=users[3].username ) DBSession.add(c) DBSession.flush() d = Bmark( url=common, username=users[4].username ) DBSession.add(d) DBSession.flush() ct = BmarkMgr.count(distinct=True) eq_(4, ct, 'We should have a total of 4: ' + str(ct))
def test_per_user(self): """We should only get a pair of results for this single user""" ct = 5 common = 'testing.com' user = User() user.username = gen_random_word(10) DBSession.add(user) usercommon = User() usercommon.username = common DBSession.add(usercommon) for i in range(ct - 2): b = Bmark(url=gen_random_word(12), username=user.username) DBSession.add(b) # add in our dupes c = Bmark( url=gen_random_word(10), username=usercommon.username, ) DBSession.add(c) DBSession.flush() d = Bmark( url=gen_random_word(10), username=usercommon.username, ) DBSession.add(d) DBSession.flush() ct = BmarkMgr.count(username=usercommon.username) eq_(2, ct, 'We should have a total of 2: ' + str(ct))
def bmark_list(request): """Display the list of bookmarks for this tag""" rdict = request.matchdict # check if we have a page count submitted tag = rdict.get('tag') page = int(rdict.get('page', 0)) # verify the tag exists before we go on # 404 if the tag isn't found exists = TagMgr.find(tags=[tag]) if not exists: raise HTTPNotFound() bmarks = BmarkMgr.by_tag(tag, limit=RESULTS_MAX, page=page) return { 'tag': tag, 'bmark_list': bmarks, 'max_count': RESULTS_MAX, 'count': len(bmarks), 'page': page, 'allow_edit': access.edit_enabled(request.registry.settings), }
def test_unique_ct(self): """Verify that our unique count method is working""" ct = 5 common = 'testing.com' users = [] for i in range(ct): user = User() user.username = gen_random_word(10) DBSession.add(user) users.append(user) for i in range(ct - 2): b = Bmark(url=gen_random_word(12), username=users[i].username) DBSession.add(b) # Add in our dupes c = Bmark(url=common, username=users[3].username) DBSession.add(c) DBSession.flush() d = Bmark(url=common, username=users[4].username) DBSession.add(d) DBSession.flush() ct = BmarkMgr.count(distinct=True) eq_(4, ct, 'We should have a total of 4: ' + str(ct))
def bmark_list(request): """Display the list of bookmarks for this tag""" rdict = request.matchdict params = request.params # check if we have a page count submitted tags = rdict.get('tags') username = rdict.get("username", None) page = int(params.get('page', 0)) # verify the tag exists before we go on # 404 if the tag isn't found exists = TagMgr.find(tags=tags) if not exists: raise HTTPNotFound() bmarks = BmarkMgr.find(tags=tags, limit=RESULTS_MAX, page=page, username=username) return { 'tags': tags, 'bmark_list': bmarks, 'max_count': RESULTS_MAX, 'count': len(bmarks), 'page': page, 'username': username, }
def __init__(self, import_io, username=None): """work on getting an importer instance""" self.file_handle = import_io self.username = username # we need to get our list of hashes to make sure we check for dupes self.hash_list = set( [b[0] for b in BmarkMgr.hash_list(username=username)])
def save_bookmark(self, url, desc, ext, tags, dt=None): """Save the bookmark to the db :param url: bookmark url :param desc: one line description :param ext: extended description/notes :param tags: The string of tags to store with this bmark :param mark: Instance of Bmark that we're storing to db """ BmarkMgr.store(url, self.username, desc, ext, tags, dt=dt, inserted_by=IMPORTED)
def extension_sync(request): """Return a list of the bookmarks we know of in the system For right now, send down a list of hash_ids """ username = request.user.username hash_list = BmarkMgr.hash_list(username=username) return {'hash_list': [hash[0] for hash in hash_list]}
def extension_sync(request): """Return a list of the bookmarks we know of in the system For right now, send down a list of hash_ids """ username = request.user.username hash_list = BmarkMgr.hash_list(username=username) return { 'hash_list': [hash[0] for hash in hash_list] }
def bmark_get(request): """Return a bookmark requested via hash_id We need to return a nested object with parts bmark - readable """ rdict = request.matchdict params = request.params hash_id = rdict.get('hash_id', None) username = request.user.username # the hash id will always be there or the route won't match bookmark = BmarkMgr.get_by_hash(hash_id, username=username) last_bmark = {} if 'last_bmark' in params and params['last_bmark'] != "false": last = BmarkMgr.get_recent_bmark(username=username) if last is not None: last_bmark = {'last': dict(last)} if bookmark is None: request.response.status_int = 404 ret = {'error': "Bookmark for hash id {0} not found".format(hash_id)} ret.update(last_bmark) return ret else: return_obj = dict(bookmark) if 'with_content' in params and params['with_content'] != 'false': if bookmark.readable: return_obj['readable'] = dict(bookmark.readable) return_obj['tags'] = [dict(tag[1]) for tag in bookmark.tags.items()] ret = {'bmark': return_obj} ret.update(last_bmark) return ret
def popular(request): """Most popular list of bookmarks capped at MAX""" rdict = request.matchdict params = request.params # check if we have a page count submitted tags = rdict.get('tags', None) page = int(params.get('page', '0')) recent_list = BmarkMgr.find(limit=RESULTS_MAX, order_by=Bmark.stored.desc(), tags=tags, page=page) return { 'bmarks': recent_list, 'max_count': RESULTS_MAX, 'count': len(recent_list), 'page': page, 'allow_edit': access.edit_enabled(request.registry.settings), } rdict = request.matchdict # check if we have a page count submitted page = int(rdict.get('page', '0')) popular_list = BmarkMgr.popular(limit=RESULTS_MAX, with_tags=True, page=page) return { 'bmarks': popular_list, 'max_count': RESULTS_MAX, 'count': len(popular_list), 'page': page, 'allow_edit': access.edit_enabled(request.registry.settings), }
def dashboard(self): """A public dashboard of the system""" # Generate some user data and stats user_count = UserMgr.count() pending_activations = ActivationMgr.count() # Generate some bookmark data. bookmark_count = BmarkMgr.count() unique_url_count = BmarkMgr.count(distinct=True) users_with_bookmarks = BmarkMgr.count(distinct_users=True) return { 'bookmark_data': { 'count': bookmark_count, 'unique_count': unique_url_count, }, 'user_data': { 'count': user_count, 'activations': pending_activations, 'with_bookmarks': users_with_bookmarks, } }
def test_total_ct(self): """Verify that our total count method is working""" ct = 5 user = User() user.username = gen_random_word(10) DBSession.add(user) for i in range(ct): b = Bmark(url=gen_random_word(12), username=user.username) b.hash_id = gen_random_word(3) DBSession.add(b) ct = BmarkMgr.count() eq_(5, ct, 'We should have a total of 5: ' + str(ct))
def dashboard(request): """A public dashboard of the system """ # Generate some user data and stats user_count = UserMgr.count() pending_activations = ActivationMgr.count() # Generate some bookmark data. bookmark_count = BmarkMgr.count() unique_url_count = BmarkMgr.count(distinct=True) users_with_bookmarks = BmarkMgr.count(distinct_users=True) return { 'bookmark_data': { 'count': bookmark_count, 'unique_count': unique_url_count, }, 'user_data': { 'count': user_count, 'activations': pending_activations, 'with_bookmarks': users_with_bookmarks, } }
def readable(request): """Display a readable version of this url if we can""" rdict = request.matchdict bid = rdict.get('hash_id', None) username = rdict.get('username', None) if bid: found = BmarkMgr.get_by_hash(bid, username=username) if found: return { 'bmark': found, 'username': username, } else: return HTTPNotFound()
def bmark_list(request): """Display the list of bookmarks for this tag""" route_name = request.matched_route.name rdict = request.matchdict params = request.params # check if we have a page count submitted tags = rdict.get('tags') page = int(params.get('page', 0)) # verify the tag exists before we go on # 404 if the tag isn't found exists = TagMgr.find(tags=tags) if not exists: raise HTTPNotFound() bmarks = BmarkMgr.find(tags=tags, limit=RESULTS_MAX, page=page,) if 'ajax' in route_name: html = render('bookie:templates/tag/bmarks.mako', { 'tags': tags, 'bmark_list': bmarks, 'max_count': RESULTS_MAX, 'count': len(bmarks), 'page': page, 'allow_edit': access.edit_enabled(request.registry.settings), }, request=request) return { 'success': True, 'message': "", 'payload': { 'html': html, } } else: return {'tags': tags, 'bmark_list': bmarks, 'max_count': RESULTS_MAX, 'count': len(bmarks), 'page': page, 'allow_edit': access.edit_enabled(request.registry.settings), }
def recent(request): """Most recent list of bookmarks capped at MAX""" rdict = request.matchdict # check if we have a page count submitted page = int(rdict.get('page', '0')) recent_list = BmarkMgr.recent(limit=RESULTS_MAX, with_tags=True, page=page) return { 'bmarks': recent_list, 'max_count': RESULTS_MAX, 'count': len(recent_list), 'page': page, 'allow_edit': access.edit_enabled(request.registry.settings), }
def test_total_ct(self): """Verify that our total count method is working""" ct = 5 user = User() user.username = gen_random_word(10) DBSession.add(user) for i in range(ct): b = Bmark( url=gen_random_word(12), username=user.username ) b.hash_id = gen_random_word(3) DBSession.add(b) ct = BmarkMgr.count() eq_(5, ct, 'We should have a total of 5: ' + str(ct))
def posts_delete(request): """Remove a bmark from the system""" params = request.params request.response.content_type = 'text/xml' if 'url' in params and params['url']: try: bmark = BmarkMgr.get_by_url(params['url'], username=request.user.username) session = DBSession() session.delete(bmark) return '<result code="done" />' except NoResultFound: # if it's not found, then there's not a bookmark to delete return '<result code="Bad Request: bookmark not found" />'
def bmark_remove(request): """Remove this bookmark from the system""" rdict = request.matchdict user = request.user try: bmark = BmarkMgr.get_by_hash(rdict['hash_id'], username=user.username) DBSession.delete(bmark) return { 'message': "done", } except NoResultFound: request.response.status_code = 404 return { 'error': 'Bookmark with hash id {0} not found.'.format(rdict['hash_id']) }
def posts_delete(request): """Remove a bmark from the system""" params = request.GET request.response_content_type = "text/xml" with Authorize(request.registry.settings.get("api_key", ""), params.get("api_key", None)): if "url" in params and params["url"]: try: bmark = BmarkMgr.get_by_url(params["url"]) session = DBSession() session.delete(bmark) return '<result code="done" />' except NoResultFound: # if it's not found, then there's not a bookmark to delete return '<result code="Bad Request: bookmark not found" />'
def bmark_remove(request): """Remove this bookmark from the system""" rdict = request.matchdict user = request.user try: bmark = BmarkMgr.get_by_hash(rdict['hash_id'], username=user.username) DBSession.delete(bmark) return { 'message': "done", } except NoResultFound: request.response.status_code = 404 return { 'error': 'Bookmark with hash id {0} not found.'.format( rdict['hash_id']) }
def posts_delete(request): """Remove a bmark from the system""" params = request.GET request.response_content_type = 'text/xml' with Authorize(request.registry.settings.get('api_key', ''), params.get('api_key', None)): if 'url' in params and params['url']: try: bmark = BmarkMgr.get_by_url(params['url']) session = DBSession() session.delete(bmark) return '<result code="done" />' except NoResultFound: # if it's not found, then there's not a bookmark to delete return '<result code="Bad Request: bookmark not found" />'
def bmark_export(request): """Export via the api call to json dump """ username = request.user.username bmark_list = BmarkMgr.user_dump(username) # log that the user exported this BmarkLog.export(username, username) def build_bmark(bmark): d = dict(bmark) d['hashed'] = dict(bmark.hashed) return d return { 'bmarks': [build_bmark(bmark) for bmark in bmark_list], 'count': len(bmark_list), 'date': str(datetime.now()) }
def bmark_export(request): """Export via the api call to json dump """ username = request.user.username bmark_list = BmarkMgr.user_dump(username) # log that the user exported this BmarkLog.export(username, username) def build_bmark(bmark): d = dict(bmark) d['hashed'] = dict(bmark.hashed) return d return { 'bmarks': [build_bmark(bmark) for bmark in bmark_list], 'count': len(bmark_list), 'date': str(datetime.utcnow()) }
def recent(request): """Most recent list of bookmarks capped at MAX""" rdict = request.matchdict params = request.params LOG.debug('in recent!') # check if we have a page count submitted page = int(params.get('page', '0')) # do we have any tags to filter upon tags = rdict.get('tags', None) if isinstance(tags, str): tags = [tags] # check for auth related stuff # are we looking for a specific user username = rdict.get('username', None) # if we don't have tags, we might have them sent by a non-js browser as a # string in a query string if not tags and 'tag_filter' in params: tags = params.get('tag_filter').split() recent_list = BmarkMgr.find(limit=RESULTS_MAX, order_by=Bmark.stored.desc(), tags=tags, page=page, username=username) ret = { 'bmarks': recent_list, 'max_count': RESULTS_MAX, 'count': len(recent_list), 'page': page, 'tags': tags, 'username': username, } return ret
def bmark_popular(request): """Get a list of the most popular bmarks for the api call""" rdict = request.matchdict params = request.params # check if we have a page count submitted page = int(params.get('page', '0')) count = int(params.get('count', RESULTS_MAX)) with_content = _check_with_content(params) if request.user and request.user.username: username = request.user.username else: username = None # thou shalt not have more then the HARD MAX # @todo move this to the .ini as a setting if count > HARD_MAX: count = HARD_MAX # do we have any tags to filter upon tags = rdict.get('tags', None) if isinstance(tags, str): tags = [tags] # if we don't have tags, we might have them sent by a non-js browser as a # string in a query string if not tags and 'tag_filter' in params: tags = params.get('tag_filter').split() popular_list = BmarkMgr.find( limit=count, order_by=Bmark.clicks.desc(), page=page, tags=tags, username=username, with_content=with_content, with_tags=True, ) result_set = [] for res in popular_list: return_obj = dict(res) return_obj['tags'] = [dict(tag[1]) for tag in res.tags.items()] # the hashed object is there as well, we need to pull the url and # clicks from it as total_clicks return_obj['url'] = res.hashed.url return_obj['total_clicks'] = res.hashed.clicks if with_content: return_obj['readable'] = dict(res.hashed.readable) result_set.append(return_obj) return { 'bmarks': result_set, 'max_count': RESULTS_MAX, 'count': len(popular_list), 'page': page, 'tag_filter': tags, }
def bmark_add(request): """Add a new bookmark to the system""" rdict = request.matchdict if 'url' in request.params or 'hash_id' in request.params: params = request.params elif 'url' in request.json_body or 'hash_id' in request.json_body: params = request.json_body else: request.response.status_int = 400 return { 'error': 'Bad Request: missing url', } user = request.user if 'url' not in params and 'hash_id' not in rdict: request.response.status_int = 400 return { 'error': 'Bad Request: missing url', } elif 'hash_id' in rdict: try: mark = BmarkMgr.get_by_hash(rdict['hash_id'], username=user.username) mark = _update_mark(mark, params) except NoResultFound: request.response.status_code = 404 return { 'error': 'Bookmark with hash id {0} not found.'.format(rdict['hash_id']) } else: # check if we already have this try: mark = BmarkMgr.get_by_url(params['url'], username=user.username) mark = _update_mark(mark, params) except NoResultFound: # then let's store this thing # if we have a dt param then set the date to be that manual # date if 'dt' in request.params: # date format by delapi specs: # CCYY-MM-DDThh:mm:ssZ fmt = "%Y-%m-%dT%H:%M:%SZ" stored_time = datetime.strptime(request.params['dt'], fmt) else: stored_time = None # check to see if we know where this is coming from inserted_by = params.get('inserted_by', 'unknown_api') mark = BmarkMgr.store( params['url'], user.username, params.get('description', ''), params.get('extended', ''), params.get('tags', ''), dt=stored_time, inserted_by=inserted_by, ) # we need to process any commands associated as well commander = Commander(mark) mark = commander.process() # if we have content, stick it on the object here if 'content' in params: content = StringIO(params['content']) content.seek(0) parsed = ReadContent.parse(content, content_type="text/html", url=mark.hashed.url) mark.readable = Readable() mark.readable.content = parsed.content mark.readable.content_type = parsed.content_type mark.readable.status_code = parsed.status mark.readable.status_message = parsed.status_message # we need to flush here for new tag ids, etc DBSession.flush() mark_data = dict(mark) mark_data['tags'] = [dict(mark.tags[tag]) for tag in mark.tags.keys()] return { 'bmark': mark_data, 'location': request.route_url('bmark_readable', hash_id=mark.hash_id, username=user.username), }
def count_total_bookmarks(): """Count the total number of bookmarks in the system""" total = BmarkMgr.count() stat = StatBookmark(attrib=TOTAL_CT, data=total) DBSession.add(stat)
def count_unique_bookmarks(): """Count the unique number of bookmarks in the system""" total = BmarkMgr.count(distinct=True) stat = StatBookmark(attrib=UNIQUE_CT, data=total) DBSession.add(stat)
tags=post['tags']) return { 'new': True, 'bmark': bmark, 'message': exc.message, 'user': request.user, } else: if 'hash_id' in rdict: hash_id = rdict['hash_id'] elif 'hash_id' in params: hash_id = params['hash_id'] bmark = BmarkMgr.get_by_hash(hash_id, request.user.username) if bmark is None: return HTTPNotFound() bmark.fromdict(post) bmark.update_tags(post['tags']) # if this is a new bookmark from a url, offer to go back to that url # for the user. if 'go_back' in params and params['comes_from'] != "": return HTTPFound(location=params['comes_from']) else: return HTTPFound( location=request.route_url('user_bmark_recent', username=request.user.username))
def bmark_recent(request, with_content=False): """Get a list of the bmarks for the api call""" rdict = request.matchdict params = request.params # check if we have a page count submitted page = int(params.get('page', '0')) count = int(params.get('count', RESULTS_MAX)) # we only want to do the username if the username is in the url username = rdict.get('username', None) # thou shalt not have more then the HARD MAX # @todo move this to the .ini as a setting if count > HARD_MAX: count = HARD_MAX # do we have any tags to filter upon tags = rdict.get('tags', None) if isinstance(tags, str): tags = [tags] # if we don't have tags, we might have them sent by a non-js browser as a # string in a query string if not tags and 'tag_filter' in params: tags = params.get('tag_filter').split() # @todo fix this! # if we allow showing of content the query hangs and fails on the # postgres side. Need to check the query and figure out what's up. # see bug #142 # We don't allow with_content by default because of this bug. recent_list = BmarkMgr.find( limit=count, order_by=Bmark.stored.desc(), page=page, tags=tags, username=username, with_tags=True, ) result_set = [] for res in recent_list: return_obj = dict(res) return_obj['tags'] = [dict(tag[1]) for tag in res.tags.items()] # we should have the hashed information, we need the url and clicks as # total clicks to send back return_obj['url'] = res.hashed.url return_obj['total_clicks'] = res.hashed.clicks if with_content: return_obj['readable'] = dict(res.readable) if res.readable else {} result_set.append(return_obj) return { 'bmarks': result_set, 'max_count': RESULTS_MAX, 'count': len(recent_list), 'page': page, 'tag_filter': tags, }