Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom rest endpoint for Up #1384

Closed
zzgvh opened this issue Mar 31, 2015 · 1 comment
Closed

Custom rest endpoint for Up #1384

zzgvh opened this issue Mar 31, 2015 · 1 comment
Assignees

Comments

@zzgvh
Copy link
Contributor

zzgvh commented Mar 31, 2015

The old Tastypie based API is struggling when Up requests all projects for large organisations. This is probably due to the nature of the code that returns the data, possibly in combination with Postgres being slower than MySQL for this complicated query.

Since we want to move away from the Tastypie API anyway, this seems like a good time to create a custom endpoint in RSR's new, DRF-based API.

The new endpoint will include the full Project model, including any thumbnails of current_image and also the Primary location as an inline object.

@KasperBrandt KasperBrandt added this to the 3.2 Flying Fish Cove milestone Mar 31, 2015
zzgvh added a commit that referenced this issue Apr 7, 2015
Add Base64ImageField.to_native() to include the full path to the image
in any API use and add the option of requesting a thumbnail as part of an
API get request.

See docstring and comments in to_native() for more details.
zzgvh added a commit that referenced this issue Apr 7, 2015
The project_up endpoint is tailored to the needs of the Up app.

It includes the full primary_location object and allows the requesting
of an image thumbnail of arbitrary size using the
Base64ImageField.to_native() method.
@zzgvh
Copy link
Contributor Author

zzgvh commented Apr 7, 2015

Test plan

This PR adds new functionality to the rest API for the models that have an image, i.e. Project, Organisation and ProjectUpdate.

By default the image field for those models now return a path to a thumbnail. This thumb is 191px wide, the format used by the updates on akvo.org.

The larger change/addition this feature adds is that it's now possible to request thumbnails and specify their size. The size is specified by either width or height to get the image to scale correctly in the other dimension or to specify both thereby setting an absolute maximum size of the image, while keeping the aspect ratio.

Example:

An example is the easiest way to show what's going on. Calling http://rsr.test.akvo.org/rest/v1/project/?image_thumb_name=big,small&image_thumb_small_width=90&image_thumb_big_max_size=300x200 should result in the returned JSON including somthing like

...
"current_image": {
    "default": "/media/cache/86/12/8612ee8e96c3f34ea213f8e1f8a06666.jpg",
    "big": "/media/cache/39/14/3914d362ec01ac9f8d2f5483457a8ddb.jpg",
    "small": "/media/cache/5a/13/5a139c48492af14223ac24c8d7a9aa07.jpg",
    "original": "/media/db/project/16/Project_16_current_image_2015-04-07_14.34.24.jpg"
}, 
...

for each project. Adding format=xml to the query and the result looks like:

...
<current_image>
    <default>/media/cache/86/12/8612ee8e96c3f34ea213f8e1f8a06666.jpg</default>
    <big>/media/cache/39/14/3914d362ec01ac9f8d2f5483457a8ddb.jpg</big>
    <small>/media/cache/5a/13/5a139c48492af14223ac24c8d7a9aa07.jpg</small>
    <original>/media/db/project/16/Project_16_current_image_2015-04-07_14.34.24.jpg</original>
</current_image>
...

Full testing

There are a number of combinations to test. The pattern for the query is thus:

/rest/v1/<endpoint>/?format=<API format>&image_thumb_name=<thumb identifiers>[&image_thumb_<name>_<dimension>=<dimension value>]

where

  • <endpoint> is one of organisation, project, project_extra, project_up, project_update or project_update_extra
  • <API format> is json or xml Can be omitted for JSON which is the default
  • <thumb identifiers> is a comma separated list of names for the thumbs. If there's only one thumb no comma is needed
  • <name> should match one of the names specified in <thumb identifiers>. The whole [&image_thumb__=] part must be repeated once for each name.
  • <dimension> is one of width, height or max_size
  • <dimension value> is an integer if <dimension> is width or height. If <dimension> is max_size then it should specify width and height separated by an x, e.g. 300x200.

Really thorough testing would include generating URLs for all combinations and checking that the generated thumbs are there and have the correct size. I think this is probably overkill though, since the underlying code is essentially the same for all API endpoints. For easier testing it might be better to use the single object API call, /rest/v1/<endpoint>/<object ID>/, followed by the query string.

Also, because thumbnails aren't created if there is no size specified, the degenerated query ?image_thumb_name=foo will return only the default thumgnail and the original, so this can be used if only the original is needed.

Testing Up

Up uses this call:

/rest/v1/project_up/?format=xml&image_thumb_name=up&image_thumb_up_width=<appropriate size>

Cache testing

To test the caching of thumbs, tests should include fetching an image using the same query string parameters several times and checking that the image path, and thus the image, is the same. This should hold for different users requesting a thumb with the same parameters and also for the same image using different endpoints, like project_update and project_update_extra.

Also test that the thumbnail changes if the original image is updated, for example if Project.current_image is changed.

zzgvh added a commit that referenced this issue Apr 8, 2015
See akvo.rest.fields.Base64ImageField.to_native() docstring
zzgvh added a commit that referenced this issue Apr 8, 2015
And set the default thumb width to 191px.
zzgvh added a commit that referenced this issue Apr 8, 2015
Catch parsing error in get_thumbnail call

Fix a couple of spelling error in the get_thumb logic

Change size to max_size for the image size query param when limiting the
image in both dimensions

Update the docstring to reflect this
KasperBrandt added a commit that referenced this issue Apr 9, 2015
@MichaelAkvo MichaelAkvo moved this to Done in RSR Dec 8, 2022
@MichaelAkvo MichaelAkvo added this to RSR Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants