feat(home/models/pages.py): add mock purchase login and auto group creation
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from django.contrib.auth.models import Group, User
|
from django.contrib.auth.models import Group, User
|
||||||
|
from django.conf import settings
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.forms import CheckboxSelectMultiple
|
from django.forms import CheckboxSelectMultiple
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
@@ -14,6 +15,8 @@ from wagtail.models.copying import ParentalManyToManyField
|
|||||||
from wagtail_color_panel.edit_handlers import NativeColorPanel
|
from wagtail_color_panel.edit_handlers import NativeColorPanel
|
||||||
from wagtail_color_panel.fields import ColorField
|
from wagtail_color_panel.fields import ColorField
|
||||||
|
|
||||||
|
from purchase.models import CoursePurchase
|
||||||
|
|
||||||
|
|
||||||
class EmptyPage(Page):
|
class EmptyPage(Page):
|
||||||
pass
|
pass
|
||||||
@@ -60,18 +63,60 @@ class CoursePage(Page):
|
|||||||
allowed_groups = ParentalManyToManyField(
|
allowed_groups = ParentalManyToManyField(
|
||||||
Group,
|
Group,
|
||||||
related_name="course_pages",
|
related_name="course_pages",
|
||||||
help_text="Select a group to restrict access to this course. Non-members will be prompted to purchase the course to view modules.",
|
help_text="Additional groups that should have access to this course, e.g. Editors. NOTE: Users who purchase the course will be automatically added to a dedicated access group for this course, so you don't need to add that group here.",
|
||||||
)
|
)
|
||||||
|
|
||||||
def _user_has_access(self, user):
|
def _user_has_access(self, user):
|
||||||
if not user.is_authenticated:
|
if not user.is_authenticated:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
user_group_ids = user.groups.values_list("id", flat=True)
|
user_group_ids = user.groups.values_list("id", flat=True)
|
||||||
return self.allowed_groups.filter(id__in=user_group_ids).exists() # pyright: ignore[reportAttributeAccessIssue]
|
if self.allowed_groups.filter(id__in=user_group_ids).exists():
|
||||||
|
return True
|
||||||
|
|
||||||
|
return CoursePurchase.objects.filter(
|
||||||
|
user=user, course=self, refunded=False
|
||||||
|
).exists()
|
||||||
|
|
||||||
|
def _user_purchase_id(self, user):
|
||||||
|
if not user.is_authenticated:
|
||||||
|
return None
|
||||||
|
|
||||||
|
purchase = CoursePurchase.objects.filter(
|
||||||
|
user=user, course=self, refunded=False
|
||||||
|
).first()
|
||||||
|
print(f"User {user} purchase for course {self}: {purchase}")
|
||||||
|
return purchase.id if purchase else None
|
||||||
|
|
||||||
|
def mock_purchase(self, user):
|
||||||
|
"""Mock method to simulate purchasing this course for a user."""
|
||||||
|
if not user.is_authenticated:
|
||||||
|
return False
|
||||||
|
obj, created = CoursePurchase.objects.get_or_create(
|
||||||
|
user=user, course=self, refunded=False
|
||||||
|
)
|
||||||
|
# Add user to dedicated access group for this course
|
||||||
|
group_name = f"course_{self.id}_access"
|
||||||
|
group, _ = Group.objects.get_or_create(name=group_name)
|
||||||
|
user.groups.add(group)
|
||||||
|
# Ensure allowed_groups only includes this access group
|
||||||
|
if not self.allowed_groups.filter(id=group.id).exists():
|
||||||
|
self.allowed_groups.add(group)
|
||||||
|
return created
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
group_name = f"course_{self.id}_access"
|
||||||
|
group, created = Group.objects.get_or_create(name=group_name)
|
||||||
|
if state := not self.allowed_groups.filter(id=group.id).exists():
|
||||||
|
print(state)
|
||||||
|
self.allowed_groups.add(group)
|
||||||
|
|
||||||
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
def get_context(self, request):
|
def get_context(self, request):
|
||||||
context = super().get_context(request)
|
context = super().get_context(request)
|
||||||
context["user_has_access"] = self._user_has_access(request.user)
|
context["user_has_access"] = self._user_has_access(request.user)
|
||||||
|
context["user_purchase_id"] = self._user_purchase_id(request.user)
|
||||||
return context
|
return context
|
||||||
|
|
||||||
content_panels = Page.content_panels + [
|
content_panels = Page.content_panels + [
|
||||||
|
|||||||
Reference in New Issue
Block a user