예제 #1
0
 def put(self, request, args):
     staff_id = args.pop("staff_id")
     shop_id = self.current_shop.id
     staff = get_staff_by_id_and_shop_id(staff_id, shop_id)
     if not staff:
         return self.send_fail(error_text="员工不存在")
     # 超管仅自己可以编辑,而且权限不可编辑
     elif staff.roles == StaffRole.SHOP_SUPER_ADMIN:
         if self.current_user.id != staff.user_id:
             return self.send_fail(error_text="超管信息仅自己可以编辑")
     serializer = StaffSerializer(staff, data=args)
     if not serializer.is_valid():
         return self.send_error(error_message=serializer.errors,
                                status_code=status.HTTP_400_BAD_REQUEST)
     serializer.save()
     return self.send_success()
예제 #2
0
 def put(self, request, args):
     shop_id = self.current_shop.id
     staff_apply_id = args.pop("staff_apply_id")
     staff_apply = get_staff_apply_by_shop_id_and_id(
         shop_id, staff_apply_id)
     if not staff_apply:
         return self.send_fail(error_text='员工申请记录不存在')
     staff_apply_serializer = StaffApplySerializer(staff_apply, args)
     # 此处无需验证,仅为了save的执行
     staff_apply_serializer.is_valid()
     staff_apply_serializer.save()
     # 申请通过,创建员工
     staff_info = {"shop_id": shop_id, "user_id": staff_apply.user.id}
     staff_info.update(args)
     # 校验员工是否存在
     staff = get_staff_by_user_id_and_shop_id(staff_apply.user.id,
                                              shop_id,
                                              filter_delete=False)
     if not staff:
         staff_serializer = StaffSerializer(data=staff_info)
         if not staff_serializer.is_valid():
             return self.send_error(error_message=staff_serializer.errors,
                                    status_code=status.HTTP_400_BAD_REQUEST)
         staff = staff_serializer.save()
     elif staff.status == StaffStatus.NORMAL:
         return self.send_fail(error_text="已经为该店铺的员工")
     else:
         # 员工状态为被删除,则将状态修改为正常
         staff_info["status"] = StaffStatus.NORMAL
         for k, v in staff_info.items():
             setattr(staff, k, v)
         staff.save()
     # 创建操作日志
     log_info = {
         "shop_id": shop_id,
         "operator_id": self.current_user.id,
         "operate_type": StaffLogType.ADD_STAFF,
         "staff_id": staff.id,
     }
     create_staff_log_interface(log_info)
     return self.send_success(staff_id=staff.id)