Django: Import from project root - django

I was searching for an answer but could not get the solution for this exact case:
So, my project structure is like this
project/
myproject/
...
app/
...
views.py
...
logic/
do_stuff.py
db.sqlite3
manage.py
And this do_stuff.py contains a function call_me()
How can I call call_me() function from views.py (For example, upon a successful file upload)?
Many thanks!
Update:
I'm able to import a do_stuff() function from a single logic.py file from project/ like this: from .logic import do_stuff
But the problem is that the logic package is already written and has its folder structure.
I just thought of installing this package via settings.py, is there any way to do so?

Normally from project.logic.do_stuff import call_me should work in views.py. Have you tried?

Related

Python packaging can't import class handler

I have two packages (diretories ) in my Python project
src
/textmining
mining.py...def mining():#...
__init.py....__all__ = ["mining"]
/crawler
crawler.py
in crawler.py I use the mining class
mining=mining()
main.py
__init__.py
my main.py is as follow:
scrapy_command = 'scrapy runspider {spider_name} -a crawling_level="{param_1}"'.format(spider_name='crawler/crawler.py',
param_1=crawling_level)
process = subprocess.Popen(scrapy_command, shell=True)
when I run crawler, it prompts
runspider: error: Unable to load 'Crawler.py': cannot import name mining
You need an __init__.py in every folder that's a part of the same package module.
src
__init__.py
/textmining
__init__.py
mining.py
/crawler
__init__.py
crawler.py
For simplicity, you should add a main.py in the src folder and call the function you want to start your program with from there as it's fairly difficult to import modules from sibling directories if you start your script in a non-root directory.
main.py
from crawler import crawler
crawler.start_function()
crawler.py
from src.textmining import mining
miner = mining()
Without turning everything into a python module you'd have to import a folder into the current script or __init__.py module by adding to path:
# In crawler.py
import sys
import os
sys.path.append(os.path.abspath('../textmining'))
import mining
However, messing around with the path requires you to keep in mind what you've done and may not be a thing you desire.

How can I store 3rd party package migrations in a Django project?

I'm using the page module in feincms and when I add extensions to my settings I'd like to be able to store the migrations in my git repo.
There is some documentation on migrations in FeinCMS, but it looks incomplete, and I'm not able to get this working.
The file structure I have is:
- app
- feincms_
- migrate
- __init__.py
...
...
settings.py
...
MIGRATION_MODULES = {
'page': 'feincms_.migrate.page',
'medialibrary': 'feincms_.migrate.medialibrary',
}
...
Ok, silly me. I needed a directory with an __init__.py for each module.
i.e.
- app
- feincms_
- migrate
- __init__.py
- page
- __init__.py
- medialibrary
- __init__.py
...
...
So to sum this up, in order to set the location for any module's migrations in Django, you have to specify a valid python path in MIGRATION_MODULES. This path can be anywhere you like, but you have to make sure that it exists.

Scrapy: No module named test_1.items

Im trying from import items from the .items file with from test_1.items import Items i keep getting the "No module named test_1.items" error. My structure is
test_1/
test_1/test_2/
test_1/scrapy.cfg
test_1/test_2/spiders/
test_1/test_2/ __init__.py
test_1/test_2/items.py
test_1/test_2/pipelines.py
test_1/test_2/settings.py
test_1/test_2/spiders/ __init__.py
test_1/test_2/spiders/today_spider.py
im coding in today_spider after i modify the items.py file and getImportError: No module named... this error. As you see i tried to change the manes not to be identical. i also tryied to start today_spider with __future__ import absolute_import. any advise?
thks a lot for your time

Flask: render_template with path

I have several different templates that I'm trying to use for my flask app.
I have tried the following but it seems to only look directly inside /templates and not /templates/folder1, templates/folder2 etc.
return render_template('index.html', template_folder='folder1')
return render_template('folder1/index.html')
both do not work as expected, how can I specify the sub folder of different templates.
The template folder can be specified when creating the Flask app (or Blueprint):
from flask import Flask
app = Flask(__name__, template_folder='folder1')
Source: http://flask.pocoo.org/docs/0.12/api/#application-object
from flask import Blueprint
auth_blueprint = Blueprint('auth', __name__, template_folder='folder1')
Source: http://flask.pocoo.org/docs/0.12/blueprints/#templates
The template_folder is relative to where the app/blueprint is located.
Use the os library to create paths to template folders outside of the app/blueprint directory.
eg.
import os
APP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(APP_PATH, 'templates/')
run from subdirectory of app root
APP_PATH retrieves parent directory path (app root)
Be sure the Python file and Template folder are all under the same working directory folder.
I think Sean is right, but also: have you tried double quotes? I'm using Blueprint, so it may be different, but this is what mine looks like:
return render_template("users/register.html")
so yours may be:
return render_template("folder1/index.html")

