Skip to content

Commit

Permalink
Improve notif and organization add member
Browse files Browse the repository at this point in the history
  • Loading branch information
cuom1999 committed Aug 13, 2024
1 parent cdbed12 commit 7406d08
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 149 deletions.
5 changes: 2 additions & 3 deletions judge/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ class AddOrganizationMemberForm(ModelForm):
label=_("New users"),
)

def clean(self):
def clean_new_users(self):
new_users = self.cleaned_data.get("new_users") or ""
usernames = new_users.split()
invalid_usernames = []
Expand All @@ -387,8 +387,7 @@ def clean(self):
usernames=str(invalid_usernames)
)
)
self.cleaned_data["new_users"] = valid_usernames
return self.cleaned_data
return valid_usernames

class Meta:
model = Organization
Expand Down
21 changes: 21 additions & 0 deletions judge/migrations/0190_deprecate_old_notif_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.2.18 on 2024-08-13 10:36

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("judge", "0189_organization_image"),
]

operations = [
migrations.RemoveField(
model_name="notification",
name="comment",
),
migrations.RemoveField(
model_name="notification",
name="read",
),
]
41 changes: 37 additions & 4 deletions judge/models/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
from judge.caching import cache_wrapper


category_to_verbose_message = {
"Add blog": _("Added a post"),
"Added to group": _("You are added to a group"),
"Comment": _("You have a new comment"),
"Delete blog": _("Deleted a post"),
"Reject blog": _("Rejected a post"),
"Approve blog": _("Approved a post"),
"Edit blog": _("Edited a post"),
"Mention": _("Mentioned you"),
"Reply": _("Replied you"),
"Ticket": _("Ticket"),
}


class Notification(models.Model):
owner = models.ForeignKey(
Profile,
Expand All @@ -27,10 +41,29 @@ class Notification(models.Model):
verbose_name=_("who trigger, used for non-comment"),
on_delete=CASCADE,
)
comment = models.ForeignKey(
Comment, null=True, verbose_name=_("comment"), on_delete=CASCADE
) # deprecated
read = models.BooleanField(verbose_name=_("read"), default=False) # deprecated

def verbose_activity(self):
if self.category in category_to_verbose_message:
return category_to_verbose_message[self.category]

if "Problem public" in self.category:
is_public = "True" in self.category
if "(" in self.category and ")" in self.category:
groups = self.category.split("(", 1)[1].strip(")")
if is_public:
verbose_message = _("The problem is public to: ") + groups
else:
verbose_message = _("The problem is private to: ") + groups
else:
verbose_message = (
_("The problem is public to everyone.")
if is_public
else _("The problem is private.")
)

return verbose_message

return self.category


class NotificationProfile(models.Model):
Expand Down
6 changes: 5 additions & 1 deletion judge/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ def get_submissions_url(self):
return reverse("organization_submissions", args=(self.id, self.slug))

def is_admin(self, profile):
return self.admins.filter(id=profile.id).exists()
return profile.id in self.get_admin_ids()

@cache_wrapper(prefix="Orgai", expected_type=list)
def get_admin_ids(self):
return list(self.admins.values_list("id", flat=True))

def is_member(self, profile):
return profile in self
Expand Down
1 change: 1 addition & 0 deletions judge/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def contest_submission_delete(sender, instance, **kwargs):
@receiver(post_save, sender=Organization)
def organization_update(sender, instance, **kwargs):
cache.delete_many([make_template_fragment_key("organization_html", (instance.id,))])
Organization.get_admin_ids.dirty(instance)


_misc_config_i18n = [code for code, _ in settings.LANGUAGES]
Expand Down
23 changes: 20 additions & 3 deletions judge/views/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,8 +793,12 @@ def get_object(self, queryset=None):
def form_valid(self, form):
new_users = form.cleaned_data["new_users"]
self.object.members.add(*new_users)
link = reverse("organization_home", args=[self.object.id, self.object.slug])
html = f'<a href="{link}">{self.object.name}</a>'
make_notification(new_users, "Added to group", html, self.request.profile)
with revisions.create_revision():
revisions.set_comment(_("Added members from site"))
usernames = ", ".join([u.username for u in new_users])
revisions.set_comment(_("Added members from site") + ": " + usernames)
revisions.set_user(self.request.user)
return super(AddOrganizationMember, self).form_valid(form)

Expand All @@ -819,7 +823,7 @@ def post(self, request, *args, **kwargs):
status=400,
)

if not organization.members.filter(id=user.id).exists():
if not organization.is_member(user):
return generic_message(
request,
_("Can't kick user"),
Expand All @@ -828,7 +832,20 @@ def post(self, request, *args, **kwargs):
status=400,
)

organization.members.remove(user)
if organization.is_admin(user):
return generic_message(
request,
_("Can't kick user"),
_("The user you are trying to kick is an organization admin."),
status=400,
)

with revisions.create_revision():
revisions.set_comment(_("Kicked member") + " " + user.username)
revisions.set_user(self.request.user)
organization.members.remove(user)
organization.save()

return HttpResponseRedirect(organization.get_users_url())


Expand Down
Loading

0 comments on commit 7406d08

Please sign in to comment.