Coverage for bookie.views.utils : 70%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
"""View callables for utilities like bookmark imports, etc"""
def import_bmarks(request): """Allow users to upload a delicious bookmark export"""
# if auth fails, it'll raise an HTTPForbidden exception
# we can't let them submit multiple times, check if this user has an # import in process # they have an import, get the information about it and shoot to # the template 'existing': True, 'import_stats': ImportQueueMgr.get_details(username=username) }
# we have some posted values
# save the file off to the temp storage storage_dir=request.registry.settings.get( 'import_files', '/tmp/bookie').format( here=request.registry.settings.get('app_root')), randdir=random.choice(string.letters), )
# make sure the directory exists # we create it with parents as well just in case
out_dir, username, files.filename)
# mark the system that there's a pending import that needs to # be completed # Schedule a task to start this import job.
username=username)) else: msg = request.session.pop_flash() if msg: data['error'] = msg else: data['error'] = None
return data else: # we need to see if they've got # just display the form 'existing': False }
def search(request): """Display the search form to the user""" # if this is a url /username/search then we need to update the search form # action to /username/results rdict = request.matchdict username = rdict.get('username', None) return {'username': username}
renderer="/utils/results_wrap.mako") renderer="/utils/results_wrap.mako") renderer="/utils/results_wrap.mako") renderer="/utils/results_wrap.mako") def search_results(request): """Search for the query terms in the matchdict/GET params
The ones in the matchdict win in the case that they both exist but we'll fall back to query string search=XXX
"""
else:
# Always search the fulltext content
# check if we have a page count submitted
content=with_content, username=username, ct=count, page=page, )
# if the route name is search_ajax we want a json response # else we just want to return the payload data to the mako template 'success': True, 'message': "", 'payload': { 'search_results': [dict(res) for res in res_list], 'result_count': len(res_list), 'phrase': phrase, 'page': page, 'username': username, } } else: 'search_results': res_list, 'count': len(res_list), 'max_count': 50, 'phrase': phrase, 'page': page, 'username': username, }
def export(request): """Handle exporting a user's bookmarks to file""" rdict = request.matchdict username = rdict.get('username')
if request.user is not None: current_user = request.user.username else: current_user = None
bmark_list = Bmark.query.join(Bmark.tags).\ options( contains_eager(Bmark.tags) ).\ join(Bmark.hashed).\ options( contains_eager(Bmark.hashed) ).\ filter(Bmark.username == username).all()
BmarkLog.export(username, current_user)
request.response_content_type = 'text/html'
headers = [('Content-Disposition', 'attachment; filename="bookie_export.html"')] setattr(request, 'response_headerlist', headers)
return { 'bmark_list': bmark_list, }
def redirect(request): """Handle redirecting to the selected url
We want to increment the clicks counter on the bookmark url here
""" rdict = request.matchdict hash_id = rdict.get('hash_id', None) username = rdict.get('username', None)
hashed = Hashed.query.get(hash_id)
if not hashed: # for some reason bad link, 404 return HTTPNotFound()
hashed.clicks = hashed.clicks + 1
if username is not None: bookmark = Bmark.query.\ filter(Bmark.hash_id == hash_id).\ filter(Bmark.username == username).one() bookmark.clicks = bookmark.clicks + 1
return HTTPFound(location=hashed.url) |