# -*- coding: utf8 -*-

# Copyright 2015 Sébastien Maccagnoni-Munch
#
# This file is part of AwesomeShop.
#
# AwesomeShop is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# AwesomeShop is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with AwesomeShop. If not, see <http://www.gnu.org/licenses/>.

from marshmallow import fields

from . import get_locale

class Count(fields.Field):
    """
    Dump only: count the number of objects in a queryset
    """
    def _serialize(self, value, attr, obj):
        if isinstance(value, list):
            return len(value)
        else:
            return value.count()

class Loc(fields.Field):
    """
    Dump only: localize a TranslationField
    """
    def _serialize(self, value, attr, obj):
        return value.get(get_locale(), '')

class LocObjField(fields.Field):
    """
    Dump only: localize a TranslationField from a linked object

    * f: the field in the object
    """
    def _serialize(self, value, attr, obj):
        return getattr(value, self.metadata['f']).get(get_locale(), '')

class NetPrice(fields.Field):
    """
    Dump only: net price for a Price object
    """
    def _serialize(self, value, attr, obj):
        return str(value.quantize('0.01').net)

class ObjField(fields.Field):
    """
    Dump: select a field from an object
    Load: select an object from the field value

    Needed arguments:
    * f: the field used to find the object
    * obj: the object class
    """
    def _serialize(self, value, attr, obj):
        return getattr(value, self.metadata['f'])

    def _deserialize(self, value, attr, data):
        return self.metadata['obj'].objects.get(**{self.metadata['f']: value})

class MultiObjField(fields.Field):
    """
    Dump: get a list of fields from a list of objects (mongoengine's ListField)
    Load: get a list of objects from a list of fields

    Needed arguments:
    * f: the field used to find the objects
    * obj: the object class
    """
    def _serialize(self, value, attr, obj):
        if self.metadata['f'] == 'id':
            return [ str(v['id']) for v in value ]
        else:
            return [ getattr(v, self.metadata['f']) for v in value ]

    def _deserialize(self, value, attr, data):
        return [ self.metadata['obj'].objects.get(**{self.metadata['f']: v}) \
                 for v in value ]

@@ -1950,32 +1950,75 @@
 ue, attr, obj):%0A
+        if self.metadata%5B'f'%5D == 'id':%0A    
         return g
@@ -2008,32 +2008,36 @@
          return 
+str(
 getattr(value, s
@@ -2053,16 +2053,93 @@
 ata%5B'f'%5D
+, ''))%0A        else:%0A            return getattr(value, self.metadata%5B'f'%5D, ''
 )%0A%0A    d
@@ -2172,32 +2172,54 @@
 e, attr, data):%0A
+        if value:%0A    
         return s
@@ -2254,38 +2254,126 @@
 get(
-**%7Bself.metadata%5B'f'%5D: value%7D)
+%0A                                                 **%7Bself.metadata%5B'f'%5D: value%7D)%0A        else:%0A            return None
 %0A%0Acl

Improve ObjField for marshmallow