Package: pirouz
Pirouz Web FrameworkA web framework built with Python.Sample Weblog ProjectThe simple weblog created with pirouz web framework.
You can see weblog sourcesherebefore that installpirouzwith pip in your system.Install Pirouzpip install pirouzImportsfrompirouzimport(App,BaseMiddleware,RenderasRender_,TextResponse,redirect,DB,ResultConfig,columns,OR)frompirouz.utilsimportcleaned_data,encryptCreate Appapp=App(__file__)Run Web ServerFor run web server using python:if__name__=='__main__':app.run(port=8001)Run server using gunicorn:gunicorn -w 4 app:app --reloadCreate ModelsclassUser(DB):username=columns.VarChar(unique=True,nullable=False)password=columns.Text(nullable=False)email=columns.Text()first_name=columns.VarChar()last_name=columns.VarChar()classPost(DB):title=columns.Text(nullable=False)body=columns.Text()author=columns.ForeignKey(User)like_count=columns.SmallInt(default=0)created=columns.Date()Create MiddlewareclassAuthMiddleware(BaseMiddleware):defprocess_request(self,request):request.user=Noneusername=request.cookies.get('username')password=request.cookies.get('password')try:user=User.filter(username=username).first()ifuser.password==password:request.user=userexceptException:passreturnrequestapp.add_middleware(AuthMiddleware)Serve Filesapp.serve_files()Add Context ProcessorclassRender(Render_):defget_context(self,request)->dict:return{'user':request.user,'posts_count':Post.all().count(),}Function View@app.route('/')defindex(request):context={'posts':Post.all(config=ResultConfig(order_by='id',reverse=True,)),}returnRender(request,'post/list.html',context=context)Class ViewA detail view for detail of post.@app.route('/post/<int:id>/')classPostDetail:defget(self,request,id):post=Post.filter(id=id).first()ifnotpost:returnTextResponse(f'Post with ID{id}not found!',status=404)context={'post':post,'author':User.filter(id=post.author).first(),}returnRender(request,'post/detail.html',context=context)defpost(self,request,id):returnself.get(request,id)A create view for create new post.@app.route("/create/",methods=["GET","POST"])classPostCreate:defget(self,request):ifnotrequest.user:returnredirect('/login/')returnRender(request,'post/create.html')defpost(self,request):ifnotrequest.user:returnredirect('/login/')cd=cleaned_data(request)post=Post(title=cd["title"],body=cd['body'],author=request.user,like_count=randint(1,100),created=str(datetime.now().date()),)returnredirect(f'/post/{post.id}/')AuthenticationRegister view for sign up a user.@app.route("/register/",methods=["GET","POST"])classRegister:defget(self,request):returnRender(request,'post/register.html')defpost(self,request):cd=cleaned_data(request)password=encrypt(cd['password'])username=cd['username']try:user=User.filter(username=username).first()ifuser:returnRender(request,'post/register.html',context={'exists':True})exceptException:passUser(username=username,password=password,email=cd['email'],first_name=cd['first_name'],last_name=cd['last_name'],)response=redirect('/')response.set_cookie('username',username)response.set_cookie('password',password)returnresponseLogin view for user login.@app.route("/login/",methods=["GET","POST"])classLogin:defget(self,request):returnRender(request,'post/login.html')defpost(self,request):cd=cleaned_data(request)password=encrypt(cd['password'])username=cd['username']try:user=User.filter(username=username).first()ifuseranduser.password==password:response=redirect('/')response.set_cookie('username',username)response.set_cookie('password',password)returnresponseexceptException:passreturnRender(request,'post/login.html',context={'wrong':True})Logout view for logout the user@app.route('/exit/',methods=['GET'])classLogout:defget(self,request):response=redirect('/')response.delete_cookie('username')response.delete_cookie('password')returnresponseSearch View@app.route('/search/')classSearch:defget(self,request):query=request.args.get('search')context={'search':True,}ifquery:posts=Post.filter(OR(title__like=f'%{query}%',body__like=f'%{query}%',),config=ResultConfig(order_by='id',reverse=True,))context.update({'posts':posts,})returnRender(request,'post/list.html',context=context)defpost(self,request):returnself.get(request)LinksDownload Source Code:Click HereMy Github Account:Click Here