2020-01-21 06:35:58 +00:00
|
|
|
from django import forms
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.core.cache import cache
|
|
|
|
from django.core.cache.utils import make_template_fragment_key
|
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from django.db import transaction
|
2020-12-28 05:45:58 +00:00
|
|
|
from django.db.models import Count, Q, Value, BooleanField
|
2020-01-21 06:35:58 +00:00
|
|
|
from django.forms import Form, modelformset_factory
|
|
|
|
from django.http import Http404, HttpResponsePermanentRedirect, HttpResponseRedirect
|
|
|
|
from django.urls import reverse
|
2020-12-28 05:45:58 +00:00
|
|
|
from django.utils import timezone
|
2021-10-10 22:37:15 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
2020-01-21 06:35:58 +00:00
|
|
|
from django.utils.translation import gettext as _, gettext_lazy, ungettext
|
|
|
|
from django.views.generic import DetailView, FormView, ListView, UpdateView, View
|
2022-05-14 17:57:27 +00:00
|
|
|
from django.views.generic.detail import (
|
|
|
|
SingleObjectMixin,
|
|
|
|
SingleObjectTemplateResponseMixin,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
from reversion import revisions
|
|
|
|
|
|
|
|
from judge.forms import EditOrganizationForm
|
2022-05-14 17:57:27 +00:00
|
|
|
from judge.models import (
|
|
|
|
BlogPost,
|
|
|
|
Comment,
|
|
|
|
Organization,
|
|
|
|
OrganizationRequest,
|
|
|
|
Problem,
|
|
|
|
Profile,
|
|
|
|
Contest,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
from judge.utils.ranker import ranker
|
2022-05-14 17:57:27 +00:00
|
|
|
from judge.utils.views import (
|
|
|
|
TitleMixin,
|
|
|
|
generic_message,
|
|
|
|
QueryStringSortMixin,
|
|
|
|
DiggPaginatorMixin,
|
|
|
|
)
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"OrganizationList",
|
|
|
|
"OrganizationHome",
|
|
|
|
"OrganizationUsers",
|
|
|
|
"OrganizationMembershipChange",
|
|
|
|
"JoinOrganization",
|
|
|
|
"LeaveOrganization",
|
|
|
|
"EditOrganization",
|
|
|
|
"RequestJoinOrganization",
|
|
|
|
"OrganizationRequestDetail",
|
|
|
|
"OrganizationRequestView",
|
|
|
|
"OrganizationRequestLog",
|
|
|
|
"KickUserWidgetView",
|
|
|
|
]
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
2020-12-28 05:45:58 +00:00
|
|
|
class OrganizationBase(object):
|
|
|
|
def can_edit_organization(self, org=None):
|
|
|
|
if org is None:
|
|
|
|
org = self.object
|
|
|
|
if not self.request.user.is_authenticated:
|
|
|
|
return False
|
|
|
|
profile_id = self.request.profile.id
|
2022-05-14 17:57:27 +00:00
|
|
|
return (
|
|
|
|
org.admins.filter(id=profile_id).exists() or org.registrant_id == profile_id
|
|
|
|
)
|
2020-12-28 05:45:58 +00:00
|
|
|
|
|
|
|
def is_member(self, org=None):
|
|
|
|
if org is None:
|
|
|
|
org = self.object
|
2022-05-14 17:57:27 +00:00
|
|
|
return (
|
|
|
|
self.request.profile in org if self.request.user.is_authenticated else False
|
|
|
|
)
|
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
|
2020-12-28 05:45:58 +00:00
|
|
|
class OrganizationMixin(OrganizationBase):
|
2022-05-14 17:57:27 +00:00
|
|
|
context_object_name = "organization"
|
2020-01-21 06:35:58 +00:00
|
|
|
model = Organization
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2022-05-14 17:57:27 +00:00
|
|
|
context["logo_override_image"] = self.object.logo_override_image
|
2020-01-21 06:35:58 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
return super(OrganizationMixin, self).dispatch(request, *args, **kwargs)
|
|
|
|
except Http404:
|
|
|
|
key = kwargs.get(self.slug_url_kwarg, None)
|
|
|
|
if key:
|
2022-05-14 17:57:27 +00:00
|
|
|
return generic_message(
|
|
|
|
request,
|
|
|
|
_("No such organization"),
|
|
|
|
_('Could not find an organization with the key "%s".') % key,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
else:
|
2022-05-14 17:57:27 +00:00
|
|
|
return generic_message(
|
|
|
|
request,
|
|
|
|
_("No such organization"),
|
|
|
|
_("Could not find such organization."),
|
|
|
|
)
|
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
class OrganizationDetailView(OrganizationMixin, DetailView):
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
2022-05-14 17:57:27 +00:00
|
|
|
if self.object.slug != kwargs["slug"]:
|
|
|
|
return HttpResponsePermanentRedirect(
|
|
|
|
request.get_full_path().replace(kwargs["slug"], self.object.slug)
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
context = self.get_context_data(object=self.object)
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
|
2020-12-28 05:45:58 +00:00
|
|
|
class OrganizationList(TitleMixin, ListView, OrganizationBase):
|
2020-01-21 06:35:58 +00:00
|
|
|
model = Organization
|
2022-05-14 17:57:27 +00:00
|
|
|
context_object_name = "organizations"
|
|
|
|
template_name = "organization/list.html"
|
|
|
|
title = gettext_lazy("Organizations")
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2022-05-14 17:57:27 +00:00
|
|
|
return (
|
|
|
|
super(OrganizationList, self)
|
|
|
|
.get_queryset()
|
|
|
|
.annotate(member_count=Count("member"))
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
2020-12-28 05:45:58 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(OrganizationList, self).get_context_data(**kwargs)
|
2022-05-14 17:57:27 +00:00
|
|
|
context["my_organizations"] = set()
|
2020-12-28 05:45:58 +00:00
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
for organization in context["organizations"]:
|
2020-12-28 05:45:58 +00:00
|
|
|
if self.can_edit_organization(organization) or self.is_member(organization):
|
2022-05-14 17:57:27 +00:00
|
|
|
context["my_organizations"].add(organization)
|
2020-12-28 05:45:58 +00:00
|
|
|
|
|
|
|
return context
|
2020-01-21 06:35:58 +00:00
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
class OrganizationHome(OrganizationDetailView):
|
2022-05-14 17:57:27 +00:00
|
|
|
template_name = "organization/home.html"
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(OrganizationHome, self).get_context_data(**kwargs)
|
2022-05-14 17:57:27 +00:00
|
|
|
context["title"] = self.object.name
|
|
|
|
context["can_edit"] = self.can_edit_organization()
|
|
|
|
context["is_member"] = self.is_member()
|
|
|
|
context["new_problems"] = Problem.objects.filter(
|
|
|
|
is_public=True, is_organization_private=True, organizations=self.object
|
|
|
|
).order_by("-date", "-id")[: settings.DMOJ_BLOG_NEW_PROBLEM_COUNT]
|
|
|
|
context["new_contests"] = Contest.objects.filter(
|
|
|
|
is_visible=True, is_organization_private=True, organizations=self.object
|
|
|
|
).order_by("-end_time", "-id")[: settings.DMOJ_BLOG_NEW_CONTEST_COUNT]
|
|
|
|
|
|
|
|
context["posts"] = (
|
|
|
|
BlogPost.objects.filter(
|
|
|
|
visible=True,
|
|
|
|
publish_on__lte=timezone.now(),
|
|
|
|
is_organization_private=True,
|
|
|
|
organizations=self.object,
|
|
|
|
)
|
|
|
|
.order_by("-sticky", "-publish_on")
|
|
|
|
.prefetch_related("authors__user", "organizations")
|
|
|
|
)
|
|
|
|
context["post_comment_counts"] = {
|
|
|
|
int(page[2:]): count
|
|
|
|
for page, count in Comment.objects.filter(
|
|
|
|
page__in=["b:%d" % post.id for post in context["posts"]], hidden=False
|
|
|
|
)
|
|
|
|
.values_list("page")
|
|
|
|
.annotate(count=Count("page"))
|
|
|
|
.order_by()
|
2020-12-28 05:45:58 +00:00
|
|
|
}
|
2022-05-14 17:57:27 +00:00
|
|
|
context["pending_count"] = OrganizationRequest.objects.filter(
|
|
|
|
state="P", organization=self.object
|
|
|
|
).count()
|
2020-01-21 06:35:58 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
2020-12-30 02:29:50 +00:00
|
|
|
class OrganizationUsers(QueryStringSortMixin, OrganizationDetailView):
|
2022-05-14 17:57:27 +00:00
|
|
|
template_name = "organization/users.html"
|
|
|
|
all_sorts = frozenset(("points", "problem_count", "rating", "performance_points"))
|
2020-12-30 02:29:50 +00:00
|
|
|
default_desc = all_sorts
|
2022-05-14 17:57:27 +00:00
|
|
|
default_sort = "-performance_points"
|
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(OrganizationUsers, self).get_context_data(**kwargs)
|
2022-05-14 17:57:27 +00:00
|
|
|
context["title"] = _("%s Members") % self.object.name
|
|
|
|
context["partial"] = True
|
|
|
|
context["is_admin"] = self.can_edit_organization()
|
|
|
|
context["kick_url"] = reverse(
|
|
|
|
"organization_user_kick", args=[self.object.id, self.object.slug]
|
|
|
|
)
|
|
|
|
|
|
|
|
context["users"] = ranker(
|
|
|
|
self.get_object()
|
|
|
|
.members.filter(is_unlisted=False)
|
|
|
|
.order_by(self.order, "id")
|
|
|
|
.select_related("user")
|
|
|
|
.only(
|
|
|
|
"display_rank",
|
|
|
|
"user__username",
|
|
|
|
"points",
|
|
|
|
"rating",
|
|
|
|
"performance_points",
|
|
|
|
"problem_count",
|
2020-12-30 02:29:50 +00:00
|
|
|
)
|
2022-05-14 17:57:27 +00:00
|
|
|
)
|
|
|
|
context["first_page_href"] = "."
|
2020-12-30 02:29:50 +00:00
|
|
|
context.update(self.get_sort_context())
|
2020-01-21 06:35:58 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
class OrganizationMembershipChange(
|
|
|
|
LoginRequiredMixin, OrganizationMixin, SingleObjectMixin, View
|
|
|
|
):
|
2020-01-21 06:35:58 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
org = self.get_object()
|
|
|
|
response = self.handle(request, org, request.profile)
|
|
|
|
if response is not None:
|
|
|
|
return response
|
|
|
|
return HttpResponseRedirect(org.get_absolute_url())
|
|
|
|
|
|
|
|
def handle(self, request, org, profile):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
class JoinOrganization(OrganizationMembershipChange):
|
|
|
|
def handle(self, request, org, profile):
|
|
|
|
if profile.organizations.filter(id=org.id).exists():
|
2022-05-14 17:57:27 +00:00
|
|
|
return generic_message(
|
|
|
|
request,
|
|
|
|
_("Joining organization"),
|
|
|
|
_("You are already in the organization."),
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
if not org.is_open:
|
2022-05-14 17:57:27 +00:00
|
|
|
return generic_message(
|
|
|
|
request, _("Joining organization"), _("This organization is not open.")
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
max_orgs = settings.DMOJ_USER_MAX_ORGANIZATION_COUNT
|
|
|
|
if profile.organizations.filter(is_open=True).count() >= max_orgs:
|
|
|
|
return generic_message(
|
2022-05-14 17:57:27 +00:00
|
|
|
request,
|
|
|
|
_("Joining organization"),
|
|
|
|
_(
|
|
|
|
"You may not be part of more than {count} public organizations."
|
|
|
|
).format(count=max_orgs),
|
2020-01-21 06:35:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
profile.organizations.add(org)
|
|
|
|
profile.save()
|
2022-05-14 17:57:27 +00:00
|
|
|
cache.delete(make_template_fragment_key("org_member_count", (org.id,)))
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LeaveOrganization(OrganizationMembershipChange):
|
|
|
|
def handle(self, request, org, profile):
|
|
|
|
if not profile.organizations.filter(id=org.id).exists():
|
2022-05-14 17:57:27 +00:00
|
|
|
return generic_message(
|
|
|
|
request,
|
|
|
|
_("Leaving organization"),
|
|
|
|
_('You are not in "%s".') % org.short_name,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
profile.organizations.remove(org)
|
2022-05-14 17:57:27 +00:00
|
|
|
cache.delete(make_template_fragment_key("org_member_count", (org.id,)))
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OrganizationRequestForm(Form):
|
|
|
|
reason = forms.CharField(widget=forms.Textarea)
|
|
|
|
|
|
|
|
|
|
|
|
class RequestJoinOrganization(LoginRequiredMixin, SingleObjectMixin, FormView):
|
|
|
|
model = Organization
|
2022-05-14 17:57:27 +00:00
|
|
|
slug_field = "key"
|
|
|
|
slug_url_kwarg = "key"
|
|
|
|
template_name = "organization/requests/request.html"
|
2020-01-21 06:35:58 +00:00
|
|
|
form_class = OrganizationRequestForm
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
return super(RequestJoinOrganization, self).dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(RequestJoinOrganization, self).get_context_data(**kwargs)
|
|
|
|
if self.object.is_open:
|
|
|
|
raise Http404()
|
2022-05-14 17:57:27 +00:00
|
|
|
context["title"] = _("Request to join %s") % self.object.name
|
2020-01-21 06:35:58 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
request = OrganizationRequest()
|
|
|
|
request.organization = self.get_object()
|
|
|
|
request.user = self.request.profile
|
2022-05-14 17:57:27 +00:00
|
|
|
request.reason = form.cleaned_data["reason"]
|
|
|
|
request.state = "P"
|
2020-01-21 06:35:58 +00:00
|
|
|
request.save()
|
2022-05-14 17:57:27 +00:00
|
|
|
return HttpResponseRedirect(
|
|
|
|
reverse(
|
|
|
|
"request_organization_detail",
|
|
|
|
args=(
|
|
|
|
request.organization.id,
|
|
|
|
request.organization.slug,
|
|
|
|
request.id,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OrganizationRequestDetail(LoginRequiredMixin, TitleMixin, DetailView):
|
|
|
|
model = OrganizationRequest
|
2022-05-14 17:57:27 +00:00
|
|
|
template_name = "organization/requests/detail.html"
|
|
|
|
title = gettext_lazy("Join request detail")
|
|
|
|
pk_url_kwarg = "rpk"
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
object = super(OrganizationRequestDetail, self).get_object(queryset)
|
|
|
|
profile = self.request.profile
|
2022-05-14 17:57:27 +00:00
|
|
|
if (
|
|
|
|
object.user_id != profile.id
|
|
|
|
and not object.organization.admins.filter(id=profile.id).exists()
|
|
|
|
):
|
2020-01-21 06:35:58 +00:00
|
|
|
raise PermissionDenied()
|
|
|
|
return object
|
|
|
|
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
OrganizationRequestFormSet = modelformset_factory(
|
|
|
|
OrganizationRequest, extra=0, fields=("state",), can_delete=True
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
class OrganizationRequestBaseView(
|
|
|
|
TitleMixin,
|
|
|
|
LoginRequiredMixin,
|
|
|
|
SingleObjectTemplateResponseMixin,
|
|
|
|
SingleObjectMixin,
|
|
|
|
View,
|
|
|
|
):
|
2020-01-21 06:35:58 +00:00
|
|
|
model = Organization
|
2022-05-14 17:57:27 +00:00
|
|
|
slug_field = "key"
|
|
|
|
slug_url_kwarg = "key"
|
2020-01-21 06:35:58 +00:00
|
|
|
tab = None
|
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
organization = super(OrganizationRequestBaseView, self).get_object(queryset)
|
2022-05-14 17:57:27 +00:00
|
|
|
if not (
|
|
|
|
organization.admins.filter(id=self.request.profile.id).exists()
|
|
|
|
or organization.registrant_id == self.request.profile.id
|
|
|
|
):
|
2020-01-21 06:35:58 +00:00
|
|
|
raise PermissionDenied()
|
|
|
|
return organization
|
|
|
|
|
2021-10-10 22:37:15 +00:00
|
|
|
def get_content_title(self):
|
2022-05-14 17:57:27 +00:00
|
|
|
href = reverse("organization_home", args=[self.object.id, self.object.slug])
|
|
|
|
return mark_safe(
|
|
|
|
f'Manage join requests for <a href="{href}">{self.object.name}</a>'
|
|
|
|
)
|
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(OrganizationRequestBaseView, self).get_context_data(**kwargs)
|
2022-05-14 17:57:27 +00:00
|
|
|
context["title"] = _("Managing join requests for %s") % self.object.name
|
|
|
|
context["tab"] = self.tab
|
2020-01-21 06:35:58 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class OrganizationRequestView(OrganizationRequestBaseView):
|
2022-05-14 17:57:27 +00:00
|
|
|
template_name = "organization/requests/pending.html"
|
|
|
|
tab = "pending"
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(OrganizationRequestView, self).get_context_data(**kwargs)
|
2022-05-14 17:57:27 +00:00
|
|
|
context["formset"] = self.formset
|
2020-01-21 06:35:58 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
self.formset = OrganizationRequestFormSet(
|
2022-05-14 17:57:27 +00:00
|
|
|
queryset=OrganizationRequest.objects.filter(
|
|
|
|
state="P", organization=self.object
|
|
|
|
),
|
2020-01-21 06:35:58 +00:00
|
|
|
)
|
|
|
|
context = self.get_context_data(object=self.object)
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.object = organization = self.get_object()
|
|
|
|
self.formset = formset = OrganizationRequestFormSet(request.POST, request.FILES)
|
|
|
|
if formset.is_valid():
|
|
|
|
if organization.slots is not None:
|
|
|
|
deleted_set = set(formset.deleted_forms)
|
2022-05-14 17:57:27 +00:00
|
|
|
to_approve = sum(
|
|
|
|
form.cleaned_data["state"] == "A"
|
|
|
|
for form in formset.forms
|
|
|
|
if form not in deleted_set
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
can_add = organization.slots - organization.members.count()
|
|
|
|
if to_approve > can_add:
|
2022-05-14 17:57:27 +00:00
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
_(
|
|
|
|
"Your organization can only receive %d more members. "
|
|
|
|
"You cannot approve %d users."
|
|
|
|
)
|
|
|
|
% (can_add, to_approve),
|
|
|
|
)
|
|
|
|
return self.render_to_response(
|
|
|
|
self.get_context_data(object=organization)
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
approved, rejected = 0, 0
|
|
|
|
for obj in formset.save():
|
2022-05-14 17:57:27 +00:00
|
|
|
if obj.state == "A":
|
2020-01-21 06:35:58 +00:00
|
|
|
obj.user.organizations.add(obj.organization)
|
|
|
|
approved += 1
|
2022-05-14 17:57:27 +00:00
|
|
|
elif obj.state == "R":
|
2020-01-21 06:35:58 +00:00
|
|
|
rejected += 1
|
2022-05-14 17:57:27 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
ungettext("Approved %d user.", "Approved %d users.", approved)
|
|
|
|
% approved
|
|
|
|
+ "\n"
|
|
|
|
+ ungettext("Rejected %d user.", "Rejected %d users.", rejected)
|
|
|
|
% rejected,
|
|
|
|
)
|
|
|
|
cache.delete(
|
|
|
|
make_template_fragment_key("org_member_count", (organization.id,))
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
return self.render_to_response(self.get_context_data(object=organization))
|
|
|
|
|
|
|
|
put = post
|
|
|
|
|
|
|
|
|
|
|
|
class OrganizationRequestLog(OrganizationRequestBaseView):
|
2022-05-14 17:57:27 +00:00
|
|
|
states = ("A", "R")
|
|
|
|
tab = "log"
|
|
|
|
template_name = "organization/requests/log.html"
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
context = self.get_context_data(object=self.object)
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(OrganizationRequestLog, self).get_context_data(**kwargs)
|
2022-05-14 17:57:27 +00:00
|
|
|
context["requests"] = self.object.requests.filter(state__in=self.states)
|
2020-01-21 06:35:58 +00:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class EditOrganization(LoginRequiredMixin, TitleMixin, OrganizationMixin, UpdateView):
|
2022-05-14 17:57:27 +00:00
|
|
|
template_name = "organization/edit.html"
|
2020-01-21 06:35:58 +00:00
|
|
|
model = Organization
|
|
|
|
form_class = EditOrganizationForm
|
|
|
|
|
|
|
|
def get_title(self):
|
2022-05-14 17:57:27 +00:00
|
|
|
return _("Editing %s") % self.object.name
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
object = super(EditOrganization, self).get_object()
|
|
|
|
if not self.can_edit_organization(object):
|
|
|
|
raise PermissionDenied()
|
|
|
|
return object
|
|
|
|
|
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super(EditOrganization, self).get_form(form_class)
|
2022-05-14 17:57:27 +00:00
|
|
|
form.fields["admins"].queryset = Profile.objects.filter(
|
|
|
|
Q(organizations=self.object) | Q(admin_of=self.object)
|
|
|
|
).distinct()
|
2020-01-21 06:35:58 +00:00
|
|
|
return form
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
with transaction.atomic(), revisions.create_revision():
|
2022-05-14 17:57:27 +00:00
|
|
|
revisions.set_comment(_("Edited from site"))
|
2020-01-21 06:35:58 +00:00
|
|
|
revisions.set_user(self.request.user)
|
|
|
|
return super(EditOrganization, self).form_valid(form)
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
return super(EditOrganization, self).dispatch(request, *args, **kwargs)
|
|
|
|
except PermissionDenied:
|
2022-05-14 17:57:27 +00:00
|
|
|
return generic_message(
|
|
|
|
request,
|
|
|
|
_("Can't edit organization"),
|
|
|
|
_("You are not allowed to edit this organization."),
|
|
|
|
status=403,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
class KickUserWidgetView(
|
|
|
|
LoginRequiredMixin, OrganizationMixin, SingleObjectMixin, View
|
|
|
|
):
|
2020-01-21 06:35:58 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
organization = self.get_object()
|
|
|
|
if not self.can_edit_organization(organization):
|
2022-05-14 17:57:27 +00:00
|
|
|
return generic_message(
|
|
|
|
request,
|
|
|
|
_("Can't edit organization"),
|
|
|
|
_("You are not allowed to kick people from this organization."),
|
|
|
|
status=403,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
try:
|
2022-05-14 17:57:27 +00:00
|
|
|
user = Profile.objects.get(id=request.POST.get("user", None))
|
2020-01-21 06:35:58 +00:00
|
|
|
except Profile.DoesNotExist:
|
2022-05-14 17:57:27 +00:00
|
|
|
return generic_message(
|
|
|
|
request,
|
|
|
|
_("Can't kick user"),
|
|
|
|
_("The user you are trying to kick does not exist!"),
|
|
|
|
status=400,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
if not organization.members.filter(id=user.id).exists():
|
2022-05-14 17:57:27 +00:00
|
|
|
return generic_message(
|
|
|
|
request,
|
|
|
|
_("Can't kick user"),
|
|
|
|
_("The user you are trying to kick is not in organization: %s.")
|
|
|
|
% organization.name,
|
|
|
|
status=400,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
organization.members.remove(user)
|
|
|
|
return HttpResponseRedirect(organization.get_users_url())
|