Coupled flask blueprints and circular imports - flask

What is the properly flask-way to implement application with coupled blueprints?
I'm currently working on project with several modules. I've separate application into modules logically, in dependence of database entities, and specify these modules as blueprints. But some of these blueprints have dependencies on each other: for example, view of blueprint A uses internal method (like a search in db, not view) from blueprint B, while another view from B uses another internal method from A.
Of course, it's cause circular imports.

The best way to handle this would to be decouple your internal Blueprint calls into a separate module. Try to concentrate your database logic in one module and only create specific calls when needed.
Your project layout might look like:
+ App
|
|`--+ models
| |`-- __init__.py
| |`-- models.py
| `-- utils.py
|
`--+ blueprints
|`-- __init__.py
|`-- blueprint_a.py
`-- blueprint_b.py
And then in App/blueprints/__init__.py you'd import your models and your models helpers. From within blueprint_a.py you'd import the model stuff you'd need and blueprint_b.py you'd import the other model stuff you need there.

Related

Flask blueprint/app factory circular import

I realise that blueprints are meant for modularity, and perform this task well. However, I have several tasks that are required by multiple blueprints as well as the root application (i.e. app/). With this mind, when I make changes to the tasks in app/tasks.py I would like it to propagate out to the blueprints that use these tasks without me having to manual change the code in each blueprints' task file (app/blueprint/tasks.py).
With this in mind I am trying to import app/tasks.py into each of the blueprints that has a copy of some task in app/tasks.py in app/blueprint/tasks.py. Of course, the main issue here is circular imports and I can't seem to get around them, in fact it seems almost impossible. My app factory does not import app/tasks.py until it has returned to application. Is it possible to make this import prior to registering the blueprints in the factory itself? If not, is there any way to circumvent the circular import problem? At the moment I see no way around it, there is no where to place from app import tasks in the blueprint files without creating a circular import.
EDIT: I realised the issue was that I registered my blueprints in my app factory. So when I would attempt from app.tasks import <func> in app/blueprint/tasks.py it would raise an import error since app had not been declared at the point of blueprint registration. I could get around this by moving the blueprint registration outside the app factory but is there a better way of doing this?
From what i can see it should work? Could it be that it is confusing things due to naming? As in thinking that the app is refering to app object and not the folder?
I just tested in my own app and i can import same things into app.py and my blueprint files.
Try maybe changing the folder names so they are not named app.

Good practice for helper functions in Django views

I am working on a project in Django. On my views.py I need to make use of multiple helper functions. In order to keep my code clean, I am going to create another file to wrap all these functions. I was planning to call the file just functions.py or helpers.py.
Which is the good practice to add helper functions for Django views? Is there any kind of convention, rule or anything?
UPDATE: These functions are closely related to the app itself. They have no sense out of their app.
Thanks!
The cleanest way would be to create multiple files with only functions related to each other. Ideally, if they are app-agnostic, put them in a python package outside of the django app you are using.
Ie.
All functions related to users go to view_helpers/users.py,
All function related to json go to view_helpers/json.py
The directory structure would be like that
django_project/
main_django_app/
__init__.py
views.py
settings.py
...
view_helpers/
__init__.py
json.py
manage.py

Is it advisable to keep the model itself in independent application?

I m working with django for a small project. I am not sure if the below approach is a best approach.
I have three applications under a project
1) app1, app2, app3
Question:
2) All three applications work with the same model, and is it fine if i keep model itself into different application so that app1, app2, app3 can access it?
ie: 1) modelStore, app1, app2, app3.
Kindly consider it is fresher question!!.
The bottom line is you put your model inside the app, which is the most relevant. Then just import it like
from app1.models import MyModel
this line will work anywhere in your code.
If you're not planning on creating a (standalone) reusable app, it's fine to share functionality across those apps.
However you should always consider if splitting up functionality into different apps is really the best solution if the usage is bound close together.
A different approach could be to have just one app, splitting out forms, admins or models (if that improves readability).
/project_root
/project
__init__.py
settings.py
/app
__init__.py # imports Foo and Bar from foo_models and bar_models
/models
foo_models.py # requires a `class Meta: app_label = 'app'`
bar_models.py # requires a `class Meta: app_label = 'app'`
/forms
foo_forms.py
manage.py
And really, just be consistent whatever you pick, there's nothing worse than inconsistent coding.

How to load custom packages from inside Django apps into views.py?

I am new to Django and have a little problem with making all the project structure clear and organized and so on.. The MVC stuff in Django is quite easy to use when making a small project. However, when I am trying to get a new layer (application logic, like three-tier architecture) between views.py and models.py I have problem to do so.
I have the following structure:
mysite/
manage.py
app1/
models.py
views.py
logic/
__init__.py
Class1.py
parser.py
...
and I want to load into views.py stuff from Class1.py and parser.py.
How do I do it?
Neither of the following works:
from app1.logic import *
from app1.logic.Class1 import Class1
Also, it would help me if somebody could list me some example of a really huge django project. It looks like either lots of classes and .py files should be in every app folder or everything is in the models.py.. Both look a little disorganised and I'm sure there is a way to make it a little bit clearer (like in Zend or Symfony).
And, I'm working with Python3 and Django 1.5b2.
Thanks.
If Class1 or parser import from views, then you have a circular dependency. You'll need to move any shared code out into a third file.
You might consider though whether you need all those separate files under logic. In Python there's no requirement to have a class in its own file, so maybe you could have a single logic.py instead of a logic directory containing several files.

django - reusing functions in many views

I have a bunch of functions that I created in some views that must be reused in many other views. Do I need to create a class and put those functions in a class? If yes how exactly has to be done in Django and then how do I call and initiate them in the new views?
Django views are just Python functions. You can call other Python functions from them just as you can from any other Python code. Put your functions into a .py file, import it, and invoke the functions.
Of course, it may make sense for other reasons to create a class to hold the functions, but you certainly don't need to in order to call them from views.
The solution would be to create the myfunctions.py file in your app folder and import it in your views. Your views file would look like:
import myfunctions
def my_view(request):
.....
foo = myfunctions.bar()
....
You look to complicated to Django. Django is just another Python application and lives in the Python world. For example you can create file library.py and import this file where is needed (in modules where the function from library is needed). If you need you library's functions to get data from database, just import models in the library.py and use them.
Django doesn't have such a big think as "module". You creating the architecture, you can define what in your case is module. In general this is just simple directory with init.py file inside to be able to import files from there.
Hope that helped.
From my point of view, if you have a lot of functions that are widely used in your project it make sense put all this in some separate application. I create some module named 'contrib' for this purposes. It can avoid some time for maintaining this code in future