Django database populate not working correctly - python

I'm building a script to populate a db with info from a json but when It finishes the only saved objects is the last. What am I doing wrong? Here is my code:
def setup_environment():
pathname = os.path.dirname(sys.argv[0])
sys.path.append(os.path.abspath(pathname))
sys.path.append(os.path.normpath(os.path.join(os.path.abspath(pathname), '../')))
os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings'
setup_environment()
import django
django.setup()
from stats.models import Champion, Item #import models for django
import json
with open("champions.json", "r") as fjson:
data = json.load(fjson)
print len(data)
for i in data:
#print data[i]["name"], data[i]["key"]
champ = Champion.objects.get(key = data[i]["key"])
champ.global_kda = data[i]["global_kda"]
champ.kills = data[i]["kills"]
champ.assists = data[i]["assists"]
champ.deaths = data[i]["deaths"]
champ.save()
Link to all the code:
https://gist.github.com/zanklord/bc667dab9864af951446

Actually I don't know what I really did because I did nothing. I modified the file in gist and copied the same in my computer. Before it was right indentent as #help_asap suggested. Anyway thanks to #help_asap

Related

Python/Django: Get AttributeError when rendering html to pdf using xhtml2pdf

Here you can see the errors I get.
I am trying to render a pdf from a html template using Django.
This worked pretty well locally when I was fiddling about with it.
Managed to get a .pdf file from it.
Now I'm stuck with this error claiming that the uri has no attribute encode.
Has anyone had a similar error while using this python package? If so, how did you manage to fix it?
from __future__ import print_function
import easy_pdf
from deliveries.models import Delivery
from django.conf import settings
from easy_pdf.views import PDFTemplateView
import os
import sys
from django.core.wsgi import get_wsgi_application
from scripts import messages as msg
basename = os.path.splitext(os.path.basename(__file__))[0]
def run(*args, **kwargs):
if len(args) < 1 or args[0] is None:
print(msg.GeneralMessages.ERROR_DELIVERY_MISSING, file=sys.stderr)
sys.exit(1)
if not str(args[0]).isdigit():
logger.error(msg.GeneralMessages.ERROR_DELIVERY_INVALID)
sys.exit(1)
delivery = Delivery.get(args[0])
application = get_wsgi_application()
context = ''
view = ''
encoding = 'utf-8'
print(context)
view = easy_pdf.rendering.render_to_pdf('report.html', context)
if view:
f = open('report.pdf', 'wb')
f.write(view)
f.close()
print ('Report has been exported as .pdf')
if not settings.configured:
settings.configure(
DEBUG=True,
TIMEZONE="UTC",
INSTALLED_APPS=["easy_pdf", "django.contrib.staticfiles"],
TEMPLATE_DIRS=["../../deliveries/templates"],
ROOT_URLCONF=basename,
WSGI_APPLICATION="{}.application".format(basename),
STATIC_URL='/deliveries/static'
)

bottle.py render static file

I'm building a bottle.py app that grabs some data from MongoDB and renders it into a web page using pygal.
The code produces a Error: 500 Internal Server Error in my browser.
On the server, I see: Exception: TypeError('serve_static() takes exactly 1 argument (0 given)',).
My question: how do I correct the code to render the .svg file?
The code:
import sys
import bottle
from bottle import get, post, request, route, run, static_file
import pymongo
import json
import pygal
connection = pymongo.MongoClient("mongodb://localhost", safe=True)
#get('/chart')
def serve_static(chart):
db = connection.control
chart = db.chart
cursor = chart.find({}, {"num":1, "x":1, "_id":0})
data = []
for doc in cursor:
data.append(doc)
list = [int(i.get('x')) for i in data]
line = pygal.Line()
line.title = 'widget quality'
line.x_labels = map(str, range(1, 20))
line.add('quality measure', list)
line.render_to_file('chart.svg')
try:
return static_file(chart.svg, root='/home/johnk/Desktop/chart/',mimetype='image/svg+xml')
except:
return "<p>Yikes! Somethin' wrong!</p>"
bottle.debug(True)
bottle.run(host='localhost', port=8080)
You didn't give a parameter to the route, so the function doesn't get any.
What you probably want to do, is either:
#get('/<chart>')
def serve_static(chart):
...
If you want /myfile.svg to work, or:
#get('/chart/<chart>')
def serve_static(chart):
...
If you want /chart/myfile.svg to work.
If you just want to show the same SVG file every time, you can just leave off the parameter:
#get('/chart')
def serve_static():
...

