From f4163fd017b1019028c7efbfb48ede1a03266053 Mon Sep 17 00:00:00 2001 From: DELL Date: Mon, 17 Oct 2022 23:04:39 +0700 Subject: [PATCH] fix edit link --- judge/views/blog.py | 21 +++++++++++++++++++++ templates/blog/blog.html | 15 +++------------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/judge/views/blog.py b/judge/views/blog.py index 061fee0..70f4be5 100644 --- a/judge/views/blog.py +++ b/judge/views/blog.py @@ -1,3 +1,4 @@ +from xmlrpc.client import boolean from django.conf import settings from django.db.models import Count, Max, Q from django.http import Http404 @@ -7,6 +8,8 @@ from django.utils.functional import lazy from django.utils.translation import ugettext as _ from django.views.generic import ListView +from itertools import chain + from judge.comments import CommentedDetailView from judge.models import ( BlogPost, @@ -24,6 +27,7 @@ from judge.utils.diggpaginator import DiggPaginator from judge.utils.problems import user_completed_ids from judge.utils.tickets import filter_visible_tickets from judge.utils.views import TitleMixin +from judge.views import organization # General view for all content list on home feed @@ -201,6 +205,23 @@ class PostView(TitleMixin, CommentedDetailView): def get_context_data(self, **kwargs): context = super(PostView, self).get_context_data(**kwargs) context["og_image"] = self.object.og_image + context["valid_user"] = False + context["valid_org"] = [] + for author in self.object.authors.all(): + if self.request.profile.user == author.user: + context["valid_user"] = True + + for valid_org in self.object.organizations.all(): + for admin in valid_org.admins.all(): + if self.request.profile.user == admin.user: + context["valid_user"] = True + + if context["valid_user"]: + for post_org in self.object.organizations.all(): + for org in self.request.profile.organizations.all(): + if post_org == org: + context["valid_org"].append(org) + return context def get_object(self, queryset=None): diff --git a/templates/blog/blog.html b/templates/blog/blog.html index e1d2444..f526732 100644 --- a/templates/blog/blog.html +++ b/templates/blog/blog.html @@ -28,20 +28,11 @@ {% if post.is_editable_by(request.user) %} [{{ _('Edit') }}] - {% else %} - {% for author in post.authors.all() %} - {% if author.user == request.user %} - {% for post_org in post.organizations.all() %} - {% for org in author.organizations.all() %} - {% if org == post_org %} - [{{ _('Edit') }}] - {% endif %} - {% endfor %} - {% endfor %} - {% endif %} + {% elif valid_user %} + {% for org in valid_org %} + [{{ _('Edit in') }} {{org.slug}}] {% endfor %} {% endif %} -
{% cache 86400 'post_content' post.id MATH_ENGINE %}