Django Using oembed to convert url to embed url(VIMEO) - django

I try to convert video url to embed url using oEmbed, but:
When I print(video_url_api_data) it keeps returning <Response [404]>
I checked it with using url in google: https://vimeo.com/api/oembed.json?url=https://vimeo.com/570924262
What did I doing wrong in my code? And what do I have to put in the status part?
This status part:
vimeo_authorization_url = v.auth_url(
['private'],
'http://127.0.0.1:8000/',
1 #status
)
I put 1 for random and it works.
import json, vimeo, requests
class article_upload(View):
def post(self ,request, *args, **kwargs):
v = vimeo.VimeoClient(
token='****',
key='****',
secret='****'
)
file_data = request.FILES["file_data"]
path = file_data.temporary_file_path()
try:
vimeo_authorization_url = v.auth_url(
['private'],
'http://127.0.0.1:8000/',
1 #status
)
video_uri = v.upload(path, data={'name': '비디오 제목', 'description': '설명'})
video_data = v.get(video_uri + '?fields=link').json()
video_url = video_data['link']
params={"url": video_url}
video_url_api_data = requests.get('https://vimeo.com/api/oembed.json', params=params)
return render(request, 'account/myvideopage.html', {context(I made it)})
except vimeo.exceptions.VideoUploadFailure as e:
print("ohohohohohoh...vimeo error")
finally:
file_data.close() # Theoretically this should remove the file
if os.path.exists(path):
os.unlink(path) # But this will do it, barring permissions

