FossilRepo

fossilrepo / core / decorators.py
Source Blame History 39 lines
c588255… ragelink 1 """Custom view decorators for fossilrepo."""
c588255… ragelink 2
c588255… ragelink 3 from functools import wraps
c588255… ragelink 4
c588255… ragelink 5 from django.contrib.auth.decorators import login_required
c588255… ragelink 6
c588255… ragelink 7
c588255… ragelink 8 def public_or_login(view_func):
c588255… ragelink 9 """Allow anonymous access to public project views.
c588255… ragelink 10
c588255… ragelink 11 For views that take a `slug` parameter: if the project is public,
c588255… ragelink 12 let anonymous users through. Otherwise, redirect to login.
c588255… ragelink 13
c588255… ragelink 14 This replaces @login_required on read-only project/fossil views.
c588255… ragelink 15 The view itself must still call require_project_read() for the
c588255… ragelink 16 actual permission check.
c588255… ragelink 17 """
c588255… ragelink 18
c588255… ragelink 19 @wraps(view_func)
c588255… ragelink 20 def wrapper(request, *args, **kwargs):
c588255… ragelink 21 if request.user.is_authenticated:
c588255… ragelink 22 return view_func(request, *args, **kwargs)
c588255… ragelink 23
c588255… ragelink 24 # Check if this is a public project
c588255… ragelink 25 slug = kwargs.get("slug") or (args[0] if args else None)
c588255… ragelink 26 if slug:
c588255… ragelink 27 from projects.models import Project
c588255… ragelink 28
c588255… ragelink 29 try:
c588255… ragelink 30 project = Project.objects.get(slug=slug, deleted_at__isnull=True)
c588255… ragelink 31 if project.visibility == "public":
c588255… ragelink 32 return view_func(request, *args, **kwargs)
c588255… ragelink 33 except Project.DoesNotExist:
c588255… ragelink 34 pass
c588255… ragelink 35
c588255… ragelink 36 # Not public or no slug -- require login
c588255… ragelink 37 return login_required(view_func)(request, *args, **kwargs)
c588255… ragelink 38
c588255… ragelink 39 return wrapper

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button