From aa3830c773892758f9a0d8a6d69927a330d2d599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81ngel=20Vel=C3=A1squez?= Date: Thu, 9 Jan 2020 01:52:44 -0500 Subject: [PATCH 1/8] Moving to Django 3.0 - Upgrade models, proxy and migrations - YAPFied - Update tests assertEqual instead assertEquals --- changuito/migrations/0001_initial.py | 52 +++++++++++++++++++++------ changuito/models.py | 38 ++++++++++++-------- changuito/proxy.py | 5 +-- tests/test_models.py | 54 +++++++++++++++------------- 4 files changed, 96 insertions(+), 53 deletions(-) diff --git a/changuito/migrations/0001_initial.py b/changuito/migrations/0001_initial.py index 13e6576..1971ec0 100644 --- a/changuito/migrations/0001_initial.py +++ b/changuito/migrations/0001_initial.py @@ -16,31 +16,61 @@ class Migration(migrations.Migration): migrations.CreateModel( name='Cart', fields=[ - ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)), - ('creation_date', models.DateTimeField(verbose_name='creation date', default=django.utils.timezone.now)), - ('checked_out', models.BooleanField(verbose_name='checked out', default=False)), - ('user', models.ForeignKey(blank=True, null=True, to=settings.AUTH_USER_MODEL)), + ('id', + models.AutoField(serialize=False, + verbose_name='ID', + primary_key=True, + auto_created=True)), + ('creation_date', + models.DateTimeField(verbose_name='creation date', + default=django.utils.timezone.now)), + ('checked_out', + models.BooleanField(verbose_name='checked out', + default=False)), + ('user', + models.ForeignKey( + blank=True, + null=True, + to=settings.AUTH_USER_MODEL, + on_delete=django.db.models.deletion.CASCADE)), ], options={ 'verbose_name_plural': 'carts', 'verbose_name': 'cart', - 'ordering': ('-creation_date',), + 'ordering': ('-creation_date', ), }, ), migrations.CreateModel( name='Item', fields=[ - ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)), - ('quantity', models.DecimalField(decimal_places=3, verbose_name='quantity', max_digits=18)), - ('unit_price', models.DecimalField(decimal_places=2, verbose_name='unit price', max_digits=18)), + ('id', + models.AutoField(serialize=False, + verbose_name='ID', + primary_key=True, + auto_created=True)), + ('quantity', + models.DecimalField(decimal_places=3, + verbose_name='quantity', + max_digits=18)), + ('unit_price', + models.DecimalField(decimal_places=2, + verbose_name='unit price', + max_digits=18)), ('object_id', models.PositiveIntegerField()), - ('cart', models.ForeignKey(verbose_name='cart', to='changuito.Cart')), - ('content_type', models.ForeignKey(to='contenttypes.ContentType')), + ('cart', + models.ForeignKey( + verbose_name='cart', + to='changuito.Cart', + on_delete=django.db.models.deletion.CASCADE)), + ('content_type', + models.ForeignKey( + to='contenttypes.ContentType', + on_delete=django.db.models.deletion.CASCADE)), ], options={ 'verbose_name_plural': 'items', 'verbose_name': 'item', - 'ordering': ('cart',), + 'ordering': ('cart', ), }, ), ] diff --git a/changuito/models.py b/changuito/models.py index 0155dc2..2961dc1 100644 --- a/changuito/models.py +++ b/changuito/models.py @@ -1,5 +1,5 @@ from django.db import models -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import gettext_lazy as _ from django.contrib.contenttypes.models import ContentType try: @@ -15,7 +15,10 @@ class Cart(models.Model): - user = models.ForeignKey(User, null=True, blank=True) + user = models.ForeignKey(User, + null=True, + blank=True, + on_delete=models.CASCADE) creation_date = models.DateTimeField(verbose_name=_('creation date'), default=timezone.now) checked_out = models.BooleanField(default=False, @@ -24,7 +27,7 @@ class Cart(models.Model): class Meta: verbose_name = _('cart') verbose_name_plural = _('carts') - ordering = ('-creation_date',) + ordering = ('-creation_date', ) app_label = 'changuito' def __unicode__(self): @@ -43,21 +46,25 @@ def total_quantity(self): class ItemManager(models.Manager): def get(self, *args, **kwargs): if 'product' in kwargs: - kwargs['content_type'] = ContentType.objects.get_for_model(type(kwargs['product']), - for_concrete_model=False) + kwargs['content_type'] = ContentType.objects.get_for_model( + type(kwargs['product']), for_concrete_model=False) kwargs['object_id'] = kwargs['product'].pk - del(kwargs['product']) + del (kwargs['product']) return super(ItemManager, self).get(*args, **kwargs) class Item(models.Model): - cart = models.ForeignKey(Cart, verbose_name=_('cart')) - quantity = models.DecimalField(max_digits=18, decimal_places=3, + cart = models.ForeignKey(Cart, + verbose_name=_('cart'), + on_delete=models.CASCADE) + quantity = models.DecimalField(max_digits=18, + decimal_places=3, verbose_name=_('quantity')) - unit_price = models.DecimalField(max_digits=18, decimal_places=2, + unit_price = models.DecimalField(max_digits=18, + decimal_places=2, verbose_name=_('unit price')) # product as generic relation - content_type = models.ForeignKey(ContentType) + content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) object_id = models.PositiveIntegerField() objects = ItemManager() @@ -65,7 +72,7 @@ class Item(models.Model): class Meta: verbose_name = _('item') verbose_name_plural = _('items') - ordering = ('cart',) + ordering = ('cart', ) app_label = 'changuito' def __unicode__(self): @@ -75,6 +82,7 @@ def __unicode__(self): def total_price(self): return float(self.quantity) * float(self.unit_price) + total_price = property(total_price) # product @@ -82,8 +90,8 @@ def get_product(self): return self.content_type.get_object_for_this_type(pk=self.object_id) def set_product(self, product): - self.content_type = ContentType.objects.get_for_model(type(product), - for_concrete_model=False) + self.content_type = ContentType.objects.get_for_model( + type(product), for_concrete_model=False) self.object_id = product.pk product = property(get_product, set_product) @@ -97,8 +105,8 @@ def update_price(self, price): self.save() def update_contenttype(self, ctype_obj): - new_content_type = ContentType.objects.get_for_model(type(ctype_obj), - for_concrete_model=False) + new_content_type = ContentType.objects.get_for_model( + type(ctype_obj), for_concrete_model=False) # Let's search if the new contenttype had previous items on the cart try: new_items = Item.objects.get(cart=self.cart, diff --git a/changuito/proxy.py b/changuito/proxy.py index 95fdca2..1da13f3 100644 --- a/changuito/proxy.py +++ b/changuito/proxy.py @@ -1,4 +1,5 @@ from django.contrib.contenttypes.models import ContentType +from django.core.exceptions import ObjectDoesNotExist from . import models @@ -32,14 +33,14 @@ def __init__(self, request): user = request.user try: # First search by user - if not user.is_anonymous(): + if not user.is_anonymous: cart = models.Cart.objects.get(user=user, checked_out=False) # If not, search by request id else: user = None cart_id = request.session.get(CART_ID) cart = models.Cart.objects.get(id=cart_id, checked_out=False) - except: + except (ObjectDoesNotExist, models.Cart.DoesNotExist): cart = self.new(request, user=user) self.cart = cart diff --git a/tests/test_models.py b/tests/test_models.py index 5e6fd23..698f471 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -27,26 +27,31 @@ def _create_item_in_db(self, product=None, quantity=2, unit_price=125): return item def test_cart_creation(self): - self.assertEquals(self.cart.id, 1) - self.assertEquals(self.cart.is_empty(), True, "Cart must be empty") + self.assertEqual(self.cart.id, 1) + self.assertEqual(self.cart.is_empty(), True, "Cart must be empty") def test_item_creation(self): item = self._create_item_in_db() item_in_cart = self.cart.item_set.all()[0] - self.assertEquals(item_in_cart, item, - "First item in cart should be equal the item we created") - self.assertEquals(self.cart.is_empty(), False) - self.assertEquals(item_in_cart.product, self.user, - "Product associated with the first item in cart should equal the user we're selling") - self.assertEquals(item_in_cart.unit_price, Decimal("125"), - "Unit price of the first item stored in the cart should equal 125") - self.assertEquals(item_in_cart.quantity, 2, - "The first item in cart should have 2 in it's quantity") + self.assertEqual( + item_in_cart, item, + "First item in cart should be equal the item we created") + self.assertEqual(self.cart.is_empty(), False) + self.assertEqual( + item_in_cart.product, self.user, + "Product associated with the first item in cart should equal the user we're selling" + ) + self.assertEqual( + item_in_cart.unit_price, Decimal("125"), + "Unit price of the first item stored in the cart should equal 125") + self.assertEqual( + item_in_cart.quantity, 2, + "The first item in cart should have 2 in it's quantity") def test_cart_total_price(self): self._create_item_in_db() self._create_item_in_db(unit_price=Decimal("100.00"), quantity=1) - self.assertEquals(self.cart.total_price(), 350, "Price == (125*2)+100") + self.assertEqual(self.cart.total_price(), 350, "Price == (125*2)+100") def test_cart_total_quantity(self): from django.contrib.sites.models import Site @@ -55,23 +60,22 @@ def test_cart_total_quantity(self): self._create_item_in_db(product=obj_site[0], unit_price=Decimal("100.00"), quantity=1) - self.assertEquals(self.cart.total_quantity(), 4) + self.assertEqual(self.cart.total_quantity(), 4) def test_cart_item_price(self): - item = self._create_item_in_db(quantity=4, - unit_price=Decimal("3.20")) - self.assertEquals(float(item.total_price), float("12.80")) + item = self._create_item_in_db(quantity=4, unit_price=Decimal("3.20")) + self.assertEqual(float(item.total_price), float("12.80")) def test_item_unicode(self): item = self._create_item_in_db() - self.assertEquals(item.__unicode__(), - "%s units of User %s" % (2, self.user.id)) + self.assertEqual(item.__unicode__(), + "%s units of User %s" % (2, self.user.id)) def test_item_update_quantity(self): item = self._create_item_in_db() - self.assertEquals(item.quantity, 2) + self.assertEqual(item.quantity, 2) item.update_quantity(7) - self.assertEquals(item.quantity, 7) + self.assertEqual(item.quantity, 7) def test_item_update_contenttype(self): # Let's import different contenttype objects @@ -89,15 +93,15 @@ def test_item_update_contenttype(self): quantity=2, unit_price=Decimal("100")) - self.assertEquals(item_user.content_type, ctype_user) - self.assertEquals(item_site.content_type, ctype_site) + self.assertEqual(item_user.content_type, ctype_user) + self.assertEqual(item_site.content_type, ctype_site) item_site.update_contenttype(obj_user) - self.assertEquals(item_site.quantity, 3) - self.assertEquals(item_site.total_price, 300) + self.assertEqual(item_site.quantity, 3) + self.assertEqual(item_site.total_price, 300) def test_item_update_price(self): item = self._create_item_in_db() item.update_price(137) - self.assertEquals(item.unit_price, 137) + self.assertEqual(item.unit_price, 137) From c45d6abba1a2d5ccb2ba90e265194f52aef4a437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81ngel=20Vel=C3=A1squez?= Date: Thu, 9 Jan 2020 02:00:44 -0500 Subject: [PATCH 2/8] Update travis and tox So tests will be ran with all version of python3 that support Django 3. (3.6, 3.7, 3.8) --- .travis.yml | 14 +++----------- tox.ini | 8 ++------ 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6630d1a..78bcdf4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,17 +2,9 @@ language: python sudo: false -env: - - TOX_ENV=py27-django1.7 - - TOX_ENV=py27-django1.8 - - TOX_ENV=py27-django1.9 - - TOX_ENV=py27-django1.10 - - TOX_ENV=py27-django1.11 - - TOX_ENV=py34-django1.7 - - TOX_ENV=py34-django1.8 - - TOX_ENV=py34-django1.9 - - TOX_ENV=py34-django1.10 - - TOX_ENV=py34-django1.11 + - TOX_ENV=py36-django3.0 + - TOX_ENV=py37-django3.0 + - TOX_ENV=py38-django3.0 install: - pip install tox coverage coveralls diff --git a/tox.ini b/tox.ini index 0a3d520..54e4acd 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] downloadcache = {toxworkdir}/_download/ -envlist = {py27,py34}-django{1.11,1.10,1.9,1.8,1.7} +envlist = {py36,py37,py38}-django3.0 [testenv] commands = @@ -9,8 +9,4 @@ setenv = PYTHONDONTWRITEBYTECODE=1 deps = -r{toxinidir}/requirements-test.txt - django1.11: Django<1.12 - django1.10: Django<1.11 - django1.9: Django<1.10 - django1.8: Django<1.9 - django1.7: Django<1.8 + django3.0: Django<3.1 From 7c5101a40ce1d66166472ec24483f39e388babd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81ngel=20Vel=C3=A1squez?= Date: Thu, 9 Jan 2020 22:44:58 -0500 Subject: [PATCH 3/8] Fix travis --- .travis.yml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 78bcdf4..47b4cc4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,14 @@ language: python sudo: false - - TOX_ENV=py36-django3.0 - - TOX_ENV=py37-django3.0 - - TOX_ENV=py38-django3.0 +matrix: + include: + - env: TOX_ENV=py36-django3.0 + python: 3.6 + - env: TOX_ENV=py37-django3.0 + python: 3.7 + - env: TOX_ENV=py38-django3.0 + python: 3.8 install: - pip install tox coverage coveralls From 54d6398047406fb79871292c3caba2d2e6cf7cf0 Mon Sep 17 00:00:00 2001 From: Angel Velasquez Date: Tue, 14 Jan 2020 16:16:26 -0500 Subject: [PATCH 4/8] Update requirements-test --- requirements-test.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements-test.txt b/requirements-test.txt index 7be4aa4..41b6bd8 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,6 +1,6 @@ coverage coveralls -flake8>=2.1.0 -tox>=2.1.1 +flake8 +tox pytest pytest-django From 89b46635bbe09e1533375023d7d52e1b11215221 Mon Sep 17 00:00:00 2001 From: Angel Velasquez Date: Tue, 14 Jan 2020 16:16:39 -0500 Subject: [PATCH 5/8] Add some basic docs and new features on docs - Add a CONTRIBUTING.rst - Link to that CONTRIBUTING from README - Add link to a heroku app that will run a live example --- CONTRIBUTING.rst | 111 +++++++++++++++++++++++++++++++++++++++++++++++ README.md | 15 +++++-- 2 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 CONTRIBUTING.rst diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst new file mode 100644 index 0000000..6305e38 --- /dev/null +++ b/CONTRIBUTING.rst @@ -0,0 +1,111 @@ +============ +Contributing +============ + +Contributions are welcome, and they are greatly appreciated! Every +little bit helps, and credit will always be given. + +You can contribute in many ways: + +Types of Contributions +---------------------- + +Report Bugs +~~~~~~~~~~~ + +Report bugs at https://github.com/angvp/django-changuito/issues. + +If you are reporting a bug, please include: + +* Your operating system name and version. +* Any details about your local setup that might be helpful in troubleshooting. +* Detailed steps to reproduce the bug. + +Fix Bugs +~~~~~~~~ + +Look through the GitHub issues for bugs. Anything tagged with "bug" +is open to whoever wants to implement it. + +Implement Features +~~~~~~~~~~~~~~~~~~ + +Look through the GitHub issues for features. Anything tagged with "feature" +is open to whoever wants to implement it. + +Write Documentation +~~~~~~~~~~~~~~~~~~~ + +django-changuito could always use more documentation, whether as part of the +official django-changuito docs, in docstrings, or even on the web in blog posts, +articles, and such. + +Submit Feedback +~~~~~~~~~~~~~~~ + +The best way to send feedback is to file an issue at https://github.com/angvp/django-changuito/issues. + +If you are proposing a feature: + +* Explain in detail how it would work. +* Keep the scope as narrow as possible, to make it easier to implement. +* Remember that this is a volunteer-driven project, and that contributions + are welcome :) + +Get Started! +------------ + +Ready to contribute? Here's how to set up `django-changuito` for local development. + +1. Fork the `django-changuito` repo on GitHub. +2. Clone your fork locally:: + + $ git clone git@github.com:your_name_here/django-changuito.git + +3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:: + + $ mkvirtualenv django-changuito + $ cd django-changuito/ + +4. Create a branch for local development:: + + $ git checkout -b name-of-your-bugfix-or-feature + +Now you can make your changes locally. + +5. When you're done making changes, check that your changes pass flake8 and the +tests, including testing other Python versions with tox:: + + $ flake8 changuito tests + $ python setup.py test + $ tox + +To get flake8 and tox, just pip install them into your virtualenv. + +6. Commit your changes and push your branch to GitHub:: + + $ git add . + $ git commit -m "Your detailed description of your changes." + $ git push origin name-of-your-bugfix-or-feature + +7. Submit a pull request through the GitHub website to the ``develop`` repo. + +Pull Request Guidelines +----------------------- + +Before you submit a pull request, check that it meets these guidelines: + +1. The pull request should include tests. +2. If the pull request adds functionality, the docs should be updated. Put + your new functionality into a function with a docstring, and add the + feature to the list in README.rst. +3. The pull request should work for Python 2.7, and 3.4, and for PyPy. Check + https://travis-ci.org/angvp/django-changuito/pull_requests + and make sure that the tests pass for all supported Python versions. + +Tips +---- + +To run a subset of tests:: + + $ python -m unittest tests.test_changuito diff --git a/README.md b/README.md index 53c53d6..dce0f3e 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,19 @@ and make everything open source on a public repo and uploaded to PyPI. We are already using it on production and we want to encourage old users of django-cart or forked projects of django-cart to migrate to changuito instead. +This was upgrade to the newest Django version, codebase will partially upgraded +in order to support all the features that Django 3 / Python 3.6+ offers to us, +as usual PR are accepted :) see [CONTRIBUTING](https://github.com/angvp/CONTRIBUTING.rst). + +# See it live! + +[https://django-changuito.herokuapp.com/](https://django-changuito.herokuapp.com/) + + ## Prerequisites -- Django 1.7, 1.8, 1.9, 1.10, 1.11 -- Python 2.7 and Python 3.4+ +- Django 3.0 +- Python 3.6+ - django content type framework in your INSTALLED_APPS ## Installation @@ -109,7 +118,7 @@ This is from the original project that I've forked, I just renamed the project s is not officialy dead and continued my work on this project ``` -This project was abandoned and I got it and added tests and South migrations, +This project was abandoned and I got it and added tests and migrations, and I will be maintaining it from now on. ``` From b6336a5c30492bcfbacc2ddb22561619e19cb503 Mon Sep 17 00:00:00 2001 From: Angel Velasquez Date: Tue, 14 Jan 2020 17:17:33 -0500 Subject: [PATCH 6/8] Modernize and prepare for the future version --- setup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 01c8980..a53af8f 100644 --- a/setup.py +++ b/setup.py @@ -1,15 +1,15 @@ #!/usr/bin/env python -from setuptools import setup +from setuptools import setup, find_packages setup( name='django-changuito', - version='1.2', + version='2.0', description='A fork of django-cart with the same simplicity but updated', maintainer='Angel Velasquez', maintainer_email='angvp@archlinux.org', license="LGPL v3", url='https://github.com/angvp/django-changuito', - packages=['changuito'], + packages=find_packages(), classifiers=[ "Development Status :: 5 - Production/Stable", "Environment :: Web Environment", From 7e11c05bd87d77df210fa3628a17530de9c250a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81ngel=20Vel=C3=A1squez?= Date: Sun, 17 Jan 2021 20:39:08 -0500 Subject: [PATCH 7/8] Add Django 3.1 support --- README.md | 3 --- runtests.py | 6 +++--- tox.ini | 3 ++- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index dce0f3e..1aaf632 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,3 @@ and I will be maintaining it from now on. This project is a fork of django-cart which was originally started by Eric Woudenberg and followed up by Marc Garcia , and then continued by Bruno Carvalho, which adds a lot of stuff and then wasn't much aware of the status of the project. The last change ocurred in Jan 29 2012. Bruno and other authors added tests and cool stuff and we are thankful for that, and we will continue with that spirit. - - -[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/angvp/django-changuito/trend.png)](https://bitdeli.com/free "Bitdeli Badge") diff --git a/runtests.py b/runtests.py index 53831a9..fd60547 100644 --- a/runtests.py +++ b/runtests.py @@ -6,15 +6,15 @@ import os import subprocess - PYTEST_ARGS = { - 'default': ['tests', ], + 'default': [ + 'tests', + ], 'fast': ['tests', '-q'], } FLAKE8_ARGS = ['changuito', 'tests', '--ignore=E501'] - sys.path.append(os.path.dirname(__file__)) diff --git a/tox.ini b/tox.ini index 54e4acd..e77a83a 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] downloadcache = {toxworkdir}/_download/ -envlist = {py36,py37,py38}-django3.0 +envlist = {py36,py37,py38,py39}-django{3.0,3.1} [testenv] commands = @@ -10,3 +10,4 @@ setenv = deps = -r{toxinidir}/requirements-test.txt django3.0: Django<3.1 + django3.1: Django<3.2 From 5379c99a426114460663fa1a5593757c51575759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81ngel=20Vel=C3=A1squez?= Date: Sun, 17 Jan 2021 20:40:06 -0500 Subject: [PATCH 8/8] Add Django 3.1 to the docs --- README.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/README.md b/README.md index 1aaf632..8537a10 100644 --- a/README.md +++ b/README.md @@ -21,14 +21,9 @@ This was upgrade to the newest Django version, codebase will partially upgraded in order to support all the features that Django 3 / Python 3.6+ offers to us, as usual PR are accepted :) see [CONTRIBUTING](https://github.com/angvp/CONTRIBUTING.rst). -# See it live! - -[https://django-changuito.herokuapp.com/](https://django-changuito.herokuapp.com/) - - ## Prerequisites -- Django 3.0 +- Django 3.0,3.1 - Python 3.6+ - django content type framework in your INSTALLED_APPS