def updatePassword(self, id, validated_data): instance = User.objects.get(pk=id) if hashers.SHA1PasswordHasher().verify(validated_data['old_password'], instance.password): instance.password = hashers.SHA1PasswordHasher().encode( validated_data['new_password'], salt='1123') instance.save() else: raise Exception("Mật khẩu cũ không đúng")
def post(self, request): username = request.data['username'] password = request.data['password'] if username is None or password is None: return Response({ "success": False, "message": "Tài khoản hoặc mật khẩu không đúng" }) try: user = User.objects.get(username=username) except User.DoesNotExist: return Response({ "success": False, "message": "Tài khoản hoặc mật khẩu không đúng" }) if hashers.SHA1PasswordHasher().verify(password, user.password): user_serializer = UserSerializer(user) jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER( user_serializer.data) return Response({ "success": True, "token": jwt_encode_handler, "role": user.role, "username": user.username }) else: return Response({ "success": False, "message": "Tài khoản hoặc mật khẩu không đúng" })
def post(seft, request): phone = request.data['phone'] mat_khau = request.data['mat_khau'] try: phu_huynh = Phu_huynh.objects.get(phone=phone) except Exception as e: print("Error:", e) return Response({ "success": False, "message": "Tài khoản không tồn tại" }) if hashers.SHA1PasswordHasher().verify(mat_khau, phu_huynh.mat_khau): studentSerializer = SerStudent(phu_huynh) jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER( studentSerializer.data) return Response({ "success": True, "token": jwt_encode_handler, "role": phu_huynh.role, "data": studentSerializer.data }) else: return Response({ "success": False, "message": "Tài khoản hoặc mật khẩu không đúng" })
def save(self, **kwargs): print(self.validated_data['username']) user = User(username=self.validated_data['username'], password=hashers.SHA1PasswordHasher().encode( self.validated_data['password'], salt='1123'), full_name=self.validated_data['full_name'], code=self.validated_data['code'], role=self.validated_data['role']) user.save()
def save(self, **kwargs): student = Phu_huynh( ho_ten=self.validated_data['ho_ten'], email=self.validated_data['email'], mat_khau=hashers.SHA1PasswordHasher().encode( self.validated_data['mat_khau'], salt='123'), phone=self.validated_data['phone'], hinh_dai_dien_url=self.validated_data['hinh_dai_dien_url'], role=self.validated_data['role']) if (not self.checkExist(student.phone)): student.save() else: raise Exception('Tài khoản đã tồn tại')
def my_function(apps, schema_editor): data = [] print(11111111111111111) with open('src/fakerdata/user.json') as f: users = json.load(f) for item in users: item['username'] = str(item['username']) item['code'] = str(item['code']) item['password'] = str(item['password']) user = User(username= item['username'],password = hashers.SHA1PasswordHasher().encode(item['password'],salt='1123'),code =item['code'],full_name=item['full_name'],role =item['role']) data.append(user) print(data[0]) User.objects.bulk_create(data)
def patch(self, request, id=None): print(request.data) try: user = User.objects.get(pk=id) user.password = hashers.SHA1PasswordHasher().encode( request.data['password'], salt='1123') user.save() return Response( { "success": True, "message": "Thay đổi thành cônng" }, ) except Exception as e: print(e) return Response({ "success": True, "message": "Thay đổi thất bại" }, )
def post(self, request): id_phu_huynh = request.data['id_phu_huynh'] student = Phu_huynh.objects.get(id_phu_huynh=id_phu_huynh) old_student = Phu_huynh.objects.get(id_phu_huynh=id_phu_huynh) print("request data:", request.data) if ("ho_ten" in request.data and (request.data['ho_ten'] != '')): ho_ten = request.data['ho_ten'] student.ho_ten = ho_ten if ("email" in request.data and (request.data['email'] != '')): email = request.data['email'] student.email = email if ("ngay_sinh" in request.data and (request.data['ngay_sinh'] != '')): ngay_sinh = request.data['ngay_sinh'] student.ngay_sinh = ngay_sinh if ("mat_khau" in request.data and (request.data['mat_khau'] != '')): mat_khau = hashers.SHA1PasswordHasher().encode( request.data['mat_khau'], salt='123') student.mat_khau = mat_khau if ("hinh_dai_dien_url" in request.data and (request.data['hinh_dai_dien_url'] != '')): hinh_dai_dien_url = request.data['hinh_dai_dien_url'] student.hinh_dai_dien_url = hinh_dai_dien_url if ("gioi_thieu" in request.data and (request.data['gioi_thieu'] != '')): gioi_thieu = request.data['gioi_thieu'] student.gioi_thieu = gioi_thieu if ("gioi_tinh" in request.data and (request.data['gioi_tinh'] != '')): gioi_tinh = request.data['gioi_tinh'] student.gioi_tinh = gioi_tinh if ("dia_chi" in request.data and (request.data['dia_chi'] != '')): dia_chi = request.data['dia_chi'] student.dia_chi = dia_chi if ("phone" in request.data and (request.data['phone'] != '')): phone = request.data['phone'] student.phone = phone try: try: student.save() return Response( { "success": True, "message": "Cập nhập thành công ", "old_value": SerStudent(old_student).data, "new_value": SerStudent(student).data }, status.HTTP_200_OK) except Exception as e: print("Error:", e) return Response( { "success": False, "message": "Số điện thoại đã tồn tại" }, status.HTTP_500_INTERNAL_SERVER_ERROR) except Exception as e: print("Error:", e) return Response({ "success": False, "message": "Lỗi hệ thống" }, status.HTTP_500_INTERNAL_SERVER_ERROR)