How can I create a downloadable VrCard on my flask web page? - python

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).

Related

Use django to expose python functions on the web

I have not worked with Django seriously and my only experience is the tutorials on their site.
I am trying to write my own application now, and what I want is to have some sort of API. My idea is that I will later be able to use it with a client written in any other language.
I have the simplest of all apps, a model that has a name and surname field.
So the idea is that I can now write an app lets say in c++ that will send two strings to my Django app so they can be saved in the database as name, surname respectively.
What I know until now is to create a form so a user can enter that information, or have the information in the url, and of curse adding them myself from the admin menu.
What I want though is some other better way, maybe creating a packet that contains that data. Later my client sends this data to my Django webpage and it will extract the info and save it as needed. But I do not know how to do this.
If my suggested method is a good idea, then I would like an example of how this is done. If not the I would like suggestions for possible things I could try out.
Typically, as stated by #DanielRoseman, you certainly want to:
Create a REST API to get data from another web site
Get data, typically in JSON or XML, that will contain all the required data (name and surname)
In the REST controller, Convert this data to the Model and save the Model to the database
Send an answer.
More information here: http://www.django-rest-framework.org/

How I can get user input from browser using python

I am in the middle of my personal website development and I am using python to create a "Comment section" which my visitors could leave comments at there in public (which means, everybody can see it, so don't worry about the user name registration things). I already set up the sql database to store those data but only thing I haven't figured out yet was how to get the user input (their comments) from the browser. So, is there any modules in python could do that? (Like, the "Charfield" things in django, but unfortunately I don't use django)
For that you would need a web framework like Bottle or Flask. Bottle is a simple WSGI based web framework for Python.
Using either of these you may write simple REST based APIs, one for set and other for get. The "set" one could accept data from your client side and store it on your database where as your "get" api should return the data by reading it from your DB.
Hope it helps.

How can I periodically run a Python script to import data into a Django app?

I have a script which scans an email inbox for specific emails. That part's working well and I'm able to acquire the data I'm interested in. I'd now like to take that data and add it to a Django app which will be used to display the information.
I can run the script on a CRON job to periodically grab new information, but how do I then get that data into the Django app?
The Django server is running on a Linux box under Apache / FastCGI if that makes a difference.
[Edit] - in response to Srikar's question When you are saying " get that data into the Django app" what exactly do you mean?...
The Django app will be responsible for storing the data in a convenient form so that it can then be displayed via a series of views. So the app will include a model with suitable members to store the incoming data. I'm just unsure how you hook into Django to make new instances of those model objects and tell Django to store them.
I think Celery is what you are looking for.
You can write custom admin command to load data according to your need and run that command through cron job. You can refer Writing custom commands
You can also try existing loaddata command, but it tries to load data from fixture added in your app directory.
I have done the same thing.
Firstly, my script was already parsing the emails and storing them in a db, so I set the db up in settings.py and used python manage.py inspectdb to create a model based on that db.
Then it's just a matter of building a view to display the information from your db.
If your script doesn't already use a db it would be simple to create a model with what information you want stored, and then force your script to write to the tables described by the model.
Forget about this being a Django app for a second. It is just a load of Python code.
What this means is, your Python script is absolutely free to import the database models you have in your Django app and use them as you would in a standard module in your project.
The only difference here, is that you may need to take care to import everything Django needs to work with those modules, whereas when a request enters through the normal web interface it would take care of that for you.
Just import Django and the required models.py/any other modules you need for it work from your app. It is your code, not a black box. You can import it from where ever the hell you want.
EDIT: The link from Rohan's answer to the Django docs for custom management commands is definitely the least painful way to do what I said above.
When you are saying " get that data into the DJango app" what exactly do you mean?
I am guessing that you are using some sort of database (like mysql). Insert whatever data you have collected from your cronjob into the respective tables that your Django app is accessing. Also insert this cron data into the same tables that your users are accessing. So that way your changes are immediately reflected to the users using the app as they will be accessing the data from the same table.
Best way?
Make a view on the django side to handle receiving the data, and have your script do a HTTP POST on a URL registered to that view.
You could also import the model and such from inside your script, but I don't think that's a very good idea.
Have your script send an HTTP Post request like so. This is the library Requests
>>> files = {'report.xls': open('report.xls', 'rb')}
>>> r = requests.post(url, files=files)
>>> r.text
then on the receiving side you can use web.py to process the info like this
x = web.input()
then do whatever you want with x
On the receiving side of the POST request import web and write a function that handles the post
for example
def POST(self):
x = web.input()
If you dont want to use HTTP to send messages back and forth you could just have the script write the email info to a .txt file and then have your django app open the file and read it.
EDIT:
You could set your CRON job to read the e-mails at say 8AM then write it to a text file info.txt. The in your code write something like
import time
if '9' == time.strftime("%H"):
file = open(info.txt)
info = file.read()
that will check the file at 9AM untill 10AM. if you want it to only check one time just add the minutes too the if statement as well.

(Python/Pyramid) Better ways to have standard list/form editors?

I'm working on a number of Pyramid (former Pylons) projects, and often I have the need to display a list of some content (let's say user accounts, log entries or simply some other data). A user should be able to paginate through the list, click on a row and get a form where he/she can edit the contents of that row.
Right now I'm always re-inventing the wheel by having Mako templates which use webhelpers for the pagination, Jquery UI for providing a dialog and I craft the editor form and AJAX requests on the client and server side by hand.
As you may know, this eats up painfully much time.
So what I'm wondering is: Is there a better way of providing lists, editor dialog and server/client communication about this, without having to re-invent the wheel every time?
I heard Django takes off a big load of that by providing user accounts and other stuff out of the box; but in my case it's not just about user accounts, it can be any kind of data that is stored on the server-side in a SQL database, which should be able to be edited by a user.
Thanks in advance!
is this what youre looking for?
pypi --> http://pypi.python.org/pypi/pyramid_formalchemy
docs --> http://docs.formalchemy.org/pyramid_formalchemy/
demo!! --> http://demo.formalchemy.org/
code --> https://github.com/FormAlchemy/pyramid_formalchemy and https://github.com/FormAlchemy/fa.jquery
Note:
only works with SQLA, I think
you have to work on authn/authz/permission yourself
you can use fa.jquery with it
I tried this few hours ago, the package from pypi is broken ( https://github.com/FormAlchemy/pyramid_formalchemy/issues/11 ), so you might want to source install/develop

Database Reporting Services in Django or Python

I am wondering if there are any django based, or even Python Based Reporting Services ala JasperReports or SQL Server Reporting Services?
Basically, I would love to be able to create reports, send them out as emails as CSV or HTML or PDF without having to code the reports. Even if I have to code the report I wouldn't mind, but the whole framework with schedules and so on would be nice!
PS. I know I could use Django Apps to do it, but I was hoping if there was any integrated solutions or even projects such as Pinax or Satchmo which brings together the apps needed.
PPS: It would have to work off Postgres
"I would love to be able to create reports ... without having to code the reports"
So would I. Sadly, however, each report seems to be unique and require custom code.
From Django model to CSV is easy. Start there with a few of your reports.
import csv
from myApp.models import This, That, TheOther
def parseCommandLine():
# setup optparse to get report query parameters
def main():
wtr= csv.DictWriter( sys.stdout, ["Col1", "Col2", "Col3"] )
this, that = parseCommandLine()
thisList= This.objects.filter( name=this, that__name=that )
for object in thisList:
write.writerow( object.col1, object.that.col2, object.theOther.col3 )
if __name__ == "__main__":
main()
HTML is pretty easy -- Django has an HTML template language. Rather than render_to_response, you simply render your template and write it to stdout. And the core of the algorithm, interestingly, is very similar to writing a CSV. Similar enough that -- without much cleverness -- you should have a design pattern that does both.
Once you have the CSV working, add the HTML using Django's templates.
PDF's are harder, because you have to actually work out the formatting in some detail. There are a lot of Python libraries for this. Interestingly, however, the overall pattern for PDF writing is very similar to CSV and HTML writing.
Emailing means using Python's smtplib directly or Django's email package. This isn't too hard. All the pieces are there, you just need to email the output files produced above to some distribution list.
Scheduling takes a little thinking to make best use of crontab. This -- perhaps -- is the hardest part of the job.
I just thought after a fair bit of investigation I would report my findings...
http://code.google.com/p/django-reporting/ - I think that this project, looks like an awesome candidate for alot of the functionality I require. Unfortunately its Django 1.1 which as of this writing (29th April 2009) has not been released.At least in the ability to create reports without too much code.
http://code.google.com/p/django-cron/ - Look promising for scheduling of jobs without cron access
http://www.xhtml2pdf.com/ - Could be used or ReportLabs PDF Libraries for conversion of HTML to PDF
All these together with Django's Email functionality could make a nice Reporting System.

Categories