Exemple #1
0
 def __init__(self, context):
     self._repository = CommodityRepository()
     super(CommodityFacade, self).__init__(context)
Exemple #2
0
class CommodityFacade(BaseFacade):
    def __init__(self, context):
        self._repository = CommodityRepository()
        super(CommodityFacade, self).__init__(context)
    def list(self, param = None):
        if (not param):
            param = CommodityQueryModel()
        param.user_id = self._context.user_id
        entities = self._repository.list(param)
        return map(lambda e: obj_map(e, CommodityViewModel, rules={
                                                                   'discount_price':lambda e:e.base_price,
                                                                   'brand_name': lambda e:None if e.brand is None else e.brand.name,
                                                                   'photos':None,
                                                                   'details':None
                                                                   }), entities)
    def get(self, entity_id):
        mapper={CommodityDetail: CommodityDetailViewModel}
        entity = self._repository.get(entity_id)
        if entity is None or (not self.is_accessable(entity)):
            return None
        return obj_map(entity, CommodityViewModel, rules={
                                                          'discount_price':lambda e:e.base_price,
                                                          'brand_name': lambda e:None if e.brand is None else e.brand.name,
                                                          'photos':lambda e: map(lambda f:UploadFileViewModel({'id':f.file_id}), e.uploadFiles),
                                                          'field_name':lambda e:e.field.field_name,
                                                          'field_type':lambda e:e.field.field_type,
                                                          'node':lambda e:e.field.note
                                                          }, mapper=mapper)
    def save(self, viewModel):
        mapper={CommodityDetailViewModel: CommodityDetail}
        entity = obj_map(viewModel, Commodity, mapper=mapper)
        if entity.user_id > 0 and (not self.is_accessable(entity)):
            return
        entity.user_id = self._context.user_id
        return self._repository.save(entity)
    def delete(self, entity_id):
        if self.get(entity_id) is None:
            return
        return self._repository.delete(entity_id)
    def batch_delete(self, entity_ids):
        for entity_id in entity_ids:
            if (self.get(entity_id) is None):
                return
        return self._repository.batch_delete(entity_ids)
    def shelve(self, entity_id, is_off_shelve):
        if self.get(entity_id) is None:
            return
        return self._repository.shelve(entity_id, is_off_shelve)
    def batch_shelve(self, entity_ids, is_off_shelve):
        for entity_id in entity_ids:
            if (self.get(entity_id) is None):
                return
        return self._repository.batch_shelve(entity_ids, is_off_shelve)
    def delete_file(self, commodity_id, file_id):
        if self.get(commodity_id) is None:
            return
        return self._repository.delete_file(commodity_id, file_id)
    def add_file(self, commodity_id, file_id, file_type):
        if self.get(commodity_id) is None:
            return
        return self._repository.add_file(commodity_id, file_id, file_type)