I solved it before! The problem was... the delay time!
here ... I upload video.. of which size is at least 10MB
video_uri = v.upload(path, data={'name': '비디오 제목', 'descriptio...
and before i finish upload it I request information by using api...
video_url_api_data = requests.get('https://vimeo.com/api/oem...

Related

Create download link file in django

I created a file in project, generation pdf from html. For this i have this method:
def generation_html_to_pdf(self):
path_pdf = None
with NamedTemporaryFile(delete=False, suffix=".pdf", dir='pdf_files') as tf:
path_pdf = tf.name
pdfkit.from_file('templates/first_page.html', tf.name)
return path_pdf
Then, in pdf_files folder i have the pdf file. I want to get a download link for this file:
my view
path_to_pdf = generation_html_to_pdf()
download_link = 'http://' + request.get_host() + path_to_pdf
json_inf_pdf = {'download_link': download_link}
return JsonResponse(json_inf_pdf, status=200)
i have json like this:
{"download_link": "http://127.0.0.1:8000/home/alex/projects/test_project/pdf_files/tmpe0nqbn01.pdf"}"
when i click in this link i have error:
Page not found (404)
You need to create download view and url. Function like this to create link:
def download_link(request):
''' Create download link '''
download_link = 'http://{}/{}'.format(request.get_host(), 'download/my_filename')
json_inf_pdf = {'download_link': download_link}
return JsonResponse(json_inf_pdf, status=200)
and to download pdf:
def download_file(request, my_filename):
''' Download file '''
# Open template
from django.conf import settings
template_url = os.path.join(settings.BASE_DIR, 'templates', 'first_page.html')
template_open = open(template_url, 'r')
# Read template
from django import template
t = template.Template(template_open.read())
c = template.Context({})
# Create pdf
pdf = pdfkit.from_string(t.render(c))
# Create and return response with created pdf
response = HttpResponse(pdf)
response['Content-Type'] = 'application/pdf'
response['Content-disposition'] = 'attachment ; filename = {}'.format(my_filename)
return response
and url:
path('/download/<str:my_filename>', views.download_file, name="download_pdf')
I can't guarantee that this will work in your case without modification, since I can't tell which html-to-pdf library you're using and without seeing your other code. It's just a basic implementation idea.

Django rest framework API calling to another API [duplicate]

Is there any way to make a RESTful api call from django view?
I am trying to pass header and parameters along a url from the django views. I am googling from half an hour but could not find anything interesting.
Any help would be appreciated
Yes of course there is. You could use urllib2.urlopen but I prefer requests.
import requests
def my_django_view(request):
if request.method == 'POST':
r = requests.post('https://www.somedomain.com/some/url/save', params=request.POST)
else:
r = requests.get('https://www.somedomain.com/some/url/save', params=request.GET)
if r.status_code == 200:
return HttpResponse('Yay, it worked')
return HttpResponse('Could not save data')
The requests library is a very simple API over the top of urllib3, everything you need to know about making a request using it can be found here.
Yes i am posting my source code it may help you
import requests
def my_django_view(request):
url = "https://test"
header = {
"Content-Type":"application/json",
"X-Client-Id":"6786787678f7dd8we77e787",
"X-Client-Secret":"96777676767585",
}
payload = {
"subscriptionId" :"3456745",
"planId" : "check",
"returnUrl": "https://www.linkedin.com/in/itsharshyadav/"
}
result = requests.post(url, data=json.dumps(payload), headers=header)
if result.status_code == 200:
return HttpResponse('Successful')
return HttpResponse('Something went wrong')
In case of Get API
import requests
def my_django_view(request):
url = "https://test"
header = {
"Content-Type":"application/json",
"X-Client-Id":"6786787678f7dd8we77e787",
"X-Client-Secret":"96777676767585",
}
result = requests.get(url,headers=header)
if result.status_code == 200:
return HttpResponse('Successful')
return HttpResponse('Something went wrong')
## POST Data To Django Server using python script ##
def sendDataToServer(server_url, people_count,store_id, brand_id, time_slot, footfall_time):
try:
result = requests.post(url="url", data={"people_count": people_count, "store_id": store_id, "brand_id": brand_id,"time_slot": time_slot, "footfall_time": footfall_time})
print(result)
lJsonResult = result.json()
if lJsonResult['ResponseCode'] == 200:
print("Data Send")
info("Data Sent to the server successfully: ")
except Exception as e:
print("Failed to send json to server....", e)

Why HttpResponseRedirect.set_cookie is not working when i use in django project?

When I use Google OAuth to verify my user, After verify is passed, I want to redirect to the page which user visit before authority, So I want to save the page path to user's cookie, so I implementation like this:
def get_login_resp(request, redirect):
print(redirect)
auth_url = "https://accounts.google.com/o/oauth2/auth?" + urlencode({
"client_id": GOOGLE_CLIENT_ID,
"response_type": "code",
"redirect_uri": make_redirect_url(request, redirect),
"scope": "profile email",
"max_auth_age": 0
})
resp = HttpResponseRedirect(auth_url)
max_age = 3600 * 24
expires = datetime.strftime(datetime.utcnow() + timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT")
print(expires)
resp.set_cookie('google_auth_redirect', redirect, max_age=max_age, expires=expires,
domain=LOGIN_COOKIE_DOMAIN, secure=True, httponly=True)
print(resp._headers)
print(resp.cookies)
return resp
ps: redirect is the page path which I want to save
But when request the login url with Postman, I can only see this headers:
response headers
And these cookies:
Cookies
So how can i do with this problem? There is not any error info for me.
Try every methods to find out what's wrong, But still failed.
So I try to run server on an other machine(a Linux server), it works!!!
BTW: My develop PC is Macbook Pro 15-inch, 2017 with macOS High Sierra 10.13.1
Update at 14/Jan/2020:
Didn't find the root cause, but I solved this issue by saving redirect_url to session data, in this solution you should check auth valid by using another request, then call google auth to reauth again, code like below:
class GoogleAuthView(RedirectView):
# google auth view
def get(self, request, *args, **kwargs):
# get redirect url from url params, frontend code should pass the param in request url
redirect_url = request.GET.get('redirect_url', None)
if redirect_url:
redirect_url = parse.unquote(redirect_url)
credentials = request.session.get("credentials", None)
if (not credentials) or ('expire_time' not in credentials) or (credentials['expire_time'] < time.time()):
request.session['redirect_url'] = redirect_url # if need google auth, save redirect url to session first
else:
if redirect_url:
return HttpResponseRedirect(redirect_url)
flow = google_auth_oauthlib.flow.Flow.from_client_config(
client_config=settings.GOOGLE_AUTH_CONFIG,
scopes=settings.GOOGLE_AUTH_SCOPES
)
flow.redirect_uri = settings.GOOGLE_AUTH_CONFIG['web']['redirect_uris'][0]
authorization_url, state = flow.authorization_url(
access_type='offline',
include_granted_scopes='true'
)
request.session['state'] = state
return HttpResponseRedirect(authorization_url)
class GoogleAuthCallBackView(BasicView):
# google callback view
def get(self, request, *args, **kwargs):
state = request.session.get('state')
flow = google_auth_oauthlib.flow.Flow.from_client_config(
client_config=settings.GOOGLE_AUTH_CONFIG,
scopes=settings.GOOGLE_AUTH_SCOPES,
state=state
)
flow.redirect_uri = settings.GOOGLE_AUTH_CONFIG['web']['redirect_uris'][0]
# get redirect url from session data if exists
redirect_url = request.session.get('redirect_url') or settings.ADMIN_LOGIN_REDIRECT_URL
response = HttpResponseRedirect(redirect_url)
try:
del request.session['redirect_url']
except KeyError:
logger.info('Delete `redirect_url` in session get KeyError.')
pass
try:
flow.fetch_token(authorization_response=request.build_absolute_uri())
except Exception as e:
logger.error(e.message)
return response
# save credentials to session
credentials = flow.credentials
request.session["credentials"] = {
'token': credentials.token,
'refresh_token': credentials.refresh_token,
'token_uri': credentials.token_uri,
'client_id': credentials.client_id,
'client_secret': credentials.client_secret,
'scopes': credentials.scopes,
'expire_time': time.time() + TOKEN_EXPIRE_TIME,
}
profile_client = googleapiclient.discovery.build(
serviceName='oauth2',
version='v2',
credentials=credentials
)
profile = profile_client.userinfo().v2().me().get().execute()
email = profile['email']
user = user_manager.get_user_by_email(email)
if user:
user.username = profile['name'] # sync username from google
user.picture = profile['picture'] # sync avatar from google
user.save()
request.session["user"] = user.to_dict()
else:
return HttpResponseRedirect("/api/non_existent_user/") # show non-existent user
return response

Read user uploaded csv file and store data in Django modele live on heroku

This is my firts question here. so I would like to thank you for your help.
I have a Django School management app. and i would like the user to be able to read csv file and store in database with specific header.
My code runs locally very well. but I recently push it on heroku so that I can test it. I may note that all static assets are stored on Amazon s3. and it works.
but when I try to read a csv file, I get an Internal server error.
here is my code to store Pupils.
def convert_header(csvHeader):
cols = [ x.replace(' ', '_').lower() for x in csvHeader ]
return cols
def import_data(request):
if request.method == 'GET':
return render(request, 'school/import.html')
if request.method == 'POST' and request.FILES['myfile']:
if request.POST.get("object") == '':
message = 'You may chose an object'
return render(request, 'school/import.html', {'message': message })
if request.POST.get("object") == 'Pupil':
myfile = request.FILES['myfile']
fs = FileSystemStorage(location='eSchool/media/documents')
filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.path(filename)
data = csv.reader(open(uploaded_file_url), delimiter=',')
header = next(data)
header_cols = convert_header(header)
i = 0
k = 0
for row in data:
pupil = Pupil()
for k in range(len(header_cols)):
row_item = row[k].split(',')
for item in row_item:
key = header_cols[k]
if key == 'responsible':
item = Responsible.objects.get(pk= int(item))
print(item.first_name)
setattr(pupil, key, item)
else:
setattr(pupil, key, item)
k +=1
pupil.save()
i = i + 1
detailed = 'Sucessfully created '+ str(i) + ' Pupils '
return render(request, 'school/import_success.html', {'detailed' : detailed })
I would like to store data in a modele called Document. I create it. and try it I still get the error. Please help.
I find the solution of that problem. I first create a Django model to store the url of the uploaded file. like that:
class Document(models.Model):
uploaded_at = models.DateTimeField(auto_now_add=True)
upload = models.FileField(upload_to='documents')
Obvously I had to configure MEDIA_ROOT and MEDIA_URL
I configure the settings file to store the reference of all media assets and all static file on Amazon s3.
So instead of using the class FileSystemStorage. I install with pip a package named "requests"
pip install requests
and I import requests,csv and codecs.
import requests
import codecs
After that my view I just make litle modification tm my function.
def import_data(request):
if request.method == 'GET':
return render(request, 'school/import.html')
if request.method == 'POST' and request.FILES['myfile']:
if request.POST.get("object") == '':
message = 'You may chose an object'
return render(request, 'school/import.html', {'message': message })
if request.POST.get("object") == 'Pupil':
myfile = request.FILES['myfile']
doc = Document()
doc.upload = myfile
doc.save()
print(doc.upload.url)
if settings.DEBUG == True: # execute that block locally
data = csv.reader(open(doc.upload.path), delimiter=',') # locally the file is red frm the path so i us doc.upload.path
else: #this block is executed in production. So the file is red from the url. so I use doc.upload.url
rep = requests.get(doc.upload.url)
text = codecs.iterdecode(rep.iter_lines(), 'latin_1') #codecs is use to decode the lines. I don't really master how it works.
data = csv.reader(text, delimiter = ',')
header = next(data)
header_cols = convert_header(header)
i = 0
k = 0
for row in data:
pupil = Pupil()
for k in range(len(header_cols)):
row_item = row[k].split(',')
for item in row_item:
key = header_cols[k]
if key == 'responsible':
item = Responsible.objects.get(pk= int(item))
#print(item.first_name)
setattr(pupil, key, item)
else:
setattr(pupil, key, item)
k +=1
pupil.save()
i = i + 1
detailed = 'Sucessfully created '+ str(i) + ' Pupils '
return render(request, 'school/import_success.html', {'detailed' : detailed })
So right now. everything work well. If you see something that i can improve about this code, I Already thank you for your help!

Django 1.6.1 : Transaction error. You can't execute queries until the end of the atomic block in unitest

Django 1.6.1 on Windows with Postgres
I m using unittest to test a view that upload a .cfg file to server. I am facing 2 error called
(1) TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the atomic block.
(2) OSError: [Errno 22] Invalid argument
I am writing down my view and test below.
Views.py
def upload(request, device_id):
owner=account.objects.get(user=request.user)
dev = get_object_or_404(device, pk=device_id, owner=owner)
state = 0
# Handle file upload
if request.method == 'POST':
form = UploadConfigForm(request.POST, request.FILES)
if form.is_valid():
new_file = form.save(commit=False)
#now ask the device to download the config file
sess, action, action_opt = add_device_action(device=dev, action=Action.AC_CONFIG_FILE_SET)
#get the action related to this file
if action_opt:
new_file.act = action_opt
else:
new_file.act = action
new_file.save()
form.save_m2m()
#add the payload to the action
if action_opt:
action_opt.payload = new_file.payload()
action_opt.save ()
else:
action.payload = new_file.payload()
action.save ()
if settings.RENDER_JSON_INSTEAD_OF_HTML:
# Render as an json response
resp = action.descriptor()
return HttpResponse(json.dumps(resp), content_type="application/json")
else:
# Redirect to the document list after POST
# return HttpResponseRedirect('..')
state = 1
else:
form = UploadConfigForm() # A empty, unbound form
tests.py
class DevAPISimulDevTests(TestCase):
"""
Class that test the device API
"""
fixtures = ['test_auth.json', 'test_users.json', 'test_device.json']
def setUp(self):
"""
"""
ret = self.client.login(username='cws', password='cws123')
self.assertEqual(ret, True)
def test_api_sim_usr_upload_config_file(self):
"""
simulate the upload of the config file ba the user. submit with a file that is ok
"""
logger.debug('test_upload_config_file')
dev_id = 1
dev = device.objects.get(pk=dev_id)
url = reverse('device-upload', kwargs={'device_id':dev_id})
myfile = open('device/fixtures/cfg_ok.csr','r')
# with transaction.atomic():
response = self.client.post(url, {'config_file':myfile})
logger.debug('POST[%s]:[%s]', url, response.status_code)
self.assertEqual(response.status_code, 200)
ad = json.loads(response.content)
# Check the ad we think we should receive.
self.assertEqual(ad[Action.AD_EP_URL_BASE], settings.API_BASE_URL)
self.assertEqual(ad[Action.AD_EP_ACTION], settings.API_EP_ACK)
self.assertEqual(ad[Action.AD_EP_ERROR], settings.API_EP_ERR)
self.assertEqual(ad[Action.AD_DEVICE_CODE], dev.access_code)
self.assertEqual(ad[Action.AD_ACTION_CODE], Action.AC_ACKNOWLEDGE)
self.assertIsNone(ad.get(Action.AD_ACTION_PAYLOAD))
self.assertIsNone(ad.get(Action.AD_ACTION_WAIT))
# Check the DB
# We should have 1 session
self.assertEqual(dev.session_set.count(), 1)
sess = Session.objects.first()
self.assertEqual(sess.state, Session.STATE_SMS_SENT)
# We should have 2 actions more
self.assertEqual(sess.action_set.count(), 2)
# The first action should have the same token as in the SMS and the status should be in progress
first_act = sess.action_set.first()
self.assertEqual(first_act.next_token, ad[Action.AD_EP_TOKEN])
self.assertEqual(first_act.status, Action.STATUS_IN_PROGRESS)
# The second action should have the same token as in the SMS and the status should be queued
next_act = sess.action_set.last()
self.assertEqual(next_act.current_token, ad[Action.AD_EP_TOKEN])
self.assertEqual(next_act.status, Action.STATUS_QUEUED)
# test the status
status_should_be = {u'0': {u'state': u'in progress', u'state_label': u'processing', u'label': u'contacting device'},
u'1': {u'state': u'queued', u'state_label': u'not started', u'label': u'uploading config file to device'}}
self.test_sessionstatus(status_should_be)
return ad
# except IntegrityError:
# pass
def test_api_sim_dev_init_session (self):
"""
API : simulate the device receiving the first AD trhough SMS and initiating the session
"""
# try:
logger.debug('test_api_sim_dev_init_session')
# with transaction.atomic():
ad = self.test_api_sim_usr_upload_config_file()
# validate that the url is correct
url = reverse('device-api_ack', kwargs={'token':ad [Action.AD_EP_TOKEN]})
url2 = ''.join (['/device/', ad [Action.AD_EP_TOKEN], '/', ad[Action.AD_EP_ACTION]])
self.assertEqual(url, url2)
# Simulate the device processing the first ad of the session
# retrieve the next command from the server
response = self.client.get(url)
logger.debug('GET[%s]:[%s]', url, response.status_code)
# check the answer
self.assertEqual(response.status_code, 200)
ad2 = json.loads(response.content)
self.assertTrue(type(ad2) is dict)
# Check the ad we think we should receive.
self.assertEqual(ad2[Action.AD_ACTION_CODE], Action.AC_CONFIG_FILE_SET)
self.assertEqual(ad2[Action.AD_EP_ACTION], settings.API_EP_CFG)
self.assertTrue(type(ad2[Action.AD_ACTION_PAYLOAD]) is dict)
#TODO: will need on time to understand why this asssrt fail.
# self.assertContains(ad2[Action.AD_ACTION_PAYLOAD]['url'], '.csr')
self.assertIsNone(ad2.get(Action.AD_DEVICE_CODE))
self.assertIsNone(ad2.get(Action.AD_EP_URL_BASE))
self.assertIsNone(ad2.get(Action.AD_EP_ERROR))
# Check the DB
# We should have 1 session in STATE_WAIT_DEV_REQ
dev_id = 1
dev = device.objects.get(pk=dev_id)
self.assertEqual(dev.session_set.count(), 1)
sess = Session.objects.first()
self.assertEqual(sess.state, Session.STATE_WAIT_DEV_REQ)
# We should still have 2 actions
self.assertEqual(sess.action_set.count(), 2)
# The first action should have a status done
first_act = sess.action_set.first()
self.assertEqual(first_act.status, Action.STATUS_DONE)
self.assertEqual(first_act.next_token, ad[Action.AD_EP_TOKEN])
# The second action should have the same token as in the SMS and the status should be queued
next_act = sess.action_set.last()
self.assertEqual(next_act.current_token, ad[Action.AD_EP_TOKEN])
self.assertEqual(next_act.next_token, ad2[Action.AD_EP_TOKEN])
self.assertEqual(next_act.status, Action.STATUS_IN_PROGRESS)
# test the status
status_should_be = {u'0': {u'state': u'done', u'state_label': u'done', u'label': u'contacting device'},
u'1': {u'state': u'in progress', u'state_label': u'processing', u'label': u'uploading config file to device'}}
self.test_sessionstatus(status_should_be)
return ad2
Please suggest what I am doing wrong here, If code being pasted here does not concern errors, please suggest what code causes this error , as I cant figure out the origin of this errors..
might want to add:
'ATOMIC_REQUESTS': True,
under
DATABASES = {
'default': {
'ENGINE':whatever,
'ATOMIC_REQUESTS': True,
}
}