writing my first Trac macro - python

Ok, I've looked all over, and I think I'm doing this right, but I'm not getting any results. Is there anyone out there who's written Trac macros that can guide me through the first steps? Here's what I've written:
from trac.wiki.macros import WikiMacroBase
from genshi.builder import tag
class MyMacro(WikiMacroBase):
"""Proof of concept"""
revision = "$Rev$"
url = "$URL$"
def expand_macro(self, formatter, name, args):
return tag.b("Hello world.")
I've saved it as a .py file and put it in my Trac project's /plugins directory. Do I need to restart apache? Am I correct in expecting [[MyMacro]] to output a Hello world. on the page?

When creating macros using that format, Trac expects your class to be named "<name>Macro". For example, if you wanted a macro named JustASample, you would name the class JustASampleMacro. Since you named your class MyMacro, Trac thinks that you want your macro to be named My. Try using [[My]] on a wiki page and see if you get the output you're expecting.
After you copy the file into the plugins directory, you will indeed want to restart the web server. Before doing so, delete any .pyc files that were created for your plugin. Also, ensure that the file is readable by the account under which the web server runs.

Related

How to integrate a python file onto a Django project?

I am trying to include a machine learning component in my Django project. I have the method written in python. How do I get to make it work on the website using Django. Can I simply drag the ".py" file into the file structure and call it from an HTML page? I am new to Django any help would be greatly appreciated.
Yes you can directly copy file into your Django Directory structure. Let's say you have a file test.py and a function written in it as def print(). And you have copied the file in app directory. Then you can call it in views.py as from app.test import print. print function will be imported in views.py and you can use it to serve in html as you want.

Django Upload file get full name and file path

I'm tinkering around with cloud storage api's and need help getting the full file name from an upload form on Django. I currently can let the user choose a file, "file.txt" from any directory, and can get the name through
for file in request.FILES.getlist('file'):
print file.name `
However I want the full file path, something more like 'home/user/documents/file.txt'
Is this possible and how would I get the full name? I'm not looking to actually upload the file, just get the full path so that I can utilize dropbox/google drive api.
For reference here is my form:
class UploadFileForm(forms.Form):
folder_name = forms.CharField(max_length = 300)
title = forms.CharField(max_length=50)
file = forms.FileField(label='Select a file')
Thanks in advance.
Browsers do not tell what directory the files come from.
They do not even give that information to the scripts on the page.
When a user uploads a file, you may know:
Its basename.
Its size.
Its type (usually guessed from extension).
Its modification time.
This is all you will get, regardless of whether you access the information straight from the browser's <input> element, or wait for it to POST it.
You may also turn this answer the other way around: if you really need to be able to post the full path of the file, you need to develop a client-side application that will send it. It could be a standalone executable, a browser addon/app, or a Java applet, whatever as long as it runs outside of the webpage sandbox.
I do not use dropbox, but I believe you need to download and install some additional software to use it. That's how it would access the full path of your files.

Is there a django app that provides a file chooser for the files on the server?

I need a component that's a browser-based file browser, and I expect some django app to currently provide this. Is there such a thing?
The full story:
I'm building a django app that is used for testing. I want to use it to serve files (and strings, and etc.) and attach custom headers to it.
Currently, I have a model FileSource which has a single file_path field, which is of type django.db.models.FileField.
When creating a FileSource from the admin, the user has a nice file upload dialog, and when saving, the file he chose, is saved on the server (in a really weird location, inside the directory where django is installed, or something weird like that, because i didn't customize the storage, nor will it help me in any way)
My problem: I only want to use the file dialog for the user to select a full path on the server. The file that the user chose must be only referenced, not copied (like currently), and it must reside on the server.
The server must thus be able to list the files it has, so i basically need a little browser-based file-browser.
At that point, I expect to be able to save a full path in my DB, and then I'll be able to access that file and serve it (together with whatever custom headers the user will chose from my app).
Currently, as you might know, the browsers always lie about the full path of the file. Chromium appends "C:\fakepath" to the file name, so I need support of the backend to accomplish this.
Also, I checked out django-filebrowser and django-filer and from what I understood, they weren't built for this. If I'm wrong, a little assistence in configuring them would be awesome.
You can use a FilePathField for that. It won't upload a file, but rather allow you to choose a pre-existing file. A caveat is that you can only use one directory. If you need multiple directories, then you'd need do go with something like django-filer.

How to create pyral configuration file in rally development?

I am trying to use python REST API to access the contents from Rally. In read me, they mentioned to use a configuration file for server name, user credentials, etc. Where can i create a configuration file and what are the contents that i need to put in that configuration file.
Please help me... Thanks.
To use a config file when running a program using pyral, use the --conf= argument syntax on the command line. Note that you do not need to specify the '.cfg' suffix portion of your configuration file name.
Also, pyral offers some fairly lenient syntax when specifying the --conf part. You can also use --config=... or --cfg=... or --rallyConfig=... in addition to the --conf=... syntax.
Within your Python script using pyral, you'll need to import rallySettings and have some code containing variable definitions, etc. Here's an example of the calling sequence that uses the contents of a config file named sample.conf:
python test_prog.py --conf=sample
sample.conf:
SERVER = rally1.rallydev.com
USER = your_Rally_user_name#your_company.com
PASSWORD = your_Rally_password
WORKSPACE = Your Rally Workspace
PROJECT = Your Rally Project

How do i output a dynamically generated web page to a .html page instead of .py cgi page?

So ive just started learning python on WAMP, ive got the results of a html form using cgi, and successfully performed a database search with mysqldb. I can return the results to a page that ends with .py by using print statements in the python cgi code, but i want to create a webpage that's .html and have that returned to the user, and/or keep them on the same webaddress when the database search results return.
thanks
paul
edit: to clarify on my local machine, i see /localhost/search.html in the address bar i submit the html form, and receive a results page at /localhost/cgi-bin/searchresults.py. i want to see the results on /localhost/results.html or /localhost/search.html. if this was on a public server im ASSUMING it would return .../cgi-bin/searchresults.py, the last time i saw /cgi-bin/ directories was in the 90s in a url. ive glanced at addhandler, as david suggested, im not sure if thats what i want.
edit: thanks all of you for your input, yep without using frameworks, mod_rewrite seems the way to go, but having looked at that, I decided to save myself the trouble and go with django with mod_wsgi, mainly because of the size of its userbase and amount of docs. i might switch to a lighter/more customisable framework, once ive got the basics
First, I'd suggest that you remember that URLs are URLs and that file extensions don't matter, and that you should just leave it.
If that isn't enough, then remember that URLs are URLs and that file extensions don't matter — and configure Apache to use a different rule to determine that is a CGI program rather than a static file to be served up as is. You can use AddHandler to add a handler for files on the hard disk with a .html extension.
Alternatively, you could use mod_rewrite to tell Apache that …/foo.html means …/foo.py
Finally, I'd suggest that if you do muck around with what URLs look like, that you remove any sign of something that looks like a file extension (so that …/foo is requested rather then …/foo.anything).
As for keeping the user on the same address for results as for the request … that is just a matter of having the program output the basic page without results if it doesn't get the query string parameters that indicate a search term had been passed.

Categories

Resources