i want to retrieve all comments for each post and put all allowed_departments from each comment in a list
i want to check in the list if the current logged in user profile department is in the list of allowed #departments for that post, then the user can view the post, below are my models and list views i have tried.
class Post(models.Model):
id = models.BigAutoField(primary_key=True)
title = models.CharField(max_length=100)
# from_office = models.ForeignKey(Department, related_name='from_posts', blank=True, null=True, on_delete=models.CASCADE)
to_office = models.ForeignKey(Department, related_name='to_posts', blank=True, null=True, on_delete=models.CASCADE)
from_office = models.CharField(max_length=15, default=None)
# content = models.TextField()
content = RichTextField(blank=False, null=False)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
comments = models.ManyToManyField('Comment', related_name='post_comments', blank=True) # ManyToMany relationship to Comment
# file = models.FileField(blank=True, null=True)
file = models.FileField(upload_to='', blank=True, null=True) # Specify the upload_to parameter
class Comment(models.Model):
post = models.ForeignKey(Post, related_name="post_comments", on_delete=models.CASCADE)
name = models.CharField(max_length=255)
body = RichTextField(blank=True, null=True)
date_added = models.DateTimeField(auto_now_add=True)
# New field to store the list of departments that can access the post
# departments = forms.ModelMultipleChoiceField(queryset=Department.objects.all(),widget=forms.CheckboxSelectMultiple,required=False) # Make it optional if needed
allowed_departments = models.ManyToManyField('Department', related_name='allowed_posts')
.
class Profile(models.Model):
id = models.BigAutoField(primary_key=True)
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
department = models.ForeignKey(Department, on_delete=models.SET_NULL, null=True, blank=True)
class PostListView(LoginRequiredMixin, ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 5
def get_all_allowed_departments(self, post):
comments = post.post_comments.all()
all_allowed_departments_ = set()
for comment in comments:
allowed_departments_manager = comment.allowed_departments
departments = allowed_departments_manager.all()
all_allowed_departments_.update(departments)
return all_allowed_departments_
def get_queryset(self):
user_profile = get_object_or_404(Profile, user=self.request.user)
user_department = user_profile.department
# Filter posts based on author's department or the current user
queryset = Post.objects.filter(
Q(author__profile__department=user_department) | Q(author=self.request.user)
).order_by('-date_posted')
return queryset
class TableHomeView(LoginRequiredMixin, ListView):
model = Post
template_name = 'table_home.html'
context_object_name = 'posts'
paginate_by = 10 # Set the number of posts per page
def get_queryset(self):
# Filter the posts based on the specified conditions
return Post.objects.filter(
models.Q(author=self.request.user) | # Check if the user is the author
models.Q(to_office=self.request.user.profile.department) | # Check if user's department is the same as 'to_office'
models.Q(author__profile__department=self.request.user.profile.department) | # Check if user's department is the same as the author's department
models.Q(comments__allowed_departments=self.request.user.profile.department) # Check if user's department is in the allowed departments
).distinct()
def get(self, request, *args, **kwargs):
# Override the get method to perform additional checks if needed
if not self.request.user.is_authenticated:
raise Http404("You do not have permission to view this page.")
return super().get(request, *args, **kwargs)