# Path: openunipay/ali_pay/models.py
# class AliPayOrder(models.Model):
#     out_trade_no = models.CharField(verbose_name='商户订单号', max_length=32, db_index=True, editable=False)
#     subject = models.CharField(verbose_name='商品名称', max_length=128, editable=False)
#     body = models.CharField(verbose_name='商品详情', max_length=512, editable=False)
#     total_fee = models.DecimalField(verbose_name='总金额(单位:元)', max_digits=6, decimal_places=2, editable=False)
#     it_b_pay = models.CharField(verbose_name='交易有效期', max_length=19, editable=False)
#     date_create = models.DateTimeField(verbose_name=u'创建时间', auto_now_add=True)
#     
#     class Meta:
#         verbose_name = u'支付宝订单'
#         verbose_name_plural = u'支付宝订单'
#         
#     def __str__(self):
#         return self.out_trade_no
#         
#     def compose_interface_data(self):
#         # sign data
#         data = self._compose_data()
#         return '{}&sign_type="RSA"&sign="{}"'.format(data, quote_plus(security.sign(data)))
#     
#     def _compose_data(self):
#         valueDict = {item:getattr(self, item) 
#                 for item in _ALIPAY_ORDER_FIELD 
#                 if getattr(self, item)}
#         valueDict['service'] = _SERVICE
#         valueDict['_input_charset'] = _CHARSET
#         valueDict['payment_type'] = _PAYMENT_TYPE
#         valueDict['partner'] = settings.ALIPAY['partner']
#         valueDict['seller_id'] = settings.ALIPAY['seller_id']
#         valueDict['notify_url'] = settings.ALIPAY['notify_url']
#         
#         temp = []
#         for key in valueDict:
#             if not valueDict[key]:
#                 continue
#             temp.append('{}="{}"'.format(key, valueDict[key]))
#         tempStr = '&'.join(temp)
#         return tempStr


from django.contrib import admin
from .models import AliPayOrder
from import_export import resources
from import_export.admin import ImportExportModelAdmin

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

class AliPayOrderResource(resources.ModelResource):
    class Meta:
        model = AliPayOrder
        import_id_fields = ('id',)

class AliPayOrderAdmin(ImportExportModelAdmin):
    resource_class = AliPayOrderResource
    # list page
    list_display = ('date_create', 'out_trade_no', 'subject', 'body', 'total_fee', 'it_b_pay', 'get_pay_result',)
    ordering = ('-date_create',)
    search_fields = ['=out_trade_no', ]
    
    def get_pay_result(self, obj):
        if hasattr(obj, 'pay_result'):
            return obj.pay_result
        else:
            return '-'
    get_pay_result.short_description = '支付结果'
    
    

# Path: openunipay/ali_pay/models.py
# class AliPayOrder(models.Model):
#     out_trade_no = models.CharField(verbose_name='商户订单号', max_length=32, db_index=True, editable=False)
#     subject = models.CharField(verbose_name='商品名称', max_length=128, editable=False)
#     body = models.CharField(verbose_name='商品详情', max_length=512, editable=False)
#     total_fee = models.DecimalField(verbose_name='总金额(单位:元)', max_digits=6, decimal_places=2, editable=False)
#     it_b_pay = models.CharField(verbose_name='交易有效期', max_length=19, editable=False)
#     date_create = models.DateTimeField(verbose_name=u'创建时间', auto_now_add=True)
#     
#     class Meta:
#         verbose_name = u'支付宝订单'
#         verbose_name_plural = u'支付宝订单'
#         
#     def __str__(self):
#         return self.out_trade_no
#         
#     def compose_interface_data(self):
#         # sign data
#         data = self._compose_data()
#         return '{}&sign_type="RSA"&sign="{}"'.format(data, quote_plus(security.sign(data)))
#     
#     def _compose_data(self):
#         valueDict = {item:getattr(self, item) 
#                 for item in _ALIPAY_ORDER_FIELD 
#                 if getattr(self, item)}
#         valueDict['service'] = _SERVICE
#         valueDict['_input_charset'] = _CHARSET
#         valueDict['payment_type'] = _PAYMENT_TYPE
#         valueDict['partner'] = settings.ALIPAY['partner']
#         valueDict['seller_id'] = settings.ALIPAY['seller_id']
#         valueDict['notify_url'] = settings.ALIPAY['notify_url']
#         
#         temp = []
#         for key in valueDict:
#             if not valueDict[key]:
#                 continue
#             temp.append('{}="{}"'.format(key, valueDict[key]))
#         tempStr = '&'.join(temp)
#         return tempStr


# Path: openunipay/ali_pay/admin.py
from django.contrib import admin
from .models import AliPayOrder
from import_export import resources
from import_export.admin import ImportExportModelAdmin

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

class AliPayOrderResource(resources.ModelResource):
    class Meta:
        model = AliPayOrder
        import_id_fields = ('id',)

class AliPayOrderAdmin(ImportExportModelAdmin):
    resource_class = AliPayOrderResource
    # list page
    list_display = ('date_create', 'out_trade_no', 'subject', 'body', 'total_fee', 'it_b_pay', 'get_pay_result',)
    ordering = ('-date_create',)
    search_fields = ['=out_trade_no', ]
    
    def get_pay_result(self, obj):
        if hasattr(obj, 'pay_result'):
            return obj.pay_result
        else:
            return '-'
    get_pay_result.short_description = '支付结果'
    
    

admin.site.register(AliPayOrder, AliPayOrderAdmin)
