def create_form(): # create a new form if not current_user.upgraded: g.log.info('Failed to create form from dashboard. User is not upgraded.') return jsonerror(402, {'error': "Please upgrade your account."}) if request.get_json(): email = request.get_json().get('email') url = request.get_json().get('url') sitewide = request.get_json().get('sitewide') else: email = request.form.get('email') url = request.form.get('url') sitewide = request.form.get('sitewide') g.log = g.log.bind(email=email, url=url, sitewide=sitewide) if not IS_VALID_EMAIL(email): g.log.info('Failed to create form from dashboard. Invalid address.') if request_wants_json(): return jsonerror(400, {'error': "The provided email address is not valid."}) else: flash('The provided email address is not valid.', 'error') return redirect(url_for('dashboard')) g.log.info('Creating a new form from the dashboard.') email = email.lower() # case-insensitive form = Form(email, owner=current_user) if url: url = 'http://' + url if not url.startswith('http') else url form.host = referrer_to_path(url) # sitewide forms, verified with a file at the root of the target domain if sitewide: if sitewide_file_check(url, email): form.host = remove_www(referrer_to_path(urljoin(url, '/'))[:-1]) form.sitewide = True else: return jsonerror(403, {'error': "Couldn't verify the file at %s." % url}) DB.session.add(form) DB.session.commit() if form.host: # when the email and url are provided, we can automatically confirm the form # but only if the email is registered for this account for email in current_user.emails: if email.address == form.email: g.log.info('No need for email confirmation.') form.confirmed = True DB.session.add(form) DB.session.commit() break else: # in case the email isn't registered for this user # we automatically send the email confirmation form.send_confirmation() if request_wants_json(): return jsonify({ 'ok': True, 'hashid': form.hashid, 'submission_url': settings.API_ROOT + '/' + form.hashid, 'confirmed': form.confirmed }) else: flash('Your new form endpoint was created!', 'success') return redirect(url_for('dashboard', new=form.hashid) + '#form-' + form.hashid)
def create_form(): # create a new form if not current_user.upgraded: g.log.info( 'Failed to create form from dashboard. User is not upgraded.') return jsonerror(402, {'error': "Please upgrade your account."}) if request.get_json(): email = request.get_json().get('email') url = request.get_json().get('url') sitewide = request.get_json().get('sitewide') else: email = request.form.get('email') url = request.form.get('url') sitewide = request.form.get('sitewide') g.log = g.log.bind(email=email, url=url, sitewide=sitewide) if not IS_VALID_EMAIL(email): g.log.info('Failed to create form from dashboard. Invalid address.') if request_wants_json(): return jsonerror( 400, {'error': "The provided email address is not valid."}) else: flash(u'The provided email address is not valid.', 'error') return redirect(url_for('dashboard')) g.log.info('Creating a new form from the dashboard.') email = email.lower() # case-insensitive form = Form(email, owner=current_user) if url: url = 'http://' + url if not url.startswith('http') else url form.host = referrer_to_path(url) # sitewide forms, verified with a file at the root of the target domain if sitewide: if sitewide_file_check(url, email): form.host = remove_www( referrer_to_path(urljoin(url, '/'))[:-1]) form.sitewide = True else: return jsonerror( 403, {'error': u"Couldn't verify the file at {}.".format(url)}) DB.session.add(form) DB.session.commit() if form.host: # when the email and url are provided, we can automatically confirm the form # but only if the email is registered for this account for email in current_user.emails: if email.address == form.email: g.log.info('No need for email confirmation.') form.confirmed = True DB.session.add(form) DB.session.commit() break else: # in case the email isn't registered for this user # we automatically send the email confirmation form.send_confirmation() if request_wants_json(): return jsonify({ 'ok': True, 'hashid': form.hashid, 'submission_url': settings.API_ROOT + '/' + form.hashid, 'confirmed': form.confirmed }) else: flash(u'Your new form endpoint was created!', 'success') return redirect( url_for('dashboard', new=form.hashid) + '#form-' + form.hashid)
def create_form(): # create a new form if not current_user.upgraded: return jsonerror(402, {'error': "Please upgrade your account."}) if request.get_json(): email = request.get_json().get('email') url = request.get_json().get('url') sitewide = request.get_json().get('sitewide') else: email = request.form.get('email') url = request.form.get('url') sitewide = request.form.get('sitewide') if not IS_VALID_EMAIL(email): if request_wants_json(): return jsonerror(400, {'error': "The provided email address is not valid."}) else: flash('The provided email address is not valid.', 'error') return redirect(url_for('dashboard')) form = Form(email, owner=current_user) if url: url = 'http://' + url if not url.startswith('http') else url form.host = referrer_to_path(url) # sitewide forms, verified with a file at the root of the target domain if sitewide: if sitewide_file_exists(url, email): form.host = remove_www(referrer_to_path(urljoin(url, '/'))[:-1]) form.sitewide = True else: return jsonerror(403, {'error': "Couldn't find the verification file."}) DB.session.add(form) DB.session.commit() if form.host: # when the email and url are provided, we can automatically confirm the form # but only if the email is registered for this account for email in current_user.emails: if email.address == form.email: form.confirmed = True DB.session.add(form) DB.session.commit() break else: # in case the email isn't registered for this user # we automatically send the email confirmation form.send_confirmation() if request_wants_json(): return jsonify({ 'ok': True, 'hashid': form.hashid, 'submission_url': settings.API_ROOT + '/' + form.hashid, 'confirmed': form.confirmed }) else: flash('Your new form endpoint was created!', 'success') return redirect(url_for('dashboard') + '#view-code-' + form.hashid)