Add class for highlighting, replacing and extracting placeholders#1
Add class for highlighting, replacing and extracting placeholders#1mannickutd merged 10 commits intoalphagov:masterfrom
Conversation
43ed5ce to
ebba054
Compare
utils/placeholders.py
Outdated
|
|
||
| @property | ||
| def missing_data(self): | ||
| return set(self.list) - set(self.values.keys()) |
There was a problem hiding this comment.
These don't need to be enclosed in sets?
Look at pythons set operators :)
https://docs.python.org/3.5/library/stdtypes.html?highlight=set#set
This commit copies the placeholder code which is currently in the admin app here: https://github.com/alphagov/notifications-admin/blob/78fe2b463aeb5b00c2984439226977679e0b4382/app/__init__.py#L126-L152 It then adds tests for existing code, which do not exist in the admin app. It makes two changes from the code in the admin app: - return strings not a markup object (because this code will be more general-purpose than just front end rendering of placeholders) - fix handling of templates which have placeholders inside brackets (eg `(((name)))`) by making a slight change to the regular expression
Takes the template as an argument to the constructor, rather than having static methods that all take template as an argument. Add __repr__ and __str__ for easier debugging.
Adds the option to have each placeholder formatted as markup, for use as column headings, for example. Since we’re importing Markup now anyway, we might as well add a method to return the template with placeholders highlighted as a Markup object.
This will also you to determine: - if there are placeholders in the template which aren’t in the user’s data - if there are columns in the user’s data which are additional to the placeholders in the template
There are multiple methods which need to know the values in order to replace placeholders or list missing ones, and passing the values multiple times if possible. Therefore it’s useful to pass the values only once. But modifying the state of an object is best avoided if possible. So the best time to pass the values is on instantiation.
In trying to use this class I found it clumsy, because it accepted one attribute of a template object (`content`) but not the others (`name`, `id`). This commit changes the class to accept an entire template object (with `name` and `id` still optional). All attributes of the template are passed through, and the additional properties available (eg `template.replaced`) stay the same, although in some cases are renamed for clarity. This commit also updates the documentation to match.
ebba054 to
386489a
Compare
|
@mannickutd You’re right, don’t need the wrapping Other changes I’ve made since: Rename from
|
386489a to
b8087d9
Compare
A user’s uploaded data may well have fields which aren’t in the template, for example `phone number` wouldn’t be in the template, but would need to be in the CSV. You wouldn’t want to raise an error when giving the template this data. This commit adds an option, when instantiating the `Template` object, to drop values from the user’s data before attempting to do any replacing of the template.
Minor version bump because new features (`Template` class) have been added, but in a non-breaking way.
b8087d9 to
43545b0
Compare
This commit brings in the `Template` util, added here: alphagov/notifications-utils#1 It also does a fair bit of tidying up, which I’ve unfortunately squashed into this one massive commit. The main change is moving 404 handling into the templates dao, so that every view isn’t littered with `try: … except(HTTPError)`. It also adds new features, in a prototypy sort of way, which are: - download a prefilled example CSV - show all the columns for your template on the 'check' page
This commit brings in the `Template` util, added here: alphagov/notifications-utils#1 It also does a fair bit of tidying up, which I’ve unfortunately squashed into this one massive commit. The main change is moving 404 handling into the templates dao, so that every view isn’t littered with `try: … except(HTTPError)`. It also adds new features, in a prototypy sort of way, which are: - download a prefilled example CSV - show all the columns for your template on the 'check' page
This commit brings in the `Template` util, added here: alphagov/notifications-utils#1 It also does a fair bit of tidying up, which I’ve unfortunately squashed into this one massive commit. The main change is moving 404 handling into the templates dao, so that every view isn’t littered with `try: … except(HTTPError)`. It also adds new features, in a prototypy sort of way, which are: - download a prefilled example CSV - show all the columns for your template on the 'check' page
This commit brings in the `Template` util, added here: alphagov/notifications-utils#1 It also does a fair bit of tidying up, which I’ve unfortunately squashed into this one massive commit. The main change is moving 404 handling into the templates dao, so that every view isn’t littered with `try: … except(HTTPError)`. It also adds new features, in a prototypy sort of way, which are: - download a prefilled example CSV - show all the columns for your template on the 'check' page
Add class for highlighting, replacing and extracting placeholders
This commit brings in the `Template` util, added here: alphagov/notifications-utils#1 It also does a fair bit of tidying up, which I’ve unfortunately squashed into this one massive commit. The main change is moving 404 handling into the templates dao, so that every view isn’t littered with `try: … except(HTTPError)`. It also adds new features, in a prototypy sort of way, which are: - download a prefilled example CSV - show all the columns for your template on the 'check' page
Notes for reviewing
Reviewing will be easier on a commit-by-commit basis
I would particularly like to know:
Usage
Given a template object, the
Templateclass can:It will also pass through, if passed the
nameandidof the template, and the user’s data, eg:If you only have the template
Highlight the placeholders:
List the placeholders in the template
If you have the template and the user’s data
You can do all of the above, plus
Replace the placeholders with the user’s data
Find out what data is missing for a given template
Find out what additional data is being passed that the template isn’t expecting
If you try to replace placeholders with bad data
…then you’ll get a
NeededByTemplateErroror aNoPlaceholderForDataError, eg