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

support-offer-files #64

Merged
merged 17 commits into from
Mar 15, 2018
Merged

support-offer-files #64

merged 17 commits into from
Mar 15, 2018

Conversation

iamanikeev
Copy link
Contributor

No description provided.

@codecov
Copy link

codecov bot commented Mar 14, 2018

Codecov Report

Merging #64 into master will decrease coverage by 0.21%.
The diff coverage is 95.65%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #64      +/-   ##
==========================================
- Coverage   99.76%   99.54%   -0.22%     
==========================================
  Files          12       13       +1     
  Lines         424      441      +17     
  Branches       49       51       +2     
==========================================
+ Hits          423      439      +16     
- Partials        1        2       +1
Impacted Files Coverage Δ
pyoffers/api.py 99.12% <100%> (+0.03%) ⬆️
pyoffers/models/offer_file.py 92.3% <92.3%> (ø)

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 08f3b7e...cb930ce. Read the comment docs.

@@ -22,6 +22,7 @@ Added

- Introduce Application model. (`iamanikeev`_)
- Support managing of OfferCategory objects. (`iamanikeev`_)
- Support managing of OfferFile objects. (`iamanikeev`_)
Copy link
Owner

Choose a reason for hiding this comment

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

Please, put it in the Added sub-section under Unreleased

docs/usage.rst Outdated

Offer files (Creatives)
~~~~~~~~
Copy link
Owner

Choose a reason for hiding this comment

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

It should be the same length as the previous line

pyoffers/api.py Outdated
@@ -99,7 +99,10 @@ def _call(self, target, method, single_result=True, raw=False, **kwargs):
**kwargs
)
self.logger.debug('Request parameters: %s', params)
response = self.session.get(self.endpoint, params=params, verify=self.verify)
if files:
Copy link
Owner

Choose a reason for hiding this comment

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

Lets create kwargs for self.session.request method. Like this:

kwargs = {'url': endpoint, 'params': params, 'verify': self.verify}
if files:
    kwargs.update({'method': 'POST', 'files': files})
else:
    kwargs['method'] = 'GET'
response = self.session.request(**kwargs)



class OfferFile(Model):

Copy link
Owner

Choose a reason for hiding this comment

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

Please, remove extra line

'If the file is in the different directory, specify its path via ``local_path``.'
file_name = kwargs.get('filename')
local_path = kwargs.pop('local_path', file_name)
return self._call('create', data=kwargs, files={file_name: open(local_path, 'rb')})
Copy link
Owner

Choose a reason for hiding this comment

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

You'd better close the file via context manager for example

assert 'filename' in kwargs, \
'You must specify ``filename`` argument, which should be a name of a readable file in current directory. ' \
'If the file is in the different directory, specify its path via ``local_path``.'
file_name = kwargs.get('filename')
Copy link
Owner

Choose a reason for hiding this comment

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

Just use a single variable for the file path. It will work in the same way as in any popular library or even in python itself.

@pytest.fixture(scope='session')
def offer_file(api):
return api.offer_files.create(
filename='test-thumbnail.jpg', display='TEST_FILE', type='offer thumbnail', width=200,
Copy link
Owner

Choose a reason for hiding this comment

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

What is the purpose of filename and local_path. Is it meaningful to use them together?

Added
~~~~~

- Support managing of OfferFile objects. (`iamanikeev`_)
Copy link
Owner

Choose a reason for hiding this comment

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

You shouldn't add new features to already released version. Please, add it under Unreleased section

def create(self, **kwargs):
assert 'filename' in kwargs, \
'You must specify ``filename`` argument, which should be a name of a readable file in current directory.'
file_name_w_path = kwargs.get('filename')
Copy link
Owner

Choose a reason for hiding this comment

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

There are 3 names for 2 things. filename, file_name_w_path and file_name.
let's do it like this:

def create(self, path, **kwargs):
    with open(path, 'rb') as fd:
        if 'filename' not in kwargs:
            kwargs['filename'] = os.path.basename(path)
        return self._call('create', data=kwargs, files={kwargs['filename']: fd})

In this case it would be possible for users to specify filename. Note, that setdefault is not used, because if there is a filename then os.path.basename will be called without a reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is similar to what it was before, in this case we have two arguments for the same thing: path and filename. I would rather stick to one argument. Auxiliary variables are just required in order not to call kwargs.get('filename') twice.

@@ -53,6 +53,23 @@ def prepend_model(self, value, model):
return value


class ValuesDict(dict):
Copy link
Owner

Choose a reason for hiding this comment

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

It is overkill, please, remove

with pytest.raises(FileNotFoundError):
api.offer_files.create(
display='TEST_FILE', type='offer thumbnail', width=200,
height=100, offer_id=438, filename='whatever.txt', local_path='whatever.txt'
Copy link
Owner

Choose a reason for hiding this comment

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

local_path is not a relevant kwarg. Please, remove it

)

MANAGER_ALIASES = ValuesDict({
Copy link
Owner

Choose a reason for hiding this comment

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

Just put 2 keys here, instead of ValuesDictz

@@ -1,4 +1,7 @@
# coding: utf-8
from pyoffers.models.offer_file import OfferFileManager # noqa
Copy link
Owner

Choose a reason for hiding this comment

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

from .offer_file import OfferFileManager

@Stranger6667 Stranger6667 merged commit cda27d0 into Stranger6667:master Mar 15, 2018
Stranger6667 pushed a commit that referenced this pull request Mar 28, 2018
* Isort code in tests

* Support offer files

* Add changelog entry

* Isort

* Isort

* Isort

* Don't override update method, since filename updates are not allowed by HO

* Inherit update from base ModelManager

* Isort!!

* Fix documentation, get rid of useless argument

* Relative import

* Fix changelog entry, remove excess class

* Simplify create method

* Remove excess test case
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

2 participants