135201
You will be muted until you read the rules. Read them! @Rules_for_Python A group about the Python programming language. Offtopic things go here: @pythonofftopic Resources to learn python: @pythonres Group for Hy: @hylang Selenium: @SeleniumPython
the problem is @login_manager.user_loader
here i cann't able to pass the Annotated's Depends from your library, as how this will resolve the Dependency?
Читать полностью…
@login_manager.user_loader
def load_user(
user_id: str,
user_repository: UserRepositoryDep,
) -> FlaskLoginUser | None:
obj_entity = user_repository.get_by_id(
user_id=user_id,
)
if obj_entity is None:
return None
return FlaskLoginUser(obj_entity)
What part of
For automatic injection of dependecies we need to wrap Flask class
DIFlask and use that as your flask appfrom flask_di import DIFlask
app = DIFlask(__name__)
i also want to for checking i used this type of code, but the problem is the dependency will how work in this user_loader callback
@login_manager.user_loader # type: ignore
def load_user(
user_id: str,
user_repository: UserRepositoryDep,
) -> FlaskLoginUser | None:
"""
https://flask-login.readthedocs.io/en/latest/#how-it-works
As per the docs it should return the obj or the None.
This will read the user_id from the cookie and convert into the user obj
from the user_obj i will get differnt data to use.
"""
obj_entity = user_repository.get_by_id(
user_id=user_id,
)
if obj_entity is None:
return None
return FlaskLoginUser(obj_entity)
Hello i have started to explore the di and so on and then integrat the di things in my own application and i want to use this perfectly, i used in my DDD structer of code where i am using like this:
for registration new user or login case i need to talk with sqlmodel so it need session, and as the docs satwe will use one engine over my repo and a new session per request so your di-flask library i used here like this i say below i am summarizing the codes form different parts in below:
def get_session() -> Generator[Session, None, None]:
with Session(engine) as session:
yield session
# TODO i need to confirm using this di-flask will ok
SessionDep = Annotated[Session,Depends(get_session)]
def get_sqlmodel_repository(
session: SessionDep,
) -> UserRepository: #This userrepository is a protocol class
return SQLModelUserRepository(
session=session,
)
if config.login_provier_value == "SQLMODEL":
user_repository_provider = get_sqlmodel_repository
UserRepositoryDep = Annotated[
UserRepository,
Depends(
user_repository_provider,
),
]
def get_registration_service(
user_repository: UserRepositoryDep,
) -> RegistrationService:
return RegistrationService( #this has business logics in methods
user_repository=user_repository,
)
# for login_service i will use like this upper UserRepositoryDep
# i will call this below Dep in the routes.py' function
RegistrationServiceDep = Annotated[
RegistrationService,
Depends(
get_registration_service,
),
]
# Below is my routes.py code having this,
@auth_bp.route(
rule="/register",
methods=["GET", "POST"],
)
def register(
register_service: RegistrationServiceDep,
)...
@user_loader which will call .find_user_by_id() method from the UserRepository, but how i will give there the session dep when it will use the Sqlmodel, @login_manager.user_loader
def load_user(user_id):
return User.get(user_id)
As that comes later
Whats the use of uv if you dont know what it does?
Those are ready blocks of code for you to download, install and use in your own code.
You install them with pip and you mostly import them to your code
As how a specific library works, you have to consult its documentation to learn.
I higly recommend you learn about virtual environments in Python if you are plannning to use third party libraries
https://docs.python.org/3/library/venv.html
Check out @PythonRes, a channel for Python resources - video courses, pdf books, tutorials and other info to help you learn Python.
Читать полностью…
No, you dont need a special class for flask login
And you dont need to use UserMixin, just implement the functions iz needs from the docs
DDD does not mean you need a class for every thing in your code
Its the separating things into their own domains which is the point
So your store code does not mix with your users code.
So your payment logic is not intermixed with users and store
And please stop tagging me.
When you have a REAL question regarding actuall code, come back
Check out @PythonRes, a channel for Python resources - video courses, pdf books, tutorials and other info to help you learn Python.
Читать полностью…
Hey everyone. I started doing leetcode in python, could anyone find me a resource where I can get a quick recap of python and its functions that can help me do DSA?
Читать полностью…
what are like this,
as i think i make one login mechanism and otp then i think for testin , i will serve my app in local and then there iw ill open the url and see all looks as i want and then also i will try to give wrong otp, correct otp, multple time i will submit and see if i am getting what i needed like this and then i think my app is ready to test on this new things i have implimentated.
this upper is what i know and learnt, what another test you means or how this come in a website i am making in flask
Guys I want to know yours opinion now, as i go to make web app with flask i found pytest is a very must needy things what do you say, should we start learning pytest before flask or how i want to make a real python ecommerce app. what is ur opinion based on flask vs pytest???
Читать полностью…
Thanks for clearify this, from now i will never think to use cookie like this what i thoght in past. you have change a lot of my thinking of web dev matters.
Читать полностью…
Thanks for clear my confusion.
What i understand is suppose user_id is :- 1938292
so in th3 cookie it get saved as a random string like the id:- "yeiekndjsiiekwpsmnsmepw", you means any user who can see the upper random string, he can generate or see 1938292, i thought they cannot know what is written there,
so now i fully understand that i should to make a mechanism to store the otp in the backend not in cookie, as far as i understand any user maybe pro hacker type user can see what is stored in the cookie but can't change this... 😋
i make like this,
def create_app() -> Flask:Читать полностью…
# app = Flask(
# import_name=__name__,
# )
app = DIFlask(
import_name=__name__,
)
login_manager.init_app( # type: ignore
app=app,
)
csrf.init_app( # type: ignore
app=app,
)
app.secret_key = settings.app.secret_key.get_secret_value()
app.register_blueprint(blueprint=auth_bp)
app.register_blueprint(blueprint=general_bp)
I have this big issue, why this not shows how i can use factory pattern here easily, as per the docs,
app = DIFlask(name)
But see, for another case we use,
def create_app() -> Flask:
app = Flask(
import_name=__name__,
)
login_manager.init_app( # type: ignore
app=app,
)
csrf.init_app( # type: ignore
app=app,
)
di_flask = DIFlask()
di_flask.init_app( #This is not working why???
app=app,
)
app.secret_key = settings.app.secret_key.get_secret_value()
app.register_blueprint(blueprint=auth_bp)
app.register_blueprint(blueprint=general_bp)
return app
My app workflow:
register route
│
▼
RegistrationServiceDep
│
▼
get_registration_service()
│
▼
UserRepositoryDep
│
▼
get_sqlmodel_repository()
│
▼
SessionDep
│
▼
get_session()
And nobody is forcing them to use anything
They can use whatever they want, and python venv is the basics of what they need
why u are not saying them to use uv from beginning, as u say me at last in reality for production we should use uv as this will easy and so on...
Читать полностью…
actually you teached me all the DDD and python stuff from 0 stage, so you understand those concept of how the reality the structer i will follow, thats why before complete my code i wanted to know if that was doing correctly or this how to make the real production code . i am really trying to solve the all code in my repo with proper ddd, and i am trying to make code like you do so that one day you will hire me for your repo
Читать полностью…
I’m trying to follow the DDD (Domain-Driven Design) pattern properly, but I’m still a little confused about one part only 😢.
see pls In Python When we follow to use the DDD structer to keep different database User logically separte, we need to convert one class to another.
Example: in my sql database User TAble has 30+ columns, but real business logic case it need only id_, email, password_hash this 3 thing for login implementation part.
Now i need to convert the UserModel to UserFlaskLoginClass (maybe any other class name having very few needy parameter using the @dataclass), as flask_login need a differnt class which need to inherit from UserMixin to have 4 propery and method availabe.
So i need to convert my database's table user to UserOfFlaskLogin class, in this case what do you say about should i convert from class to class using Pydantic's baseModel will be justified in real production.
As i listen pydantic class i can easily convert one into another, will it ok to use pydantic class for htis i need to use pydantic in presentation layer and also in the sqlalchemy layer will this justified and not breaks any rule???
This is not a Python specific question, please delete your offtopic message, move to @PythonOfftopic and ask there.
Читать полностью…
Thats not how that works
https://flask.palletsprojects.com/en/stable/testing/
You learn them in parallel
Testing your software is as important as building it.
Testing helps you make informed decisions and make sure you dont brake something that works.
That being said, its not about Pytest or any other testing framework.
Its about testing itself which you need to learn and realize.
There are also diffrent kind of tests (unit tests, integration tests, end to end tests)
They all do testing, just on a diffrent scope.
So go start learning how testing software actually looks like, then you can start writing your own tests
Search online for "decode Flask session cookie". It's a handy script to have around for Flask development work.
Читать полностью…
so u means any frontend user sitting on the browser side, can get the to know the original string or key value pair written on the cookie without knowing my flask's secret key, but he can't modify this value without knowing the secret key!!!
is this what u means he xan see original things but can't modify, there why not to keep otp in session cookie
Okay, it looks like, on user registration, you email them an OTP, then expect them to get it from the email and enter it on the website. In that case, you can't store it in the session cookie because, as I said above, the user can see the contents of the cookie and hence read the OTP from there instead of from the email.
Читать полностью…
Flask signs its session cookie using the secret key, so it can tell if it has been tampered with. You don't need to reimplement this. The session cookie is not encrypted, so the user can see the contents (with a simple Python script to decode it), but they can't change the value without knowing the secret key and have the server accept the cookie.
Читать полностью…