forked from mozilla/kitsune
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Staff members to message groups (mozilla#5922)
* API for messages to get users/groups * Added custom form field type in `form_fields.py` to handle group AND user selection and validation * Update form, models, urls and tests for messages * Update group profile page to add button for messaging group * Removed unnecessary validation from `view.py` for `new_messages` * Rewrote `new_messages` to handle Groups * New .js file for handling drop down for messages `messages.autocomplete.js` * Updated webpack `entrypoints.js` to handle new file * Updated `tokenizer.js` * Updated `outbox.html`, `read-outbox.html` and `_inbox.scss` * Created abstracted group in `settings.py` for our `Staff` group * Added new cached property to `Profile` model for `in_staff_group` * Added `in_staff_group` function to `sumo/utils.py` ( thanks @escattone ! )
- Loading branch information
1 parent
8ec0709
commit 7cdffd7
Showing
27 changed files
with
1,511 additions
and
927 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from django.conf import settings | ||
|
||
from django.contrib.auth.models import Group, User | ||
from django.db.models import Q | ||
from django.views.decorators.http import require_GET | ||
|
||
from kitsune.access.decorators import login_required | ||
from kitsune.sumo.decorators import json_view | ||
from kitsune.sumo.utils import webpack_static | ||
from kitsune.users.templatetags.jinja_helpers import profile_avatar | ||
|
||
|
||
@login_required | ||
@require_GET | ||
@json_view | ||
def get_autocomplete_suggestions(request): | ||
"""An API to provide auto-complete data for user names or groups.""" | ||
pre = request.GET.get("term", "") or request.GET.get("query", "") | ||
if not pre or not request.user.is_authenticated: | ||
return [] | ||
|
||
def create_suggestion(item): | ||
"""Create a dictionary object for the autocomplete suggestion.""" | ||
is_user = isinstance(item, User) | ||
return { | ||
"type": "User" if is_user else "Group", | ||
"type_icon": webpack_static( | ||
settings.DEFAULT_USER_ICON if is_user else settings.DEFAULT_GROUP_ICON | ||
), | ||
"name": item.username if is_user else item.name, | ||
"display_name": item.profile.name if is_user else item.name, | ||
"avatar": profile_avatar(item, 24) | ||
if is_user | ||
else webpack_static(settings.DEFAULT_AVATAR), | ||
} | ||
|
||
suggestions = [] | ||
user_criteria = Q(username__istartswith=pre) | Q(profile__name__istartswith=pre) | ||
users = User.objects.filter( | ||
user_criteria, is_active=True, profile__is_fxa_migrated=True | ||
).select_related("profile")[:10] | ||
|
||
for user in users: | ||
suggestions.append(create_suggestion(user)) | ||
|
||
if request.user.profile.in_staff_group: | ||
groups = Group.objects.filter(name__istartswith=pre)[:10] | ||
for group in groups: | ||
suggestions.append(create_suggestion(group)) | ||
|
||
return suggestions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
kitsune/messages/migrations/0002_inboxmessage_to_group_outboxmessage_to_group.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 4.2.10 on 2024-03-18 11:23 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("auth", "0012_alter_user_first_name_max_length"), | ||
("kitsune_messages", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="inboxmessage", | ||
name="to_group", | ||
field=models.ManyToManyField(blank=True, null=True, to="auth.group"), | ||
), | ||
migrations.AddField( | ||
model_name="outboxmessage", | ||
name="to_group", | ||
field=models.ManyToManyField(blank=True, null=True, to="auth.group"), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.