ValueError: Missing scheme in request url: h - python-2.7

I am a beginner in scrapy, python. I tried to deploy the spider code in scrapinghub and I encountered the following error. Below is the code.
import scrapy
from bs4 import BeautifulSoup,SoupStrainer
import urllib2
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
import re
import pkgutil
from pkg_resources import resource_string
from tues1402.items import Tues1402Item
data = pkgutil.get_data("tues1402","resources/urllist.txt")
class SpiderTuesday (scrapy.Spider):
name = 'tuesday'
self.start_urls = [url.strip() for url in data]
def parse(self, response):
story = Tues1402Item()
story['url'] = response.url
story['title'] = response.xpath("//title/text()").extract()
return story
is my spider.py code
import scrapy
class Tues1402Item(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
title = scrapy.Field()
url = scrapy.Field()
is the items.py code and
from setuptools import setup, find_packages
setup(
name = 'tues1402',
version = '1.0',
packages = find_packages(),
entry_points = {'scrapy': ['settings = tues1402.settings']},
package_data = {'tues1402':['resources/urllist.txt']},
zip_safe = False,
)
is the setup.py code.
The following is the error.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/scrapy/core/engine.py", line 126, in _next_request
request = next(slot.start_requests)
File "/usr/local/lib/python2.7/site-packages/scrapy/spiders/init.py", line 70, in start_requests
yield self.make_requests_from_url(url)
File "/usr/local/lib/python2.7/site-packages/scrapy/spiders/init.py", line 73, in make_requests_from_url
return Request(url, dont_filter=True)
File "/usr/local/lib/python2.7/site-packages/scrapy/http/request/init.py", line 25, in init
self._set_url(url)
File "/usr/local/lib/python2.7/site-packages/scrapy/http/request/init.py", line 57, in _set_url
raise ValueError('Missing scheme in request url: %s' % self._url)
ValueError: Missing scheme in request url: h
Thank you in Advance

Your error means that url h is not a valid url. You should print out your self.start_urls and see what urls you have there, you most likely have a string h as your first url.
Seems like your spider iterates through text instead of a list of urls here:
data = pkgutil.get_data("tues1402","resources/urllist.txt")
class SpiderTuesday (scrapy.Spider):
name = 'tuesday'
self.start_urls = [url.strip() for url in data]
Assuming that you store your urls with some separator in urllist.txt file you should split that:
# assuming file has url in every line
self.start_urls = [url.strip() for url in data.splitlines()]

Related

How to authenticate with django-grahql-jwt in a graphene-django Using GraphQLTestCase?

I use Django and Graphene in my project. I wrote tests with GraphQLTestCase. When i try to authenticate users using JWT, i usually get errors.
Here is my code:
from django.test import TestCase
import json
from graphene_django.utils.testing import GraphQLTestCase
from resume.graph.schema import schema
from .models import Post
from django.contrib.auth import get_user_model
from graphql_jwt.shortcuts import get_token
User = get_user_model()
class PostTestCase(GraphQLTestCase):
GRAPHQL_SCHEMA = schema
def test_post_list(self):
token = get_token(User.objects.get(pk=1))
headers = {"HTTP_AUTHORIZATION": f"JWT {token}"}
response = self.query(
'''
query {
post{
user
text
}
}
''',
op_name = 'post',
headers=headers,
)
content = json.loads(response.content)
self.assertResponseNoErrors(response)
Here are the errors I get after running python manage.py test.
Traceback (most recent call last):
File "C:\Users\Udemezue\Desktop\resume\post\tests.py", line 25, in test_post_list
token = get_token(User.objects.get(pk=9))
File "C:\Users\Udemezue\Desktop\resume\env\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Udemezue\Desktop\resume\env\lib\site-packages\django\db\models\query.py", line 415, in get
raise self.model.DoesNotExist(
accounts.models.User.DoesNotExist: User matching query does not exist.
Here is the errors i get.
======================================================================
FAIL: test_post_list (post.tests.PostTestCase)
Traceback (most recent call last):
File "C:\Users\Udemezue\Desktop\resume\post\tests.py", line 62, in test_post_list
self.assertResponseNoErrors(response)
File "C:\Users\Udemezue\Desktop\resume\env\lib\site-packages\graphene_django\utils\testing.py", line 75, in assertResponseNoErrors
self.assertEqual(resp.status_code, 200)
AssertionError: 400 != 200
Ran 1 test in 0.137s
FAILED (failures=1)
Destroying test database for alias 'default'...
This works for me.
from django.contrib.auth import get_user_model
from graphql_jwt.shortcuts import get_token
User = get_user_model()
import json
class PostTestCase(GraphQLTestCase):
def test_post_list(self):
user = get_user_model().objects.create(username='myuser')
token = get_token(user)
headers = {"HTTP_AUTHORIZATION": f"JWT {token}"}
response = self.query(
'''
query GetUser($username: String!) {
user(username: $username) {
id
}
}
''',
headers=headers,
)
content = json.loads(response.content)
self.assertResponseNoErrors(response)

scrapy TypeError: object() takes no parameters

I am new to Scrapy and trying to crawl a couple of links as a test using Scrapy. Whenever I run scrapy crawl tier1, I get "TypeError: object() takes no parameters" as the following:
Traceback (most recent call last):
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/Users/btaek/TaeksProgramming/adv/crawler/adv_crawler/adv_crawler/spiders/tier1_crawler.py", line 93, in parse
mk_loader.add_xpath('title', 'h1[#class="top_title"]') # Title of the article
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 167, in add_xpath
self.add_value(field_name, values, *processors, **kw)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 77, in add_value
self._add_value(field_name, value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 91, in _add_value
processed_value = self._process_input_value(field_name, value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 150, in _process_input_value
return proc(value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/processors.py", line 28, in __call__
next_values += arg_to_iter(func(v))
TypeError: object() takes no parameters
2017-08-23 17:25:02 [tier1-parse-logger] INFO: Entered the parse function to parse and index: http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166
2017-08-23 17:25:02 [tier1-parse-logger] ERROR: Error (object() takes no parameters) when trying to parse <<date>> from a mk article: http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166
2017-08-23 17:25:02 [tier1-parse-logger] ERROR: Error (object() takes no parameters) when trying to parse <<author>> from a mk article: http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166
2017-08-23 17:25:02 [scrapy.core.scraper] ERROR: Spider error processing <GET http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166> (referer: None)
Traceback (most recent call last):
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/Users/btaek/TaeksProgramming/adv/crawler/adv_crawler/adv_crawler/spiders/tier1_crawler.py", line 93, in parse
mk_loader.add_xpath('title', 'h1[#class="top_title"]') # Title of the article
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 167, in add_xpath
self.add_value(field_name, values, *processors, **kw)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 77, in add_value
self._add_value(field_name, value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 91, in _add_value
processed_value = self._process_input_value(field_name, value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 150, in _process_input_value
return proc(value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/processors.py", line 28, in __call__
next_values += arg_to_iter(func(v))
TypeError: object() takes no parameters
And, my spider file (tier1_crawler.py):
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import os
sys.path.append(os.path.abspath('..'))
import logging
import scrapy
from scrapy.loader import ItemLoader
from adv_crawler.items import AdvCrawlerItem
from datetime import datetime, date, time
t1_parse_logger = logging.getLogger("tier1-parse-logger")
t1_parse_logger.LOG_FILE = "Tier1-log.txt"
content_type_dic = {
'news': 'news',
}
class Tier1Crawler(scrapy.Spider):
name = "tier1"
def start_requests(self):
urls = ['http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535982',
'http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166',
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
t1_parse_logger.info("Entered the parse function to parse and index: %s" % response.url) # Log at the beginning of the parse function
item_loader = ItemLoader(item=AdvCrawlerItem(), response=response)
if 'mk.co.kr' in response.url:
mk_loader = item_loader.nested_xpath('//div[#id="top_header"]/div[#class="news_title"]/div[#class="news_title_text"]')
try:
mk_loader.add_xpath('date', 'div[#class="news_title_author"]/ul/li[#class="lasttime"]')
except AttributeError: # if the date is not in "lasttime" li tag
mk_loader.add_xpath('date', 'div[#class="news_title_author"]/ul/li[#class="lasttime1"]')
except Exception as e: # in case the error is not AttributeError
t1_parse_logger.error("Error "+"("+str(e)+")"+" when trying to parse <<date>> from a mk article: %s" % response.url)
try:
mk_loader.add_xpath('author', 'div[#class="news_title_author"]/ul/li[#class="author"]')
except AttributeError: # in case there is no author (some mk articles have no author)
item_loader.add_value('author', "None") # ir error, replace with the line below
# item['author'] = "None" # if the above gives any error, replace the above with this line
except Exception as e: # in case the error is not AttributeError
t1_parse_logger.error("Error "+"("+str(e)+")"+" when trying to parse <<author>> from a mk article: %s" % response.url)
item_loader.add_xpath('content', '//div[#id="Content"]/div[#class="left_content"]/div[#id="article_body"]/div[#class="art_txt"]') # Content of the article (entire contents)
mk_loader.add_xpath('title', 'h1[#class="top_title"]') # Title of the article
item_loader.add_value('content_type', content_type_dic['news'])
item_loader.add_value('timestamp', str(datetime.now())) # timestamp of when the document is being indexed
item_loader.add_value('url', response.url) # url of the article
t1_parse_logger.info("Parsed and indexed: %s" % response.url)
return item_loader.load_item()
And, my items.py file:
# -*- coding: utf-8 -*-
import scrapy
from scrapy.loader.processors import Join, MapCompose, TakeFirst
from w3lib.html import remove_tags
def filter_date(value):
if isinstance(value, unicode):
(year, month, day) = str(value.split(" ")[-2]).split(".")
return year+"-"+month+"-"+day
def filter_utf(value):
if isinstance(value, unicode):
return value.encode('utf-8')
class AdvCrawlerItem(scrapy.Item):
author = scrapy.Field(input_processor=MapCompose(remove_tags, TakeFirst, filter_utf),) # Name of the publisher/author
content = scrapy.Field(input_processor=MapCompose(remove_tags, Join, filter_utf),) # Content of the article (entire contents)
content_type = scrapy.Field()
date = scrapy.Field(input_processor=MapCompose(remove_tags, TakeFirst, filter_date),)
timestamp = scrapy.Field() # timestamp of when the document is being indexed
title = scrapy.Field(input_processor=MapCompose(remove_tags, TakeFirst, filter_utf),) # title of the article
url = scrapy.Field() # url of the article
And, pipelines.py file:
import json
from scrapy import signals
from scrapy.exporters import JsonLinesItemExporter
class AdvCrawlerJsonExportPipeline(object):
def open_spider(self, spider):
self.file = open('crawled-articles1.txt', 'w')
def close_spider(self, spider):
self.file.close()
def process_item(self, item, spider):
line = json.dummps(dict(item)) + "\n"
self.file.write(line)
return item
I am aware that "TypeError: object() takes no parameters" error is usually thrown when __init__ method of a class is not defined at all or not defined to take in parameter(s).
However, in the case above, how can i fix the error? Am I doing something wrong using the item loader or nested item loader??
When using scrapy processors you should use the classes to create objects that do the processing:
# wrong
field = Field(output_processor=MapCompose(TakeFirst))
# right
field = Field(output_processor=MapCompose(TakeFirst()))
^^

Pesky server error on Python web app

I've been having a problem with a website that I'm working on not attaching pictures to html emails. Thought I had it fixed but then every time someone tries to register on it I get a Server Error (500). I've only changed a couple of references so no idea what went wrong there, anyways error log is as follows:
2016-08-31 08:26:15,757 :Internal Server Error: /register/
Traceback (most recent call last):
File "/home/asranet/.virtualenvs/testenv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/asranet/.virtualenvs/testenv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./register/views.py", line 14, in index
form.save(commit=True)
File "/home/asranet/.virtualenvs/testenv/local/lib/python2.7/site-packages/django/forms/models.py", line 451, in save
self.instance.save()
File "./register/models.py", line 35, in save
email_client(self, site_settings.site_name + "Conference Registration", "You are officially registered for AdWind 2017")
File "./adWind/email_functionality.py", line 31, in email_client
fp = open(os.path.join(os.path.dirname(__file__), f), 'rb')
IOError: [Errno 2] No such file or directory: u'./adWind/static/Images/asranetLogo.jpg'
I checked and the file is there. No idea how to proceed, could really use some help. Thank you in advance!
P.S. here's the code for email functionality:
from __future__ import unicode_literals
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
import os
from email.mime.image import MIMEImage
from reportlab.platypus.doctemplate import SimpleDocTemplate
from reportlab.platypus import Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from django.http import HttpResponse
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
def email_client(self, subject, text):
# Send the client an email
html_content = render_to_string("../templates/baseTemplates/emailToUser.html", {'salutation': self.salutation,
'last_name':
self.last_name,
'text_body': text})
msg = EmailMultiAlternatives(subject, 'Dear ' + self.salutation + ' ' +
self.last_name + '/n' + text,
'adwind#asranet.co.uk', [self.email], )
msg.attach_alternative(html_content, "text/html")
msg.attach_file('/static/Images/asranetLogo.jpg')
msg.mixed_subtype = 'related'
f = '/static/Images/asranetLogo.jpg'
fp = open(os.path.join(os.path.dirname(__file__), f), 'rb')
msg_img = MIMEImage(fp.read())
fp.close()
msg_img.add_header('Content-ID', '<{}>'.format(f))
msg.attach(msg_img)
msg.send(fail_silently=True)
def email_admin(self, subject, text, sorted_self):
styleSheet = getSampleStyleSheet()
# Send the admin a PDF of client details
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="clientDetails.pdf"'
string_buffer = StringIO()
new_pdf = []
header = Paragraph("AdWind Attendee Details", styleSheet['Heading1'])
new_pdf.append(header)
for element in sorted_self:
new_pdf.append(Paragraph(element[0], styleSheet['Heading3']))
new_pdf.append(Paragraph(element[1], styleSheet['BodyText']))
new_pdf.append(Spacer(1, 2))
doc = SimpleDocTemplate(string_buffer)
doc.build(new_pdf)
pdf = string_buffer.getvalue()
string_buffer.close()
msg = EmailMultiAlternatives(subject, text, "adwind#asranet.co.uk", ["adwind#asranet.co.uk"])
msg.attach(self.first_name + self.last_name + "adWind.pdf", pdf, "application/pdf")
msg.send(fail_silently=True)
In Django the static directory is usually under a module_name directory. I suggest changing your paths into a relative one:
msg.attach_file('static/[module_name]/Images/asranetLogo.jpg')
and
f = 'static/[module_name]/Images/asranetLogo.jpg'

Python- Assertion error when performing nosetests

I am new to web.py . I was following ex52 LPTHW , and I had to perform nosetests on all the tests in tests folder. But I am getting 1 failed test due to assertion error. I tried reading different assertion errors and why they occur , but I cant figure out this one. I tried expanding the errors which the server could possibly show up like 500 Internal , 400 but its still failing the test. I have created the other code just like mentioned in the book: http://learnpythonthehardway.org/book/ex52.html
Here is my traceback:
C:\lpthw\gothonweb>cd tests
C:\lpthw\gothonweb\tests>nosetests
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\web\application.py", line 239, in process
return self.handle()
File "C:\Python27\lib\site-packages\web\application.py", line 230, in handle
return self._delegate(fn, self.fvars, args)
File "C:\Python27\lib\site-packages\web\application.py", line 420, in _delegate
return handle_class(cls)
File "C:\Python27\lib\site-packages\web\application.py", line 396, in handle_class
return tocall(*args)
File "C:\lpthw\gothonweb\bin\app.py", line 29, in GET
return render.hello_form()
File "C:\Python27\lib\site-packages\web\template.py", line 1017, in __getattr__
t = self._template(name)
File "C:\Python27\lib\site-packages\web\template.py", line 1014, in _template
return self._load_template(name)
File "C:\Python27\lib\site-packages\web\template.py", line 1001, in _load_template
raise AttributeError, "No template named " + name
AttributeError: No template named hello_form
F...
======================================================================
FAIL: tests.app_tests.test_index
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "C:\lpthw\gothonweb\tests\app_tests.py", line 12, in test_index
assert_response(resp)
File "C:\lpthw\gothonweb\tests\tools.py", line 5, in assert_response
assert status in resp.status, "Expected response %r not in %r" % (status , resp.status)
AssertionError: Expected response '200' not in '500 Internal Server Error'
----------------------------------------------------------------------
Ran 4 tests in 0.562s
FAILED (failures=1)
My tests code: app_tests.py
from nose.tools import *
from bin.app import app
from tests.tools import assert_response
def test_index():
#check that we get a 404 on the / URL
resp = app.request("/")
assert_response(resp, status= "404")
#test our first GET request to /hello
resp = app.request("/hello")
assert_response(resp)
#make sure default values work for the form
resp = app.request("/hello" , method="POST")
assert_response(resp , contains="Nobody")
#test that we get expected values
data = {'name':'Tejas','greet':'Ola!'}
resp = app.request("/hello " , method= "POST", data=data)
assert_response(resp , contains="Zed")
tools.py :
from nose.tools import *
import re
def assert_response(resp, contains=None, matches=None, headers=None, status= "200"):
assert status in resp.status, "Expected response %r not in %r" % (status , resp.status)
if status == "200":
assert resp.data , "Response data is empty."
if contains:
assert contains in resp.data, "Response does not contain %r" % contains
if matches:
reg = re.compile(matches)
assert reg.matches(resp.data), "Response does not match %r" % matches
if headers:
assert_equal(resp.headers , headers)
app.py code:
import web
from gothonweb import map
urls = (
'/game' , 'GameEngine' ,
'/' , 'Index',
)
app = web.application(urls, globals())
#little hack so that debug mode works with sessions
if web.config.get('_session') is None:
store= web.session.DiskStore('sessions')
session= web.session.Session(app, store, initializer={'room':None})
web.config._session = session
else:
session= web.config._session
render = web.template.render('templates/', base="layout")
class Index(object):
def GET(self):
#this is used to "setup" the session with starting values
session.room= map.START
web.seeother("/game")
class GameEngine(object):
def GET(self):
if session.room:
return render.show_room(room= session.room)
else:
#why is there here? do you need it?
return render.you_died()
def POST(self):
form= web.input(action=None)
if session.room and form.action:
session.room= session.room.go(form.action)
else:
session.room= None
if __name__ == "__main__" :
app.run()
After going ahead with the exercise now it gives me 2 import errors:
PS C:\lpthw\gothonweb\tests> nosetests
EE
======================================================================
ERROR: Failure: SyntaxError (invalid syntax (app.py, line 2))
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose\loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "C:\Python27\lib\site-packages\nose\importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "C:\Python27\lib\site-packages\nose\importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "C:\lpthw\gothonweb\tests\app_tests.py", line 2, in <module>
from bin.app import app
File "C:\lpthw\gothonweb\bin\app.py", line 2
from gothonweb import map.py
^
SyntaxError: invalid syntax
======================================================================
ERROR: Failure: ImportError (cannot import name map)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose\loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "C:\Python27\lib\site-packages\nose\importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "C:\Python27\lib\site-packages\nose\importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "C:\lpthw\gothonweb\tests\map_tests.py", line 2, in <module>
from gothonweb import map
ImportError: cannot import name map
----------------------------------------------------------------------
Ran 2 tests in 0.002s
FAILED (errors=2)
directory structure:
C:/lpthw/
gothonweb/
bin build dist doc sessions Gothonweb templates tests map.py app.py
tests/
map_tests.py
app_tests.py
__init__.py
tools.py
What should I do to fix the error? Thanks for the suggestions.
For this error,
"AttributeError: No template named hello_form"
use an absolute path and see if that works, instead of
"render = web.template.render('templates/', base="layout")
try:
"render = web.template.render('C:/lpthw/gothonweb/bin/templates', base="layout")
Amazingly, your test has nothing to do with app.py.
from nose.tools import *
from bin.app import app
from tests.tools import assert_response
def test_index():
#check that we get a 404 on the / URL
resp = app.request("/")
assert_response(resp, status= "404")
#test our first GET request to /hello
resp = app.request("/hello")
assert_response(resp)
#make sure default values work for the form
resp = app.request("/hello" , method="POST")
assert_response(resp , contains="Nobody")
#test that we get expected values
data = {'name':'Tejas','greet':'Ola!'}
resp = app.request("/hello " , method= "POST", data=data)
assert_response(resp , contains="Zed")
App.py
import web
from gothonweb import map
urls = (
'/game' , 'GameEngine' ,
'/' , 'Index',
)
app = web.application(urls, globals())
#little hack so that debug mode works with sessions
if web.config.get('_session') is None:
store= web.session.DiskStore('sessions')
session= web.session.Session(app, store, initializer={'room':None})
web.config._session = session
else:
session= web.config._session
render = web.template.render('templates/', base="layout")
class Index(object):
def GET(self):
#this is used to "setup" the session with starting values
session.room= map.START
web.seeother("/game")
class GameEngine(object):
def GET(self):
if session.room:
return render.show_room(room= session.room)
else:
#why is there here? do you need it?
return render.you_died()
def POST(self):
form= web.input(action=None)
if session.room and form.action:
session.room= session.room.go(form.action)
else:
session.room= None
if __name__ == "__main__" :
app.run()
Can you see, how they are not related
Your first test says you should get a 404 on the '/' url but that would only happen if '/' didn't exist. Your app.py code clearly shows that '/' calls on GET in the Index class.
Second test, resp = app.request("/hello") now that would give you a 404 error because that url doesn't exist on app.py
The same goes for the third test as '/hello' doesn't exist in your tuple of urls in app.py
And the fifth test as well
Although your tests are invalid, the primary problem you have though is trying to perform the automated test from inside the tests directory; that's wrong.
C:\lpthw\gothonweb\tests> nosetests
This is not the right thing to do, you need to be a directory below for all your imports to work e.g In app_tests you are trying to import bin/app.py but that's not in the 'tests' directory
Do this instead
C:\lpthw\gothonweb> nosetests
This way all the files that you need would be imported
Back to app_tests.py. I'll write out a very simple test that's actually related to app.py
from bin.app import app
from test.tools import assert_response
def test_index():
#check that we get a 303 on the / url
#this is because web.seeother() will always send the browser this http code
resp = app.request("/")
assert_response(resp, status= "303")
def test_game():
#check that we get a 200 on /game
resp = app.request("/game")
assert_response(resp, status = '200')
#check that we have response data on a form sumbit
resp = app.request("/game", method = 'POST')
aseert_response(resp.data)
The assertion_error you getting because your ran nosetests in this directory
/gothroweb/test
you should run it in
/gothroweb
bin docs gothonweb templates tests
./bin:
app.py app.pyc __init__.py __init__.pyc
./docs:
./gothonweb:
__init__.py __init__.pyc
./templates:
hello_form.html index.html layout.html
./tests:
app_tests.py __init__.py test.html tools.pyc
app_tests.pyc __init__.pyc tools.py
cheers

Exporting data from App Engine datastore as a Google Drive spreadsheet

I have an app engine application on which I mark my monthly expenses along with some comments or reason. I would like to export these data into a
Google Drive Spreadsheet. I use Django framework.
I had gone through the tutorials provided by Google here.
But they have implemented it using webapp2 and jinja. Moreover, the doc for Implementing Using Django seems way too obsolete since I do not use Django ORM.
Below is my code sample which I use to upload. I strongly apologize if what I paste below is rubbish. Please help.
from django.utils.datastructures import SortedDict
import os
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.appengine import OAuth2DecoratorFromClientSecrets
decorator = OAuth2DecoratorFromClientSecrets(os.path.join(os.path.dirname(__file__ ), 'clientSecrets.json'), 'https://www.googleapis.com/auth/drive')
drive_service = build('drive', 'v2')
class Exporter(object):
serializedObjects = []
mime_type = 'text/plain'
fileToExport = None
request = None
def __init__(self, serializedObjects, request):
self.serializedObjects = serializedObjects
self.request = request
def createCSV(self):
import csv
import StringIO
stdout = StringIO.StringIO()
writer = csv.writer(stdout)
for obj in self.serializedObjects:
for value in obj.values():
writer.writerow([value])
# I will get the csv produced from my datastore objects here.
# I would like to upload this into a Google Spreadsheet.
# The child class ExportToSpreadSheet tries to do this.
return stdout.getvalue()
class ExportToSpreadSheet(Exporter):
def __init__(self, *args, **kwargs):
super(ExportToSpreadSheet, self).__init__(*args, **kwargs)
self.mime_type = 'application/vnd.google-apps.spreadsheet'
def create(self):
import datetime
valueToDrive = self.createCSV()
media_body = MediaFileUpload(valueToDrive, mimetype=self.mime_type, resumable=True)
body = {
'title' : 'MyExpense_%s' % datetime.datetime.now().strftime('%d_%b_%Y_%H_%M_%S'),
'description' : '',
'mimeType' : self.mime_type
}
self.fileToExport = drive_service.files().insert(body=body, media_body=media_body, convert=True)
return self.fileToExport
#decorator.oauth_aware
def upload(self):
if decorator.has_credentials():
self.create()
self.fileToExport.execute(decorator.http())
return self.fileToExport
raise Exception('user does not have the credentials to upload to google drive.')
#decorator.oauth_aware only works on webapp.RequestHandler subclasses. Why I am saying it is because I got this error while I ran the code.
INFO 2013-09-19 11:28:04,550 discovery.py:190] URL being requested: https://www.googleapis.com/discovery/v1/apis/drive/v2/rest?userIp=%3A%3A1
ERROR 2013-09-19 11:28:05,670 main.py:13] Exception in request:
Traceback (most recent call last):
File "/home/dev5/divya/jk/MyApp/ItsMyTuition/SDK/google_appengine/lib/django-1.2/django/core/handlers/base.py", line 100, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/dev5/divya/jk/MyApp/ItsMyTuition/ItsMyTuition/src/tuition/json/ajaxHandler.py", line 27, in mainHandler
responseValues = funtionToCall(*args)
File "/home/dev5/divya/jk/MyApp/ItsMyTuition/ItsMyTuition/src/tuition/json/ajaxHandler.py", line 69, in export
uploadedFile = exporterInstance.upload()
File "/home/dev5/divya/jk/MyApp/ItsMyTuition/ItsMyTuition/src/oauth2client/appengine.py", line 770, in setup_oauth
self._create_flow(request_handler)
File "/home/dev5/divya/jk/MyApp/ItsMyTuition/ItsMyTuition/src/oauth2client/appengine.py", line 734, in _create_flow
redirect_uri = request_handler.request.relative_url(
AttributeError: 'ExportToSpreadSheet' object has no attribute 'request'
INFO 2013-09-19 11:28:05,777 module.py:593] default: "POST /ajaxCall/export HTTP/1.1" 200 964
Since I am using Django framework I cannot get a Request handler as they except.
How can I integrate or do it in my scenario? I would very much appreciate any code samples or relevant links I may have missed.
Moreover, the whole thing happens in an ajax call.
Thanks in advance.
Use mimeType=text/csv and during the upload, request a conversion from csv to Spreadsheets:
drive_service.files().insert(covert=True, body=body, media_body=media_body, convert=True)