def set_default(cls, admin, user_address):
     with database.transaction():
         UserAddressModel.update(is_default=False).where(
             UserAddressModel.user_id == user_address.user_id,
             UserAddressModel.deleted_at == None).execute()
         now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
         user_address.updated_at = now
         user_address.is_default = True
         user_address.save()
         return cls.success()
 def update(cls, admin, data):
     user_address_id = data.get('user_address_id')
     now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
     if user_address_id:
         del data['user_address_id']
         data.update({'updated_at': now})
         UserAddressModel.update(**data).where(
             UserAddressModel.id == user_address_id,
             UserAddressModel.deleted_at == None).execute()
         return cls.success_with_result({'updated_at': now})
     else:
         raise UserAddressModel.LackOfFieldError(u'请传入用户地址ID')
 def decorator(*args, **kwargs):
     data = request.json or request.form or request.args
     user_address_id = data.get('user_address_id')
     if user_address_id:
         try:
             user_address = UserAddressModel.get(
                 UserAddressModel.id == user_address_id,
                 UserAddressModel.deleted_at == None)
             kwargs.update({'user_address': user_address})
             return f(*args, **kwargs)
         except UserAddressModel.DoesNotExist as e:
             raise UserAddressModel.NotFoundError(u'该用户地址不存在')
     else:
         raise UserAddressModel.LackOfFieldError(u'请传入用户地址ID')
 def list(cls, admin, data):
     user_id = data.get('user_id')
     q = UserAddressModel.select().where(
         UserAddressModel.user_id == user_id,
         UserAddressModel.deleted_at == None)
     result = []
     for x in q.order_by(UserAddressModel.created_at.desc()):
         user_address = x.format(
             'id,user_id,region_code,street,is_default,consignee,contact,created_at'
         )
         result.append(user_address)
     return cls.success_with_list_result(len(result), result)
def migrate():
    if Order.table_exists():
        Order.drop_table()
    Order.create_table()
    user = User.get(User.phone_number == '13632324433')
    user_address = UserAddress.get(UserAddress.user_id == user.id,
                                   UserAddress.is_default == True)
    Order.create(user_id=user.id,
                 logistics_user_address_id=user_address.id,
                 status=order_status.COMPLETED,
                 remark='这是测试的订单',
                 total_price=597,
                 paid_price=577)
def migrate():
    if UserAddress.table_exists():
        UserAddress.drop_table()
    UserAddress.create_table()
    user = User.get(User.phone_number == '13632324433')
    UserAddress.create(user_id=user.id,
                       region_code='440606',
                       street='勒流镇众涌村仁厚街2号',
                       is_default=True,
                       consignee='卢炳成',
                       contact='13632324433')
def user_address():
    from backend_common.models.user_address import UserAddress as UserAddressModel
    user_address = UserAddressModel.select().where(
        UserAddressModel.deleted_at == None).order_by(
            UserAddressModel.created_at.desc()).first()
    return user_address
 def delete(cls, admin, data):
     user_address_id = data.get('user_address_id')
     now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
     UserAddressModel.update(deleted_at=now).where(
         UserAddressModel.id == user_address_id).execute()
     return cls.success_with_result({'deleted_at': now})
 def create(cls, admin, data):
     user_address = UserAddressModel.create(**data)
     return cls.success_with_result(user_address.format('id,created_at'))