Flask extension, failing to return image path when using sub-path of root - flask

I'm working on adding additional functionality to Flask-Resize, specifically adding a feature that should serve the original file instead of generating a cached file if the size and other parameters are the same as the original.
I have everything worked out in the checks and so forth, and everything works fine if the image is in the RESIZE_ROOT directory but if not, when the image generator detects that it doesn't need to do anything and returns the original file path, jinja2 doesn't seem to fetch the image.
Using an image test_img.jpg with a size of 200x300px, in the RESIZE_ROOT directory works fine:
<img src="{{ 'test_img.jpg'|resize('200') }}"></img>
Output:
http://127.0.0.1:5000/static/images/test_img.jpg
127.0.0.1 - - [23/Oct/2015 03:44:28] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [23/Oct/2015 03:44:28] "GET /static/css/main.css HTTP/1.1" 200 -
127.0.0.1 - - [23/Oct/2015 03:44:28] "GET /static/images/test_img.jpg HTTP/1.1" 200 -
-- test_img.jpg fetched and displayed correctly
However when the image is moved to a sub-directory say ad/test_img.jpg, then the console output doesn't even indicate that it is fetching the image
http://127.0.0.1:5000/static/images/ad/test_img.jpg
127.0.0.1 - - [23/Oct/2015 03:58:42] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [23/Oct/2015 03:58:42] "GET /static/css/main.css HTTP/1.1" 200 -
-- ad/test_img.jpg isn't even being fetched
Putting image generates a valid link to the image, so the file path is getting returned correctly so I have no idea what's going on.
This is the relevant code; generate_image raises a exc.StopImageGeneration exception if it detects that it should not generate an image.
if not os.path.exists(full_cache_path):
try:
generate_image(inpath=original_path, outpath=full_cache_path,
format=format, width=width, height=height,
bgcolor=bgcolor, upscale=upscale, fill=fill,
anchor=anchor, quality=quality,
progressive=progressive,
placeholder_reason=placeholder_reason,
force_cache=force_cache)
except exc.StopImageGeneration:
full_cache_url = unicode(resize_url+image_url)
print (full_cache_url)
return full_cache_url.replace('\\', '/')
And my Flask-Resize initialisation parameters if it matters:
(RESIZE_URL='http://127.0.0.1:5000/static/images/', RESIZE_ROOT='static/images/')

So turns out this has nothing to do with Flask/Jinja2, it's Adblock's fault; it was blocking the images silently.
After I added an exception for localhost and 127.0.0.1 there was no problem.

Related

Django admin won't load on my local after logging in

I'm new to python and django.
I've copied my production app, that another developer wrote, onto my local. It's working perfectly. When I run django admin I get the login form. But when I enter my ID and PW I get redirected to the same login form, only it's blank; I never get logged in.
The server console reads:
[26/Apr/2022 06:25:21] "POST /admin/login/?next=/admin/ HTTP/1.1" 302 0
[26/Apr/2022 06:25:21] "GET /admin/ HTTP/1.1" 302 0
[26/Apr/2022 06:25:21] "GET /admin/login/?next=/admin/ HTTP/1.1" 200 11020
I don't understand what's needed to change or get added. Any help appreciated.
I've sorted the issue.
I commented out SESSION_COOKIE_DOMAIN from my settings.py file and that solved it.

Parsing corrupt Apache logs using regex

