# -*- coding: utf-8 -*-

import os

from django.db import models
from django.utils.translation import ugettext_lazy as _
from TimeConvert import TimeConvert as tc

from course.basemodels import CreateUpdateMixin
from utils.url_utils import upload_file_url


def upload_path(instance, old_filename):
    return 'file/{ym}/{stamp}{ext}'.format(
        ym=tc.local_string(format='%Y%m'),
        stamp=tc.local_timestamp(ms=True),
        ext=os.path.splitext(old_filename)[1].lower(),
    )


class CourseCodeSettingInfo(CreateUpdateMixin):
    cover_image = models.ImageField(_(u'cover_image'), upload_to=upload_path, blank=True, null=True, help_text=u'兑换课程页图片')

    class Meta:
        verbose_name = _(u'coursecodesettinginfo')
        verbose_name_plural = _(u'coursecodesettinginfo')

    def __unicode__(self):
        return unicode(self.pk)

    @property
    def cover_image_url(self):
        return upload_file_url(self.cover_image)
