|
# -*- coding: utf-8 -*-
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_models_ext import BaseModelMixin, upload_file_path, upload_file_url, upload_path
from jsonfield import JSONField
from shortuuidfield import ShortUUIDField
from TimeConvert import TimeConvert as tc
from simditor.fields import RichTextField
class CouponInfo(BaseModelMixin):
FIXED_EXPIRED_TIME = 0
CHANGED_EXPIRED_TIME = 1
COUPON_EXPIRED_TIME_TUPLE = (
(FIXED_EXPIRED_TIME, u'固定结束时间'),
(CHANGED_EXPIRED_TIME, u'可变结束时间'),
)
brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')
coupon_id = ShortUUIDField(_(u'coupon_id'), max_length=32, blank=True, null=True, help_text=u'券唯一标识', db_index=True, unique=True)
coupon_title = models.CharField(_(u'coupon_title'), max_length=255, blank=True, null=True, help_text=u'券标题')
coupon_detail = RichTextField(_(u'coupon_detail'), blank=True, null=True, help_text=u'券详情')
coupon_value = models.IntegerField(_(u'coupon_value'), default=0, help_text=_(u'维修券金额(单位:分)'))
coupon_image = models.ImageField(_(u'coupon_image'), upload_to=upload_path, blank=True, null=True, help_text=u'券图片')
coupon_expire_type = models.IntegerField(_(u'coupon_expire_type'), choices=COUPON_EXPIRED_TIME_TUPLE, default=FIXED_EXPIRED_TIME, help_text=_(u'维修券类型'))
coupon_valid_period = models.IntegerField(_(u'coupon_valid_period'), default=0, help_text=_(u'维修券有效时间(单位:天)'))
coupon_expire_at = models.DateTimeField(_(u'coupon_expire_at'), blank=True, null=True, help_text=_(u'维修券过期时间'))
coupon_limit_brand_ids = JSONField(_(u'coupon_limit_brand_ids'), blank=True, null=True, help_text=u'券限制使用 brand_ids')
class Meta:
verbose_name = _(u'券信息')
verbose_name_plural = _(u'券信息')
def __unicode__(self):
return unicode(self.pk)
class UserCouponInfo(BaseModelMixin):
brand_id = models.CharField(_(u'brand_id'), max_length=32, blank=True, null=True, help_text=u'品牌唯一标识', db_index=True)
brand_name = models.CharField(_(u'brand_name'), max_length=255, blank=True, null=True, help_text=u'品牌名称')
coupon_id = ShortUUIDField(_(u'coupon_id'), max_length=32, blank=True, null=True, help_text=u'券唯一标识', db_index=True, unique=True)
user_id = models.CharField(_(u'user_id'), max_length=32, blank=True, null=True, help_text=u'用户唯一标识', db_index=True)
coupon_title = models.CharField(_(u'coupon_title'), max_length=255, blank=True, null=True, help_text=u'券标题')
coupon_detail = RichTextField(_(u'coupon_detail'), blank=True, null=True, help_text=u'券详情')
coupon_value = models.IntegerField(_(u'coupon_value'), default=0, blank=True, null=True, help_text=u'券金额(单位:分)')
coupon_image = models.ImageField(_(u'coupon_image'), upload_to=upload_path, blank=True, null=True, help_text=u'券图片')
active_at = models.DateTimeField(_(u'active_at'), blank=True, null=True, help_text=_(u'生效时间'))
expire_at = models.DateTimeField(_(u'expire_at'), blank=True, null=True, help_text=_(u'过期时间'))
coupon_valid_period = models.IntegerField(_(u'coupon_valid_period'), default=0, help_text=_(u'券有效时间(单位:天)'))
coupon_limit_brand_ids = JSONField(_(u'coupon_limit_brand_ids'), blank=True, null=True, help_text=u'券限制使用 brand_ids')
has_used = models.BooleanField(_(u'has_used'), default=False, help_text=u'是否已核销', db_index=True)
admin_id = models.CharField(_(u'admin_id'), max_length=32, blank=True, null=True, help_text=u'核销员唯一标识', db_index=True)
used_at = models.DateTimeField(_(u'used_at'), blank=True, null=True, help_text=u'维修券核销时间')
class Meta:
verbose_name = _(u'用户券信息')
verbose_name_plural = _(u'用户券信息')
def __unicode__(self):
return unicode(self.pk)
@property
def coupon_image(self):
return upload_file_path(self.coupon_image)
@property
def coupon_image_url(self):
return upload_file_url(self.coupon_image)
@property
def data(self):
return {
'coupon_id': self.coupon_id,
'coupon_title': self.coupon_title,
'coupon_detail': self.coupon_detail,
'coupon_amount': self.coupon_value,
'coupon_value': self.coupon_value,
'coupon_image_url': self.coupon_image_url,
'active_at': tc.local_string(self.active_at, format='%Y%m%d'),
'expire_at': tc.local_string(self.expire_at, format='%Y%m%d'),
'coupon_valid_period': self.coupon_valid_period,
'coupon_limit_brand_ids': self.coupon_limit_brand_ids,
'has_used': self.has_used,
'admin_id': self.admin_id,
'used_at': self.used_at,
}
|