Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #3 from fedora-infra/feature/FAS-username
Browse files Browse the repository at this point in the history
Try to figure out the fas username and use that for fedmsg instead.
  • Loading branch information
ralphbean committed Mar 26, 2014
2 parents e589ee1 + 6059468 commit 28383c2
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions askbot_fedmsg.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@
site_visited,
)

from askbot.deps import django_authopenid


def username(user):
""" Return the user's username... *unless* that user logged in via FAS
openid, in which case the FAS username is returned.
"""
assocs = django_authopenid.models.UserAssociation.objects.filter(user=user)
for association in assocs:
url = association.openid_url
if 'id.fedoraproject.org' in url:
return url.split('://')[1].split('.')[0]

# Otherwise
return user.username


def mangle_kwargs(kwargs):
""" Take kwargs as given to us by askbot and turn them into something that
Expand All @@ -74,15 +90,15 @@ def mangle_kwargs(kwargs):
user_keys = ['user', 'mark_by', 'delete_by', 'updated_by']
for key in user_keys:
if key in kwargs:
kwargs['agent'] = kwargs[key].username
kwargs['agent'] = username(kwargs[key])
del kwargs[key]

if 'newly_mentioned_users' in kwargs:
kwargs['newly_mentioned_users'] = [
user.username for user in list(kwargs['newly_mentioned_users'])]
username(user) for user in list(kwargs['newly_mentioned_users'])]

if 'revision' in kwargs:
kwargs['agent'] = kwargs['revision'].author.username
kwargs['agent'] = username(kwargs['revision'].author)
kwargs['revision'] = dict(
(key, getattr(kwargs['revision'], key)) for key in (
'tagnames', 'text', 'title', 'summary', 'pk',
Expand Down Expand Up @@ -122,6 +138,7 @@ def mangle_kwargs(kwargs):

return kwargs


def fedmsg_callback(sender, topic=None, **kwargs):
kwargs = mangle_kwargs(kwargs)
fedmsg.publish(topic=topic, modname="askbot", msg=kwargs)
Expand Down

0 comments on commit 28383c2

Please sign in to comment.