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

Improve filtering #121

Merged
merged 5 commits into from Apr 13, 2020
Merged

Improve filtering #121

merged 5 commits into from Apr 13, 2020

Conversation

rbagrov
Copy link
Contributor

@rbagrov rbagrov commented Dec 5, 2019

Task: Click

Description: Query methods ( .filter() and .count() ) were clearing _filters attribute which resulted in single usage of a filtered query instance.

Reproduce:

In [3]: reproduce = api.tours.filter(product_line='PPP')
In [4]: reproduce._filters
Out[4]: {'product_line': 'PPP'}
In [5]: reproduce.count()
Out[5]: 1
In [6]: reproduce._filters
Out[6]: {}
In [7]: reproduce.count()
Out[7]: 1249

and

 >>> len([x for x in g.departures.filter(**{'tour_dossier.id': '22758'}).all()])
    280
>>> len([x for x in g.departures.filter(**{'structured_itineraries.variation_id': 4364}).all()])
    0
 >>> len([x for x in g.departures.filter(**{'structured_itineraries.variation_id': 4364}).all()])
    91
 >>> len([x for x in g.departures.filter(**{'tour_dossier.id': '22758'}).all()])
    0

Solution: Mimic Django ORM approach as close as possible

@marz619
Copy link
Contributor

marz619 commented Dec 13, 2019

@rbagrov could you remove the version changes. For now we make those directly in master after merging the feature/change/bug/etc. branch.

Also, if you could rebase from origin/master as we've pushed new changes since you created this branch.

N.B. I removed the link to clickup as this is a public repo, but now I realize that no-one outside of the G-org would be able to visit it; (I put it back 😅).

@rbagrov rbagrov changed the title Release: 2.24.1 (2019-12-05) Improve filtering Dec 13, 2019
@rbagrov
Copy link
Contributor Author

rbagrov commented Dec 13, 2019

Hey @marz619 :)

  1. Branch is rebased;
  2. Versioning is removed;

Please let me know when you have a chance to review/test the proposed changes, if there is anything I can improve.

@marz619 marz619 self-requested a review January 27, 2020 21:22
rbagrov and others added 4 commits April 10, 2020 12:22
* remove the use of the class Empty as it is incompatible with py2

The idea behind this to preserve the state of the _current_ Query object
in regards to modifying the _filters.

!Breaking changes!

.all() will *not* clear the filters
.count() will *not* clear the filters
* Add docs where possible
* consistent formatting across the file
* move limit arg check in Query.all
  * Add a TypeError return
  * simplifies the method call to islice
  * adds test for bad type args
@marz619 marz619 self-assigned this Apr 10, 2020
Copy link
Contributor

@rafikdraoui rafikdraoui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment inline, but otherwise looks good to me.

gapipy/query.py Outdated Show resolved Hide resolved
Copy link
Contributor

@silent1mezzo silent1mezzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@marz619 marz619 merged commit 9ca3f1e into master Apr 13, 2020
@marz619 marz619 deleted the fix_query_filter_removal branch April 13, 2020 20:57
marz619 added a commit that referenced this pull request Apr 14, 2020
--------------------
! BREAKING CHANGES !
--------------------
* The ``Query.filter`` method will return a clone/copy of itself. This will
  preserve the state of ``filters`` on the original Query object.
* The ``Query.all`` method will **not** clear the filters after returning.
* The ``Query.all`` method will return a ``TypeError`` if a type other than
  an ``int`` is passed to the ``limit`` argument.
* The ``Query.count`` method will **not** clear the filters after returning.
* see: #121

* The ``AgencyChain.agencies`` field will be returned as a list of Resource objects
* see: f34afd52
Copy link
Contributor

@marz619 marz619 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 😅

marz619 added a commit that referenced this pull request Apr 14, 2020
This is a result of a change introduced in 2.20.0 in Query.all

>> We check if the raw_data has an href and use that. This was because
of the the bookings/{id}/requirements endpoint actually returns
RequirementSet resources and the default behaviour would build the URI
as /bookings/{id}/requirement_sets

We will preserve the change from 2.20.0 and make the following
modification to the `APIRequestor.list_raw` method. Check the `uri`
passed in for a '?' and make the assumption that if the '?' is in the
URI, then we will not use the `params` the requestor may have been
initialised with.

+ Adds test cases to ensure params are not passed when a ? is present in
the URI
marz619 added a commit that referenced this pull request Apr 17, 2020
This is a result of a change introduced in 2.20.0 in Query.all

>> We check if the raw_data has an href and use that. This was because
of the the bookings/{id}/requirements endpoint actually returns
RequirementSet resources and the default behaviour would build the URI
as /bookings/{id}/requirement_sets

We will preserve the change from 2.20.0 and make the following
modification to the `APIRequestor.list_raw` method. Check the `uri`
passed in for a '?' and make the assumption that if the '?' is in the
URI, then we will not use the `params` the requestor may have been
initialised with.

+ Adds test cases to ensure params are not passed when a ? is present in
the URI
marz619 added a commit that referenced this pull request Apr 17, 2020
…123)

* FIX-#121 - modify APIRequestor.list_raw to check URI for params

* This is a result of a change introduced in 2.20.0 in Query.all

>>> We check if the raw_data has an href and use that. This was because
of the the bookings/{id}/requirements endpoint actually returns
RequirementSet resources and the default behaviour would build the URI
as /bookings/{id}/requirement_sets

We will preserve the change from 2.20.0 and make the following
modification to the `APIRequestor.list_raw` method. Check the `uri`
passed in for a '?' and make the assumption that if the '?' is in the
URI, then we will not use the `params` the requestor may have been
initialised with.

+ Adds test cases to ensure params are not passed when a ? is present in
the URI

* use `urlparse` to determine if the uri contains query parameters

* use `namedtuple` to store `parent` attribute

This will still allows any code of the form:

   # destructure namedtuple
   u, i, vi = x.parent

   # index access
   u, i, vi = x.parent[0], x.parent[1], x.parent[2]

to continue to work correctly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants