I am trying to create a script that populates a database with test users. I am new to Django and Python. I keep on getting:
Runtime error: App registry isn't ready yet.
Here is the output and error:
starting population script
Traceback (most recent call last):
File "populate.py", line 32, in <module>
populate()
File "populate.py", line 22, in populate
i.save()
File "c:\Python27\lib\site-packages\django-1.7a2-py2.7.egg\django\db\models\base.py", line 603, in save
force_update=force_update, update_fields=update_fields)
...
...
...
File "c:\Python27\lib\site-packages\django-1.7a2-py2.7.egg\django\apps\registry.py", line 156, in get_models
self.check_ready()
File "c:\Python27\lib\site-packages\django-1.7a2-py2.7.egg\django\apps\registry.py", line 119, in check_ready
raise RuntimeError("App registry isn't ready yet.")
RuntimeError: App registry isn't ready yet.
Here is the code:
import os
import datetime
def populate():
freer = User.objects.create_user( 'joyyie', 'lolcats#gmail.com', 'e')
cat = User.objects.create_user( 'steve', 'l2olcats#gmail.com', 'e')
dog = User.objects.create_user( 'aasd', 'lo3lcats#gmail.com', 'ad')
cow = User.objects.create_user( 'sadsfa', 'lol4cats#gmail.com', 't' )
pig = User.objects.create_user( 'regibald', 'lolc5ats#gmail.com', '0')
donkey = User.objects.create_user( 'turnip', 'lolca6ts#gmail.com', 'pop')
human = User.objects.create_user( 'tutu', 'lolcat7s#gmail.com', 'pa')
a = [freer,cat,dog,cow,pig,donkey,human]
for i in a:
i.first_name= 'jackee'
i.is_superuser=True
i.is_staff=False
i.date_joined=datetime.datetime.today()
i.last_login=datetime.datetime.today()
i.save()
if __name__=='__main__':
print "starting population script"
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'infosmos.settings')
from django.conf import settings
from django.db import models
from django.contrib.auth.models import User
populate()
Is there a way to force the user profile creation to wait for the registry app by using a signal or something?
[django 1.7] The Standalone script you wish to run, import django and settings like below
import os
import django
[...]
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()
This is a known and intended behaviour according to Django's 1.7 release notes, under the "startup sequence" clause:
Another common culprit is django.contrib.auth.get_user_model(). Use the AUTH_USER_MODEL setting to reference the User model at import time.
and that should do the trick
for reference:
https://docs.djangoproject.com/en/dev/releases/1.7/#app-loading-changes
I found out that if I run populate through the manage.py shell with the execfile() command then it runs properly. Everything needed to be setup before I start modifying the database or run outside code. Thanks to lanzz for the hint.
Related
I am currently trying to build an api using connexion. However, I am having some issues using relative local module imports through the connexion module, which modifies the underlying flask app. Here is a simplified overview of my file structure:
hall_of_fame_api
controller
____init____.py
routes.py
model
____init____.py
routes.py
____init____.py
config.py
create_db.py
swagger.yml
I am getting an error when I try to run 'python config.py' in my terminal. Here is config.py:
import os
import connexion
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
basedir = os.path.abspath(os.path.dirname(__file__))
# Create the Connexion application instance
connex_app = connexion.App(__name__, specification_dir=basedir)
# Get the underlying Flask app instance
app = connex_app.app
connex_app.add_api('swagger.yml')
# Configure the SQLAlchemy part of the app instance
app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://doadmin:password#nba-player-db-do-user-7027314-0.db.ondigitalocean.com:25060/nba_test_1?sslmode=require'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Create the SQLAlchemy db instance
db = SQLAlchemy(app)
# Initialize Marshmallow
ma = Marshmallow(app)
And here is the error it gives:
Failed to add operation for GET /api/players
Failed to add operation for GET /api/players
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/connexion/apis/abstract.py", line 209, in add_paths
self.add_operation(path, method)
File "/usr/local/lib/python3.7/site-packages/connexion/apis/abstract.py", line 173, in add_operation
pass_context_arg_name=self.pass_context_arg_name
File "/usr/local/lib/python3.7/site-packages/connexion/operations/__init__.py", line 8, in make_operation
return spec.operation_cls.from_spec(spec, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/connexion/operations/swagger2.py", line 137, in from_spec
**kwargs
File "/usr/local/lib/python3.7/site-packages/connexion/operations/swagger2.py", line 96, in __init__
pass_context_arg_name=pass_context_arg_name
File "/usr/local/lib/python3.7/site-packages/connexion/operations/abstract.py", line 96, in __init__
self._resolution = resolver.resolve(self)
File "/usr/local/lib/python3.7/site-packages/connexion/resolver.py", line 40, in resolve
return Resolution(self.resolve_function_from_operation_id(operation_id), operation_id)
File "/usr/local/lib/python3.7/site-packages/connexion/resolver.py", line 66, in resolve_function_from_operation_id
raise ResolverError(str(e), sys.exc_info())
connexion.exceptions.ResolverError: <ResolverError: module 'controller.routes' has no attribute 'read_all'>
This error comes specifically from line 12 where connexion is trying to add the swagger.yml file, here is that for reference as well:
swagger: "2.0"
info:
description: This is the swagger file that goes with our server code
version: "1.0.0"
title: Swagger REST Article
consumes:
- "application/json"
produces:
- "application/json"
basePath: "/api"
# Paths supported by the server application
paths:
/players:
get:
operationId: "controller.routes.read_all"
tags:
- "People"
summary: "The people data structure supported by the server application"
description: "Read the list of people"
responses:
200:
description: "Successful read people list operation"
schema:
type: "array"
items:
properties:
fname:
type: "string"
lname:
type: "string"
timestamp:
type: "string"
Now here is where I am confused, because my routes.py file does have a function defined as read_all(), here is that file:
from model.models import Regseason, RegSchema, Playoffs, PlayoffSchema
def read_all():
return Regseason.query.all()
I have been racking my brain over this bug for almost 24 hours, any guidance would be greatly appreciated. Thanks in advance!
Please add a extra field x-openapi-router-controller below operationid. It is used by Connexion to map which module to send requests to. It is combined together with OperationId to go to correct module and function.
I have a python script that is being run on some Macs by my MDM tool. This means that the script is being run as root. There is a part of the script I need to run as the currently logged in local user on the account. I found this article below for using Popen to do this:
Run child processes as a different user from a long-running process
However, I am getting an error when I attempt to use this method on any pre macOS 10.13 computers. These are still modern OS versions such as 10.12 and 10.11. I have not been able to track this error down. Please see the code below.
Note: There are likely some extra import statements as this is pulled from a larger script. This snippet should work as-is.
#!/usr/bin/python
import subprocess
import platform
import os
import pwd
import sys
import hashlib
import plistlib
import time
from SystemConfiguration import SCDynamicStoreCopyConsoleUser
from distutils.version import StrictVersion as SV
def getLoggedInUserUID():
userUID = SCDynamicStoreCopyConsoleUser(None, None, None)[1]
return userUID
def getLoggedInUsername():
username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]
return username
def getLoggedInUserGID():
username = getLoggedInUsername()
pwRecord = pwd.getpwnam(username)
userGID = pwRecord.pw_gid
return userGID
def getLoggedInUserHomeDir():
username = getLoggedInUsername()
pwRecord = pwd.getpwnam(username)
homeDir = pwRecord.pw_dir
return homeDir
def demote():
def result():
os.setgid(getLoggedInUserGID())
os.setuid(getLoggedInUserUID())
return result
def setupEnvironment():
environment = os.environ.copy()
environment['HOME'] = str(getLoggedInUserHomeDir())
environment['LOGNAME'] = str(getLoggedInUsername())
environment['PWD'] = str(getLoggedInUserHomeDir())
environment['USER'] = str(getLoggedInUsername())
return environment
def launchCommand():
command = ['echo', 'whoami']
process = subprocess.Popen(command,
stdout=subprocess.PIPE,
preexec_fn=demote(),
cwd=str(getLoggedInUserHomeDir()),
env=setupEnvironment())
def main():
launchCommand()
if __name__== "__main__":
main()
The error that I get is:
Traceback (most recent call last):
File "testScript.py", line 60, in <module>
main()
File "testScript.py", line 57, in main
launchCommand()
File "testScript.py", line 54, in launchCommand
env=setupEnvironment())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
KeyError: 'getpwnam(): name not found: '
It looks like it is missing some key value but I cannot for the like of me figure out what it is. Any help in tracking this down so I can run the command as the logged in user would help greatly.
Thanks in Advance,
Ed
The way that I did in my small mdm managed environment is like this:
I developed small windowless LoginItem helperApp that starts for every open user session on the mac and listens for custom distributed system notification. It also has a function for executing terminal commands without showing terminal window (You can find examples for this on stackowerflow).
I transmit to all apps currently running on the system two params in the notification: an username and a terminal command string. All of the users running instances get the notification, than they check the username for if they run in that user and the one that does - executes the command in that users name.
Try this if it fits your requirement.
Currently I am working on jython script that can execute an external jython script. The executed (external) jython script must run in the same weblogic session where my script runs in order to be able to cancel the session (in case of error) or activate it. The weblogic connection is being created in my script and the called script has to use the created (with ‘startEdit()’) session.
I found a hybrid solution but perhaps it can be done better.
The executed script in the working solution:
import wlst_operations as wl
print 'Start'
wl.cd('/')
print wl.cmo
wl.cd('/Servers/AdminServer')
print wl.cmo
wl.cd('/JDBCSystemResources/pasDataSource/JDBCResource/pasDataSource/JDBCConnectionPoolPara ms/pasDataSource')
print wl.cmo
wl.cmo.setInitialCapacity(6)
The wlst_operations jython was taken from http://www.javamonamour.org/2013/08/wlst-nameerror-cd.html.
As you can see an object like reference (‘wl.’) must be put in front of each WLST command…
The output is fine:
[MBeanServerInvocationHandler]com.bea:Name=gintdev1,Type=Domain
[MBeanServerInvocationHandler]com.bea:Name=AdminServer,Type=Server
[MBeanServerInvocationHandler]com.bea:Name=pasDataSource,Type=weblogic.j2ee.descriptor.wl.JDBCConnectionPoolParamsBean,Parent=[gintdev1]/JDBCSystemResources[pasDataSource],Path=JDBCResource[pasDataSource]/JDBCConnectionPoolParams
When I don’t use the object reference:
from wlstModule import *
print 'user defined'
cd('/')
print cmo
cd('/Servers/AdminServer')
print cmo
cd('/JDBCSystemResources/pasDataSource/JDBCResource/pasDataSource/JDBCConnectionPoolParams/pasDataSource')
print cmo
cmo.setInitialCapacity(6)
Then the output is:
[MBeanServerInvocationHandler]com.bea:Name=gintdev1,Type=Domain
[MBeanServerInvocationHandler]com.bea:Name=gintdev1,Type=Domain
[MBeanServerInvocationHandler]com.bea:Name=gintdev1,Type=Domain
Problem invoking WLST - Traceback (innermost last):
File "/tmp/lv30083/./orchestrator.py", line 83, in ?
File "/tmp/lv30083/./orchestrator.py", line 66, in main
File "/tmp/lv30083/./utils/orch_wl.py", line 55, in execute
File "user_defined_script_incorrect.py", line 11, in ?
AttributeError: setInitialCapacity
i.e. the cd commands are executed (not getting error) but it just doesn’t jump to the datasource…
My script is
import orch_logging
import sys
from wlstModule import *
class WeblogicManager(object):
def connect_to_server(self, p_ssl, p_domainName, p_userConfigFile, p_userKeyFile):
logger = orch_logging.Logger()
logger.info('Trying to connect to the node manager. domainName='+p_domainName+',userConfigFile='+p_userConfigFile+',ssl='+p_ssl+',p_userKeyFile='+p_userKeyFile)
try:
connect(domainName=p_domainName,userConfigFile=p_userConfigFile,userKeyFile=p_userKeyFile)
return True
except:
logger.error("Error while trying to connect to node manager!")
return False
def startEdit(self):
edit()
startEdit()
def activate(self):
activate()
def undo(self):
cancelEdit('y')
def disconnect(self):
disconnect()
def execute(self, path):
execfile(path)
Is there any way to use the WLST commands without using the 'wl.' reference in front of them?
Thanks,
V.
I had to modify my script. All the operations are now in one context (in the context of my script)
import sys
from wlstModule import *
from weblogic.management.scripting.utils import WLSTUtil
import sys
# needed to execute normal (without object reference) WLST commands in child scripts
origPrompt = sys.ps1
theInterpreter = WLSTUtil.ensureInterpreter();
WLSTUtil.ensureWLCtx(theInterpreter)
execfile(WLSTUtil.getWLSTScriptPath())
execfile(WLSTUtil.getOfflineWLSTScriptPath())
exec(WLSTUtil.getOfflineWLSTScriptForModule())
execfile(WLSTUtil.getWLSTCommonModulePath())
theInterpreter = None
sys.ps1 = origPrompt
modules = WLSTUtil.getWLSTModules()
for mods in modules:
execfile(mods.getAbsolutePath())
wlstPrompt = "false"
class WeblogicManager(object):
...
def execute(self, path):
execfile(path)
I'm trying to find a way how to test thumbnail generation when using Django and sorl-thumbnail's get_thumbnail() method.
Environment:
Django==1.5.5
Pillow==2.1.0
sorl-thumbnail==11.12
Simplified code under test (ran in test environment):
from StringIO import StringIO
from PIL import Image
from django.conf import settings
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.db import models
from sorl.thumbnail import get_thumbnail
# simple class with ImageField
class User(models.Model):
avatar = models.ImageField(upload_to='avatars', default=None, null=True, blank=True)
def get_thumbnail_uri(self):
avatar_thumbnail = get_thumbnail(self.avatar, '100x100')
return avatar_thumbnail.url
# make sure we're using in-memory test env.
assert settings.THUMBNAIL_STORAGE == 'inmemorystorage.InMemoryStorage'
assert settings.DEFAULT_FILE_STORAGE == 'inmemorystorage.InMemoryStorage'
# prepare image
fake_file = StringIO()
picture = Image.new(mode='RGBA', size=(500, 500), color=(255, 0, 0, 0))
picture.save(fake_file, 'JPEG')
fake_file.name = 'test.jpg'
fake_file.seek(0)
uploaded_image = InMemoryUploadedFile(fake_file, field_name=None, name='test.jpg',
content_type='image/jpeg', size=fake_file.len,
charset=None)
# add image to user
u = User()
u.avatar = uploaded_image
assert u.get_thumbnail_uri() is not None
The above always fails on the last line:
Traceback (most recent call last):
File "/vagrant/path/to/file.py", line 1440, in test_stackprep
assert u.get_thumbnail_uri() is not None
File "/vagrant/path/to/file.py", line 1413, in get_thumbnail_uri
avatar_thumbnail = get_thumbnail(self.avatar, '100x100')
File "/home/vagrant/.virtualenv/appname/local/lib/python2.7/site-packages/sorl/thumbnail/shortcuts.py", line 8, in get_thumbnail
return default.backend.get_thumbnail(file_, geometry_string, **options)
File "/home/vagrant/.virtualenv/appname/local/lib/python2.7/site-packages/sorl/thumbnail/base.py", line 56, in get_thumbnail
source_image = default.engine.get_image(source)
File "/home/vagrant/.virtualenv/appname/local/lib/python2.7/site-packages/sorl/thumbnail/engines/pil_engine.py", line 13, in get_image
return Image.open(buf)
File "/home/vagrant/.virtualenv/appname/local/lib/python2.7/site-packages/PIL/Image.py", line 2008, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
I assume that either Django or sorl-thumbnail gets out of inmemorystorage while running the test. I've been at it for a long time, but I failed to find any configuration that works, with the exception of testing stuff directly on the filesystem (which I'd like to avoid).
Did anyone manage to get sorl's get_thumbnail() method working in tests, please?
Thanks.
Version 11.12, which is the latest one on PyPI today, is 2 years old. There's version 12.0 available in repo, which should work. It's not on PyPI, because there was change of maintainers and I guess they hadn't chance to do it yet.
I've tried master version and it works ok with your example.
from django.db import models
from djangosphinx.models import SphinxSearch
class MyModel(models.Model):
search = SphinxSearch() # optional: defaults to db_table
# If your index name does not match MyModel._meta.db_table
# Note: You can only generate automatic configurations from the ./manage.py script
# if your index name matches.
search = SphinxSearch('index_name')
# Or maybe we want to be more.. specific
searchdelta = SphinxSearch(
index='index_name delta_name',
weights={
'name': 100,
'description': 10,
'tags': 80,
},
mode='SPH_MATCH_ALL',
rankmode='SPH_RANK_NONE',
)
queryset = MyModel.search.query('query')
results1 = queryset.order_by('#weight', '#id', 'my_attribute')
results2 = queryset.filter(my_attribute=5)
results3 = queryset.filter(my_other_attribute=[5, 3,4])
results4 = queryset.exclude(my_attribute=5)[0:10]
results5 = queryset.count()
# as of 2.0 you can now access an attribute to get the weight and similar arguments
for result in results1:
print result, result._sphinx
# you can also access a similar set of meta data on the queryset itself (once it's been sliced or executed in any way)
print results1._sphinx
and
Traceback (most recent call last):
File "D:\zjm_code\sphinx_test\models.py", line 1, in <module>
from django.db import models
File "D:\Python25\Lib\site-packages\django\db\__init__.py", line 10, in <module>
if not settings.DATABASE_ENGINE:
File "D:\Python25\Lib\site-packages\django\utils\functional.py", line 269, in __getattr__
self._setup()
File "D:\Python25\Lib\site-packages\django\conf\__init__.py", line 38, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
I know what the problem is :)
You are trying to run this script as stand-alone. But since you use django models. all of them have to be imported in your namespace. This is what is the error. It clearly days ImportError.
To solve - go to your django-project directory & then type python manage.py shell. This imports all Django-env files. Now import your models & try out the search.
The error is about the environment. Django cannot find your settings file; Are you doing
python manage.py runserver
itself, or something else?