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

refactor: remove repetitive safe_query #7017

Merged
merged 7 commits into from
May 17, 2020

Conversation

satya7289
Copy link
Member

Fixes #7001

Short description of what this resolves:

change the all safe_query(db, Session, 'id', view_kwargs['session_id'], 'session_id') into safe_query_kwargs(Session, view_kwargs, 'session_id')

Changes proposed in this pull request:

  • Remove repetitive safe_query for session

Checklist

  • I have read the Contribution & Best practices Guide and my PR follows them.
  • My branch is up-to-date with the Upstream development branch.
  • The unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • All the functions created/modified in this PR contain relevant docstrings.

@@ -1,7 +1,7 @@
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship

from app.api.bootstrap import api
from app.api.helpers.db import safe_query
from app.api.helpers.db import safe_query, safe_query_kwargs

Choose a reason for hiding this comment

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

'app.api.helpers.db.safe_query' imported but unused

@@ -1,7 +1,7 @@
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship

from app.api.bootstrap import api
from app.api.helpers.db import safe_query
from app.api.helpers.db import safe_query, safe_query_kwargs

Choose a reason for hiding this comment

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

'app.api.helpers.db.safe_query' imported but unused

@@ -1,7 +1,7 @@
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship

from app.api.bootstrap import api
from app.api.helpers.db import safe_query
from app.api.helpers.db import safe_query, safe_query_kwargs

Choose a reason for hiding this comment

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

'app.api.helpers.db.safe_query' imported but unused

parameter_name,
# TODO(Areeb): Check that only admin can pass this parameter
request.args.get('get_trashed') != 'true',
)

Choose a reason for hiding this comment

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

no newline at end of file

@@ -129,3 +129,21 @@ def get_new_slug(model, name):
return slug
else:
return '{}-{}'.format(slug, uuid.uuid4().hex)

def safe_query_kwargs(model, kwargs, parameter_name):

Choose a reason for hiding this comment

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

expected 2 blank lines, found 1

@codecov
Copy link

codecov bot commented May 16, 2020

Codecov Report

Merging #7017 into development will decrease coverage by 0.00%.
The diff coverage is 20.46%.

Impacted file tree graph

@@               Coverage Diff               @@
##           development    #7017      +/-   ##
===============================================
- Coverage        60.55%   60.55%   -0.01%     
===============================================
  Files              260      260              
  Lines            12875    12877       +2     
===============================================
+ Hits              7797     7798       +1     
- Misses            5078     5079       +1     
Impacted Files Coverage Δ
app/api/orders.py 27.05% <0.00%> (ø)
app/api/events.py 21.70% <2.77%> (ø)
app/api/users.py 29.82% <7.69%> (ø)
app/api/discount_codes.py 22.27% <8.33%> (ø)
app/api/event_invoices.py 48.88% <16.66%> (ø)
app/api/sessions.py 41.46% <16.66%> (ø)
app/api/tickets.py 46.66% <16.66%> (ø)
app/api/event_sub_topics.py 64.40% <20.00%> (ø)
app/api/feedbacks.py 50.64% <20.00%> (ø)
app/api/notifications.py 76.27% <20.00%> (ø)
... and 26 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 069d8f8...df41a00. Read the comment docs.

@@ -129,3 +129,21 @@ def get_new_slug(model, name):
return slug
else:
return '{}-{}'.format(slug, uuid.uuid4().hex)

def safe_query_kwargs(model, kwargs, parameter_name):
Copy link
Member

Choose a reason for hiding this comment

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

Wrong place to add the method

Copy link
Member Author

Choose a reason for hiding this comment

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

Where should I place this method?

Copy link
Member

Choose a reason for hiding this comment

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

Near others

@iamareebjamal
Copy link
Member

Fix hound and codacy issues

message eg 'event_id'
:return:
"""
return safe_query_without_soft_deleted_entries(
Copy link
Member

@iamareebjamal iamareebjamal May 17, 2020

Choose a reason for hiding this comment

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

You are replacing safe_query method, so you should use it instead, not safe_query_without_soft_deleted_entries

And move it above safe_query_without_soft_deleted_entries

@iamareebjamal
Copy link
Member

Great job, but a lot of safe_query calls are still remaining

@satya7289
Copy link
Member Author

Great job, but a lot of safe_query calls are still remaining

Yes, do I cover all safe_query calls in this PR?

@iamareebjamal
Copy link
Member

Yes, obviously the issue will only be resolved once all such calls are handled

@satya7289
Copy link
Member Author

Yes, obviously the issue will only be resolved once all such calls are handled

Ok

@iamareebjamal
Copy link
Member

Also, calls like this are very similar:

safe_query(EventInvoice, 'identifier', view_kwargs['event_invoice_identifier'], 'event_invoice_identifier')

The only difference is identifier instead of id, so safe_query_kwargs can take a keyword argument, column with default value id, and then the call will change to safe_query(EventInvoice, view_kwargs, 'event_invoice_identifier', column='identifier')

@satya7289
Copy link
Member Author

Also, calls like this are very similar:

safe_query(EventInvoice, 'identifier', view_kwargs['event_invoice_identifier'], 'event_invoice_identifier')

The only difference is identifier instead of id, so safe_query_kwargs can take a keyword argument, column with default value id, and then the call will change to safe_query(EventInvoice, view_kwargs, 'event_invoice_identifier', column='identifier')

ok

@@ -631,7 +632,7 @@ def before_get(self, args, kwargs):
)
kwargs['id'] = order.id
elif kwargs.get('id'):
order = safe_query(Order, 'id', kwargs['id'], 'id')
order = safe_query_kwargs(Order, kwargs, 'id')
Copy link
Member

Choose a reason for hiding this comment

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

Use safe_query_by_id here

@iamareebjamal
Copy link
Member

Great job. Final thing, please change the PR title to match semantic guidelines, see other merged PRs for reference

@@ -16,6 +16,8 @@
from app.api.helpers.db import (
safe_query,
save_to_db,
safe_query_kwargs,
safe_query_by_id

Choose a reason for hiding this comment

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

Black would make changes.

@niranjan94
Copy link
Member

Codacy Here is an overview of what got changed by this pull request:

Clones removed
==============
+ app/api/event_topics.py  -1
+ app/api/event_copyright.py  -1
+ app/api/feedbacks.py  -2
+ app/api/custom_forms.py  -2
+ app/api/event_types.py  -1
+ app/api/tax.py  -2
+ app/api/tickets.py  -1
+ app/api/event_sub_topics.py  -1
         

See the complete overview on Codacy

@satya7289 satya7289 changed the title added safe_query_kwargs refactor: Remove repetitive safe_query May 17, 2020
@satya7289 satya7289 changed the title refactor: Remove repetitive safe_query refactor: remove repetitive safe_query May 17, 2020
Copy link
Member

@iamareebjamal iamareebjamal left a comment

Choose a reason for hiding this comment

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

Great Job!

@satya7289
Copy link
Member Author

Great Job!

Thank you so much.

@iamareebjamal iamareebjamal merged commit a914592 into fossasia:development May 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove repetitive safe_query
4 participants