def dimensions_update(self, dataset, format='html'): dry_run = False self._get_dataset(dataset) operation = request.params.get('operation', 'Save') if operation == 'Verify': dry_run = True require.dataset.update(c.dataset) if len(c.dataset): abort(400, _("You cannot edit the dimensions model when " \ "data is loaded for the dataset.")) errors, mapping = {}, None try: mapping = json.loads(request.params.get('mapping')) model = c.dataset.model model['mapping'] = mapping schema = mapping_schema(ValidationState(model)) new_mapping = schema.deserialize(mapping) if not dry_run: # erm... c.dataset.data['mapping'] = new_mapping c.dataset.drop() c.dataset._load_model() c.dataset.generate() db.session.commit() h.flash_success(_("The mapping has been updated.")) else: h.flash_success(_("The mapping has been validated successfully.")) except (ValueError, TypeError, AttributeError): abort(400, _("The mapping data could not be decoded as JSON!")) except Invalid, i: errors = i.asdict()
def load(self, dataset, id): """ Load the dataset into the database. If a url parameter 'sample' is provided then its value is converted into a boolean. If the value equals true we only perform a sample run, else we do a full load. """ # Get our source (and dataset) self._get_source(dataset, id) # We require that the user can update the dataset require.dataset.update(c.dataset) # If the source is already running we flash an error declaring that # we're already running this source if c.source.is_running: h.flash_error(_("Already running!")) # If the source isn't already running we try to load it (or sample it) else: try: sample = asbool(request.params.get('sample', 'false')) load_source.delay(c.source.id, sample) # Let the user know we're loading the source h.flash_success(_("Now loading...")) except Exception, e: abort(400, e)
def load(self, dataset, id): """ Load the dataset into the database. If a url parameter 'sample' is provided then its value is converted into a boolean. If the value equals true we only perform a sample run, else we do a full load. """ # Get our source (and dataset) self._get_source(dataset, id) # We require that the user can update the dataset require.dataset.update(c.dataset) # If the source is already running we flash an error declaring that # we're already running this source if c.source.is_running: h.flash_error(_("Already running!")) # If the source isn't already running we try to load it (or sample it) else: try: sample = asbool(request.params.get('sample', 'false')) load_source.delay(c.source.id, sample) # Let the user know we're loading the source h.flash_success(_("Now loading...")) except Exception as e: abort(400, e) # Send the user to the editor index page for this dataset redirect( h.url_for(controller='editor', action='index', dataset=c.dataset.name))
def run(self, dataset, format='html'): self._get_dataset(dataset) require.dataset.update(c.dataset) code = request.params.get('code', '') mine.delay(c.dataset.name, code) h.flash_success(_("Your task has been initiated..")) return self.index(dataset, code=code)
def after_login(self): if c.account is not None: h.flash_success(_("Welcome back, %s!") % c.account.name) redirect("/") else: h.flash_error(_("Incorrect user name or password!")) redirect("/login")
def after_login(self): self._disable_cache() if c.account is not None: h.flash_success(_("Welcome back, %s!") % c.account.name) redirect(h.url_for(controller='account', action='dashboard')) else: h.flash_error(_("Incorrect user name or password!")) redirect(h.url_for(controller='account', action='login'))
def delete(self, dataset): self._get_dataset(dataset) require.dataset.delete(c.dataset) c.dataset.drop() db.session.delete(c.dataset) db.session.commit() h.flash_success(_("The dataset has been deleted.")) redirect(h.url_for(controller='dataset', action='index'))
def delete(self, dataset): self._get_dataset(dataset) require.dataset.delete(c.dataset) c.dataset.drop() solr.drop_index(c.dataset.name) db.session.delete(c.dataset) db.session.commit() h.flash_success(_("The dataset has been deleted.")) redirect(h.url_for(controller='dataset', action='index'))
def delete(self, dataset, name): self._get_named_view(dataset, name) if not can.view.delete(c.dataset, c.named_view): abort(403, _("You are not authorized to delete this view.")) h.flash_success(_("'%s' has been deleted.") % c.named_view.label) db.session.delete(c.named_view) db.session.commit() return redirect(h.url_for(controller='view', action='index', dataset=c.dataset.name))
def drop(self, dataset): self._get_dataset(dataset) require.dataset.update(c.dataset) c.dataset.drop() c.dataset.init() c.dataset.generate() AggregationCache(c.dataset).invalidate() db.session.commit() h.flash_success(_("The dataset has been cleared.")) redirect(h.url_for(controller='editor', action='index', dataset=c.dataset.name))
def retract(self, dataset): self._get_dataset(dataset) require.dataset.update(c.dataset) if c.dataset.private: abort(400, _("This dataset is already private!")) c.dataset.private = True AggregationCache(c.dataset).invalidate() db.session.commit() h.flash_success(_("The dataset has been retracted. " \ "It is no longer visible to others.")) redirect(h.url_for(controller='editor', action='index', dataset=c.dataset.name))
def locale(self): return_to = request.params.get('return_to', '/') locale = request.params.get('locale') if locale is not None: flash_success(_("Language has been set to: English")) set_session_locale(locale) else: flash_error(_("No language given!")) return_to += '&' if '?' in return_to else '?' # hack to prevent next page being cached return_to += '__cache=%s' % int(random.random() * 100000000) redirect(return_to.encode('utf-8'))
def templates_update(self, dataset, format='html'): self._get_dataset(dataset) require.dataset.update(c.dataset) errors, values = {}, None try: values = dict(request.params.items()) c.dataset.serp_title = values.get('serp_title', None) c.dataset.serp_teaser = values.get('serp_teaser', None) db.session.commit() h.flash_success(_("The templates have been updated.")) except Invalid, i: errors = i.asdict()
def locale(self): return_to = request.params.get("return_to", "/") locale = request.params.get("locale") if locale is not None: flash_success(_("Language has been set to: English")) set_session_locale(locale) else: flash_error(_("No language given!")) return_to += "&" if "?" in return_to else "?" # hack to prevent next page being cached return_to += "__cache=%s" % int(random.random() * 100000000) redirect(return_to.encode("utf-8"))
def publish(self, dataset): self._get_dataset(dataset) require.dataset.update(c.dataset) if not c.dataset.private: abort(400, _("This dataset is already public!")) c.dataset.private = False db.session.commit() public_url = h.url_for(controller='dataset', action='view', dataset=c.dataset.name, qualified=True) h.flash_success(_("Congratulations, the dataset has been " \ "published. It is now available at: %s") % public_url) redirect(h.url_for(controller='editor', action='index', dataset=c.dataset.name))
def views_update(self, dataset, format='html'): self._get_dataset(dataset) require.dataset.update(c.dataset) errors, views = {}, None try: views = json.loads(request.params.get('views')) schema = views_schema(ValidationState(c.dataset.model)) c.dataset.data['views'] = schema.deserialize(views) db.session.commit() h.flash_success(_("The views have been updated.")) except (ValueError, TypeError): abort(400, _("The views could not be decoded as JSON!")) except Invalid, i: errors = i.asdict()
def trigger_reset(self): self._disable_cache() if request.method == 'GET': return render('account/trigger_reset.html') email = request.params.get('email') if email is None or not len(email): h.flash_error(_("Please enter an email address!")) return render('account/trigger_reset.html') account = Account.by_email(email) if account is None: h.flash_error(_("No user is registered under this address!")) return render('account/trigger_reset.html') send_reset_link(account) h.flash_success(_("You've received an email with a link to reset your " + "password. Please check your inbox.")) redirect(h.url_for(controller='account', action='login'))
def create(self, dataset): self._get_dataset(dataset) require.dataset.update(c.dataset) try: schema = source_schema() data = schema.deserialize(request.params) source = Source(c.dataset, c.account, data['url']) db.session.add(source) db.session.commit() analyze_source.apply_async(args=[source.id], countdown=2) h.flash_success(_("The source has been created.")) redirect(h.url_for(controller='editor', action='index', dataset=c.dataset.name)) except Invalid, i: errors = i.asdict() errors = [(k[len('source.'):], v) for k, v in errors.items()] return self.new(dataset, dict(errors))
def trigger_reset(self): self._disable_cache() if request.method == 'GET': return render('account/trigger_reset.html') email = request.params.get('email') if email is None or not len(email): h.flash_error(_("Please enter an email address!")) return render('account/trigger_reset.html') account = Account.by_email(email) if account is None: h.flash_error(_("No user is registered under this address!")) return render('account/trigger_reset.html') send_reset_link(account) h.flash_success( _("You've received an email with a link to reset your " + "password. Please check your inbox.")) redirect(h.url_for(controller='account', action='login'))
def team_update(self, dataset, format='html'): self._get_dataset(dataset) require.dataset.update(c.dataset) errors, accounts = {}, [] for account_name in request.params.getall('accounts'): account = Account.by_name(account_name) if account is None: errors[account_name] = _("User account cannot be found.") else: accounts.append(account) if not c.account in accounts: accounts.append(c.account) if not len(errors): c.dataset.managers = accounts c.dataset.updated_at = datetime.utcnow() db.session.commit() h.flash_success(_("The team has been updated.")) return self.team_edit(dataset, errors=errors, accounts=accounts)
def team_update(self, dataset, format='html'): self._get_dataset(dataset) require.dataset.update(c.dataset) errors, accounts = {}, [] for account_name in request.params.getall('accounts'): account = Account.by_name(account_name) if account is None: errors[account_name] = _("User account cannot be found.") else: accounts.append(account) if c.account not in accounts: accounts.append(c.account) if not len(errors): c.dataset.managers = accounts c.dataset.updated_at = datetime.utcnow() db.session.commit() h.flash_success(_("The team has been updated.")) return self.team_edit(dataset, errors=errors, accounts=accounts)
def create(self, dataset): self._get_dataset(dataset) require.dataset.update(c.dataset) try: schema = source_schema() data = schema.deserialize(request.params) source = Source(c.dataset, c.account, data['url']) db.session.add(source) db.session.commit() analyze_source.apply_async(args=[source.id], countdown=2) h.flash_success(_("The source has been created.")) redirect( h.url_for(controller='editor', action='index', dataset=c.dataset.name)) except Invalid as i: errors = i.asdict() errors = [(k[len('source.'):], v) for k, v in errors.items()] return self.new(dataset, dict(errors))
def core_update(self, dataset, format='html'): self._get_dataset(dataset) require.dataset.update(c.dataset) errors = {} try: schema = dataset_schema(ValidationState(c.dataset.model)) data = dict(request.params) data['territories'] = request.params.getall('territories') data['languages'] = request.params.getall('languages') data = schema.deserialize(data) c.dataset.label = data['label'] c.dataset.currency = data['currency'] c.dataset.description = data['description'] c.dataset.territories = data['territories'] c.dataset.languages = data['languages'] db.session.commit() h.flash_success(_("The dataset has been updated.")) except Invalid, i: errors = i.asdict()
def drop(self, dataset): self._get_dataset(dataset) require.dataset.update(c.dataset) c.dataset.updated_at = datetime.utcnow() c.dataset.drop() solr.drop_index(c.dataset.name) c.dataset.init() c.dataset.generate() AggregationCache(c.dataset).invalidate() # For every source in the dataset we set the status to removed for source in c.dataset.sources: for run in source.runs: run.status = Run.STATUS_REMOVED db.session.commit() h.flash_success(_("The dataset has been cleared.")) redirect(h.url_for(controller='editor', action='index', dataset=c.dataset.name))
def do_reset(self): email = request.params.get('email') if email is None or not len(email): h.flash_error(_("The reset link is invalid!")) redirect(h.url_for(controller='account', action='login')) account = Account.by_email(email) if account is None: h.flash_error(_("No user is registered under this address!")) redirect(h.url_for(controller='account', action='login')) if request.params.get('token') != account.token: h.flash_error(_("The reset link is invalid!")) redirect(h.url_for(controller='account', action='login')) who_api = request.environ['repoze.who.plugins']['auth_tkt'] headers = who_api.remember(request.environ, {'repoze.who.userid': account.name}) response.headers.extend(headers) h.flash_success(_("Thanks! You have now been signed in - please change " + "your password!")) redirect(h.url_for(controller='account', action='settings'))
def do_reset(self): email = request.params.get('email') if email is None or not len(email): h.flash_error(_("The reset link is invalid!")) redirect(h.url_for(controller='account', action='login')) account = Account.by_email(email) if account is None: h.flash_error(_("No user is registered under this address!")) redirect(h.url_for(controller='account', action='login')) if request.params.get('token') != account.token: h.flash_error(_("The reset link is invalid!")) redirect(h.url_for(controller='account', action='login')) who_api = request.environ['repoze.who.plugins']['auth_tkt'] headers = who_api.remember(request.environ, {'repoze.who.userid': account.name}) response.headers.extend(headers) h.flash_success( _("Thanks! You have now been signed in - please change " + "your password!")) redirect(h.url_for(controller='account', action='settings'))
def settings(self): errors, values = {}, c.account if request.method == 'POST': try: schema = Settings() values = request.params data = schema.deserialize(values) if not data['password1'] == data['password2']: raise colander.Invalid(Register.password1, _("Passwords don't match!")) if len(data['password1']): password = data['password1'] data['password_hash'] = generate_password_hash(password) del data['password1'] del data['password2'] account.update(c.account, {"$set": data}) h.flash_success(_("Your settings have been updated.")) except colander.Invalid, i: errors = i.asdict()
def settings(self): require.account.update(c.account) errors, values = {}, c.account if request.method == 'POST': try: schema = AccountSettings() values = request.params data = schema.deserialize(values) if not data['password1'] == data['password2']: raise colander.Invalid(AccountSettings.password1, _("Passwords don't match!")) c.account.fullname = data['fullname'] c.account.email = data['email'] if data['password1'] is not None and len(data['password1']): c.account.password = generate_password_hash(data['password1']) db.session.add(c.account) db.session.commit() h.flash_success(_("Your settings have been updated.")) except colander.Invalid, i: errors = i.asdict()
def settings(self): require.account.update(c.account) errors, values = {}, c.account if request.method == 'POST': try: schema = AccountSettings() values = request.params data = schema.deserialize(values) if not data['password1'] == data['password2']: raise colander.Invalid(AccountSettings.password1, _("Passwords don't match!")) c.account.fullname = data['fullname'] c.account.email = data['email'] if data['password1'] is not None and len(data['password1']): c.account.password = generate_password_hash( data['password1']) db.session.add(c.account) db.session.commit() h.flash_success(_("Your settings have been updated.")) except colander.Invalid, i: errors = i.asdict()
def trigger_reset(self): """ Allow user to trigger a reset of the password in case they forget it """ # Disable the cache self._disable_cache() # If it's a simple GET method we return the form if request.method == 'GET': return templating.render('account/trigger_reset.html') # Get the email email = request.params.get('email') # Simple check to see if the email was provided. Flash error if not if email is None or not len(email): h.flash_error(_("Please enter an email address!")) return templating.render('account/trigger_reset.html') # Get the account for this email account = Account.by_email(email) # If no account is found we let the user know that it's not registered if account is None: h.flash_error(_("No user is registered under this address!")) return templating.render('account/trigger_reset.html') # Send the reset link to the email of this account send_reset_link(account) # Let the user know that email with link has been sent h.flash_success( _("You've received an email with a link to reset your " + "password. Please check your inbox.")) # Redirect to the login page redirect(h.url_for(controller='account', action='login'))
def trigger_reset(self): """ Allow user to trigger a reset of the password in case they forget it """ # Disable the cache self._disable_cache() # If it's a simple GET method we return the form if request.method == 'GET': return templating.render('account/trigger_reset.html') # Get the email email = request.params.get('email') # Simple check to see if the email was provided. Flash error if not if email is None or not len(email): h.flash_error(_("Please enter an email address!")) return templating.render('account/trigger_reset.html') # Get the account for this email account = Account.by_email(email) # If no account is found we let the user know that it's not registered if account is None: h.flash_error(_("No user is registered under this address!")) return templating.render('account/trigger_reset.html') # Send the reset link to the email of this account send_reset_link(account) # Let the user know that email with link has been sent h.flash_success(_("You've received an email with a link to reset your " + "password. Please check your inbox.")) # Redirect to the login page redirect(h.url_for(controller='account', action='login'))
def after_logout(self): self._disable_cache() h.flash_success(_("You have been logged out.")) redirect("/")
return to_jsonp([src.as_dict() for src in c.dataset.sources]) def _get_source(self, dataset, id): self._get_dataset(dataset) c.source = Source.by_id(id) if c.source is None or c.source.dataset != c.dataset: abort(404, _("There is no source '%s'") % id) def view(self, dataset, id): self._get_source(dataset, id) redirect(c.source.url) def load(self, dataset, id): self._get_source(dataset, id) require.dataset.update(c.dataset) try: sample = asbool(request.params.get('sample', 'false')) load_source.delay(c.source.id, sample) except Exception, e: abort(400, e) h.flash_success(_("Now loading...")) redirect( h.url_for(controller='editor', action='index', dataset=c.dataset.name)) def analysis(self, dataset, source, format='json'): self._get_source(dataset, source) return to_jsonp(c.source.analysis)
def settings(self): """ Change settings for the logged in user """ # The logged in user must be able to update the account require.account.update(c.account) # Disable the cache self._disable_cache() # Initial values and errors errors, values = {}, c.account # If POST the user is trying to update the settings if request.method == 'POST': try: # Get the account settings schema (for validation) schema = AccountSettings() # Set values from the request parameters # (for validation and so we can autofill forms) values = request.params # Grab the actual data and validate it data = schema.deserialize(values) # If the passwords don't match we notify the user if not data['password1'] == data['password2']: raise colander.Invalid(AccountSettings.password1, _("Passwords don't match!")) # Update full name c.account.fullname = data['fullname'] # Update the script root c.account.script_root = data['script_root'] # Update email and whether email should be public c.account.email = data['email'] c.account.public_email = data['public_email'] # If twitter handle is provided we update it # (and if it should be public) if data['twitter'] is not None: c.account.twitter_handle = data['twitter'].lstrip('@') c.account.public_twitter = data['public_twitter'] # If a new password was provided we update it as well if data['password1'] is not None and len(data['password1']): c.account.password = generate_password_hash( data['password1']) # Do the actual update in the database db.session.add(c.account) db.session.commit() # Let the user know we've updated successfully h.flash_success(_("Your settings have been updated.")) except colander.Invalid as i: # Load errors if we get here errors = i.asdict() else: # Get the account values to autofill the form values = c.account.as_dict() # We need to put public checks separately because they're not # a part of the dictionary representation of the account if c.account.public_email: values['public_email'] = c.account.public_email if c.account.public_twitter: values['public_twitter'] = c.account.public_twitter # Return the rendered template return templating.render('account/settings.html', form_fill=values, form_errors=errors)
def ping(self): from openspending.tasks import ping ping.delay() flash_success(_("Sent ping!")) redirect('/')
def index(self, dataset, format='json'): self._get_dataset(dataset) return to_jsonp([src.as_dict() for src in c.dataset.sources]) def _get_source(self, dataset, id): self._get_dataset(dataset) c.source = Source.by_id(id) if c.source is None or c.source.dataset != c.dataset: abort(404, _("There is no source '%s'") % id) def view(self, dataset, id): self._get_source(dataset, id) redirect(c.source.url) def load(self, dataset, id): self._get_source(dataset, id) require.dataset.update(c.dataset) try: sample = asbool(request.params.get('sample', 'false')) load_source.delay(c.source.id, sample) except Exception, e: abort(400, e) h.flash_success(_("Now loading...")) redirect(h.url_for(controller='editor', action='index', dataset=c.dataset.name)) def analysis(self, dataset, source, format='json'): self._get_source(dataset, source) return to_jsonp(c.source.analysis)
def settings(self): """ Change settings for the logged in user """ # The logged in user must be able to update the account require.account.update(c.account) # Disable the cache self._disable_cache() # Initial values and errors errors, values = {}, c.account # If POST the user is trying to update the settings if request.method == 'POST': try: # Get the account settings schema (for validation) schema = AccountSettings() # Set values from the request parameters # (for validation and so we can autofill forms) values = request.params # Grab the actual data and validate it data = schema.deserialize(values) # If the passwords don't match we notify the user if not data['password1'] == data['password2']: raise colander.Invalid(AccountSettings.password1, _("Passwords don't match!")) # Update full name c.account.fullname = data['fullname'] # Update the script root c.account.script_root = data['script_root'] # Update email and whether email should be public c.account.email = data['email'] c.account.public_email = data['public_email'] # If twitter handle is provided we update it # (and if it should be public) if data['twitter'] is not None: c.account.twitter_handle = data['twitter'].lstrip('@') c.account.public_twitter = data['public_twitter'] # If a new password was provided we update it as well if data['password1'] is not None and len(data['password1']): c.account.password = generate_password_hash(data['password1']) # Do the actual update in the database db.session.add(c.account) db.session.commit() # Let the user know we've updated successfully h.flash_success(_("Your settings have been updated.")) except colander.Invalid, i: # Load errors if we get here errors = i.asdict()
def after_logout(self): h.flash_success(_("You have been logged out.")) redirect("/")