def create_account(username: str, password: str, register_type=account_constant.ACCOUNT_REGISTER_TYPE_EMAIL, **extra_fields): """ 创建用户 :param username: 用户名 str :param password: 密码 str :param register_type: 注册类型 int (默认为邮箱类型) :param extra_fields: 额外扩展字段 :return: created, user """ try: email = extra_fields.get("email", None) phone = extra_fields.get("phone", None) avatar = extra_fields.get("avatar", None) nickname = extra_fields.get("nickname", None) gender = extra_fields.get("gender") user = User() user.email = email user.phone = phone user.avatar = avatar user.nickname = nickname user.gender = gender user.username = username user.register_type = register_type user.set_password(password) # 初始化修改头像等时间为10天前,以便可以创建后直接修改头像等 default_change_datetime = timezone.datetime.now().date( ) - timezone.timedelta(days=10) user.avatar_change_date = default_change_datetime user.nickname_change_date = default_change_datetime user.save() return True, user except Exception as e: logger.error(e) return False, None
def post(self, request): parameter_missing = False parameter_missing_name = "" if request.data.get("first_name") is None or request.data.get( "first_name") == "": parameter_missing_name = parameter_missing_name + "first_name, " parameter_missing = True elif request.data.get("last_name") is None or request.data.get( "last_name") == "": parameter_missing_name = parameter_missing_name + "last_name, " parameter_missing = True elif request.data.get("email") is None or request.data.get( "email") == "": parameter_missing_name = parameter_missing_name + "email, " parameter_missing = True elif request.data.get("phone") is None or request.data.get( "phone") == "": parameter_missing_name = parameter_missing_name + "phone, " parameter_missing = True elif request.data.get("aadhar") is None or request.data.get( "aadhar") == "": parameter_missing_name = parameter_missing_name + "aadhar, " parameter_missing = True if parameter_missing: return Response({ "status": False, "status_code": HTTP_204_NO_CONTENT, "message": parameter_missing_name + " missing" }) existed_user_email = User.objects.filter( email=request.data.get("email")).first() if existed_user_email is None: existed_user_phone = User.objects.filter( phone=request.data.get("phone")).first() if existed_user_phone is None: existed_user_aadhar = User.objects.filter( aadhar=request.data.get("aadhar")).first() if existed_user_aadhar is None: user = User() user.first_name = request.data.get("first_name") user.middle_name = request.data.get("middle_name") user.last_name = request.data.get("last_name") user.email = request.data.get("email") user.phone = request.data.get("phone") user.aadhar = request.data.get("aadhar") user.country = request.data.get("country") user.state = request.data.get("state") user.city = request.data.get("city") user.constitucy = request.data.get("constitucy") user.ward = request.data.get("ward") user.landmark = request.data.get("landmark") user.pincode = request.data.get("pincode") user.gender = request.data.get("gender") user.date_of_birth = request.data.get("date_of_birth") user.role = "member" user.occupation_type = request.data.get("occupation_type") user.occupation_title = request.data.get( "occupation_title") user.save() return Response({ "status": True, "status_code": HTTP_200_OK, "message": "User Created.", "user": UserSerializer(user).data }) else: return Response({ "status": False, "status_code": HTTP_302_FOUND, "message": "Aadhar Exist." }) else: return Response({ "status": False, "status_code": HTTP_302_FOUND, "message": "Phone Exist." }) else: return Response({ "status": False, "status_code": HTTP_302_FOUND, "message": "Email Exist." })