| 
              # -*- 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 account.models import UserInfo
from simditor.fields import RichTextField
class GoodsInfo(BaseModelMixin):
    good_id = ShortUUIDField(_('good_id'), max_length=32, blank=True, null=True, help_text='商品唯一标识', db_index=True, unique=True)
    title = models.CharField(_('title'), max_length=128, blank=True, null=True, help_text='商品名称')
    image = models.ImageField(_('image'), upload_to=upload_path, blank=True, null=True, help_text='商品图片')
    intro = models.TextField(_('intro'), blank=True, null=True, help_text='商品简介')
    price = models.IntegerField(_('price'), default=0, help_text='商品价格(分)')
    class Meta:
        verbose_name = _('商品信息')
        verbose_name_plural = _('商品信息')
    def __unicode__(self):
        return self.pk
    @property
    def image_path(self):
        return upload_file_path(self.image)
    @property
    def image_url(self):
        return upload_file_url(self.image)
    @property
    def data(self):
        return {
            'good_id': self.good_id,
            'title': self.title,
            'image_url': self.image_url,
            'intro': self.intro,
            'price': self.price,
        }
class PackInfo(BaseModelMixin):
    pack_id = ShortUUIDField(_('pack_id'), max_length=32, blank=True, null=True, help_text='包唯一标识', db_index=True, unique=True)
    title = models.CharField(_('title'), max_length=128, blank=True, null=True, help_text='标题', db_index=True)
    expired_at = models.DateTimeField(_('expired_at'), blank=True, null=True, help_text=_('过期时间'))
    pack_detail = RichTextField(_('pack_detail'), blank=True, null=True, help_text='包详情')
    kol_id = models.CharField(_('kol_id'), max_length=32, blank=True, null=True, help_text='KOL 唯一标识', db_index=True)
    class Meta:
        verbose_name = _('包信息')
        verbose_name_plural = _('包信息')
    def __unicode__(self):
        return self.pk
    @property
    def data(self):
        return {
            'title': self.title,
            'expired_at': tc.local_string(utc_dt=self.expired_at, format='%Y-%m-%d %H:%M'),
            'pack_detail': self.pack_detail,
        }
class PackGoodsInfo(BaseModelMixin):
    pack_id = models.CharField(_('pack_id'), max_length=32, blank=True, null=True, help_text='包唯一标识', db_index=True)
    good_id = models.CharField(_('good_id'), max_length=32, blank=True, null=True, help_text='商品唯一标识', db_index=True)
    inventory = models.IntegerField(_('inventory'), default=0, help_text='库存数量')
    has_sale_num = models.IntegerField(_('has_sale_num'), default=0, help_text='已购数量')
    class Meta:
        verbose_name = _('包-商品信息')
        verbose_name_plural = _('包-商品信息')
    def __unicode__(self):
        return self.pk
    @property
    def good_info(self):
        try:
            good = GoodsInfo.objects.get(good_id=self.good_id)
        except GoodsInfo.DoesNotExist:
            good = {}
        data = good.data
        data.update({'inventory': self.inventory, 'has_sale_num': self.has_sale_num})
        return data
class PackGoodsSaleInfo(BaseModelMixin):
    pack_id = models.CharField(_('pack_id'), max_length=32, blank=True, null=True, help_text='包唯一标识', db_index=True)
    user_id = models.CharField(_('user_id'), max_length=32, blank=True, null=True, help_text='用户唯一标识', db_index=True)
    # 商品名称,数量,单价
    # [{'title': '', 'amount': 1, 'price': 1}]
    saleinfo = JSONField(_('saleinfo'), default=[], blank=True, null=True, help_text='商品销售信息')
    class Meta:
        verbose_name = _('包-商品-销售信息')
        verbose_name_plural = _('包-商品-销售信息')
    def __unicode__(self):
        return self.pk
    @property
    def data(self):
        try:
            user = UserInfo.objects.get(user_id=self.user_id)
        except UserInfo.DoesNotExist:
            user = None
        return {
            'userinfo': user.userinfo if user else {},
            'saleinfo': self.saleinfo,
            'created_at': tc.local_string(utc_dt=self.created_at),
        }
 
  |