xxxxxxxxxx
# app/views/user_views.py
from flask import Blueprint, request, jsonify
from app.models.user_model import User, UserSchema
from app.models import db
from flask_jwt_extended import create_access_token, jwt_required, get_jwt_identity
user_blueprint = Blueprint("user", __name__)
@user_blueprint.route('/register', methods=['POST'])
def register_user():
try:
data = request.json
errors = UserSchema().validate(data)
if errors:
return jsonify({"error": errors}), 400
new_user = User(**data)
db.session.add(new_user)
db.session.commit()
return UserSchema().jsonify(new_user), 201
except Exception as e:
return jsonify({"error": str(e)}), 500
@user_blueprint.route('/login', methods=['POST'])
def login_user():
try:
data = request.json
errors = UserSchema(only=('email', 'password')).validate(data)
if errors:
return jsonify({"error": errors}), 400
user = User.query.filter_by(email=data['email']).first()
if user and user.check_password(data['password']):
access_token = create_access_token(identity=user.id)
return jsonify(access_token=access_token), 200
else:
return jsonify({"error": "Invalid credentials"}), 401
except Exception as e:
return jsonify({"error": str(e)}), 500
xxxxxxxxxx
Y
.-^-.
/ \ .- ~ ~ -.
() () / _ _ `. _ _ _
\_ _/ / / \ \ . ~ _ _ ~ .
| | / / \ \ .' .~ ~-. `.
| | / / ) ) / / `.`.
\ \_ _/ / / / / / `'
\_ _ _.' / / ( (
/ / \ \
/ / \ \
/ / ) )
( ( / /
`. `. .' /
`. ~ - - - - ~ .'
~ . _ _ _ _ . ~
xxxxxxxxxx
Python is an interpreted, high-level,
general-purpose programming language.
//as you can also see to your right --------------------->
but also note interpreted, not compiled.
xxxxxxxxxx
Python is a High level and multi-purpose Programming language. It is commonly
used in data science ,AI(Artificial intelligence) and ML(Machine Learning).
If you are a beginner in programming it is mostly prefered because its
code is very simple and somehow similar to English Language.Even master level
people use python. Encourage you to learn it.
xxxxxxxxxx
Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
xxxxxxxxxx
'''Python is an interpreted, high-level and general-purpose programming
language.Python's design philosophy emphasizes code readability with its
notable use of significant whitespace. '''
#Examples
print("Hello World")
repeat = 5
for i in range(repeat):
print("This is the %i iteration" % i)
#2nd variation - print("This is the " + i + "iteration")
#3rd variation - print(f"This is the {i!r} iteration")
#4th variation - print(("This is the {} iteration).format(i))
import random #In order to use the random functions
class Person:
def __init__(self, name, age, height):
self.name = str(name)
self.age = int(age)
self.height = str(height)
person1 = Person("Name", random.randint(1, 100), str(random.randint(0, 7)) + "'" + str(random.randint(0, 11)) + '"')
print(f"Your name is {person1.name} and you are {person1.age} years old. You are also {person1.height}.")