Skip to content

Commit

Permalink
Add user groups report (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamDudley authored Apr 4, 2024
1 parent eb57adf commit 9b2b22c
Show file tree
Hide file tree
Showing 40 changed files with 83 additions and 18 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ markers = [
omit = ["*/migrations/*", "*/test/*", "e2e_tests/*"]

[tool.ruff]
extend-select = ["B", "S"]
src = ["src"]
extend-select = ["B", "S", "I"]
ignore = ["B904", "E501", "N818", "S101"]
extend-exclude = [
"manage.py",
Expand Down
1 change: 1 addition & 0 deletions src/config/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from celery import Celery
from celery.schedules import crontab


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.prod")

celery_app = Celery("DjangoCelery")
Expand Down
1 change: 1 addition & 0 deletions src/config/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .base import * # noqa


APP_ENV = "test"
DEBUG = True
TEMPLATE_DEBUG = True
Expand Down
1 change: 1 addition & 0 deletions src/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from core.urls import urlpatterns as core_urlpatterns
from peoplefinder.urls import api_urlpatterns, people_urlpatterns, teams_urlpatterns


urlpatterns = [
# URLs for Staff SSO Auth broker
path("auth/", include(authbroker_client_urls)),
Expand Down
1 change: 1 addition & 0 deletions src/config/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.core.wsgi import get_wsgi_application
from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.prod")

application = get_wsgi_application()
Expand Down
1 change: 1 addition & 0 deletions src/content/management/commands/create_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
WorkingAtDITHome,
)


TOP_LEVEL_PAGE_TYPES = [
AboutUsHome,
HowDoIHome,
Expand Down
3 changes: 2 additions & 1 deletion src/content/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
from content import blocks
from content.utils import manage_excluded, manage_pinned, truncate_words_and_chars
from core.utils import set_seen_cookie_banner
from extended_search.index import Indexed
from extended_search.index import DWIndexedField as IndexedField
from extended_search.index import Indexed
from peoplefinder.widgets import PersonChooser
from search.utils import split_query
from user.models import User as UserModel


User = get_user_model()

RICH_TEXT_FEATURES = [
Expand Down
2 changes: 1 addition & 1 deletion src/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from django.db import models
from simple_history import register
from simple_history.models import HistoricalRecords
from waffle.models import AbstractUserFlag
from wagtail.documents.models import Document
from wagtail.snippets.models import register_snippet
from waffle.models import AbstractUserFlag


@register_snippet
Expand Down
1 change: 1 addition & 0 deletions src/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
urlpatterns = [
path("problem-found/", views.page_problem_found, name="page_problem_found"),
path("deactivated/", views.deactivated, name="deactivated"),
path("report/user-groups", views.user_groups_report, name="report:user-groups"),
]
34 changes: 34 additions & 0 deletions src/core/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import csv
import logging

from django.conf import settings
from django.contrib.postgres.aggregates import StringAgg
from django.core.exceptions import PermissionDenied
from django.http import HttpRequest, HttpResponse
from django.template.response import TemplateResponse
from django.views.decorators.http import require_GET
from notifications_python_client.notifications import NotificationsAPIClient
from sentry_sdk import capture_message

from core.forms import PageProblemFoundForm
from user.models import User


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -86,3 +92,31 @@ def page_problem_found(request):
"core/page_problem_found.html",
{"form": form, "message_sent": message_sent},
)


@require_GET
def user_groups_report(request: HttpRequest, *args, **kwargs) -> HttpResponse:
if not request.user.is_superuser:
raise PermissionDenied

header = ["ID", "Email", "First name", "Last name", "Groups"]

qs = (
User.objects.filter(groups__isnull=False)
.annotate(group_names=StringAgg("groups__name", delimiter=", "))
.values_list("id", "email", "first_name", "last_name", "group_names")
)

filename = "user_groups.csv"

response = HttpResponse(
content_type="text/csv",
headers={"Content-Disposition": f'attachment; filename="{filename}"'},
)

writer = csv.writer(response)
writer.writerow(header)
for obj in qs:
writer.writerow(obj)

return response
1 change: 1 addition & 0 deletions src/e2e_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
recreate_db,
)


os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"


Expand Down
2 changes: 1 addition & 1 deletion src/extended_search/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from django.contrib import admin

from core.admin import admin_site
from extended_search.models import Setting
from extended_search import settings
from extended_search.models import Setting


class SettingAdminForm(forms.ModelForm):
Expand Down
2 changes: 1 addition & 1 deletion src/extended_search/backends/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from wagtail.search.index import SearchField
from wagtail.search.query import MATCH_NONE, Fuzzy, MatchAll, Not, Phrase, PlainText

from extended_search.query import Filtered, Nested, OnlyFields
from extended_search.index import RelatedFields
from extended_search.query import Filtered, Nested, OnlyFields


class FilteredSearchMapping(Elasticsearch7Mapping):
Expand Down
1 change: 1 addition & 0 deletions src/extended_search/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from extended_search.types import AnalysisType


logger = logging.getLogger(__name__)


Expand Down
1 change: 1 addition & 0 deletions src/extended_search/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from extended_search.query import Filtered, Nested, OnlyFields
from extended_search.types import AnalysisType, SearchQueryType


logger = logging.getLogger(__name__)


Expand Down
1 change: 1 addition & 0 deletions src/extended_search/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
get_indexed_models,
)


env_file_path = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
".env",
Expand Down
4 changes: 2 additions & 2 deletions src/extended_search/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db.models.signals import post_save, post_delete
from django.db.models.signals import post_delete, post_save

from extended_search.models import Setting
from extended_search import settings
from extended_search.models import Setting


def update_searchsetting_queryset(sender, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions src/extended_search/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from wagtail.search.query import (
MATCH_NONE,
Fuzzy,
Phrase,
PlainText,
MatchAll,
Not,
Phrase,
PlainText,
SearchQuery,
)

Expand Down
2 changes: 1 addition & 1 deletion src/extended_search/tests/test_query.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from wagtail.search.query import PlainText

from extended_search.query import Filtered, Nested, OnlyFields
from wagtail.search.query import PlainText


class TestOnlyFields:
Expand Down
5 changes: 3 additions & 2 deletions src/extended_search/tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
from enum import Enum

from extended_search.types import SearchQueryType, AnalysisType
import pytest

from extended_search.types import AnalysisType, SearchQueryType


class TestAnalysisType:
Expand Down
2 changes: 1 addition & 1 deletion src/home/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import tweepy
from django.conf import settings
from tweepy.auth import OAuthHandler
from sentry_sdk import capture_exception
from tweepy.auth import OAuthHandler


def get_tweets():
Expand Down
1 change: 1 addition & 0 deletions src/news/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from news.forms import CommentForm
from working_at_dit.models import PageWithTopics


UserModel = get_user_model()


Expand Down
4 changes: 3 additions & 1 deletion src/patch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from extended_search.index import get_indexed_models, class_is_indexed
from wagtail.search import index

from extended_search.index import class_is_indexed, get_indexed_models


index.get_indexed_models = get_indexed_models
index.class_is_indexed = class_is_indexed

Expand Down
1 change: 1 addition & 0 deletions src/peoplefinder/forms/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.contrib.auth import get_user_model
from django.core.validators import ValidationError


User = get_user_model()


Expand Down
1 change: 1 addition & 0 deletions src/peoplefinder/legacy_migration/migrate_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
from peoplefinder.services.team import TeamService
from user.models import User


BATCH_SIZE = 100

logger = logging.getLogger(__name__)
Expand Down
1 change: 1 addition & 0 deletions src/peoplefinder/management/commands/create_test_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from peoplefinder.models import Team
from peoplefinder.services.team import TeamService


TREE_HELP = """Team tree:
.
└── SpaceX
Expand Down
1 change: 1 addition & 0 deletions src/peoplefinder/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from extended_search.index import DWIndexedField as IndexedField
from extended_search.index import Indexed, RelatedFields


# United Kingdom
DEFAULT_COUNTRY_PK = "CTHMTC00260"

Expand Down
1 change: 1 addition & 0 deletions src/peoplefinder/services/person.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from peoplefinder.types import EditSections, ProfileSections
from user.models import User


logger = logging.getLogger(__name__)

LEFT_DIT_LOG_MESSAGE = """People Finder deletion request: {profile_name}
Expand Down
1 change: 1 addition & 0 deletions src/peoplefinder/templatetags/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe


register = template.Library()


Expand Down
1 change: 1 addition & 0 deletions src/peoplefinder/templatetags/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from peoplefinder.models import Person


register = template.Library()


Expand Down
1 change: 1 addition & 0 deletions src/peoplefinder/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
TeamTreeView,
)


people_urlpatterns = [
path("", PeopleHome.as_view(), name="people-home"),
path(
Expand Down
1 change: 1 addition & 0 deletions src/peoplefinder/views/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

from .base import HtmxFormView, PeoplefinderView


User = get_user_model()


Expand Down
1 change: 1 addition & 0 deletions src/peoplefinder/views/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from .base import PeoplefinderView


# TODO: Potential to refactor for the common parts.


Expand Down
1 change: 1 addition & 0 deletions src/search/templatetags/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from search import search as search_vectors


# from search.utils import has_only_bad_results
# from silk.profiling.profiler import silk_profile

Expand Down
1 change: 1 addition & 0 deletions src/search/templatetags/search_explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from search import search as search_vectors
from search.templatetags import search as main_search


register = template.Library()


Expand Down
2 changes: 1 addition & 1 deletion src/search/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.urls import path
from django.views.generic import RedirectView

from .views import explore, search, autocomplete
from .views import autocomplete, explore, search


app_name = "search"
Expand Down
2 changes: 1 addition & 1 deletion src/search/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from django.conf import settings
from wagtail.search.query import Fuzzy, Or, Phrase, PlainText

from extended_search import settings as search_settings
from extended_search.index import Indexed
from extended_search.query import OnlyFields
from extended_search.query_builder import CustomQueryBuilder
from extended_search import settings as search_settings


def sanitize_search_query(query: Optional[str] = None) -> str:
Expand Down
1 change: 1 addition & 0 deletions src/search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from search.templatetags import search as search_template_tag
from search.utils import get_query_info_for_model


logger = logging.getLogger(__name__)


Expand Down
1 change: 1 addition & 0 deletions src/tools/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from core.admin import admin_site
from tools.models import IrapToolData, IrapToolDataImport, Tool


# Really crude admin, to be used for development

admin_site.register(IrapToolData)
Expand Down
Loading

0 comments on commit 9b2b22c

Please sign in to comment.