|
# -*- 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'维修券过期时间'))
is_coupon_admin_writeoff = models.BooleanField(_(u'is_coupon_admin_writeoff'), default=True, help_text=_(u'是否是管理员核销'), db_index=True)
coupon_limit_model_ids = JSONField(_(u'coupon_limit_model_ids'), blank=True, null=True, help_text=u'券限制使用 model_ids')
class Meta:
verbose_name = _(u'券信息')
verbose_name_plural = _(u'券信息')
def __unicode__(self):
return unicode(self.pk)
@property
def coupon_image_path(self):
return upload_file_path(self.coupon_image)
@property
def coupon_image_url(self):
return upload_file_url(self.coupon_image)
@property
def final_expire_at(self):
if self.coupon_expire_type == CouponInfo.FIXED_EXPIRED_TIME:
return self.coupon_expire_at
return tc.utc_datetime(days=self.coupon_valid_period)
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'品牌名称')
user_coupon_id = ShortUUIDField(_(u'user_coupon_id'), max_length=32, blank=True, null=True, help_text=u'用户券唯一标识', db_index=True, unique=True)
coupon_id = models.CharField(_(u'coupon_id'), max_length=32, blank=True, null=True, help_text=u'券唯一标识', db_index=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_model_ids = JSONField(_(u'coupon_limit_model_ids'), blank=True, null=True, help_text=u'券限制使用 model_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)
admin_name = models.CharField(_(u'admin_name'), max_length=255, blank=True, null=True, help_text=u'核销员名称', db_index=True)
used_at = models.DateTimeField(_(u'used_at'), blank=True, null=True, help_text=u'维修券核销时间')
is_coupon_admin_writeoff = models.BooleanField(_(u'is_coupon_admin_writeoff'), default=True, help_text=_(u'是否是管理员核销'), db_index=True)
clerk_id = models.CharField(_(u'admin_id'), max_length=32, blank=True, null=True, help_text=u'销售员唯一标识', db_index=True)
clerk_name = models.CharField(_(u'clerk_name'), max_length=32, blank=True, null=True, help_text=u'销售员名称', db_index=True)
distributor_id = models.CharField(_(u'distributor_id'), max_length=32, blank=True, null=True, help_text=u'经销商唯一标识', db_index=True)
distributor_name = models.CharField(_(u'distributor_name'), max_length=32, blank=True, null=True, help_text=u'经销商名称', db_index=True)
coupon_from = models.CharField(_(u'coupon_from'), default='MEMBER_BENEFITS', max_length=32, blank=True, null=True, help_text=u'劵来源', db_index=True)
activity_id = models.CharField(_(u'activity_id'), max_length=32, blank=True, null=True, help_text=u'活动唯一标识', db_index=True)
activity_name = models.CharField(_(u'activity_name'), max_length=32, blank=True, null=True, help_text=u'活动名称', db_index=True)
class Meta:
verbose_name = _(u'用户券信息')
verbose_name_plural = _(u'用户券信息')
def __unicode__(self):
return unicode(self.pk)
@property
def coupon_image_path(self):
return upload_file_path(self.coupon_image)
@property
def coupon_image_url(self):
return upload_file_url(self.coupon_image)
@property
def has_actived(self):
if not self.active_at:
return True
return tc.utc_datetime() > self.active_at
@property
def has_expired(self):
if not self.expire_at:
return False
return tc.utc_datetime() > self.expire_at
@property
def coupon_source(self):
if self.coupon_from == 'INTEGRAL_MALL':
return '积分商城'.encode('utf-8')
elif self.coupon_from == 'MEMBER_BENEFITS':
return '会员权益'.encode('utf-8')
elif self.coupon_from == 'PROMOTION':
return self.activity_name
return ''
@property
def data(self):
return {
'user_coupon_id': self.user_coupon_id,
'coupon_id': self.coupon_id,
'user_id': self.user_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,
'is_coupon_admin_writeoff': self.is_coupon_admin_writeoff,
'active_at': tc.local_string(utc_dt=self.active_at, format='%Y%m%d'),
'expire_at': tc.local_string(utc_dt=self.expire_at, format='%Y-%m-%d'),
'coupon_valid_period': self.coupon_valid_period,
'coupon_limit_model_ids': self.coupon_limit_model_ids,
'has_actived': self.has_actived,
'has_expired': self.has_expired,
'has_used': self.has_used,
'admin_id': self.admin_id,
'used_at': self.used_at
}
@property
def admindata(self):
return {
'user_coupon_id': self.user_coupon_id,
'coupon_id': self.coupon_id,
'user_id': self.user_id,
'coupon_title': self.coupon_title,
'coupon_value': self.coupon_value,
'coupon_image_url': self.coupon_image_url,
'is_coupon_admin_writeoff': self.is_coupon_admin_writeoff,
'active_at': tc.local_string(utc_dt=self.active_at, format='%Y-%m-%d'),
'expire_at': tc.local_string(utc_dt=self.expire_at, format='%Y-%m-%d'),
'coupon_valid_period': self.coupon_valid_period,
'coupon_limit_model_ids': self.coupon_limit_model_ids,
'has_actived': self.has_actived,
'has_expired': self.has_expired,
'has_used': self.has_used,
'admin_id': self.admin_id,
'admin_name': self.admin_name,
'used_at': self.used_at,
'coupon_from': self.activity_name if self.coupon_from == 'PROMOTION' else self.coupon_from,
'clerk_name': self.clerk_name,
'distributor_name': self.distributor_name,
}
|