from rest_framework import serializers

from olympia.addons.models import Addon
from olympia.addons.serializers import AddonSerializer, VersionSerializer
from olympia.versions.models import Version


class DiscoveryVersionSerializer(VersionSerializer):
    class Meta:
        fields = ('compatibility', 'files',)
        model = Version


class DiscoveryAddonSerializer(AddonSerializer):
    current_version = DiscoveryVersionSerializer()

    class Meta:
        fields = ('id', 'current_version', 'icon_url',
                  'theme_data', 'type', 'url',)
        model = Addon


class DiscoverySerializer(serializers.Serializer):
    heading = serializers.CharField()
    description = serializers.CharField()
    addon = DiscoveryAddonSerializer()

    def to_representation(self, instance):
        data = super(DiscoverySerializer, self).to_representation(instance)
        if data['heading'] is None:
            if instance.addon.listed_authors:
                data['heading'] = u'%s by %s' % (
                    unicode(instance.addon.name),
                    instance.addon.listed_authors[0].name)
            else:
                data['heading'] = unicode(instance.addon.name)
        return data

from rest_framework import serializers

from olympia.addons.models import Addon
from olympia.addons.serializers import AddonSerializer, VersionSerializer
from olympia.versions.models import Version


class DiscoveryVersionSerializer(VersionSerializer):
    class Meta:
        fields = ('compatibility', 'files',)
        model = Version


class DiscoveryAddonSerializer(AddonSerializer):
    current_version = DiscoveryVersionSerializer()

    class Meta:
        fields = ('id', 'current_version', 'guid', 'icon_url',
                  'theme_data', 'type', 'url',)
        model = Addon


class DiscoverySerializer(serializers.Serializer):
    heading = serializers.CharField()
    description = serializers.CharField()
    addon = DiscoveryAddonSerializer()

    def to_representation(self, instance):
        data = super(DiscoverySerializer, self).to_representation(instance)
        if data['heading'] is None:
            if instance.addon.listed_authors:
                data['heading'] = u'%s by %s' % (
                    unicode(instance.addon.name),
                    instance.addon.listed_authors[0].name)
            else:
                data['heading'] = unicode(instance.addon.name)
        return data

Add back guid in the discovery pane API
Add back guid in the discovery pane API
