I've been given the task to write a plugin for Trac.
It should provide burndown data for the ticketcount and estimations filed in the issue tracking system.
The user writes his request as a wikimacro and is provided a link/ button for downloading the burndown as a csv-file, output as a chart is also planned, but has lower priority.
I've got a working solution for processing the data but I'm left with the following problem.
My Question
How can I provide a downloadlink/- button on the Wikipage for a file which is dynamically created by the users request?
I've seen some attempts to send files in the trac source itself and other plugins, but since I'm new to web programming that doesn't really help.
Update1
I've been trying to solve the problem the way Felix suggested, which opened up a new problem for me.
This (stupid) example should demonstrate my problem.
My Macro generates the following URL and adds it as a link to the wikipage.
//http://servername.com/projectname/wiki/page_name?teddy=bear
But the RequestHandler doesn't react, even if the condition returns true.
Edit: This piece of code now shows the working version for the example.
New URL:
#example url
#http://127.0.0.1:8000/prove/files/new
class CustomRequestHandlerModule(Component):
implements(IRequestHandler)
def match_request(self,req):
#old, not working
#return "teddy=bear"== str(req.path_info).split('?')[1]
#new
accept="/files/new"== str(req.path_info)
return accept
def process_request(self,req):
csvfile = self.create_csv()
req.send_response(200)
req.send_header('Content-Type', 'text/csv')
req.send_header('Content-length', len(csvfile))
req.send_header('Content-Disposition','filename=lala.csv')
req.end_headers()
req.write(csvfile)
raise RequestDone
Update2
Inserting loggingstatements shows match_request never gets called.
What am I doing wrong? (Yes, the create_csv() exists already)
Update 3 Thx, for helping =)
If match_request isn't getting called, then process_request never has a chance to execute. Assuming that there's nothing wrong with your plugin that's preventing Trac from loading it correctly, what's probably happening is that another handler is matching the URL before your version of match_request gets called. Try increasing your log level to "Debug" and see if it provides enough information to tell who is processing that request.
Another option is to create a custom "namespace" for your auto-generated files. Try replacing 'wiki' in the generated URLs with something like 'files'. This should prevent any of the built-in handlers from handling the request before your plugin's match_request method gets called.
Basically you need to write your own IRequestHandler which handles a specific URL and returns your dynamically created data. Afterwards you macro should return a url which is configured for your request handler.
Related
I am working on my scrapping project which requires login to the page, and two questions just appeared in front of me. Ok, maybe two blocks of questions. (please have mercy with me as I am beginner learning new things and probably I already poorly designed the whole project).
The design is: as the page requires login, I wanted to use python request.Session() method object and with "with context manager" keep to be logged in.
So my first question is:
1, If I want to have, lets say main.php file, which will call python file with login function (the one with context manager), do I have to have the rest of the functions in this same python file (all the functions for the specific scraping) ? more precisely all functions within this "with block" ? What if I want to have each important function in separate python file (one for login.py, another update.py, table.py, etc)? If I call other functions than login function, how the session will be still valid and I wont get logged out ? or should I just call the functions with "s" object somehow ?
Second question:
2, It's related due the sessions. I went through some questions and not sure if I found the correct answer. But when I logged into the page, through network tab on chrome I saw some session ID. But when I checked session ID via "what_in_session = s.cookies.get_dict()",
ID was different. When I moved on the page the session id in network was still same, however, via python was different every time.
Am I doing something wrong ? Or is it correct behavior ? I found this but I am not sure: Why are the IDs different in each request?.
I guess, once the "with request.Session" block is used, I dont need to care about the rest ? Then back to the first question, how to mange these python functions ?
Hope that you'll understand the questions. (if not I can try to rephrase them)
many thanks
I want to get all the latest changes possible like commits, pull requests, issues, their comments, discussions, etc of a Github repository.
For that, I want to create some decorators such as #repo.on_new_commit() or #repo.on_new_issue_comment(), where repo is a Github Repo object with at least the repository name. The code will always be running and I want #repo.on_new_commit() to return the new commit immediately when the commit occurs. Or a single decorator #repo.on_update() which returns all new changes.
How can I do that? Any suggestion is accepted.
GIT! and the OS module
like
import os
#get activy
activity=os.system('git log github.com/githubuser/repo')
so our 'activity' variable is our
activity and the one on top is the latest!
I finally got the exact answer. This can only be done through GitHub Webhooks and a server like Django or Flask (for Python)
Everytime a new event happens, github sends a POST request to the payload url defined by the user in the webhooks. Through that, you can easily extract the json data from the body along with the github event from the request headers and do any external work you want!
Thanks for the suggestions.
I would like to create a vcf file on my website that users can download and add the file info to their contacts on their mobile phones.
So far I have made this:
Download
When I click the link it downloads a vcf file. When I open it, it redirects me to my contacts app and throws me this error: "No importable cards were found." That´s because I haven´t set any information in any VCard. I would like to know how can I set/create a VCard with the information I have in my SQLAlchemy database (name, email,phonenumber,website,etc.) Thanks in advance
I had to solve this problem recently for work. Here is how I did it!
The broad strokes: Created a Jinja2 template based on my team's needs for vcard output, a data model to lay over the template, a service to render the template from the database query, and finally, using io.BytesIO and flask.send_file to transmit the data in file format to the user.
The gist linked above doesn't have the more contextual parts of the implementation, but does provide an example of how to wire up flask to do this.
Edit: I evaluated the vobject library that i found recommended, but I honestly didn't think it was intuitive to use or very pythonic, meaning it wasn't something I wanted to depend on in my code base. However, maybe it'll work better for you (or others).
I am trying to get up to speed with CouchDB. As a relatively new user on Python, I am trying to set up a view-server so that I can pass on python functions to couchdb.design.ViewDefinitions function. As I understand, ViewDefinitions take javascript code to execute map/reduce function.
Here is what I struggle to understand - I am well aware that this might be a basic question. According to wiki (http://wiki.apache.org/couchdb/View_server):
To register query servers with CouchDB, add a line for each server to local.ini. The basic syntax is:
'[query_servers] python=/usr/bin/couchpy'
How do I access local.ini file? I am an 10.6.8 Mac user. Thanks!
Update: Thank you Kxepal. It seems I was able to create the design/view on Futon in Python. Alternatively, I figured that python viewserver can be created as follows:
curl -X PUT http://[localhost]/_config/query_servers/python '"/path/to/couchpy"'
However, I still cannot execute the python script. Running the view on Couch results in following:
'Error: An error occurred accessing the view no response'
I would appreciate if somebody can point in the right direction. Thanks!
I encountered the same "Error: An error occurred accessing the view no response" problem, and it turned out to be a bug in my python code. Specifically, in the javascript implementation I had something like:
function (doc) {
emit(doc.somefield, doc);
}
I had converted this to:
def map(doc):
yield doc.somefield, doc
However, this gave me the "no response" error you describe.
Changing it to the following fixed the problem.
def map(doc):
yield doc['somefield'], doc
I don't know there OSX keeps CouchDB config files, but you always may setup Python query server through Futon. On sidebar click Configuration, than "Add section" at the bottom of the page and fill the fields with same data as you planned to write into local.ini. As bonus, you don't need restart CouchDB - configuration changes through HTTP API are applied instantly, but be sure that you'd specified correct values.
I have a web page with a link to a url eg./customer/showitem?id=7, which displays details of a specific customer in a child-window using method showitem() in class customer. The method may set the value of a customer class attribute that controls an alert which is displayed when the page is loaded (eg. self.onloadalert="Warning! Customer is in debt.").
If the customer window is closed, then opened again (perhaps with a different id eg. /customer/showitem?id=8), details of the new customer are displayed correctly, but the onload warning above still appears because customer.onloadalert has not changed since the last call (I've verified this it via. debugging). It looks as though even though the method runs from scratch on the 2nd url call, the customer class (and all its attribute values) still persists from the previous call.
I can solve the problem for this particular attribute by resetting it at the beginning of showitem(), but what about other customer.attributes? (especially if there are a lot of them) - I can't reset them all by name! How can I ensure that the class reloads (hence re-initialises) for each url call?
I am using CherryPy (3.20rc1), although I guess the question is applicable to other frameworks that use the same /class/method?params url format.
Any help would be appreciated.
Alan
If you want data to persist for just one request, stick it on the cherrypy.request object:
cherrypy.request.onloadalert="Warning!"
The cherrypy.request object is completely destroyed and recreeated for each request, even though it's safely importable. Figuring out how is left as an exercise for the reader. ;)