Vacation Of Code
This all started when pingou gave me the opportunity to write a utility which helps to user to edit comment on pagure.Now for those who don’t know what pagure is , so pagure is open source equivalent of Github, to be precise :
Pagure is a light-weight git-centered forge based on pygit2. Currently, Pagure offers a web-interface for git repositories, a ticket system and possibilities to create new projects, fork existing ones and create/merge pull-requests across or within projects.
So the utility that was allotted to me was an edit button that privilege the owner of the comment to modify the comment on the pull request. That sounds really simple but writing the whole code and modifying others code are two different things.
I finally started exploring Flask and its feature so Flask is framework that helps you write servers , Server as the name suggest is use to serve request.I had a brief idea about the type of request but never got the chance to explore it.
There are many types of request that help the client and server to talk to each other famous ones are POST and GET.
The GET method
GET method is the method used by the browser to ask the server to send back a given resource: “Hey server, I want to get this resource.” In this case, the browser sends an empty body. Because the body is empty, if a form is sent using this method, the data sent to the server is appended to the URL.
The POST method
POST method is a little different. It’s the method the browser sends the server to ask for a response that takes into account the data provided in the body of the HTTP request: “Hey server, take a look at this data and send me back an appropriate result.” If a form is sent using this method, the data is appended to the body of the HTTP request.
So in first attempt I wrote a shabby code and somehow make it work , but as I told it was really shabby so pingou and subho told me to use hidden form technique which is used to trigger certain functions without actually making the user know what is going on . It is more of a hack to get things done your way.The problem I faced was delete comment and edit comment use the same form to send request so both have the same end point hence inside the function that deletes comment I devised a way to distinguish how to differentiate when to edit comment and when to delete it.
The algorithm was simple get comment id , get new comment , replace the old comment by new one and make entry in the database.The code snippet that really helped me to understand things to a new depth was:
Consider the following form:
<form action="http://foo.com" method="get"> <input name="say" value="Hi"> <input name="to" value="Mom"> <button>Send my greetings</button> </form>
GET method, the HTTP request looks like this:
HTTP/1.1 Host: foo.com
Consider this form (the same one as above):
<form action="http://foo.com" method="post"> <input name="say" value="Hi"> <input name="to" value="Mom"> <button>Send my greetings</button> </form>
When sent using the
POST method, the HTTP request looks like this:
POST / HTTP/1.1 Host: foo.com Content-Type: application/x-www-form-urlencoded Content-Length: 13 say=Hi&to=Mom
My Pull request is still under consideration so you can actually check what I did in pagure. Do let me know what you think about it.