deadline = None after using urlfetch.set_default_fetch_deadline(n)

I'm working on a web application with Python and Google App Engine.
I tried to set the default URLFetch deadline globally as suggested in a previous thread:
https://stackoverflow.com/a/14698687/2653179
urlfetch.set_default_fetch_deadline(45)
However it doesn't work - When I print its value in one of the functions: urlfetch.get_default_fetch_deadline() is None.
Here is main.py:
from google.appengine.api import users
import webapp2
import jinja2
import random
import string
import hashlib
import CQutils
import time
import os
import httpRequests
import logging
from google.appengine.api import urlfetch
urlfetch.set_default_fetch_deadline(45)
...
class Del(webapp2.RequestHandler):
def get(self):
id = self.request.get('id')
ext = self.request.get('ext')
user_id = httpRequests.advance(id,ext)
d2 = urlfetch.get_default_fetch_deadline()
logging.debug("value of deadline = %s", d2)
Prints in the Log console:
DEBUG 2013-09-05 07:38:21,654 main.py:427] value of deadline = None
The function which is being called in httpRequests.py:
def advance(id, ext=None):
url = "http://localhost:8080/api/" + id + "/advance"
if ext is None:
ext = ""
params = urllib.urlencode({'ext': ext})
result = urlfetch.fetch(url=url,
payload=params,
method=urlfetch.POST,
headers={'Content-Type': 'application/x-www-form-urlencoded'})
if (result.status_code == 200):
return result.content
I know this is an old question, but recently ran into the issue.
The setting is placed into a thread-local, meaning that if your application is set to thread-safe and you handle a request in a different thread than the one you set the default deadline for, it can be lost. For me, the solution was to set the deadline before every request as part of the middleware chain.
This is not documented, and required looking through the source to figure it out.

to insert the values in database table without using interactive python shell in django