I'm writing a Python 3.7.2 program to parse Apache logs looking for all successful response codes. I've got regex written right now that will parse all correct Apache log entries into individual tuples of [origin] [date/time] [HTML method/file/protocol] [response code] and [file size] and then I just check to see if the response code is 3xx. The problem is there are several entries that are corrupt, some corrupt enough to be unreadable so I've stripped them out in a different part of the program. Several are just missing the closing " (quotation mark) on the method/protocol item causing it to throw an error each time I parse that line. I'm thinking I need to use a RegEx Or expression for " OR whitespace but that seems to break the quote into a different tuple item instead of looking for say, "GET 613.html HTTP/1.0" OR "GET 613.html HTTP/1.0 I'm new to regex and thoroughly stumped, can anyone explain what I'm doing wrong?
I should note that the logs have been scrubbed of some info, instead of origin IP it only shows 'local' or 'remote' and the OS/browser info is removed entirely.
This is the regex for the relevant tuple item that works with valid entries: "(.*)?" I've also tried:
"(.*)?("|\s) - creates another tuple item and still throws error
Here's a snippet of the log entries including the last entry which is missing it's closing "
local - - [27/Oct/1994:18:47:03 -0600] "GET index.html HTTP/1.0" 200 3185
local - - [27/Oct/1994:18:48:53 -0600] "GET index.html HTTP/1.0" 404 -
local - - [27/Oct/1994:18:49:55 -0600] "GET index.html HTTP/1.0" 303 3185
local - - [27/Oct/1994:18:50:25 -0600] "GET 612.html HTTP/1.0" 404 -
local - - [27/Oct/1994:18:50:41 -0600] "GET index.html HTTP/1.0" 200 388
local - - [27/Oct/1994:18:50:52 -0600] "GET 613.html HTTP/1.0 303 728
regex = '([(\w+)]+) - - \[(.*?)\] "(.*)?" (\d+) (\S+)'
import re
with open("validlogs.txt") as validlogs:
i = 0
array = []
successcodes = 0
for line in validlogs:
array.append(line)
loglength = len(array)
while (i < loglength):
line = re.match(regex, array[i]).groups()
if(line[3].startswith("3")):
successcodes+=1
i+=1
print("Number of successcodes: ", successcodes)
Parsing the log responses above should give Number of success codes: 2
Instead I get: Traceback (most recent call last):
File "test.py", line 24, in
line = re.match(regex, array[i]).groups()
AttributeError: 'NoneType' object has no attribute 'groups'
because (I believe) regex is looking explicitly for a " and can't handle the line entry that's missing it.
So I originally used re.match with ([(\w+)]+) - - \[(.*?)\] "(.*?)" (\d+) (\d+) with a Try: / Except: continue code to parse all the logs that actually matched the pattern. Since ~100,000 of the ~750,000 lines didn't conform to the correct Apache logs pattern, I wound up changing my code to re.search with much smaller segments instead.
For instance:
with open("./http_access_log.txt") as logs:
for line in logs:
if re.search('\s*(30\d)\s\S+', line): #Checking for 30x redirect codes
redirectCounter += 1
I've read that re.match is faster than re.search but I felt that being able to accurately capture the most possible log entries (this handles all but about 2000 lines, most of which have no usable info) was more important.

Odoo v9 Webservice Report without layout

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.

Django SAML integration

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.

wso2 api manager how to close http_access.log

I finid that at {wso2am_home}repository/logs/ have logs:
http_access_2013-10-28.log
tm.out wso2-apigw-errors.log
wso2-apigw-service.log
wso2-apigw-trace.log
wso2carbon-trace-messages.log
wso2carbon.log
and I configure all the log4j.properties INFO to OFF. I don't know where to close the http_access.log.
I find when I call 1 time api,it write the http_access.log: gwmanager.apim-wso2.com:8280 - - - "GET /direct/1.0.5 HTTP/1.1" - - "-" "Jakarta Commons-HttpClient/3.1" 128.6.X.X:80 - - - "GET http://128.6.X.X:80 HTTP/1.1" - - "-" "Synapse-HttpComponents-NIO so,as I call api time more and more ,the file is more and ---------- more big.
Do you know how to close the http_access.log?
If you want to disable http access logs in WSO2 products then go to catalina-server.xml which is located {CARBON_HOME}/repository/conf/tomcat directory, and remove the following property
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="${carbon.home}/repository/logs"
prefix="http_access_" suffix=".log"
pattern="combined" />
Please refer this for more details