web2py PDF - Where do I put this code? - python

http://code.google.com/p/pyfpdf/wiki/Web2Py#Sample_Table_Listing
This would be my first time using web2py, I'm using it because the example code is exactly what I need for part of a project.
My problem is I have no idea where to put this code. I'm using Google App Engine.

To understand where to put that code, you'll need at least a basic understanding of how web2py applications are structured. I recommend at least looking at the Overview chapter of the book.
The function definitions shown (i.e., report(), listing(), and invoice()) would go in a controller file in your applications's '/controllers' folder (the scaffolding application includes a 'default.py' controller file, though you could rename that or create a new controller file). The calls to db.define_table would typically go in a model file in your application's '/models' folder (the scaffolding application includes a 'db.py' model file, though again, you could rename that or create a new model file).
Note, there was a recent discussion on the mailing list regarding getting pyfpdf to work on GAE.

Related

Python Gtk Glade app structure. What are the best practises?

Since I discovered Python, I've created four little applications with Glade + Python + Gtk. For each of them, the structure is a folder, with the name of the app, containing :
an "images" folder
a main .py file which contains all classes that handle all project windows
a .glade file which contains all windows (main app window, about dialog, config dialog, etc.)
a style.css file
(other files as sqlite file and/or json file if neccessary)
Now I'm wondering if this organization is a good practise. Because I've seen a video tutorial in which each window has its own ".ui" file and .py file. Also, other apps seem to be organized like this too and Anjuta projects seem to follow this rule too.
Furthermore, the tutorials that show how to create a Python-Gtk-Glade app are almost all one-window projects...
So my question is : how do you organize your Python-Gtk-Glade projects or what is the best way to do it?
Thanks in advance.
The way you set up your apps will be dictated by your preferences (coding style), the size of the app, and the purpose of the app. However, here are a couple considerations:
Will total lines of Python code be greater than 1000 lines? A file with over a 1000 lines will be unwieldy to navigate. I know code folding, snippets, and etc help but still.
If you have more than one GtkWindow, put each window's .ui and .py separately. This helps when tracking down bugs or adding features. You mentioned sqlite, and sql queries within a file are easier to master 5 years down the road if the files are somewhat purpose specific. If you put dialogs in the same ui file as the parent window, it makes it easier to use set_transient_for and similar programming. Having more than one GtkWindow in a .ui file will make drag and drop widget reordering a pain.
Are parts of the app code reusable? If you find that you are duplicating features in several different files that have different purposes, make a class or function to simplify that one feature (only applicable to multi-file setup).
And finally, when your folder with .ui and .py files starts becoming large (? 25 - 50 files ?) you might want a subfolder that has a group of files with similar functionality. This is not written in stone. Nemo doesn't use this a lot, others do.
I actively develop my own business management software using Python + Gtk found here. I found the principles described above to work well for me.
I think the reason the documentation for Gtk typically shows single file setups is because they are merely proof of concept and not best app management tutorials. App management (for me, anyway) was trial and error, and from studying other open source apps out there.
Disclaimer: These suggestions are my opinion only. They are not endorsed by Python, Gtk, or Glade. You will need to evaluate these suggestions based on your use case.

Update Flask code with new code generated by Swagger

I recently started using Swagger to generate flask templates: http://editor.swagger.io/#/
My workflow is flawed, and I am wondering where I am going wrong. I:
Use the UI to write the API V1 .yaml
Generate the code using the UI editor, which downloads a stubbed out zip
Write the functions that were stubbed out
This part of the process is fine. However, let's say we want to add a new endpoint or change an existing endpoint. Now what? I:
reload the swagger editor
edit the yaml
generate the code, which downloads a new zip and blows away the old code
take the newly generated code and do a "self-merge" where I copy over the new stub into the old code and copy over the new yaml into the old
It seems there is a gap between the initial generation of the flask template and ongoing maintenance. What am I doing wrong?
Yeah, there isn't really a good workflow for that yet AFAICS.
One thing you can do is check the original generated code into git on a branch called "generated" or similar. Then merge that to master and start working on it. If, at a later point, you extend your swagger definition, you can generate the code again, switch to the generated branch, overwrite the existing code with the newly generated code, commit and merge to master again. If all you had were some additional endpoints, this should even work without any merge conflicts.
It would of course be nicer if the swagger tools had a concept of which code they generated and be able to update generated code, but until then this should be a bearable workaround.

How do I make files downloadable for a particular role in Plone?