My app name is search_keywords. As on creating this app, I wiil have one file called models.py
in which I have written this piece if code :
from django.db import models
class Keywords(models.Model):
file_name = models.CharField(primary_key=True, max_length=100)
frequency_count = models.IntegerField()
then add this app to INSTALLED_APPS and run python manage.py syncdb. On running this command , I will get a table automatically created in django. Then run python manage.py sql search_keywords. It will show the table as desired.
Then the next step is to run python manage.py shell.Instead of running this I want to insert the values in my table created through the python code that I have created.The code is:
#!/usr/bin/python
#here skey.py is another file created by me and has the imported functions
#in this code
from skey import find_root_tags, count, sorting_list
str1 = raw_input("enter the word to be searched\n")
list = []
fo = open("xml.txt","r")
for i in range(count.__len__()):
file = fo.readline()
file = file.rstrip('\n')
find_root_tags(file,str1,i)
list.append((file,count[i]))
sorting_list(list)
fo.close()
I want to insert this list elements in the the table created by the django and that too after the function sorting_list has been called. The list contains the file name and it's count. e.g. list=[('books.xml','3'),('news.xml,'2')].
How can I do that?
Please Help.
//////////////////////////////////////////////////////////////////////////////
Hey I have written the code:
#!/usr/bin/python
#to tell django which settings module to use
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from search.models import Keywords
from skey import find_root_tags, count, sorting_list
str1 = raw_input("enter the word to be searched\n")
list = []
fo = open("xml.txt","r")
for i in range(count.__len__()):
file = fo.readline()
file = file.rstrip('\n')
find_root_tags(file,str1,i)
list.append((file,count[i]))
sorting_list(list)
for name, count in list:
s = Keywords(file_name=name,frequency_count=count)
s.save()
fo.close()
Here django_project = mysite #my project's name
and app = search #my app's name
on running this code it gives me error :
Traceback (most recent call last):
File "call.py", line 7, in
from search.models import Keywords
ImportError: No module named search.models
and on including :
import sys
sys.path.insert(0, path_to_django_project)
this in above code it gives error:
Traceback (most recent call last):
File "call.py", line 4, in
sys.path.insert(0,path_to_mysite)
NameError: name 'path_to_mysite' is not defined
Why?I have my project on the desktop and the above python code file as well.
Please Help!!
//////////////////////////////////////////
now it's giving me this error , please help.see it at :
error in accessing table created in django in the python code
It shouldn't be a probjem just importing your models and create object instances to persist to the database:
# setup sys.path first if needed
import sys
sys.path.insert(0, path_to_django_project)
# tell django which settings module to use
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'
# import application models
import test_app.models as m
# create and save db entries...
o = m.TestObject()
o.var = 'spam'
o.par = 1
o.save()
For each element in your list, create an instance of Keywords_Search. After that, call .save() on each of them. For example:
for name, count in mylist:
s = Keywords_Search(file_name=name, frequency_count=count)
s.save()
First, there are few problems with your code (for example, you are calling methods that are built-ins, not to mention count.__len__()). Instead of trying to figure out what you are doing, here are the two recommended way to do this in django:
Django provides the functionality to provide initial data for models in SQL, XML, YAML or JSON. This allows for automatic data insertion everytime you run syncdb. You don't have to run a command again.
Create a custom management command and run it with manage.py
I'm assuming you have a file of keywords, and you want to search how many of the entered keyword is in the file.
Here is how you would create a custom management command that does this:
from collections import Counter
from django.core.management.base import BaseCommand, CommandError
from search.models import Keywords
class Command(BaseCommand):
args = 'search_string'
help = 'Enter the search string'
def handle(self, *args, **options):
file_contents = []
with open('keyword_master.txt') as f:
file_contents = [l for l in f.readlines() if l.rstrip('\n')]
if not file_contents:
raise CommandError("Cannot read keyword_master.txt")
c = Counter(file_contents)
for search_string in args:
freq = c[search_string]
Keywords.object.create(keyword=search_string,frequency_count=freq)
self.stdout.write('Added %s freq for "%s"' % (freq,search_string))
Create a folder called commands inside any app. Inside this folder, create a blank file called __init__.py. In the same folder, save this file as "keyword_importer.py". So now you have:
/search
..__init__.py
../commands
.....__init__.py
.....keyword_importer.py
.....keyword_master.txt
Now run it with python manage.py keyword_importer mykeyword

How do you IXR_Base64 in python?

What I'm trying to do is upload a picture to wordpress using wp.uploadFile xmlrpc method.
To do this, in PHP there is an example here: https://stackoverflow.com/a/8910496/1212382
I'm trying to do the same thing in python but I don't know how.
Anyone any ideas?
ok, the answer lies in the xmlrpclib class.
To send base64 bits to wordpress from python you need to use the xmlrpclib class like so:
base64bits = xmlrpclib.Binary(file_content)
then you just add the base64bits variable to the 'bits' parameter in your wp.uploadFile xmlrpc request.
to be a little more exact, here's the complete code in python of how this should be done:
import xmlrpclib
import urllib2
from datetime import date
import time
def get_url_content(url):
try:
content = urllib2.urlopen(url)
return content.read()
except:
print 'error! NOOOOOO!!!'
file_url = 'http://the path to your picture'
extension = file_url.split(".")
leng = extension.__len__()
extension = extension[leng-1]
if (extension=='jpg'):
xfileType = 'image/jpeg'
elif(extension=='png'):
xfileType='image/png'
elif(extension=='bmp'):
xfileType = 'image/bmp'
file = get_url_content(file_url)
file = xmlrpclib.Binary(file)
server = xmlrpclib.Server('http://website.com/xmlrpc.php')
filename = str(date.today())+str(time.strftime('%H:%M:%S'))
mediarray = {'name':filename+'.'+extension,
'type':xfileType,
'bits':file,
'overwrite':'false'}
xarr = ['1', 'USERHERE', 'PASSWORDHERE', mediarray]
result = server.wp.uploadFile(xarr)
print result

Categories