Skip to content

Commit

Permalink
Swap requests for httpx.
Browse files Browse the repository at this point in the history
  • Loading branch information
amanning9 committed May 30, 2024
1 parent d39424c commit 8900da8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 35 deletions.
48 changes: 14 additions & 34 deletions jasmin_services/notifications.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
"""
This module contains signals used by the JASMIN account app.
"""
"""This module contains signals used by the JASMIN account app."""

__author__ = "Matt Pryor"
__copyright__ = "Copyright 2015 UK Science and Technology Facilities Council"

import datetime as dt
import logging
import os
import re
from datetime import date

import requests
from dateutil.relativedelta import relativedelta
import httpx
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db.models import signals
Expand All @@ -30,9 +28,7 @@


def register_notifications(app_config, verbosity=2, interactive=True, **kwargs):
"""
``post_migrate`` signal handler that registers notification types for this app.
"""
"""``post_migrate`` signal handler that registers notification types for this app."""
NotificationType.create(
name="request_confirm",
level=NotificationLevel.INFO,
Expand Down Expand Up @@ -78,9 +74,7 @@ def register_notifications(app_config, verbosity=2, interactive=True, **kwargs):

@receiver(signals.post_save, sender=Request)
def confirm_request(sender, instance, created, **kwargs):
"""
Notifies the user that their request was received.
"""
"""Notifies the user that their request was received."""
if created and instance.active and instance.state == RequestState.PENDING:
instance.access.user.notify(
"request_confirm",
Expand All @@ -96,9 +90,7 @@ def confirm_request(sender, instance, created, **kwargs):


def notify_approvers(instance):
"""
Notifies potential approvers for a request to poke them into action.
"""
"""Notifies potential approvers for a request to poke them into action."""
if instance.active and instance.state == RequestState.PENDING:
approvers = instance.access.role.approvers.exclude(pk=instance.access.user.pk)
# If the role has some approvers, notify them
Expand All @@ -111,7 +103,7 @@ def notify_approvers(instance):
link = settings.BASE_URL + reverse(
"admin:jasmin_services_request_decide", args=(instance.pk,)
)
requests.post(
httpx.post(
settings.SLACK_WEBHOOK,
json={
"username": os.uname()[1],
Expand All @@ -129,27 +121,21 @@ def notify_approvers(instance):

@receiver(signals.post_save, sender=Request)
def notify_approvers_created(sender, instance, created, **kwargs):
"""
Notifies potential approvers to poke them into action.
"""
"""Notifies potential approvers to poke them into action."""
if created:
notify_approvers(instance)


@receiver(signals.post_save, sender=Request)
def request_decided(sender, instance, created, **kwargs):
"""
When a request is decided, clear any request_pending notifications associated with it.
"""
"""When a request is decided, clear any request_pending notifications associated with it."""
if instance.state in [RequestState.APPROVED, RequestState.REJECTED]:
Notification.objects.filter_target(instance).update(followed_at=timezone.now())


@receiver(signals.post_save, sender=Request)
def request_rejected(sender, instance, created, **kwargs):
"""
Notifies the user when their request has been decided.
"""
"""Notifies the user when their request has been decided."""
if instance.active and instance.state == RequestState.REJECTED:
# Only send the notification once
template = "request_incomplete" if instance.incomplete else "request_rejected"
Expand All @@ -169,9 +155,7 @@ def request_rejected(sender, instance, created, **kwargs):

@receiver(signals.post_save, sender=Grant)
def grant_created(sender, instance, created, **kwargs):
"""
Notifies the user when a grant is created.
"""
"""Notifies the user when a grant is created."""
if created and instance.active and not re.match(r"train\d{3}", instance.access.user.username):
instance.access.user.notify(
"grant_created",
Expand All @@ -188,9 +172,7 @@ def grant_created(sender, instance, created, **kwargs):

@receiver(signals.post_save, sender=Grant)
def grant_revoked(sender, instance, created, **kwargs):
"""
Notifies the user when a grant is revoked. Also ensures that access is revoked.
"""
"""Notifies the user when a grant is revoked. Also ensures that access is revoked."""
if (
instance.active
and instance.revoked
Expand All @@ -212,9 +194,7 @@ def grant_revoked(sender, instance, created, **kwargs):

@receiver(signals.post_save, sender=Grant)
def grant_sync_access(sender, instance, created, **kwargs):
"""
Synchronises access whenever a grant is saved, if the grant is active.
"""
"""Synchronises access whenever a grant is saved, if the grant is active."""
if instance.active:
if instance.revoked or instance.expired:
instance.access.role.disable(instance.access.user)
Expand Down Expand Up @@ -271,7 +251,7 @@ def account_reactivated(sender, instance, created, **kwargs):
Grant.objects.create(
access=access,
granted_by=grant.granted_by,
expires=date.today() + relativedelta(months=1),
expires=date.today() + dt.timedelta(days=30),
previous_grant=grant,
)
for req in Request.objects.filter(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ django-bootstrap5 = "^24.1"
django-markdown-deux = "^1.0.6"
python-dateutil = "^2.8.2"
django-polymorphic = {git = "https://github.com/jazzband/django-polymorphic.git", rev = "f4286f583d6f91e896b73e450154af0fea3935a4"}
requests = "^2.27.1"
django-countries = "^7.6.0"
djangorestframework = {version = "^3.13.1", optional = true}
django-filter = {version = "^24.1", optional = true}
drf-spectacular = {version="^0.27.1", optional = true}
drf-spectacular-sidecar = {version="^2024.1.1", optional = true}
django-picklefield = "^3.1"
httpx = "^0.27.0"

# Optional dependencies for behaviours.
jasmin-ldap-django = {git = "https://github.com/cedadev/jasmin-ldap-django.git", rev = "2b708c9136f31e8819ffff00fcb95567c27ed659", optional=true}
Expand Down

0 comments on commit 8900da8

Please sign in to comment.