Generic django admin per client - django

In my django project, i would like to use Django Admin for multiple organization. So in django urls system, i try to use a system like that :
urlpatterns = [
path('<organization>/admin', admin.site.urls)
]
With that syntax,on a simple example (default startproject+setup), i got an 'NoReverseMatch' :
Reverse for 'logout' with no arguments not found. 1 pattern(s) tried: ['(?P<client>[^/]+)\\/admin\\/logout\\/$']
At the end i try to implement a system like that:
Basic auth user (maybe change backend for email)
Organization:
Organization_User (inherit from basic auth or relationship)
Organization_Area (manyTomany with Organization_User)
and for urls i don't know if syntax bellow works:
www.example.com/admin => admin SuperUser only
www.example.com/organization1/admin => Admin for Organization 1
www.example.com/organization2/admin => Admin for Organization 2
I'm asking myself, is it possible to do that only with Django Admin system ?
For example, use one Admin and try URL filtering or use AdminSite inheritence
Best Regards,

Could you just assign users to groups depending on which organisation they belong to? Then alter the groups permissions so they can only see the parts you wish them to have access to.

Related

Django. One project. Separate auth systems and templates for different apps

I have a project with the following structure:
main_app
accounts_app (stores CustomUser model)
my_app1
users_app (inside my_app1)
my_app2
users_app (inside my_app2)
The reason behind separate users_app is that I want to reuse my_app1 and my_app2 in other projects. So I want them to be as separate as possible.
I want to have a separate auth system (custom user model, registration, templates, login/logout redirect, etc) for each app. So for example, let's say I have my-app1 at url localhost:8000/my-app1/. This means that registration will be at /my-app1/signup/, login at /my-app1/login/ and login redirect goes to /my-app1/
I also want to use allauth on some or all apps.
Questions:
Does this project structure even make sense? (yes/no)
Is it possible to have separate signup/sigin/etc templates for each app using allauth? (yes/no - link to doc?) I couldn't find the answer to this.
How do I avoid DB conflicts with migrations when creating custom user for each app? Right now I'm inheriting from AbstractUser in each app's model.py, like so: class MainCustomUser(AbstractUser), class AppOneUser(AbstractUser), class AppTwoUser(AbstractUser) But that doesn't seem to work.
Answers to these questions will serve me as guidance in the right direction, right now I sort of confused myself a bit.
settings.py:
...
# django-allauth config
SITE_ID = 1
LOGIN_REDIRECT_URL = 'main_app:index'
ACCOUNT_LOGOUT_REDIRECT = 'main_app:index'
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)
...

In django admin panel permissions given to the users by group not working

In Django admin panel
I create groups and give permission to them.
eg:
Create a Student Group and give it permission to view the student model.
Create a Teacher Group and give it permission to view the student model and add the student model.
create user using API and added to those groups,
and also staff status checked for each user.
When users log in on the admin panel that shows Site administration You don’t have permission to view or edit anything.
How to solve it.
I've got the same issue, tried every solution out there but they're all either outdated or not useful to my case. First let me ask you some questions.
1- What version of django are you using?
2- Do you have ModelBackend set in your AUTHENTICATION_BACKENDS like that?
AUTHENTICATION_BACKENDS = [
...
"django.contrib.auth.backends.ModelBackend",
...
]
3- If you're using a custom user model, have you extended PermissionsMixin in your class like that?
class CustomUser(AbstractBaseUser, PermissionsMixin):
# Fields
If you've done all of that and still stuck at it, like myself, then i'm not really sure what could help us. Anyway, i'll keep you posted if i get to know anything else.

admin specific app in django

I'm using Django 1.11
I want to create an admin specific app which is only accessible by admin interface.
I want to create an app to add country and state records which can be added or edited only by Admin from /admin.
Also, these records can be used by any application to populate a select field in adding a record like address.
If I create country app in mydangoapp/country and add URL to mydangoapp/mydangoapp/urls.py then it can be accessed by other users too by www.example.com/country which I don't want.
Where to create a model for this? Is it possible to create an admin specific app only?
Every app can be used only as admin specific till the time you don't give a urlpatterns for that app to be accessed by users.
So, you just create an app, make models, register them. But don't write views, forms and urls for that app.
In your case you should do something like that in your mydjangoapp urls.py:-
from country import views as country_views
urlpatterns = [
url(r'^admin/custom_prefix/country/', include('country.urls')),
url(r'^admin/', admin.site.urls),
]
Make sure u enter it in this order(i.e. before the r'^admin/' regex) in urlpatterns list.
Hope this solves your issues :-)

Django restrict views by User permissions which are editable in the Admin

I have a Django application where I need to restrict specific Views to subset of Users. I also want to be bale to edit which Users have this permission via the Django Admin. So in the admin I would like to be able to see all users and have a check box which can be checked to give permission to see this specific Views.
I believe the way to approach to this is to a permissions decorator on the Views in question:
from django.contrib.auth.decorators import permission_required
#login_required
#permission_required('user.can_view_restricted', login_url='/accounts/login/')
def Restrictedview(request, template_name='restricted.html'):
...
# restricted stuff
Now I know I need to define this permission (in permissions.py?), and register it with the Admin. I am unsure of how to do this and how to properly associate the permission with a specific User instance. Should this be an extra field on 'User', or a separate model to hold model to hole Users and Permissions?
You can read in details about django permissions in the docs
https://docs.djangoproject.com/en/dev/topics/auth/default/#permissions-and-authorization
Basically Django permissions use the Permission model, which is found at django.contrib.auth.models, but for most applications you don't need to directly import or use that model.
By default Django creates 3 default permissions for any model you have in your app. If you have a model named MyModel in an app named myapp, then Django will create create_mymodel, change_mymodel, and delete_mymodel permissions by default.
You can check if the user has a certain permission by calling
user.has_perm('myapp.create_mymodel')
if you're checking for the create permission for example. Or, like you did, you can use the decorator
permission_required('myapp.create_mymodel')
In addition to the default permissions provided by django, you can define custom permissions on your models by specifying the permissions attribute in the Meta class of your model like this:
class MyModel(models.Model):
[...]
class Meta:
permissions = (
("can_deliver_pizzas", "Can deliver pizzas"),
)
More on defining custom permissions here: https://docs.djangoproject.com/en/dev/ref/models/options/#permissions
By default, permissions can be easily edited for every user using the admin interface. Just visit a certain user's page and there will be a field named User Permissions with a list of all permissions in your project, from which you can add or remove permissions for your particular user.

Two different templates for django admin

I'd like to open up the django admin of a new project I'm working on to our users as well as our staff. The problem I'm having is that users and admins have different ways of using the software, and having the same interface be shared between the two groups would lead to inefficiencies.
Is it possible to have the django admin use a specific template if the user is or isn't staff? Or would I need to create two different admin sites?
You need to create different instances of django.contrib.admin.sites.AdminSite
Docs
Example usage,
# admin.py
from django.contrib.admin.sites import AdminSite
new_admin = AdminSite(name='My New Admin Panel')
new_admin.index_template = "new_admin/index.html"
# urls.py
from myproject.admin import new_admin
(r'^new_admin/(*.)', new_admin.urls)