How do I ignore static files of a particular app only with collectstatic?

Just to get this out of the way, if at all possible, I'd like to do this without nesting them all inside a directory with the app's name inside the app's static folder, it feels redundant. If it's the only way then such is life.
I am using:
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
and:
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
which compiles the JS and SASS when running collectstatic. These are both located in the APP_ROOT/static/ directory.
The only 'problem' is that it brings along all the source sass and js files alongside it. At some point I'm going to be pushing this all to S3 and I'd like to avoid that if possible.
I found that if you run:
python manage.py collectstatic -i sass -i js
It still compiles the JS and CSS files I specified, while leaving the rest of the 'source' files out. Unfortunately, it also ignores every js file in /admin/ as it matches /admin/js/ etc. I don't even know if that's likely to be a problem for this particular project, but I can foresee in the future other apps where I definitely will want to include static js/css kept in an app.
What I'd like to be able to do is something like:
python manage.py collectstatic -i app_name/sass -i app_name/js
And as I mentioned at the top the easy solution is just to prefix my static files in the folder with app_name/, much like how django.contrib.admin does it. At this point however, you end up with a directory structure of PROJECT_ROOT/app_name/static/app_name/[js|sass|img|data]/ and I think it should be possible to avoid the redundancy.
Then again, maybe it's the best option, so as to guarantee avoiding conflict with other apps?
I've looked into writing custom storages and finders, and I think it's possible to roll my own. I wanted to check here though first, to see if this is a problem someone else has solved, or, to get a reality check if the overwhelming response is to just add the prefix directory.
If I was to roll my own, the path I think I would take would be extending django.contrib.staticfiles.finders.AppDirectoriesFinder and overriding list(). I'm not yet positive this approach would work, I need to more trace how things progress from the collectstatic management command, so if anyone has done this or something simlilar before, or knows why it will/won't work, any help is appreciated.
Thanks!
I managed to solve this by subclassing Django finders like this:
PYTHON 2.X
from django.contrib.staticfiles import finders
from django.conf import settings
def add_ignores(ignore_patterns):
ignore = settings.STATICFILES_FINDERS_IGNORE
if ignore:
if ignore_patterns:
ignore_patterns.extend(ignore)
else:
ignore_patterns = ignore
return ignore_patterns
class FileSystemFinderIgnore(finders.FileSystemFinder):
def list(self, ignore_patterns):
return super(FileSystemFinderIgnore, self).list(add_ignores(ignore_patterns))
class AppDirectoriesFinderIgnore(finders.AppDirectoriesFinder):
def list(self, ignore_patterns):
return super(AppDirectoriesFinderIgnore, self).list(add_ignores(ignore_patterns))
class DefaultStorageFinderIgnore(finders.DefaultStorageFinder):
def list(self, ignore_patterns):
return super(DefaultStorageFinderIgnore, self).list(add_ignores(ignore_patterns))
PYTHON 3.X
from django.contrib.staticfiles import finders
from django.conf import settings
def add_ignores(ignore_patterns):
ignore = settings.STATICFILES_FINDERS_IGNORE
if ignore:
if ignore_patterns:
ignore_patterns.extend(ignore)
else:
ignore_patterns = ignore
return ignore_patterns
class FileSystemFinderIgnore(finders.FileSystemFinder):
def list(self, ignore_patterns):
return super().list(add_ignores(ignore_patterns))
class AppDirectoriesFinderIgnore(finders.AppDirectoriesFinder):
def list(self, ignore_patterns):
return super().list(add_ignores(ignore_patterns))
class DefaultStorageFinderIgnore(finders.DefaultStorageFinder):
def list(self, ignore_patterns):
return super().list(add_ignores(ignore_patterns))
and adding this to my settings:
STATICFILES_FINDERS_IGNORE = [
'*.scss',
'*.js',
]
I'm new to django-pipeline, but I believe it now has pipeline.finders.FileSystemFinder and pipeline.finders.AppDirectoriesFinder to do exactly this.
See the section "If you want to exclude Pipelinable content from your collected static files" on https://django-pipeline.readthedocs.org/en/latest/storages.html.
Also, from the source code of 1.5:
class AppDirectoriesFinder(PatternFilterMixin, DjangoAppDirectoriesFinder):
"""
Like AppDirectoriesFinder, but doesn't return any additional ignored
patterns.
This allows us to concentrate/compress our components without dragging
the raw versions in via collectstatic.
"""
Note that at the time of writing, this results in empty compressed/minified content when DEBUG==True, see https://github.com/cyberdelia/django-pipeline/issues/418. I assume this will be fixed in future versions of django-pipeline.