# coding=utf-8

from django.utils import timezone
from django.conf import settings
from rest_framework import serializers
from apps.activity.models import Activity

class ActivitySerializer(serializers.ModelSerializer):
    create_user_text = serializers.CharField(source='create_user.employee.name', read_only=True)
    playbill_url = serializers.SerializerMethodField()
    cover = serializers.SerializerMethodField()
    status = serializers.SerializerMethodField()

    def get_status(self, obj):
        if obj.end_date >= timezone.now().date():
            return u'进行中'
        return u'已结束'

    def get_playbill_url(self, obj):
        if obj.cover:
            return '%s%s' % (settings.MEDIA_URL, obj.cover.picture)
        return ''

    def get_cover(self, obj):
        if obj.cover:
            return {'width': obj.cover.width, 'height': obj.cover.height, 'url': obj.cover.get_path(), 'id': obj.id}
        return ''


    class Meta:
        model = Activity
        fields = '__all__'