Best Python code snippet using locust
authors.py
Source:authors.py
1import os2from flask import Blueprint, request, url_for, current_app3from werkzeug.utils import secure_filename4from api.utils.responses import response_with5from api.utils import responses as resp6from api.models.authors import Author, AuthorSchema7from api.utils.database import db8from api.utils.imagehelper import allowed_file, allowed_extensions9from flask_jwt_extended import jwt_required10author_routes = Blueprint("author_routes", __name__)11@author_routes.route('/', methods=['POST'])12@jwt_required()13def create_author():14 try:15 data = request.get_json()16 author_schema = AuthorSchema()17 author = author_schema.load(data)18 result = author_schema.dump(author.create())19 return response_with(resp.SUCCESS_201, value={"author": result})20 except Exception as e:21 print(e)22 return response_with(resp.INVALID_INPUT_422)23@author_routes.route('/avatar/<int:author_id>', methods=['POST'])24@jwt_required()25def upsert_author_avatar(author_id):26 try:27 file = request.files['avatar']28 get_author = Author.query.get_or_404(author_id)29 if file and allowed_file(file.content_type):30 filename = secure_filename(file.filename)31 file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename))32 get_author.avatar = url_for('uploaded_file', filename=filename, _external=True)33 db.session.add(get_author)34 db.session.commit()35 author_schema = AuthorSchema()36 author = author_schema.dump(get_author)37 return response_with(resp.SUCCESS_200, value={'author': author})38 except Exception as e:39 print(e)40 return response_with(resp.INVALID_INPUT_422)41@author_routes.route('/', methods=['GET'])42def get_author_list():43 fetched = Author.query.all()44 author_schema = AuthorSchema(many=True, only=['first_name', 'last_name', 'id'])45 authors = author_schema.dump(fetched)46 return response_with(resp.SUCCESS_200, value={"authors": authors})47@author_routes.route('/<int:author_id>', methods=['GET'])48def get_author_detail(author_id):49 fetched = Author.query.get_or_404(author_id)50 author_schema = AuthorSchema()51 author = author_schema.dump(fetched)52 return response_with(resp.SUCCESS_200, value={"author": author})53@author_routes.route('/<int:id>', methods=['PUT'])54def update_author_detail(id):55 data = request.get_json()56 get_author = Author.query.get_or_404(id)57 get_author.first_name = data['first_name']58 get_author.last_name = data['last_name']59 db.session.add(get_author)60 db.session.commit()61 author_schema = AuthorSchema()62 author = author_schema.dump(get_author)63 return response_with(resp.SUCCESS_200, value={'author': author})64@author_routes.route('/<int:id>', methods=['PATCH'])65def modify_author_detail(id):66 data = request.get_json()67 get_author = Author.query.get(id)68 if data.get('first_name'):69 get_author.first_name = data['first_name']70 if data.get('last_name'):71 get_author.last_name = data['last_name']72 db.session.add(get_author)73 db.session.commit()74 author_schema = AuthorSchema()75 author = author_schema.dump(get_author)76 return response_with(resp.SUCCESS_200, value={'author': author})77@author_routes.route('/<int:id>', methods=['DELETE'])78def delete_author(id):79 get_author = Author.query.get_or_404(id)80 db.session.delete(get_author)81 db.session.commit()...
admin.py
Source:admin.py
...11 'user',12 'reaction',13 'reacted_at',14 )15 def get_author(self,obj):16 return obj.post.user17 get_author.short_description = 'Post Creator'18 get_author.admin_order_field = 'author'19class CommentAdmin(admin.ModelAdmin):20 def full_name(self,obj):21 return obj.get_full_name()22 list_display = (23 'post',24 'get_author',25 'parent',26 'user',27 'commentText',28 'commented_at',29 )30 def get_author(self,obj):31 return obj.post.user32 get_author.short_description = 'Post Creator'33 get_author.admin_order_field = 'author'34 35 def get_queryset(self,request):36 return self.model.all_objects.all()37class ShareAdmin(admin.ModelAdmin):38 def full_name(self,obj):39 return obj.get_full_name()40 list_display = (41 'post',42 'get_author',43 'user',44 'shared_at',45 )46 def get_author(self,obj):47 return obj.post.user48 get_author.short_description = 'Post Creator'49 get_author.admin_order_field = 'author'50# Register your models here.51admin.site.register(Reaction,ReactionAdmin)52admin.site.register(Comment,CommentAdmin)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!