def post(self): user = User.query.filter_by(id=current_user_id()).first() new_data = request.get_json(force=True) if User.query.filter_by(email=new_data.get('email', None)).first( ) and user.email != new_data.get('email', None): return abort(409, 'Correo ya registrado') for key, value in new_data.items(): if 'avatar' not in key: setattr(user, key, value) avatar_image = new_data.get('avatar_file_data', None) if avatar_image: avatar_image = avatar_image.replace('data:image/png;base64,', '') image_storage = FileStorage(BytesIO(b64decode(avatar_image)), filename=new_data.get('avatar')) file_name = prefix_name(user, image_storage) photos.save(image_storage, name=file_name) user.avatar = file_name user.avatar_url = url_for('static', filename='images/' + file_name) db.session.commit() return jsonify({ 'message': 'Perfil editado exitosamente!', 'redirect': 'profile' })
def post(self, tipo=None): data = request.get_json(force=True) post = Post() user = User.query.get(current_user_id()) if Post.query.filter_by(title=data['title']).first(): return abort(409, 'Ya existe un post con ese titulo.') post.user = user post.title = data['title'] post.post_text = data['post_text'] post.post_date = datetime.datetime.now() post.post_modified = datetime.datetime.now() tag_acum = [] for tag in data['tags']: if tag: tag_acum.append(add_tags(tag)) post.tags = tag_acum db.session.flush() manage_images(post) db.session.commit() return jsonify({ 'message': 'Post creado exitosamente', 'redirect': f'posts/view/{post.id}' })
def put(self, tipo=None): data = request.get_json(force=True) post = Post.query.get(data['id']) title_check = Post.query.filter_by(title=data['title']).first() if title_check: if title_check.id != post.id: return abort(409, 'Ya existe un post con ese titulo.') if post.user.id != current_user_id(): return abort( 401, 'Solo el autor del post o un admin pueden editar el post.') post.title = data['title'] post.post_text = data['post_text'] post.post_modified = datetime.datetime.now() tag_acum = [] for tag in data['tags']: if tag: tag_acum.append(add_tags(tag)) post.tags = tag_acum db.session.flush() manage_images(post) db.session.commit() return jsonify({ 'message': 'Post editado exitosamente', 'redirect': f'posts/view/{post.id}' })
def post(self): image = request.files["file"] image_name = request.files["file"].filename user = User.query.get(current_user_id()) image_db = ImagePost(user=user) db.session.add(image_db) db.session.flush() image_db.image_name = secure_filename( str(image_db.id) + "-" + image_name) image_name = image_db.image_name db.session.commit() photos.save(image, name=image_name) return jsonify( {"location": url_for("static", filename="images/" + image_name)})
def post(self, param=None): data = request.get_json(force=True) comment = Comment() user = User.query.get(current_user_id()) comment.user = user comment.content = data.get('content', None) comment.date = datetime.datetime.now() post = Post.query.get(param) post.comments.append(comment) db.session.commit() return jsonify({ 'message': 'Comentario creado exitosamente', 'redirect': f'posts/view/{post.id}' })
def test_current_user_id(self, user_class, db, default_guard): """ This test verifies that the current user id can be successfully determined based on jwt token data that has been added to the current flask app's context. """ jwt_data = {} add_jwt_data_to_app_context(jwt_data) with pytest.raises(PraetorianError) as err_info: current_user() assert 'Could not fetch an id' in str(err_info.value) jwt_data = {'id': 31} add_jwt_data_to_app_context(jwt_data) assert current_user_id() == 31
def get(self): user = User.query.filter_by(id=current_user_id()).first() user_schema = UserSchema(exclude=['password', 'avatar', 'roles']) return user_schema.dump(user)