from flask import Blueprint, request, jsonify, make_response from flask_restful import Api, Resource from app.models import db, Users, UsersSchema from sqlalchemy.exc import SQLAlchemyError from marshmallow import ValidationError users = Blueprint('users', __name__) schema = UsersSchema(strict=True) api = Api(users) class CreateListUsers(Resource): def get(self): users_query = Users.query.all() results = schema.dump(users_query, many=True).data return results def post(self): raw_dict = request.get_json(force=True) try: schema.validate(raw_dict) request_dict = raw_dict['data']['attributes'] print(raw_dict) user = Users(request_dict['firstName'], request_dict['lastName'], request_dict['email'], request_dict['age'], request_dict['birthDate'], request_dict['zipcode']) user.add(user)
from app.main import bp from app.models import Users, UsersSchema from app.errors.handlers import bad_request from flask_jwt_extended import jwt_required, current_user # Declare database schemas so they can be returned as JSON objects user_schema = UsersSchema(exclude=("email", "password_hash")) users_schema = UsersSchema(many=True, exclude=("email", "password_hash")) # Let's users retrieve their own user information when logged in @bp.route('/', methods=['GET']) @jwt_required def user_page(): user = current_user return user_schema.jsonify(user) # Lets users retrieve a user profile when logged in @bp.route('/user/<username>', methods=['GET']) @jwt_required def get_user(username): user = Users.query.filter_by(username=username).first() if user is None: return bad_request("User not found") return user_schema.jsonify(user)
import os from app import create_app, auth, db from . import main from flask import abort, request, jsonify, g, url_for, Response from app.models import Users, UsersSchema users_schema = UsersSchema(many=True) # config_name = os.getenv('APP_SETTINGS') app = create_app() # This method is called for user specific resources def check_user_permissions(username=None, admin_required=False): user = Users.query.filter_by(username=g.user.username).first() if user.is_admin: return True if not admin_required: if username == g.user.username: return True abort(401) @auth.verify_password def verify_password(username, password): # try to authenticate with username/password user = Users.query.filter_by(username=username).first() if not user: return False passwd = user.verify_password(password) if not passwd:
from sqlalchemy.exc import SQLAlchemyError from marshmallow import ValidationError from app import app, db from config import SQLALCHEMY_DATABASE_URI from sqlalchemy import text, create_engine import string import random def password_generator(size=8, chars=string.ascii_uppercase + string.digits): return ''.join(random.choice(chars) for _ in range(size)) users = Blueprint('users', __name__) schema = UsersSchema() api = Api(users) engine = create_engine(SQLALCHEMY_DATABASE_URI) class UsersList(Resource): def get(self): users_query = Post.query.all() results = schema.dump(users_query, many=True).data return jsonify(results) def post(self): raw_dict = request.get_json(force=True) try: schema.validate(raw_dict)