def test_is_submitted_cust_2(self): """tests if a form is submitted in case of POST request""" with self.app.test_request_context('/feedback/', method='POST', data=dict(name="Johnny Miller",email="*****@*****.**", feedback_type="bug",feedback_text='foobar')): form = MyTestForm(request.values) self.assertTrue(form.is_submitted()) self.assertTrue(ff.is_submitted_cust(form))
def metrics(**args): """ Entry point of input for Metrics: form to input bibcodes """ # query = request.args.get('q', None) form = MetricsInputForm() bibcodes = [] results = None format = '' if 'bibcodes' in args: bibcodes = args['bibcodes'] format = 'json' if is_submitted_cust(form): # the form was submitted, so get the contents from the submit box # make sure we have a list of what seem to be bibcodes bibcodes = map(lambda a: str(a).strip(), form.bibcodes.data.strip().split('\n')) bibcodes = filter(lambda a: len(a) == 19, bibcodes) # no bibcodes? display message and re-display input form if len(bibcodes) == 0: flash('Metrics returned no results. Reason: No valid bibcodes were supplied') return render_template('metrics.html', form=form) if len(bibcodes) > 0: # we have a list of bibcodes, so start working try: results = generate_metrics(bibcodes=bibcodes, types='statistics,histograms,metrics,series', fmt=format) except Exception, err: app.logger.error('ID %s. Unable to get results! (%s)' % (g.user_cookie_id,err))
def activate(): """ User activation form """ #if the user is logged in he needs to logout before signup as another user if current_user.is_authenticated(): flash('Please, <a href="%s">logout</a> before activate another user' % url_for('user.logout'), 'warn') return redirect(generate_redirect_url()) #if everything looks fine, it's time to proceed form_params_creation ={'csrf_enabled':False} if request.method == 'GET' and request.values.get('id'): #need to specify the value to initialize the form because it cannot find this value in request.form form_params_creation.update({'id': request.values.get('id')}) form = ActivateUserForm(**form_params_creation) if is_submitted_cust(form): if form.validate(): success, message, message_type = activate_user(form.id.data) if not success: flash('Activation failed: %s' % message, message_type) statsd.incr("user.activate.success") else: flash('Your account is now active.', 'success') statsd.incr("user.activate.failed") session['user_login_email'] = None return redirect(generate_redirect_url(url_for('user.login'))) elif not session.get('user_login_email'): #if there is no email in the session, the user needs to provide it pre_form = PreActivateUserForm(csrf_enabled=False) if not is_submitted_cust(pre_form) or not pre_form.validate(): return render_template('activate.html', form=pre_form, pre_activation=True) #if there is the email as input, check if the user exists locally and is not active if not login_exists_local(pre_form.act_em.data): if not login_exists_classic(pre_form.act_em.data): flash('There is no user with the provided email. Please sign up first.', 'error') return redirect(generate_redirect_url(url_for('user.signup'))) else: flash('The user is already active. Please log in.', 'warn') return redirect(generate_redirect_url(url_for('user.login'))) elif login_is_active(pre_form.act_em.data): flash('The user is already active. Please log in.', 'warn') return redirect(generate_redirect_url(url_for('user.login'))) #if everything looks fine with the user login email, it can be stored in the session session['user_login_email'] = pre_form.act_em.data return render_template('activate.html', form=form, pre_activation=False)
def citation_helper(**args): """ Entry point of input for Citation Helper: form to input bibcodes """ form = CitationHelperInputForm() # If we were called from results form, get bibcodes # from URL try: bibcodes = request.args.getlist('bibcode') except: bibcodes = [] results = None format = '' # check if we were called with bibcodes if 'bibcodes' in args: bibcodes = args['bibcodes'] format = 'json' if 'Nsuggest' in args: number_of_suggestions = args['Nsuggest'] else: number_of_suggestions = config.BIBUTILS_DEFAULT_SUGGESTIONS if is_submitted_cust(form): # the form was submitted, so get the contents from the submit box # make sure we have a list of what seem to be bibcodes bibcodes = map(lambda a: str(a).strip(), form.bibcodes.data.strip().split('\n')) bibcodes = filter(lambda a: len(a) == 19, bibcodes) # no bibcodes? display message and re-display input form if len(bibcodes) == 0: flash('Citation Helper returned no results. Reason: No bibcodes were supplied') return render_template('citation_helper.html', form=form) # get the maximum number of suggestions try: number_of_suggestions = int(form.return_nr.data) except: number_of_suggestions = config.BIBUTILS_DEFAULT_SUGGESTIONS layout = form.layout.data or "NO" app.logger.info('ID %s. Requesting %s suggestions. Input: %s'%(g.user_cookie_id,number_of_suggestions,str(bibcodes))) if len(bibcodes) > 0: # we have all we need, so it's time to get the suggestions try: suggestions = get_suggestions(bibcodes=bibcodes,Nsuggest=number_of_suggestions) except CitationHelperCannotGetResults, e: # if we end up here, something went boink during data retrieval app.logger.error('ID %s. Unable to get results! (%s)' % (g.user_cookie_id,e)) # if we got results, return them, otherwise say that we did not find any, # and re-display the input form. # no suggestions are possible if there are no 'friends-of-friends' if len(suggestions) > 0: app.logger.info('ID %s. Found %s suggestions. Suggestions: %s'%(g.user_cookie_id,len(suggestions),str(suggestions))) if format == 'json': return jsonify(suggestions=suggestions) else: return render_template('citation_helper_results.html', page_var='Citation Helper Results', results=suggestions, include_layout=layout) else: app.logger.info('ID %s. No suggestions found.'%g.user_cookie_id) if layout == 'NO': return render_template('citation_helper_no_results.html') else: flash('Citation Helper returned no results. Reason: No suggestions were found.')
def suggestions(cookie,format): """ Get personal suggestions from recently viewed articles """ bibcodes = [] if not cookie: cookie = None form = SuggestionsInputForm() if is_submitted_cust(form): # the form was submitted, so get the contents from the submit box # make sure we have a list of what seem to be bibcodes bibcodes = map(lambda a: str(a).strip(), form.bibcodes.data.strip().split('\n')) bibcodes = filter(lambda a: len(a) == 19, bibcodes) # no bibcodes? display message and re-display input form if len(bibcodes) == 0: flash('No bibcodes were supplied') return render_template('suggestions.html', form=form) elif not cookie and len(bibcodes) == 0: return render_template('suggestions.html', form=form) try: results = get_suggestions(cookie=cookie,bibcodes=bibcodes) except RecommenderCannotGetResults, e: app.logger.error('ID %s. Unable to get results! (%s)' % (g.user_cookie_id,e))
def test_is_submitted_cust_1(self): """tests if a form is submitted in case of GET request""" with self.app.test_request_context('/feedback/?name=Johnny+Miller&[email protected]&feedback_type=bug&feedback_text=foobar'): form = MyTestForm(request.values) self.assertFalse(form.is_submitted()) self.assertTrue(ff.is_submitted_cust(form))
'Content-Type': 'text/xls; charset=UTF-8', 'Content-Disposition': 'attachment; filename=\"Metrics.xls\";', 'Content-Transfer-Encoding': 'binary', 'Content-Length': len(response.data) }) response.headers = response_headers response.set_cookie('fileDownload', 'true', path='/') return response results = None format = '' layout = 'YES' if 'bibcodes' in args: bibcodes = args['bibcodes'] format = 'json' if is_submitted_cust(form): try: layout = form.layout.data except: layout = 'NO' # the form was submitted, so get the contents from the submit box # make sure we have a list of what seem to be bibcodes bibcodes = map(lambda a: str(a).strip(), form.bibcodes.data.strip().split('\n')) bibcodes = filter(lambda a: len(a) == 19, bibcodes) # no bibcodes? display message and re-display input form if len(bibcodes) == 0: flash('Metrics returned no results. Reason: No valid bibcodes were supplied') return render_template('metrics.html', form=form) if len(bibcodes) > 0: # we have a list of bibcodes, so start working try:
def citation_helper(**args): """ Entry point of input for Citation Helper: form to input bibcodes """ form = CitationHelperInputForm() # If we were called from results form, get bibcodes # from URL try: bibcodes = request.args.getlist('bibcode') except: bibcodes = [] results = None format = '' # check if we were called with bibcodes if 'bibcodes' in args: bibcodes = args['bibcodes'] format = 'json' if 'Nsuggest' in args: number_of_suggestions = args['Nsuggest'] else: number_of_suggestions = config.BIBUTILS_DEFAULT_SUGGESTIONS if is_submitted_cust(form): try: number_of_records = int(form.numRecs.data) except: number_of_records = config.MAX_EXPORTS['citation_helper'] # the form was submitted, so get the contents from the submit box # make sure we have a list of what seem to be bibcodes bibcodes = map(lambda a: str(a).strip(), form.bibcodes.data.strip().split('\n')) bibcodes = filter(lambda a: len(a) == 19, bibcodes) list_type = request.values.get('list_type', None) if len(bibcodes) == 0: try: query_par = str(form.current_search_parameters.data.strip()) query = json.loads(query_par)['q'] sort = json.loads(query_par).get('sort', None) bigquery_id = form.bigquery.data if sort is None: # this might be an abstract citation/reference list view so get the sort from config if list_type is not None and list_type in config.ABS_SORT_OPTIONS_MAP: sort = [config.ABS_SORT_OPTIONS_MAP[list_type]] else: sort = [] bibcodes = get_publications_from_query(query, sort, list_type, bigquery_id)[:number_of_records] except: bibcodes = [] # no bibcodes? display message and re-display input form if len(bibcodes) == 0: flash('Citation Helper returned no results. Reason: No bibcodes were supplied') return render_template('citation_helper.html', form=form) # get the maximum number of suggestions try: number_of_suggestions = int(form.return_nr.data) except: number_of_suggestions = config.BIBUTILS_DEFAULT_SUGGESTIONS layout = form.layout.data or "NO" app.logger.info('ID %s. Requesting %s suggestions. Input: %s'%(g.user_cookie_id,number_of_suggestions,str(bibcodes))) if len(bibcodes) > 0: # we have all we need, so it's time to get the suggestions try: suggestions = get_suggestions(bibcodes=bibcodes,Nsuggest=number_of_suggestions) except CitationHelperCannotGetResults, e: # if we end up here, something went boink during data retrieval app.logger.error('ID %s. Unable to get results! (%s)' % (g.user_cookie_id,e)) # if we got results, return them, otherwise say that we did not find any, # and re-display the input form. # no suggestions are possible if there are no 'friends-of-friends' if len(suggestions) > 0: app.logger.info('ID %s. Found %s suggestions. Suggestions: %s'%(g.user_cookie_id,len(suggestions),str(suggestions))) if format == 'json': return jsonify(suggestions=suggestions) else: return render_template('citation_helper_results.html', page_var='Citation Helper Results', results=suggestions, include_layout=layout) else: app.logger.info('ID %s. No suggestions found.'%g.user_cookie_id) if layout == 'NO': return render_template('citation_helper_no_results.html') else: flash('Citation Helper returned no results. Reason: No suggestions were found.')