I am writing some test code using pure python unittest. I did it like this:
# retrieve the demo object to get the initial attributes
demo = DemoModel.objects.get(id='1')
init_name = demo.name
# use the requests module to send a request to change attributes of demo
requests.post(...)
# retrieve the demo object again to see if it's changed
demo = DemoModel.objects.get(id='1')
assertNotEqual(init_name, demo.name)
Turns out the demo object wasn't changed! I searched and checked for a long time, but, there is no cache in Django getquery, mysql neither.
You said you did this in "pure unittest" and using the requests module. If you're not careful in doing so, it's quite possible that your request failed -- and you aren't checking the status code of your POST. (One possible reason it could fail would be if you don't provide Django's CSRF value, for example, though it might also fail from a permission check, etc).
Do you know if the post returned successfully?
Seems not Django but Mysql transaction issue. See How do I force Django to ignore any caches and reload data?
Related
I'm trying to use the etsy API and I was finally able to get it running from the source. I gave it my key, and it returned the following when printed out.
<etsy._v2.EtsyV2 object at 0xb7284ccc>
However I have no idea what to do with it. The github-repo doesn't have much documentation, and the command that is suppose to follow doesn't work. I read the Etsy API and didn't find the mentioned command getFrontFeaturedListings like the github listed.
I've had this issue before with an HTTP response object and I was told to use response.content to check out more info on the object. It didn't work for this object so I'm wondering if there was a simple way to test any generic object, or at least see what this object contains?
When in doubt you can always use the dir built-in method on an arbitrary python object. This will show you methods and fields attached to the object. https://docs.python.org/3/library/functions.html#dir
Anyway, sorry to hear about the poor documentation of the library. Last time I used Etsy's API I just created a little class that used requests. It wasn't much work since Etsy lays out all of the URIs + documentation nicely on their developer site. https://www.etsy.com/developers/documentation/reference/favoritelisting
I've been working on an AppEngine-based project and I wanted to know if it's possible to ignore a ProtoRPC message field.
With the Java SDK, you can use #ApiResourceProperty to ignore a property (this means it's not contained within the response returned to the browser). However, I have not come across a way of doing this using the Python SDK.
Is there anything like this in the Python SDK?
Thanks, Adil
Nope, unfortunately not (at least not to my knowledge).
Two possible solutions depending on your use-case.
Set field values to None before returning the message in your method. That way they will be skipped/not included in the JSON response.
If your messages are hooked up to datastore models you can use the endpoints-proto-datastore library which allows you to use your ndb models directly in your API methods. Additionally it allows for request_fields and response_fields parameters in the method decorator which will limit the request or response to the specified subset of message/model fields. (internally it creates the necessary message classes for you)
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 written a plugin that sends a signal to activate my code. However, it doesn't send the user-request object to my code. I am looking for a way to retrieve the current request without modifying the main application. I cannot find any documentation related to global request (like $_SERVER['REMOTE_ADDR'] in PHP).
I would like to know if there are any variable to do like that in Python/Django.
Django doesn't provide a global request object (it would actually be a thread local, not a global). But there are a few techniques you can use to get the same effect yourself: http://nedbatchelder.com/blog/201008/global_django_requests.html
AFAIK it is not available, except you make it available.
You can copy+paste the snippets provided in the other answers, or you can use this library: https://pypi.python.org/pypi/django-crequest
Middleware to make current request always available.
you can attach it to current request via middleware and retrieve it back
https://github.com/jedie/django-tools/blob/master/django_tools/middlewares/ThreadLocal.py
Based on Ned Batchelder's reply I've compiled a solution. Although I wouldn't recommend it for anything but debugging/troubleshooting. There's a better solution on the linked page.
Put module m1 at a project root:
import inspect
def get_request():
for f in inspect.stack():
f_code = inspect.getmembers(f.frame, inspect.iscode)[0][1]
f_locals = [v for (n, v) in inspect.getmembers(f.frame) if n == 'f_locals'][0]
co_varnames = [v for (n, v) in inspect.getmembers(f_code) if n == 'co_varnames'][0]
if 'request' in co_varnames:
return f_locals['request']
Then in any other file:
import m1
print(m1.get_response().path)
You might want to make sure you don't introduce reference cycles. I haven't understood under which particular conditions I must do what exactly. Not that it matters in my case. But your mileage might vary.
One solution is django-middleware-global-request.
It provides a way to get the request from anywhere once the request has been constructed by Django in the first place. It returns None if no request object is available, for example when running in a manage.py shell.
As I know it, you define your Django view using a number of methods like:
def detail(request, some_param):
# [...]
The parameter request contains information about the HTTP request. request.META['HTTP_X_FORWARDED_FOR'] for example, returns the client's IP address.
If your plugin has something to do with requests, its classes and function probably will be instantiated/called from your view. This means you need to pass it the current request object, as it makes no sense to have a global request object around.
In PHP, this is possible, as every request causes the whole code to be executed from scratch, but in Django requests are dispatched by a server and passed around in the framework using HttpRequest objects. Also refer to this part of the Django documentation for more information.
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.