def register(self): require.account.create() c.config = config self._disable_cache() errors, values = {}, None if request.method == 'POST': try: schema = AccountRegister() values = request.params data = schema.deserialize(values) if Account.by_name(data['name']): raise colander.Invalid( AccountRegister.name, _("Login name already exists, please choose a " "different one")) if not data['password1'] == data['password2']: raise colander.Invalid(AccountRegister.password1, _("Passwords don't match!")) account = Account() account.name = data['name'] account.fullname = data['fullname'] account.email = data['email'] account.password = generate_password_hash(data['password1']) db.session.add(account) db.session.commit() who_api = get_api(request.environ) authenticated, headers = who_api.login({ "login": account.name, "password": data['password1'] }) errors = subscribe_lists(('community', 'developer'), data) if errors: h.flash_notice( _("Subscription to the following mailing " + "lists probably failed: %s.") % ', '.join(errors)) response.headers.extend(headers) return redirect("/") except colander.Invalid, i: errors = i.asdict()
def register(self): require.account.create() c.config = config self._disable_cache() errors, values = {}, None if request.method == 'POST': try: schema = AccountRegister() values = request.params data = schema.deserialize(values) if Account.by_name(data['name']): raise colander.Invalid( AccountRegister.name, _("Login name already exists, please choose a " "different one")) if not data['password1'] == data['password2']: raise colander.Invalid(AccountRegister.password1, _("Passwords don't match!")) account = Account() account.name = data['name'] account.fullname = data['fullname'] account.email = data['email'] account.password = generate_password_hash(data['password1']) db.session.add(account) db.session.commit() who_api = get_api(request.environ) authenticated, headers = who_api.login({ "login": account.name, "password": data['password1'] }) errors = subscribe_lists(('community', 'developer'), data) if errors: h.flash_notice(_("Subscription to the following mailing " + "lists probably failed: %s.") % ', '.join(errors)) response.headers.extend(headers) return redirect("/") except colander.Invalid, i: errors = i.asdict()
def register(self): """ Perform registration of a new user """ # We must allow account creation require.account.create() # We add the config as a context variable in case anything happens # (like with login we need this to allow subscriptions to mailing lists) c.config = config # Disable the cache (don't want anything getting in the way) self._disable_cache() # Initial values and errors errors, values = {}, None # If this is a POST operation somebody is trying to register if request.method == 'POST': try: # Get the account register schema (for validation) schema = AccountRegister() # 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) # Check if the username already exists, return an error if so if Account.by_name(data['name']): raise colander.Invalid( AccountRegister.name, _("Login name already exists, please choose a " "different one")) # Check if passwords match, return error if not if not data['password1'] == data['password2']: raise colander.Invalid(AccountRegister.password1, _("Passwords don't match!")) # Create the account account = Account() # Set username and full name account.name = data['name'] account.fullname = data['fullname'] # Set email and if email address should be public account.email = data['email'] account.public_email = data['public_email'] # Hash the password and store the hash account.password = generate_password_hash(data['password1']) # Commit the new user account to the database db.session.add(account) db.session.commit() # Perform a login for the user who_api = get_api(request.environ) authenticated, headers = who_api.login({ "login": account.name, "password": data['password1'] }) # Add the login headers response.headers.extend(headers) # Subscribe the user to the mailing lists errors = subscribe_lists(('community', 'developer'), data) # Notify if the mailing list subscriptions failed if errors: h.flash_notice(_("Subscription to the following mailing " + "lists probably failed: %s.") % ', '.join(errors)) # Registration successful - Redirect to the front page return redirect("/") except colander.Invalid, i: # Mark colander errors errors = i.asdict()
def view(self, dataset, id, format='html'): """ Get a specific entry in the dataset, identified by the id. Entry can be return as html (default), json or csv. """ # Generate the dataset self._get_dataset(dataset) # Get the entry that matches the given id. c.dataset.entries is # a generator so we create a list from it's responses based on the # given constraint entries = list(c.dataset.entries(c.dataset.alias.c.id == id)) # Since we're trying to get a single entry the list should only # contain one entry, if not then we return an error if not len(entries) == 1: abort(404, _('Sorry, there is no entry %r') % id) # Add urls to the dataset and assign assign it as a context variable c.entry = entry_apply_links(dataset, entries.pop()) # Get and set some context variables from the entry # This shouldn't really be necessary but it's here so nothing gets # broken c.id = c.entry.get('id') c.from_ = c.entry.get('from') c.to = c.entry.get('to') c.currency = c.entry.get('currency', c.dataset.currency).upper() c.time = c.entry.get('time') # Get the amount for the entry amount = c.entry.get('amount') # We adjust for inflation if the user as asked for this to be inflated if request.params.has_key('inflate'): try: # Inflate the amount. Target date is provided in request.params # as value for inflate and reference date is the date of the # entry. We also provide a list of the territories to extract # a single country for which to do the inflation c.inflation = h.inflate(amount, request.params['inflate'], c.time, c.dataset.territories) # The amount to show should be the inflated amount # and overwrite the entry's amount as well c.amount = c.inflation['inflated'] c.entry['amount'] = c.inflation['inflated'] # We include the inflation response in the entry's dict # HTML description assumes every dict value for the entry # includes a label so we include a default "Inflation # adjustment" for it to work. c.inflation['label'] = 'Inflation adjustment' c.entry['inflation_adjustment'] = c.inflation except: # If anything goes wrong in the try clause (and there's a lot # that can go wrong). We just say that we can't adjust for # inflation and set the context amount as the original amount h.flash_notice(_('Unable to adjust for inflation')) c.amount = amount else: # If we haven't been asked to inflate then we just use the # original amount c.amount = amount # Add custom html for the dataset entry if the dataset has some # custom html # 2013-11-17 disabled this as part of removal of genshi as depended on # a genshi specific helper. # TODO: reinstate if important # c.custom_html = h.render_entry_custom_html(c.dataset, c.entry) # Add the rest of the dimensions relating to this entry into a # extras dictionary. We first need to exclude all dimensions that # are already shown and then we can loop through the dimensions excluded_keys = ('time', 'amount', 'currency', 'from', 'to', 'dataset', 'id', 'name', 'description') c.extras = {} if c.dataset: # Create a dictionary of the dataset dimensions c.desc = dict([(d.name, d) for d in c.dataset.dimensions]) # Loop through dimensions of the entry for key in c.entry: # Entry dimension must be a dataset dimension and not in # the predefined excluded keys if key in c.desc and \ not key in excluded_keys: c.extras[key] = c.entry[key] # Return entry based on if format == 'json': return to_jsonp(c.entry) elif format == 'csv': return write_csv([c.entry], response) else: return templating.render('entry/view.html')
def view(self, dataset, id, format='html'): """ Get a specific entry in the dataset, identified by the id. Entry can be return as html (default), json or csv. """ # Generate the dataset self._get_dataset(dataset) # Get the entry that matches the given id. c.dataset.entries is # a generator so we create a list from it's responses based on the # given constraint entries = list(c.dataset.entries(c.dataset.alias.c.id == id)) # Since we're trying to get a single entry the list should only # contain one entry, if not then we return an error if not len(entries) == 1: abort(404, _('Sorry, there is no entry %r') % id) # Add urls to the dataset and assign assign it as a context variable c.entry = entry_apply_links(dataset, entries.pop()) # Get and set some context variables from the entry # This shouldn't really be necessary but it's here so nothing gets # broken c.id = c.entry.get('id') c.from_ = c.entry.get('from') c.to = c.entry.get('to') c.currency = c.entry.get('currency', c.dataset.currency).upper() c.time = c.entry.get('time') # Get the amount for the entry amount = c.entry.get('amount') # We adjust for inflation if the user as asked for this to be inflated if 'inflate' in request.params: try: # Inflate the amount. Target date is provided in request.params # as value for inflate and reference date is the date of the # entry. We also provide a list of the territories to extract # a single country for which to do the inflation c.inflation = h.inflate(amount, request.params['inflate'], c.time, c.dataset.territories) # The amount to show should be the inflated amount # and overwrite the entry's amount as well c.amount = c.inflation['inflated'] c.entry['amount'] = c.inflation['inflated'] # We include the inflation response in the entry's dict # HTML description assumes every dict value for the entry # includes a label so we include a default "Inflation # adjustment" for it to work. c.inflation['label'] = 'Inflation adjustment' c.entry['inflation_adjustment'] = c.inflation except: # If anything goes wrong in the try clause (and there's a lot # that can go wrong). We just say that we can't adjust for # inflation and set the context amount as the original amount h.flash_notice(_('Unable to adjust for inflation')) c.amount = amount else: # If we haven't been asked to inflate then we just use the # original amount c.amount = amount # Add custom html for the dataset entry if the dataset has some # custom html # 2013-11-17 disabled this as part of removal of genshi as depended on # a genshi specific helper. # TODO: reinstate if important # c.custom_html = h.render_entry_custom_html(c.dataset, c.entry) # Add the rest of the dimensions relating to this entry into a # extras dictionary. We first need to exclude all dimensions that # are already shown and then we can loop through the dimensions excluded_keys = ('time', 'amount', 'currency', 'from', 'to', 'dataset', 'id', 'name', 'description') c.extras = {} if c.dataset: # Create a dictionary of the dataset dimensions c.desc = dict([(d.name, d) for d in c.dataset.dimensions]) # Loop through dimensions of the entry for key in c.entry: # Entry dimension must be a dataset dimension and not in # the predefined excluded keys if key in c.desc and \ key not in excluded_keys: c.extras[key] = c.entry[key] # Return entry based on if format == 'json': return to_jsonp(c.entry) elif format == 'csv': return write_csv([c.entry], response) else: return templating.render('entry/view.html')
def register(self): """ Perform registration of a new user """ # We must allow account creation require.account.create() # We add the config as a context variable in case anything happens # (like with login we need this for subscriptions to mailing lists) c.config = config # Disable the cache (don't want anything getting in the way) self._disable_cache() # Initial values and errors errors, values = {}, None # If this is a POST operation somebody is trying to register if request.method == 'POST': try: # Get the account register schema (for validation) schema = AccountRegister() # 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) # Check if the username already exists, return an error if so if Account.by_name(data['name']): raise colander.Invalid( AccountRegister.name, _("Login name already exists, please choose a " "different one")) # Check if passwords match, return error if not if not data['password1'] == data['password2']: raise colander.Invalid(AccountRegister.password1, _("Passwords don't match!")) # Create the account account = Account() # Set username and full name account.name = data['name'] account.fullname = data['fullname'] # Set email and if email address should be public account.email = data['email'] account.public_email = data['public_email'] # Hash the password and store the hash account.password = generate_password_hash(data['password1']) # Commit the new user account to the database db.session.add(account) db.session.commit() # Perform a login for the user who_api = get_api(request.environ) authenticated, headers = who_api.login({ "login": account.name, "password": data['password1'] }) # Add the login headers response.headers.extend(headers) # Subscribe the user to the mailing lists errors = subscribe_lists(('community', 'developer'), data) # Notify if the mailing list subscriptions failed if errors: h.flash_notice( _("Subscription to the following mailing " + "lists probably failed: %s.") % ', '.join(errors)) # Registration successful - Redirect to the front page return redirect("/") except colander.Invalid as i: # Mark colander errors errors = i.asdict() # Show the templates (with possible errors and form values) return templating.render('account/login.html', form_fill=values, form_errors=errors)