Skip to content

Commit

Permalink
Merge pull request #4 from dmarcelino/29_choices
Browse files Browse the repository at this point in the history
Fix byteweaver#29: use choices only in the form
  • Loading branch information
Amurmurmur committed Sep 5, 2018
1 parent 313f8f7 commit 785842a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
4 changes: 2 additions & 2 deletions coupons/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.utils.translation import ugettext_lazy as _
from django.views.generic.base import TemplateView

from .forms import CouponGenerationForm
from .forms import CouponGenerationForm, CouponAdminForm
from .models import Coupon, CouponUser, Campaign


Expand All @@ -19,6 +19,7 @@ def get_max_num(self, request, obj=None, **kwargs):


class CouponAdmin(admin.ModelAdmin):
form = CouponAdminForm
list_display = [
'created_at', 'code', 'type', 'value', 'user_count', 'user_limit', 'is_redeemed', 'valid_until', 'campaign'
]
Expand All @@ -36,7 +37,6 @@ def get_urls(self):
my_urls = [
url(r'generate-coupons', self.admin_site.admin_view(GenerateCouponsAdminView.as_view()),
name='generate_coupons'),

]
return my_urls + urls

Expand Down
8 changes: 8 additions & 0 deletions coupons/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
from .settings import COUPON_TYPES


class CouponAdminForm(forms.ModelForm):
type = forms.ChoiceField(label=_("Type"), choices=COUPON_TYPES)

class Meta:
fields = '__all__'
model = Coupon


class CouponGenerationForm(forms.Form):
quantity = forms.IntegerField(label=_("Quantity"))
value = forms.IntegerField(label=_("Value"))
Expand Down
20 changes: 20 additions & 0 deletions coupons/migrations/0008_auto_20180326_1414.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2018-03-26 13:14
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('coupons', '0007_auto_20151105_2328'),
]

operations = [
migrations.AlterField(
model_name='coupon',
name='type',
field=models.CharField(max_length=20, verbose_name='Type'),
),
]
11 changes: 7 additions & 4 deletions coupons/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def unused(self):
return self.filter(users__redeemed_at__isnull=True)

def expired(self):
return self.filter(valid_until__lt=timezone.now())
return self.filter(valid_until__lt=timezone.localtime(timezone.now()))


@python_2_unicode_compatible
Expand All @@ -70,9 +70,12 @@ class Coupon(models.Model):
code = models.CharField(
_("Code"), max_length=30, unique=True, blank=True,
help_text=_("Leaving this field empty will generate a random code."))
type = models.CharField(_("Type"), max_length=20, choices=COUPON_TYPES)
type = models.CharField(_("Type"), max_length=20)
user_limit = models.PositiveIntegerField(_("User limit"), default=1)
created_at = models.DateTimeField(_("Created at"), auto_now_add=True)
valid_from = models.DateTimeField(
_("Valid from"), blank=True, null=True,
help_text=_("Leave empty for coupons that never expire"))
valid_until = models.DateTimeField(
_("Valid until"), blank=True, null=True,
help_text=_("Leave empty for coupons that never expire"))
Expand All @@ -95,7 +98,7 @@ def save(self, *args, **kwargs):
super(Coupon, self).save(*args, **kwargs)

def expired(self):
return self.valid_until is not None and self.valid_until < timezone.now()
return self.valid_until is not None and self.valid_until < timezone.localtime(timezone.now())

@property
def is_redeemed(self):
Expand Down Expand Up @@ -129,7 +132,7 @@ def redeem(self, user=None):
coupon_user.user = user
except CouponUser.DoesNotExist:
coupon_user = CouponUser(coupon=self, user=user)
coupon_user.redeemed_at = timezone.now()
coupon_user.redeemed_at = timezone.localtime(timezone.now())
coupon_user.save()
redeem_done.send(sender=self.__class__, coupon=self)

Expand Down
2 changes: 1 addition & 1 deletion coupons/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_reuse(self):
self.assertFalse(form.is_valid())

def test_expired(self):
self.coupon.valid_until = timezone.now() - timedelta(1)
self.coupon.valid_until = timezone.localtime(timezone.now()) - timedelta(1)
self.coupon.save()
form_data = {'code': self.coupon.code}
form = CouponForm(data=form_data, user=self.user)
Expand Down
2 changes: 1 addition & 1 deletion coupons/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_expired(self):
coupon = Coupon.objects.create_coupon('monetary', 100)
self.assertFalse(coupon.expired())
self.assertEqual(Coupon.objects.expired().count(), 0)
coupon.valid_until = timezone.now() - timedelta(1)
coupon.valid_until = timezone.localtime(timezone.now()) - timedelta(1)
coupon.save()
self.assertTrue(coupon.expired())
self.assertEqual(Coupon.objects.expired().count(), 1)
Expand Down

0 comments on commit 785842a

Please sign in to comment.