Skip to content

Commit

Permalink
Merge branch 'hotfix/v0.1.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
myyang committed May 20, 2017
2 parents f27844c + 50fd30b commit f22619c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ python:

env:
- DJANGO='django>=1.8,<1.9'
- DJANGO='django>=1.9,<1.10'
- DJANGO='django>=1.10,<1.11'
- DJANGO='django>=1.11'

install:
- make install
Expand Down
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ django-pb-model
.. image:: https://travis-ci.org/myyang/django-pb-model.svg?branch=master
:target: https://travis-ci.org/myyang/django-pb-model

.. image:: https://img.shields.io/pypi/v/django-pb-model.svg
:target: https://pypi.python.org/pypi/django-pb-model


Django-pb-model provides model mixin mapping/converting protobuf message.
Currently support basic value fields and naive relation convertion, including:
Expand All @@ -26,7 +29,7 @@ Compatibility
Currnetly tested with metrics:

* Python2.7, 3.4, 3.5, 3.6
* Django1.8
* Django1.8, 1,9, 1.10, 1.11

Install
-------
Expand Down
7 changes: 6 additions & 1 deletion pb_model/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ def _protobuf_to_relation(self, dj_field_name, dj_field, pb_field, pb_value):
if dj_field.many_to_many:
self._protobuf_to_m2m(dj_field, pb_value)
return
setattr(self, dj_field_name, dj_field.related.model().from_pb(pb_value))

if hasattr(dj_field, 'related_model'):
# django > 1.8 compatible
setattr(self, dj_field_name, dj_field.related_model().from_pb(pb_value))
else:
setattr(self, dj_field_name, dj_field.related.model().from_pb(pb_value))

def _protobuf_to_m2m(self, dj_field, pb_repeated_set):
"""
Expand Down
8 changes: 6 additions & 2 deletions pb_model/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ def test_model_with_key(self):
msg="{}(src) != {}(target)".format(main_item.bool_field, main_item2.bool_field))
self.assertEqual(main_item.choices_field, main_item2.choices_field,
msg="{}(src) != {}(target)".format(main_item.choices_field, main_item2.choices_field))
self.assertEqual(main_item.datetime_field, main_item2.datetime_field,
msg="{}(src) != {}(target)".format(main_item.datetime_field, main_item2.datetime_field))

time_diff = main_item.datetime_field - main_item2.datetime_field
# convertion may affect 1 microsecond due to resolution difference
# between Protobuf timestamp and Python datetime
self.assertAlmostEqual(0, time_diff.total_seconds(), delta=1e-6,
msg="{}(src) != {}(target)".format(main_item.datetime_field, main_item2.datetime_field))

self.assertEqual(main_item.fk_field.id, main_item2.fk_field.id,
msg="{}(src) != {}(target)".format(main_item.fk_field.id, main_item2.fk_field.id))
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup(
name='django-pb-model',
version='v0.1.2',
version='v0.1.3',
packages=find_packages(),
include_package_data=True,
license='MIT License',
Expand All @@ -26,7 +26,7 @@
author='myyang',
author_email='[email protected]',
install_requires=[
'django>=1.8,<1.9',
'django>=1.8',
'protobuf>=3.1',
],
classifiers=[
Expand Down

0 comments on commit f22619c

Please sign in to comment.