HTML File link - force download always - html

I have a Django view that renders a list of uploaded files, and the user can click on them to begin the download.
When the project was deployed, we found there's one file that browsers open instead of download it. It seems related to the .dxf extension.
This is how the link is created:
...
As a result:
http://localhost:8003/media/folder/whatever.dxf
So, why the same browser behaves differently? if I run it on localhost, then it downloads the file. But accessing the real server, it'd open it. Can I prevent the server to open them in browsers?

You can try adding new Django view that will handle download.
urls.py
from django.conf.urls import url
import views
urlpatterns = [
url(r'^download/$', views.DownloadView.as_view(), name='download')
]
views.py
import urllib
from django.http import HttpResponse
from django.views.generic.base import View
class DownloadView(View):
def get(self, request):
location = request.GET.get('location')
file = urllib.urlretrieve(location)
contents = open(file[0], 'r')
content_type = '%s' % file[1].type
response = HttpResponse(contents, content_type=content_type)
response['Content-Disposition'] = 'attachment; filename="%s"' % location.split('/')[-1]
return response
template.html
...

Related

How can I process json file and display some data in form of charts in django

Basically, I have implemented Sentiment analysis for the Amazon review dataset in python. Now I want to make a web app for it. I try to look on google for it but all the projects are for Twitter data. Till now I make a Django app which can take json file from the user and display the success message. Any ideas on how to load the json file in my script and put these data on the charts
Create a model to upload the file. Create a director upload for uploading at root of your project
from django.db import models
class JSONFile(models.Model):
file = models.FileField(upload_to='uploads')
You can use a form:
from django import forms
class JSONFileForm(forms.Form):
file = forms.FileField(
label='Select a file',
help_text='max. 42 megabytes'
)
Create templates as 'chart.html' to show your chart accordingly and 'upload.html' to render above form
In your views.py:
from forms import JSONFileForm
from models import JSONFile
from django.shortcuts import render
def chart(request):
if request.method == 'POST':
form = JSONFileForm(request.POST, request.FILES)
if form.is_valid():
newfile = JSONFile(file = request.FILES['file'])
newfile.save()
json.dumps(request.FILES['file'])
#format your data here
return render(request,"chart.html",{}) #pass data context in {} as dict
else:
form = JSONFileForm(request.POST)
else:
form = JSONFileForm()
return render(request,"upload.html",{"form":form})

Why does my program not retrieve the images from the urls using Python3?

For some reason this code will say it has downloaded my picture but nothing will pop up in the directory, I thought it might be because you can't access i.redd.it files in where I live so I used a proxy, this still did not fix my problems.
This is my code:
import json
import urllib.request
proxy = urllib.request.ProxyHandler({'http': '176.221.34.7'})
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)
with open('/Users/eucar/Documents/Crawler/Crawler/Crawler/image_links.json') as images:
images = json.load(images)
for idx, image_url in enumerate(images):
try :
image_url = image_url.strip()
file_name = '/Users/eucar/Desktop/Instagrammemes/{}.{}'.format(idx, image_url.strip().split('.')[-1])
print('About to download {} to file {}'.format(image_url, file_name))
urllib.request.urlretrieve(image_url, file_name)
except :
print("All done")
This is the json file:
["https://i.redd.it/9q7r48kd2dh21.jpg",
"https://i.redd.it/yix3rq5t5dh21.jpg",
"https://i.redd.it/1vm3bd2vvch21.jpg",
"https://i.redd.it/wy7uszuigch21.jpg",
"https://i.redd.it/4gunzkkghch21.jpg",
"https://i.redd.it/4sd2hbe5sch21.jpg", "https://i.redd.it/bv3qior3ybh21.jpg"]

Entering Value into Search Bar and Downloading Output from Webpage

