Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hypha
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ots
hypha
Commits
132f9f46
Commit
132f9f46
authored
5 years ago
by
Fredrik Jonsson
Browse files
Options
Downloads
Patches
Plain Diff
Notify via Slack when new user is created or edited with roles manually.
parent
f9bfd193
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
opentech/apply/users/models.py
+4
-0
4 additions, 0 deletions
opentech/apply/users/models.py
opentech/apply/users/wagtail_hooks.py
+23
-0
23 additions, 0 deletions
opentech/apply/users/wagtail_hooks.py
opentech/apply/utils/notifications.py
+57
-0
57 additions, 0 deletions
opentech/apply/utils/notifications.py
with
84 additions
and
0 deletions
opentech/apply/users/models.py
+
4
−
0
View file @
132f9f46
from
django.db
import
models
from
django.db
import
models
from
django.db.models
import
Q
from
django.db.models
import
Q
from
django.contrib.auth.models
import
AbstractUser
,
BaseUserManager
from
django.contrib.auth.models
import
AbstractUser
,
BaseUserManager
from
django.urls
import
reverse
from
django.utils.functional
import
cached_property
from
django.utils.functional
import
cached_property
from
django.utils.translation
import
gettext_lazy
as
_
from
django.utils.translation
import
gettext_lazy
as
_
...
@@ -86,6 +87,9 @@ class User(AbstractUser):
...
@@ -86,6 +87,9 @@ class User(AbstractUser):
objects
=
UserManager
()
objects
=
UserManager
()
def
get_absolute_url
(
self
):
return
reverse
(
'
wagtailusers_users:edit
'
,
args
=
(
self
.
id
,))
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
get_full_name
()
if
self
.
get_full_name
()
else
self
.
get_short_name
()
return
self
.
get_full_name
()
if
self
.
get_full_name
()
else
self
.
get_short_name
()
...
...
This diff is collapsed.
Click to expand it.
opentech/apply/users/wagtail_hooks.py
+
23
−
0
View file @
132f9f46
...
@@ -2,6 +2,8 @@ from django.conf.urls import url
...
@@ -2,6 +2,8 @@ from django.conf.urls import url
from
wagtail.core
import
hooks
from
wagtail.core
import
hooks
from
opentech.apply.utils.notifications
import
slack_notify
from
.admin_views
import
index
from
.admin_views
import
index
...
@@ -10,3 +12,24 @@ def register_admin_urls():
...
@@ -10,3 +12,24 @@ def register_admin_urls():
return
[
return
[
url
(
r
'
^users/$
'
,
index
,
name
=
'
index
'
),
url
(
r
'
^users/$
'
,
index
,
name
=
'
index
'
),
]
]
@hooks.register
(
'
after_create_user
'
)
def
notify_after_create_user
(
request
,
user
):
slack_notify
(
message
=
f
'
{
request
.
user
}
has crated a new account for
{
user
}
.
'
,
request
=
request
,
related
=
user
,
)
@hooks.register
(
'
after_edit_user
'
)
def
notify_after_edit_user
(
request
,
user
):
roles
=
list
(
user
.
groups
.
values_list
(
'
name
'
,
flat
=
True
))
if
roles
:
roles
=
'
,
'
.
join
(
roles
)
slack_notify
(
message
=
f
'
{
request
.
user
}
has edited the account for
{
user
}
that now have these roles:
{
roles
}
.
'
,
request
=
request
,
related
=
user
,
)
This diff is collapsed.
Click to expand it.
opentech/apply/utils/notifications.py
0 → 100644
+
57
−
0
View file @
132f9f46
import
requests
from
django.conf
import
settings
class
SlackNotifications
():
def
__init__
(
self
):
self
.
destination
=
settings
.
SLACK_DESTINATION_URL
self
.
target_room
=
settings
.
SLACK_DESTINATION_ROOM
def
__call__
(
self
,
*
args
,
recipients
=
None
,
related
=
None
,
**
kwargs
):
return
self
.
send_message
(
*
args
,
recipients
=
None
,
related
=
related
,
**
kwargs
)
def
slack_users
(
self
,
users
):
slack_users
=
[]
for
user
in
users
:
if
user
.
slack
:
slack_users
.
append
(
f
'
<
{
user
.
slack
}
>
'
)
return
'
'
.
join
(
slack_users
)
def
slack_link
(
self
,
request
,
related
):
slack_link
=
''
try
:
link
=
request
.
scheme
+
'
://
'
+
request
.
get_host
()
+
related
.
get_absolute_url
()
except
AttributeError
:
pass
else
:
title
=
str
(
related
)
slack_link
=
f
'
<
{
link
}
|
{
title
}
>
'
return
slack_link
def
send_message
(
self
,
message
,
request
,
recipients
=
None
,
related
=
None
,
**
kwargs
):
if
not
self
.
destination
or
not
self
.
target_room
:
errors
=
list
()
if
not
self
.
destination
:
errors
.
append
(
'
Destination URL
'
)
if
not
self
.
target_room
:
errors
.
append
(
'
Room ID
'
)
return
'
Missing configuration: {}
'
.
format
(
'
,
'
.
join
(
errors
))
slack_users
=
self
.
slack_users
(
recipients
)
if
recipients
else
''
slack_link
=
self
.
slack_link
(
request
,
related
)
if
related
else
''
message
=
'
'
.
join
([
slack_users
,
message
,
slack_link
]).
strip
()
data
=
{
"
room
"
:
self
.
target_room
,
"
message
"
:
message
,
}
response
=
requests
.
post
(
self
.
destination
,
json
=
data
)
return
str
(
response
.
status_code
)
+
'
:
'
+
response
.
content
.
decode
()
slack_notify
=
SlackNotifications
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment