-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
support-offer-files #64
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
docs/changelog.rst
Outdated
@@ -22,6 +22,7 @@ Added | |||
|
|||
- Introduce Application model. (`iamanikeev`_) | |||
- Support managing of OfferCategory objects. (`iamanikeev`_) | |||
- Support managing of OfferFile objects. (`iamanikeev`_) |
There was a problem hiding this comment.
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) | ||
~~~~~~~~ |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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)
pyoffers/models/offer_file.py
Outdated
|
||
|
||
class OfferFile(Model): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, remove extra line
pyoffers/models/offer_file.py
Outdated
'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')}) |
There was a problem hiding this comment.
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
pyoffers/models/offer_file.py
Outdated
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') |
There was a problem hiding this comment.
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.
tests/conftest.py
Outdated
@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, |
There was a problem hiding this comment.
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`_) |
There was a problem hiding this comment.
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
pyoffers/models/offer_file.py
Outdated
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') |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
pyoffers/utils.py
Outdated
@@ -53,6 +53,23 @@ def prepend_model(self, value, model): | |||
return value | |||
|
|||
|
|||
class ValuesDict(dict): |
There was a problem hiding this comment.
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
tests/models/test_offer_file.py
Outdated
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' |
There was a problem hiding this comment.
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
pyoffers/models/__init__.py
Outdated
) | ||
|
||
MANAGER_ALIASES = ValuesDict({ |
There was a problem hiding this comment.
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
pyoffers/models/__init__.py
Outdated
@@ -1,4 +1,7 @@ | |||
# coding: utf-8 | |||
from pyoffers.models.offer_file import OfferFileManager # noqa |
There was a problem hiding this comment.
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
* 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
No description provided.