Skip to content

Commit

Permalink
Design pending blogs in organizations (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
anhkha2003 authored Jul 23, 2024
1 parent 66f6212 commit 44554d7
Show file tree
Hide file tree
Showing 8 changed files with 423 additions and 306 deletions.
11 changes: 3 additions & 8 deletions judge/views/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,9 @@ def get_context_data(self, **kwargs):
orgs = list(self.object.organizations.all())

if self.request.profile:
if self.request.profile.id in self.object.get_authors():
for org in orgs:
if org.is_member(self.request.profile):
context["editable_orgs"].append(org)
else:
for org in orgs:
if org.is_admin(self.request.profile):
context["editable_orgs"].append(org)
for org in orgs:
if self.request.profile.can_edit_organization(org):
context["editable_orgs"].append(org)

return context

Expand Down
45 changes: 33 additions & 12 deletions judge/views/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ class EditOrganizationBlog(
LoginRequiredMixin,
TitleMixin,
OrganizationHomeView,
MemberOrganizationMixin,
AdminOrganizationMixin,
UpdateView,
):
template_name = "organization/blog/edit.html"
Expand All @@ -1134,19 +1134,20 @@ def setup_blog(self, request, *args, **kwargs):
self.blog_id = kwargs["blog_pk"]
self.blog = BlogPost.objects.get(id=self.blog_id)
if self.organization not in self.blog.organizations.all():
raise Exception("This blog does not belong to this organization")
if (
self.request.profile.id not in self.blog.get_authors()
and not self.can_edit_organization(self.organization)
):
raise Exception("Not allowed to edit this blog")
except:
raise Exception(_("This blog does not belong to this organization"))
if not self.request.profile.can_edit_organization(self.organization):
raise Exception(_("Not allowed to edit this blog"))
except Exception as e:
return generic_message(
request,
_("Permission denied"),
_("Not allowed to edit this blog"),
e,
)

def publish_blog(self, request, *args, **kwargs):
self.blog_id = kwargs["blog_pk"]
BlogPost.objects.filter(pk=self.blog_id).update(visible=True)

def delete_blog(self, request, *args, **kwargs):
self.blog_id = kwargs["blog_pk"]
BlogPost.objects.get(pk=self.blog_id).delete()
Expand All @@ -1164,6 +1165,22 @@ def post(self, request, *args, **kwargs):
if request.POST["action"] == "Delete":
self.create_notification("Delete blog")
self.delete_blog(request, *args, **kwargs)
cur_url = reverse(
"organization_home",
args=(self.organization_id, self.organization.slug),
)
return HttpResponseRedirect(cur_url)
elif request.POST["action"] == "Reject":
self.create_notification("Reject blog")
self.delete_blog(request, *args, **kwargs)
cur_url = reverse(
"organization_pending_blogs",
args=(self.organization_id, self.organization.slug),
)
return HttpResponseRedirect(cur_url)
elif request.POST["action"] == "Approve":
self.create_notification("Approve blog")
self.publish_blog(request, *args, **kwargs)
cur_url = reverse(
"organization_pending_blogs",
args=(self.organization_id, self.organization.slug),
Expand All @@ -1185,9 +1202,8 @@ def create_notification(self, action):
args=[self.organization.id, self.organization.slug, self.blog_id],
)
html = f'<a href="{link}">{blog.title} - {self.organization.name}</a>'
post_authors = blog.authors.all()
posible_users = self.organization.admins.all() | post_authors
make_notification(posible_users, action, html, self.request.profile)
to_users = (self.organization.admins.all() | blog.get_authors()).distinct()
make_notification(to_users, action, html, self.request.profile)

def form_valid(self, form):
with revisions.create_revision():
Expand Down Expand Up @@ -1224,3 +1240,8 @@ def get_queryset(self):

def get_title(self):
return _("Pending blogs in %s") % self.organization.name

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["org"] = self.organization
return context
Loading

0 comments on commit 44554d7

Please sign in to comment.