31 lines
794 B
Python
31 lines
794 B
Python
from django.db.models import Q
|
|
|
|
from .models import Project
|
|
|
|
|
|
def can_create(user):
|
|
return bool(user and user.is_authenticated and user.is_active)
|
|
|
|
|
|
def can_import_external(user):
|
|
"""Binding a company external record requires an explicit elevated permission."""
|
|
return bool(
|
|
user
|
|
and user.is_authenticated
|
|
and user.is_active
|
|
and (
|
|
user.is_staff or user.is_superuser or user.has_perm("projects.add_project")
|
|
)
|
|
)
|
|
|
|
|
|
def can_manage(user, project):
|
|
return user.is_staff or user.is_superuser or project.manager_id == user.pk
|
|
|
|
|
|
def visible_projects(user):
|
|
qs = Project.objects.all()
|
|
if user.is_staff or user.is_superuser:
|
|
return qs
|
|
return qs.filter(Q(manager=user) | Q(memberships__employee=user)).distinct()
|