I'm building a wiki and faced a problem when the result during debugging seems to differ from that during the program run.
class WikiPage(Handler):
def get(self, id):
id, existing_article, article_content = self.get_stripped_id_article_content(id)
logging.error(("GET! id: {0}; article: {1}; content: {2}").format(id, existing_article, article_content))
<...>
def strip_id(self, id):
id = id.replace("/", "")
return id
def get_stripped_id_article_content(self, id):
id = self.strip_id(id)
q = Article.all()
q.filter("id = ", id)
existing_article = q.get()
content = existing_article.content if existing_article else ""
return id, existing_article, content
<...>
class CreateEditPage(WikiPage):
def post(self, id):
id, existing_article, article_content = self.get_stripped_id_article_content(id)
user = self.get_user(self.request)
input_content = self.request.get("content")
if existing_article:
existing_article.content = input_content
existing_article.put()
else:
new_article = Article(id = id, content = input_content)
new_article.put()
id, existing_article, article_content = self.get_stripped_id_article_content(id)
logging.error(("POST! In Else. id: {0}; article: {1}; content: {2}").format(id, existing_article, article_content))
id, existing_article, article_content = self.get_stripped_id_article_content(id)
logging.error(("POST! Outside Else. id: {0}; article: {1}; content: {2}").format(id, existing_article, article_content))
self.redirect("/" + str(id))
PAGE_RE = r'(/(?:[a-zA-Z0-9_-]+/?)*)'
app = webapp2.WSGIApplication([('/_edit' + PAGE_RE, CreateEditPage),
(PAGE_RE, WikiPage),
],
debug=True)
First of all, I clear all the content from the database and fluch the memcache.
I run my application:
INFO 2015-10-14 21:06:52,744 sdk_update_checker.py:229] Checking for updates to the SDK.
INFO 2015-10-14 21:06:53,135 api_server.py:205] Starting API server at: http://localhost:53588
INFO 2015-10-14 21:06:53,141 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO 2015-10-14 21:06:53,142 admin_server.py:118] Starting admin server at: http://localhost:8000
ERROR 2015-10-14 21:10:10,804 gmv_wiki.py:194] GET! id: ; article: None; content:
INFO 2015-10-14 21:10:10,810 module.py:809] default: "GET / HTTP/1.1" 302 -
INFO 2015-10-14 21:10:10,847 module.py:809] default: "GET /_edit/ HTTP/1.1" 200 348
ERROR 2015-10-14 21:10:12,228 gmv_wiki.py:194] GET! id: newpost; article: None; content:
INFO 2015-10-14 21:10:12,239 module.py:809] default: "GET /newpost HTTP/1.1" 302 -
INFO 2015-10-14 21:10:12,264 module.py:809] default: "GET /_edit/newpost HTTP/1.1" 200 348
ERROR 2015-10-14 21:10:18,945 gmv_wiki.py:194] GET! id: new_article; article: None; content:
INFO 2015-10-14 21:10:18,951 module.py:809] default: "GET /new_article HTTP/1.1" 302 -
INFO 2015-10-14 21:10:18,976 module.py:809] default: "GET /_edit/new_article HTTP/1.1" 200 348
ERROR 2015-10-14 21:10:50,979 gmv_wiki.py:246] POST! In Else. id: new_article; article: None; content:
ERROR 2015-10-14 21:10:50,985 gmv_wiki.py:251] POST! Outside Else. id: new_article; article: None; content:
INFO 2015-10-14 21:10:50,991 module.py:809] default: "POST /_edit/new_article HTTP/1.1" 302 -
ERROR 2015-10-14 21:10:51,014 gmv_wiki.py:194] GET! id: new_article; article: None; content:
INFO 2015-10-14 21:10:51,022 module.py:809] default: "GET /new_article HTTP/1.1" 302 -
INFO 2015-10-14 21:10:51,051 module.py:809] default: "GET /_edit/new_article HTTP/1.1" 200 348
ERROR 2015-10-14 21:11:18,321 gmv_wiki.py:251] POST! Outside Else. id: new_article; article: <gmv_wiki.Article object at 0x7f684d2ba250>; content: New article content.
INFO 2015-10-14 21:11:18,326 module.py:809] default: "POST /_edit/new_article HTTP/1.1" 302 -
ERROR 2015-10-14 21:11:18,351 gmv_wiki.py:194] GET! id: new_article; article: <gmv_wiki.Article object at 0x7f684d2663d0>; content: New article content.
INFO 2015-10-14 21:11:18,358 module.py:809] default: "GET /new_article HTTP/1.1" 200 262
I debug my application:
pydev debugger: starting (pid: 10230)
INFO 2015-10-14 21:12:24,730 sdk_update_checker.py:229] Checking for updates to the SDK.
INFO 2015-10-14 21:12:25,435 api_server.py:205] Starting API server at: http://localhost:44302
INFO 2015-10-14 21:12:25,463 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO 2015-10-14 21:12:25,467 admin_server.py:118] Starting admin server at: http://localhost:8000
pydev debugger: starting (pid: 10250)
ERROR 2015-10-14 21:12:36,523 gmv_wiki.py:194] GET! id: new_article_1; article: None; content:
INFO 2015-10-14 21:12:36,547 module.py:809] default: "GET /new_article_1 HTTP/1.1" 302 -
INFO 2015-10-14 21:12:36,766 module.py:809] default: "GET /_edit/new_article_1 HTTP/1.1" 200 348
ERROR 2015-10-14 21:12:54,394 gmv_wiki.py:246] POST! In Else. id: new_article_1; article: None; content:
ERROR 2015-10-14 21:12:54,430 gmv_wiki.py:251] POST! Outside Else. id: new_article_1; article: None; content:
INFO 2015-10-14 21:12:54,452 module.py:809] default: "POST /_edit/new_article_1 HTTP/1.1" 302 -
ERROR 2015-10-14 21:12:54,587 gmv_wiki.py:194] GET! id: new_article_1; article: <gmv_wiki.Article object at 0x7fad181bef10>; content: New article 1 content.
INFO 2015-10-14 21:12:54,610 module.py:809] default: "GET /new_article_1 HTTP/1.1" 200 266
Could you have a look at cases of logging.error in the code. And the results in the log.
Two absolutely equal situations.
http://localhost:8080/new_article # ordinary run
http://localhost:8080/new_article_1 # debug
The behaviour of the program is as follows:
1. During debugging everything works well: edit form for a new article is created and when I press "Submit", the content is placed to the database and I'm redirected to the wiki page of that article.
2. When I just run the program (not debug it), edit form for a new article opens, I input something and press "Submit". Then I'm redirected to the wiki page. No wiki page is found at that address. I'm redirected again to the edit page. And then when I again input the content, the content is finally put into the database and the wiki page opens.
Mysteries for me are:
1. Why there is a difference between running and debugging?
2. Why after I have done new_article.put(), I still have no article object found in the post function?
3. Then I'm redirected to the wiki page, and somehow article object is found. Strange.
4. Why existing_article.put() works well whereas new_article.put() don't place anything to the database?
Well, I seem to be at my wits end. Could you be so kind as to help me with these problems. Thank you in advice.
I suspect you're running into GAE datastore eventual consistency issues. Basically a new article saved in new_article.put() won't be found in the Article.all() query for a (typically short) while. You can still access it by id, tho, but you'd have to adjust your code for that.
I suspect the difference in behaviour is caused by the lower overall speed of debug-mode execution which can hide the eventual consistency issues.
Related
I am using gcloud CLI to configure my region and zone:
gcloud config set compute/region us-central1
gcloud config set compute/zone us-central1-c
But each command lasts for about 15 seconds, and I get a warning:
WARNING: Property validation for compute/region was skipped
Everything works fine, but why do I have 15 seconds delay, and a warning?
With verbose argument, the output is:
DEBUG: Running [gcloud.config.set] with arguments: [--verbosity: "debug", SECTION/PROPERTY: "compute/region", VALUE: "us-central1"]
Updated property [compute/region].
DEBUG: Making request: GET http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/GOOGLE_AACOUNT_REPLACED#cloudbuild.gserviceaccount.com/?recursive=true
DEBUG: Starting new HTTP connection (1): metadata.google.internal:80
DEBUG: http://metadata.google.internal:80 "GET /computeMetadata/v1/instance/service-accounts/GOOGLE_AACOUNT_REPLACED#cloudbuild.gserviceaccount.com/?recursive=true HTTP/1.1" 200 185
DEBUG: Making request: GET http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/GOOGLE_AACOUNT_REPLACED#cloudbuild.gserviceaccount.com/token
DEBUG: http://metadata.google.internal:80 "GET /computeMetadata/v1/instance/service-accounts/GOOGLE_AACOUNT_REPLACED#cloudbuild.gserviceaccount.com/token HTTP/1.1" 200 1050
DEBUG: Making request: GET http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/GOOGLE_AACOUNT_REPLACED#cloudbuild.gserviceaccount.com/?recursive=true
DEBUG: Starting new HTTP connection (1): metadata.google.internal:80
DEBUG: http://metadata.google.internal:80 "GET /computeMetadata/v1/instance/service-accounts/GOOGLE_AACOUNT_REPLACED#cloudbuild.gserviceaccount.com/?recursive=true HTTP/1.1" 200 185
DEBUG: Making request: GET http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/GOOGLE_AACOUNT_REPLACED#cloudbuild.gserviceaccount.com/token
DEBUG: http://metadata.google.internal:80 "GET /computeMetadata/v1/instance/service-accounts/GOOGLE_AACOUNT_REPLACED#cloudbuild.gserviceaccount.com/token HTTP/1.1" 200 1050
DEBUG: Starting new HTTPS connection (1): compute.googleapis.com:443
DEBUG: https://compute.googleapis.com:443 "POST /batch/compute/v1 HTTP/1.1" 200 None
DEBUG: https://compute.googleapis.com:443 "POST /batch/compute/v1 HTTP/1.1" 200 None
DEBUG: https://compute.googleapis.com:443 "POST /batch/compute/v1 HTTP/1.1" 200 None
DEBUG: https://compute.googleapis.com:443 "POST /batch/compute/v1 HTTP/1.1" 200 None
DEBUG: https://compute.googleapis.com:443 "POST /batch/compute/v1 HTTP/1.1" 200 None
WARNING: Property validation for compute/region was skipped.
To make the gcloud tool easier to use, the Google Cloud will try and validate the values provided, including “compute/region”. In this case, it has to fetch a full list of available regions from the API. If this fails, for whatever reason, then it will show this warning message.
One of the many reasons may be that the Compute Engine API is not enabled. It could also be a lack of authentication, although Cloud Build will have authentication enabled by default and you don't need any special permissions to run this command.
To find out what exactly is going wrong, you can try adding the --log-http parameter to your gcloud command line. This will display the full details of any interactions with the API, including any error message in the response.
In any case, this is simply a warning, and the config entry is still being updated. This happens even if the validation fails, e.g. the region does not exist. As I mentioned above, this is just a feature to help letting the user know if they make certain types of simple mistakes.
I'm using a Flask REST API for my application and I've noticed that when I send requests from outside my own network, it's sometimes very, very slow. Most calls get completed within 150ms, but some take 8 seconds. The database connection is to a MySQL database using DBUtils.PersistentDB
The code for the endpoint:
#app.route("/name", methods=["POST"])
#jwt_refresh_token_required
def get_name_and_company():
user = get_jwt_identity()
response_object = server_functions.get_name_and_company(user)
return response_object
The function it uses:
def get_name_and_company(user):
sql = "SELECT fysios.firstname, fysios.lastname, companies.name FROM
fysios " +\
"INNER JOIN companies ON fysios.companyID = companies.id WHERE fysios.email = %s"
cursor = flask_server.get_db().cursor()
cursor.execute(sql, user)
data = cursor.fetchall()
first_name = data[0]['firstname']
last_name = data[0]['lastname']
company = data[0]['name']
response_object = name_and_company(first_name, last_name, company)
return make_response(jsonify(response_object)), 200
Here are the timestamps on the Flask server (it is the internal dev server, but I am running it with threaded=True):
[08/Mar/2019 22:16:54] "OPTIONS /login HTTP/1.1" 200 -
[08/Mar/2019 22:16:55] "POST /login HTTP/1.1" 200 -
[08/Mar/2019 22:16:55] "OPTIONS /clients HTTP/1.1" 200 -
[08/Mar/2019 22:16:55] "OPTIONS /verifyLogin HTTP/1.1" 200 -
[08/Mar/2019 22:16:55] "POST /clients HTTP/1.1" 200 -
[08/Mar/2019 22:16:57] "POST /verifyLogin HTTP/1.1" 200 -
[08/Mar/2019 22:16:57] "OPTIONS /name HTTP/1.1" 200 -
[08/Mar/2019 22:16:58] "POST /clients HTTP/1.1" 200 -
[08/Mar/2019 22:17:05] "POST /name HTTP/1.1" 200 -
As you can see, /name takes a total of 8 seconds and I can't find out why. This call to /name is just an example, it can happen on any of the calls. Is there a way to find out where the Flask application is actually stuck on?
Deploying to AWS Beanstalk solved the problem. I don't know if the limitations of the build-in dev server is to blame, but that's what did it for me.
After following the documentation in https://www.odoo.com/documentation/9.0/api_integration.html I have encountered a problem with the generated PDF report.
I call the webservice to generate an invoice report and after rendering the pdf report, it returns without its layout ( located: account.report_invoice )
I do the following to render the report:
url = 'http://{0}:{1}/xmlrpc/2/report'.format(self._connect['host'], self._connect['port'])
sock_print = xmlrpclib.ServerProxy(url)
#Here, the 'render_report' function returns the base64 pdf without the specified layout
result = sock_print.render_report(db_name, uid, pwd, report_name, ids, {'model': 'account.invoice', 'report_type': 'qweb-pdf'})
string_pdf = base64.decodestring(report['result'])
return True, string_pdf
After, the function above is done, I save the file in a directory to check if the file was generated with the correct layout.
So far, the pdf was generated but without its layout for account.report_invoice.
Any ideas on what might be happening or what I might be missing?
Thank you for your time.
[EDIT 1]
2018-09-17 14:34:09,599 30522 INFO ? werkzeug: 127.0.0.1 - - [17/Sep/2018 14:34:09] "GET /web/content/323-c1e807b/report.assets_common.0.css HTTP/1.1" 404 -
2018-09-17 14:34:09,617 30522 INFO ? werkzeug: 127.0.0.1 - - [17/Sep/2018 14:34:09] "GET /web/content/328-9a5a204/report.assets_pdf.0.css HTTP/1.1" 404 -
2018-09-17 14:34:09,879 30522 INFO ? werkzeug: 127.0.0.1 - - [17/Sep/2018 14:34:09] "GET /web/content/328-9a5a204/report.assets_pdf.0.css HTTP/1.1" 404 -
2018-09-17 14:34:09,883 30522 INFO ? werkzeug: 127.0.0.1 - - [17/Sep/2018 14:34:09] "GET /web/content/323-c1e807b/report.assets_common.0.css HTTP/1.1" 404 -
Found this when trying to call via webservice.
When I print the reports directly from odoo interface it's O.K, but via webservice it doesn't recognise its own core css.
I am using Django 1.9, Python 3, running locally on Docker (for testing)
Trying to integrate django-saml2-auth into my application.
Pretty much followed all the steps in the docs:
1) All installations were successful
2) New URLs were imported above the rest
3) Installed apps includes 'django_saml2_auth'
4) 'SAML2_AUTH' dict was placed in settings (and all attributes were mapped)
5) In the SAML2 identity provider (using OneLogin), the Single-sign-on URL and Audience URI(SP Entity ID) was set to http://127.0.0.1:8000/saml2_auth/acs/
What happens is that when I get to http://127.0.0.1:8000/admin the browser goes into an infinite redirect loop:
...
[02/May/2018 15:43:06] "GET /admin/ HTTP/1.1" 302 0
[02/May/2018 15:43:06] "GET /admin/login/?next=/admin/ HTTP/1.1" 302 0
[02/May/2018 15:43:07] "POST /saml2_auth/acs/ HTTP/1.1" 302 0
[02/May/2018 15:43:07] "GET /admin/ HTTP/1.1" 302 0
[02/May/2018 15:43:07] "GET /admin/login/?next=/admin/ HTTP/1.1" 302 0
[02/May/2018 15:43:08] "POST /saml2_auth/acs/ HTTP/1.1" 302 0
[02/May/2018 15:43:08] "GET /admin/ HTTP/1.1" 302 0
...
When I disable django-saml2-auth I see that a staff user was created.
In the OneLogin interface I can see that I logged in successfully.
Overriding django_saml2_auth.views.signin(r), where r is a django.core.handlers.wsgi.WSGIRequest, for <WSGIRequest: GET '/admin/login/?next=/admin/'>, and in the request, the user is set to AnonymousUser, COOKIES contain sessionid and csrftoken.
I would expect that a session would start for the user that was created/fetched, and that I will get to an /admin/<whatever> page.
I will appreciate any help in debugging this, thank you!
EDIT: I was able to get it to work by removing AUTHENTICATION_BACKENDS from settings.py- I have 3 other backends that I use. It seems like they conflict with django-saml2-auth.
Is there any way to get django-saml2-auth to work with other backends?
EDIT 2: Will try to integrate django-saml2-pro-auth, which has a backend so will not conflict. I would really appreciate some insight though.
EDIT 3: back to EDIT 2, when I remove all the backends and they don't conflict, the log flow looks like that:
[04/May/2018 15:24:26] "GET /admin/ HTTP/1.1" 302 0
[04/May/2018 15:24:27] "GET /admin/login/?next=/admin/ HTTP/1.1" 302
[04/May/2018 15:26:27] "POST /saml2_auth/acs/ HTTP/1.1" 302 0
[04/May/2018 15:26:27] "GET /admin/ HTTP/1.1" 200 38398
Where the last GET does not get redirected, with 200.
Issue resolved:
After taking a deeper dive- it seems like this code is the issue:
In django_saml2_auth/views.py, acs():
if target_user.is_active:
target_user.backend = 'django.contrib.auth.backends.ModelBackend'
login(r, target_user)
else:
return HttpResponseRedirect(get_reverse([denied, 'denied', 'django_saml2_auth:denied']))
It seems like the default ModelBackend is necessary.
When other backends are used, the default is no longer used by Django, and hence the infinite loop.
If the default backend is added to the list of backends, everything works as intended.
I'm new to django and bootstrap3 and I've been trying to implement the typeahead by twitter.
my urls.py
url(r'^name_autocomplete/?q=$', 'home.views.name_autocomplete'),
my script:
<script type="text/javascript">
$(document).ready(function() {
$("#navPersonSearch").typeahead({
name: 'people',
remote: 'name_autocomplete/?q=%QUERY'
});
});
my view
def name_autocomplete(request):
query = request.GET.get('query','')
if(len(query) > 0):
print "hello"
results = Person.objects.filter(short__istartswith=query)
result_list = []
for item in results:
result_list.append(item.short)
else:
result_list = []
response_text = json.dumps(result_list, separators=(',',':'))
return HttpResponse(response_text, content_type="application/json")
The requests seem to be getting through from the console output:
[05/Oct/2013 01:34:12] "GET / HTTP/1.1" 200 2007
[05/Oct/2013 01:34:12] "GET /static/bootstrap/css/bootstrap.min.css HTTP/1.1" 304 0
[05/Oct/2013 01:34:12] "GET /static/bootstrap/css/typeahead.js-bootstrap.css HTTP/1.1" 304 0
[05/Oct/2013 01:34:12] "GET /static/bootstrap/js/typeahead.js HTTP/1.1" 304 0
[05/Oct/2013 01:34:15] "GET /name_autocomplete/?q=J HTTP/1.1" 200 2
[05/Oct/2013 01:34:30] "GET /name_autocomplete/?q=Jac HTTP/1.1" 200 2
But the typeahead isn't working.
Replace the urlpattern as follow (you should not specify query string part):
url(r'^name_autocomplete/$', 'home.views.name_autocomplete'),
And in the view, you get query, while the javascript send query string as q=; so replace the following line:
query = request.GET.get('query','')
with
query = request.GET.get('q','')