I'm trying to search a webpage (http://www.phillyhistory.org/historicstreets/). I think the relevent source html is this:
<input name="txtStreetName" type="text" id="txtStreetName">
You can see the rest of the source html at the website. I want to go into the that text box and enter an street name and download an output (ie enter 'Jefferson' in the search box of the page and see historic street names with Jefferson). I have tried using requests.post, and tried typing ?get=Jefferson in the url to test if that works with no luck. Anyone have any ideas how to get this page? Thanks,
Cameron
code that I currently tried (some imports are unused as I plan to parse etc):
import requests
from bs4 import BeautifulSoup
import csv
from string import ascii_lowercase
import codecs
import os.path
import time
arrayofstreets = []
arrayofstreets = ['Jefferson']
for each in arrayofstreets:
url = 'http://www.phillyhistory.org/historicstreets/default.aspx'
payload = {'txtStreetName': each}
r = requests.post(url, data=payload).content
outfile = "raw/" + each + ".html"
with open(outfile, "w") as code:
code.write(r)
time.sleep(2)
This did not work and only gave me the default webpage downloaded (ie Jefferson not entered in the search bar and retrieved.
I'm guessing your reference to 'requests.post' relates to the requests module for python.
As you have not specified what you want to scrape from the search results I will simply give you a snippet to get the html for a given search query:
import requests
query = 'Jefferson'
url = 'http://www.phillyhistory.org/historicstreets/default.aspx'
post_data = {'txtStreetName': query}
html_result = requests.post(url, data=post_data).content
print html_result
If you need to further process the html file to extract some data, I suggest you use the Beautiful Soup module to do so.
UPDATED VERSION:
#!/usr/bin/python
import requests
from bs4 import BeautifulSoup
import csv
from string import ascii_lowercase
import codecs
import os.path
import time
def get_post_data(html_soup, query):
view_state = html_soup.find('input', {'name': '__VIEWSTATE'})['value']
event_validation = html_soup.find('input', {'name': '__EVENTVALIDATION'})['value']
textbox1 = ''
btn_search = 'Find'
return {'__VIEWSTATE': view_state,
'__EVENTVALIDATION': event_validation,
'Textbox1': '',
'txtStreetName': query,
'btnSearch': btn_search
}
arrayofstreets = ['Jefferson']
url = 'http://www.phillyhistory.org/historicstreets/default.aspx'
html = requests.get(url).content
for each in arrayofstreets:
payload = get_post_data(BeautifulSoup(html, 'lxml'), each)
r = requests.post(url, data=payload).content
outfile = "raw/" + each + ".html"
with open(outfile, "w") as code:
code.write(r)
time.sleep(2)
The problem in my/your first version was that we weren't posting all the required parameters. To find out what you need to send, open the network monitor in your browser (Ctrl+Shitf+Q in Firefox) and make that search as you would normally. If you select the POST request in the network log, on the right you should see 'parameters tab' where the post parameters your browser sent.

Flask: how can a client download a created JSON file

I have a JSON string that I am reading from a web form that I would like to create a temporary file out of and allow the file to be downloaded to the local client machine. In other words my app.route reads the string, writes the string to a file and then sends the file to the client:
#app.route('/sendFile', methods=['POST'])
def sendFile():
content = str(request.form['jsonval'])
with open('zones.geojson', 'w') as f:
f.write(content)
return send_file(f)
What's the best way to make this work?
From this answer all that is needed is to specify the correct Response header:
From flask import Response
#app.route('/sendFile', methods=['POST'])
def sendFile():
content = str(request.form['jsonval'])
return Response(content,
mimetype='application/json',
headers={'Content-Disposition':'attachment;filename=zones.geojson'})

Changing between applications in a django site

I have 2 applications in my site:
users
base
The main url conf always raises urls with the appname of base/index.html
When a url action say href="login" is provided from base/index.html it automatically
searches for "base/login.html"
I want it to switch the application and search for "users/login.html"
How do I force it to switch applications either in the html href or in the urlconf. I prefer to use url conf but don't konw how to.
users/url.py
from django.conf.urls import patterns, include, url
from emp_users.views import *
from django.conf import settings
from django.views.generic.base import RedirectView
urlpatterns = patterns('',
url(r'^index/$', RedirectView.as_view(url='/hr_base/index', permanent=False), name='index'),
url(r'^new_user/$', register_user),
url(r'^new_hr_user/$', register_hr_user),
)
site/urls.py : This is the main project urls.py
urlpatterns = patterns('',
url(r'^base/index$','hr_base.views.index'),
url(r'^users/', include('emp_users.urls')),
)
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT},),# 'show_indices':True}),
)
site/base/templates/base/index.html
Login
That should pointgenerate the url users/login and then access user/views.py
Django is not adding any URLs anywhere. You are simply providing a relative URL in your template, so your browser is adding the current path to it. If you want to specify a different path, then you need to provide that full URL: <a href="/users/new_user">.
However you should not be hard-coding URLs in your templates in any case. You should let Django provide the full URL:
Login
urlpatterns = patterns('',
url(r'^base/login/$', 'base.views.login'),
url(r'^users/login/$', 'users.views.login')
)
or
in main url.py file
urlpatterns = patterns('',
url(r'^base/', include('base.url'),
url(r'^users/', include('users.url') ,
)