I wish to make the contents of a folder in Plone downloadable only for certain roles. Can this be done easily? At present anybody who clicks the hyperlink for file name in the folder contents can download the file easily. I know about the site-wide option of overriding the at_download code using ZMI.
The codeless way to do this is to make use of Plone's workflow system.
Out-of-the-box, Plone's file and image content types do not have their own workflow. That means that files and images will simply inherit the publication state of their parent folder. This is easy and sensible, but it doesn't meet the need you're describing.
To change the situation, you may use the "types" configuration panel to turn on independent workflow for files and images. Then, their publication status may be set separately from their containing folders. Typically, you'd choose the same workflow that you're using for documents. Then, you may publish a folder and list its contents while having the files within be private -- thus requiring login for viewing.
If you need this to work differently in different places, you may turn on "placeful" workflow (turn it on by adding it in the add-ons panel; it's pre-installed, but not active). This allows different workflows in different parts of a site. It increases complexity, but is often an ideal solution to this kind of puzzle.
This is probably not so simple and you need to add some line of code in a little Plone product (no way TTW). Code snippets below are not tested.
Plone file are developed using the Archetypes framework (this will probably change on Plone 5). What you need to change is the read_permission of the file field (see the Archetypes field reference).
from Products.Archetypes.content.file import ATFile
ATFile.schema['file'].read_permission = 'you new permission'
The you simply need to assign your new permission to a role.
This could be not enough (probably step 1 is not useful nowadays). You need to perform the same operation for the [plone.app.blob extension][2]:
from plone.app.blob.subtypes import SchemaExtender
SchemaExtender.fields[0]..read_permission = 'you new permission'
Last one: you probably need to customize the file_view template or an "Unauthorized" error will be raised when a user without the permission will visit the file view.

Using Python FTP Server (pyftplib) - create HTML file on log in

I have a FTP server working great using Python and the pyftplib library (https://code.google.com/p/pyftpdlib/). I would like to, on login (either anonymous or user), create a html file reflecting the latest state of the server in a nice looking way. For example, all the files that are on the server and their properties nicely separated and looking nice. I thought that since I was already doing everything in Python, and my html wouldn't be overly complex, I would just have python write the html file on log in, and then the user could open the html file for the information that was written seconds before.
My main problem is that when I override the "public callbacks" section of the handlers.py (or any section so far), no file is created that I can find. I am new to python, but it seems like a modification in the handlers.py file should affect the Handler class. Another idea I plan on trying is to override the handler base class with my "on_login" function that does create the html file.
What I am really asking for is
1) Advice from anybody who has done/tried this before
2) Any red flags that are going off in your head regarding my plan
3) Any other ideas (ideally strictly using python)
Thanks!
What worked for me was not editing the handler.py file, but rather creating my own subclass (myFTPHandler) and then redefining the onconnect method to write my html file then.
Thanks for the help though!

Convert CVS/SVN to a Programming Snippets Site

I use cvs to maintain all my python snippets, notes, c, c++ code. As the hosting provider provides a public web- server also, I was thinking that I should convert the cvs automatically to a programming snippets website.
cvsweb is not what I mean.
doxygen is for a complete project and to browse the self-referencing codes online.I think doxygen is more like web based ctags.
I tried with rest2web, it is requires that I write /restweb headers and files to be .txt files and it will interfere with the programming language syntax.
An approach I have thought is:
1) run source-hightlight and create .html pages for all the scripts.
2) now write a script to index those script .htmls and create webpage.
3) Create the website of those pages.
before proceeding, I thought I shall discuss here, if the members have any suggestion.
What do do, when you want to maintain your snippets and notes in cvs and also auto generate it into a good website. I like rest2web for converting notes to html.
Run Trac on the server linked to the (svn) repository. The Trac wiki can conveniently refer to files and changesets. You get TODO tickets, too.
enscript or pygmentize (part of pygments) can be used to convert code to HTML. You can use a custom header or footer to link to the actual code for download.
I finally settled for rest2web. I had to do the following.
Use a separate python script to recursively copy the files in the CVS to a separate directory.
Added extra files index.txt and template.txt to all the directories which I wanted to be in the webpage.
The best thing about rest2web is that it supports python scripting within the template.txt, so I just ran a loop of the contents and indexed them in the page.
There is still lot more to go to automate the entire process. For eg. Inline viewing of programs and colorization, which I think can be done with some more trials.
I have the completed website here, It is called uthcode.

Categories