web2py and DB transactions - python

When exactly the database transaction is being commited? Is it for example at the end of every response generation?
To explain the question: I need to develop a bit more sophisticated application where I have to control DB transactions less or more manually. Especialy I have to be able to design a set of forms with some complex logics behind the forms (some kind of 'wizard') but the database operations must not be commited until the last form and the confirmation.
Of course I could put everything to the session without making any DB change but it's not a solution, the changes are quite complex and realy have to be performed. So the only way is to keep it uncommited.
Now back to the question: if I undertand how is it working in web2py it will be easier for me to decide if thats a good framework for me. I am a java and php programmer, I know python but I don't know web2py yet ...
If you know any web page when it's explained I also wppreciate.
THanks!

you can call db.commit() and db.rollback() pretty much everywhere. If you do not and the action does not raise an exception, it commits before returning a response to the client. If it raises an exception and it is not explicitly caught, it rollsback.

Have you checked out the official documentation? It explains commit policies and distributed transactions pretty well.

Related

django query is slow, but my sql query is fast

I have a very simple vie in my django application
def notProjectsView(request):
context = {
'projects':notProject.objects.all(),
'title':'Not Projects | Dark White Studios'
}
return render(request, 'not-projects.html', context)
When I removed the context it ran fast, but it's a simple query and shouldn't be so long, the database also doesn't have a lot of records, it has 1 project and in the template I query project.notProjectImage.all which could cause an n+1 issue but I removed that part to test it and it was still slow
Django debug toobar shows a ~50ms sql query while the actual request in the time tab is over 15 seconds
So, you have eliminated the DB query as the reason it's slow. You need to look elsewhere. Is it slow when you test the view (using the Django test framework to GET html or POST data and check html response)? Then you've eliminated something happening in the browser, like a misbehaving script or framework. Which leaves the project's Python code, or something wrong with the machine it is running on. Is something looping far more than it ought to? Or is your developer machine page-thrashing because it is short of RAM?
(Oh, and where is the DB located? Developers usually host it on the machine they are developing with, but if it's cloud-based, look at the performance of the internet connection and possibly of the service itself).
Always work like Sherlock Holmes. "Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth."
BTW I recently traced occasional slowness on my developer machine to a bug in the (Cinnamon/Linux) GUI. That was of course the last place I though to look until I had eliminated absolutely everything else.

In a Flask application that is using SQLAlchemy, is it ok to permanently put `session.rollback` at the beginning of the app?

I'm new to Flask and web development in general. I have a Flask web-application that is using SQLAlchemy, is it ok to put session.rollback at the beginning of the app in order to keep it running even after a transaction fails?
I had a problem with my website when it stopped working after I was attempting to delete records of one table. The error log showed that the deletion failed due to entries in another table still referencing these records as their foreign key. The error log suggested using session.rollback to rollback this change, so I put it at the beginning of my app just after binding my database and creating the session and my website worked. This gave me the hint to leave that line there. Is my move right, safe and ok? Can anyone tell me what is the correct thing to do if this is somewhat endangering the functionality or logic of my website by any chance?
I'd say by that you are by definition cargo cult coding and should try to determine why you're finding these errors in the first place instead of just including a bit of code for a reason you don't understand.
The problem you're describing is the result of using foreign keys to ensure data integrity in your database. Typically SQLAlchemy will nullify all of the depending foreign keys, but since I don't know anything about your set up I can't explain why it wasn't. It is perhaps a difference between databases.
One massive problem with putting the rollback at the beginning of a route (or the entire global app) is that you might rollback data which you didn't want to. You haven't provided an MVCE so no one can really help you debug your problem.
Cargo cult coding in circumstances like this is understandable, but it's never a good practice. To solve this problem, investigate the cascades in SQLAlchemy. Also, fire up your actual SQL db interface and look at the data's structure, and set SQLALCHEMY_ECHO = 1 in your config file to see what's actually getting emitted.
Good luck!
You should not use the rollback at the beginning but when a database operation fails.
The error is due to an integrity condition in your database. Some rows in your table are being referenced by another table. So, you have to remove referencing rows first.

Django Admin using RESTful API v.s. Database

This is a bit of a strange question, I know, but bear with me. We've developed a RESTful platform using Python for one of our iPhone apps. The webapp version has been built using Django, which makes use of this API as well. We were thinking it would be a great idea to use Django's built-in control panel capabilities to help manage the data.
This itself isn't the issue. The problem is that everyone has decided it would be best of the admin center was essentially a client that sits on top of the RESTful platform.
So, my question is, is there a way to manipulate the model layer of Django to access our API directly, rather than communicated directly with the database? The model layer would act as the client passing requests and responses to and from the admin center.
I'm sure this is possible, but I'm not so sure as to where I would start. Any input?
I remember I once thought about doing such thing. At the time, I created a custom Manager using a custom QuerySet. And I overrode some methods such as _filter_or_exclude(), count(), exists(), select_related(), ... and added some properties. It took less than a week to become a total mess that had probably no chance to work one day. So I immediately stopped everything and found a more suitable solution.
If I had to do it once again, I would take a long time to consider alternatives. And if it really sounds like the best thing to do, I'd probably create a custom database backend. This backend would, rather than converting Django ORM queries to SQL queries, convert them to HTTP requests.
To do so, I think the best starting point would be to get familiar with django source code concerning database backends.
I also think there are some important things to consider before starting such development:
Is the API able to handle any Django ORM request? Put another way: Will any Django ORM query be translatable to an API request?
If not, may "untranslatable" queries be safely ignored? For instance, an ORDER BY clause might be safe to ignore. While a GROUP BY clause is very unlikely to be safely dismissed.
If some queries can't be neither translated nor ignored, may them be reasonably emulated. For instance, if your API does not support a COUNT() operation, you could emulate it by getting the whole data and count it in python with len(), but is this reasonable?
If they are still some queries that you won't be able to handle (which is more than likely): Are all "common" queries (in this case, all queries potentially used by Django Admin) covered and will it be possible to upgrade the API if an uncovered case is discovered lately or is introduced in a future version of Django?
According to the use case, there are probably tons of other considerations to take, such as:
the integrity of the data
support of transactions
the timing of a query which will be probably much higher than just querying a local (or even remote) database.

Django/python and Apache Solr: pysolr or solrpy?

brand new on this forum and this is my first post!
At work we're starting a project which uses Apache Solr and i'm in charge of the frontend system (Django-based).
Our solr database isn't related to any other db engine nor to any models' class, so Haystack isn't good for us (since its strictly related to the models).
I was looking at http://code.google.com/p/pysolr/ and http://code.google.com/p/solrpy/
Basically, they're similar. I like more solrpy, since it uses POST requests and we can mask our users queries, but this makes its paginator harder to use (i guess..).
Other side, pysolr, thanks to the GET method, performs better (lower query timing), but so far i couldn't execute a query without getting a badrequest error.
Before choosing one, i wanted to ask the community any opinion. Users need to do only searches, our data is handled by a java process, no other db is used (except for storing user informations), and we need to use all solr features (faceting, highlight, word stopping, analyzers...).
What will you choose? And why? Any good code example you can point me at? I was looking throu the haystack source to see how they did implement all...
Thanks all!
We have used 'solrpy', but encountered some problems with it.
Sunburnt is actually an interesting API:
https://github.com/tow/sunburnt/
Actively developed, and easy to use. Unfortunately it introduces some additional dependencies.

How do I properly unit test a Django session?

The behavior of Django sessions changes between "standard" views code and test code, making it unclear how test code is written for sessions. Googling this yields two relevant discussions about this issue:
Easier manipulation of sessions by
test client
test.Client.session.save() raises
error for anonymous users
I'm confused because both tickets have different ways of dealing with this problem and they were both Accepted. I assume this means they were patched and the behavior is now different. I also don't know to which versions these patches would pertain.
If I'm writing a unit test in Django 1.0, how would I set up my session store for sessions to work as they do in the browser?
I don't quite understand what do you mean by saying the behavior changes between "standard" view and "test" code, maybe you should elaborate on that.
but regarding how to test the session, I do think there are approaches.
you have to understand how django session works, read the unit test for the session package you used in your application. this is regarding understand how server side works.
you probably need to capture a few conversations between browser and server( using FIREBUG for example )
so the issue for you looks like that you are not passing session_id you get when you log in back to server when you talk to server. like put it in (POST,GET,COOKIES I don't quite remember that ).
The important thing here is understand how session works in HTTP, once you get that, you definitely have a clear idea about what is happening there, and make explainations accordingly.

Categories