jazzband / django-invitations

Generic invitations app for Django
GNU General Public License v3.0
567 stars 166 forks source link

Can you reference the Invitation key? #145

Open Redhaus opened 4 years ago

Redhaus commented 4 years ago

Hello,

Thanks for this great project!

Quick question. I would like to reference the invitation key so that I can do a lookup on the inviters_id. I would use the email address for the lookup but if the user clicks a link then wishes to signup with a different email this method would not work. However, if I could reference the invitation key that would be a unique string. Is there a way for me to get the invitation key in a variable?

thank you in advance.

ashersuman commented 4 years ago

i am also looking for this. please reply

Redhaus commented 4 years ago

@ashersuman this isn't the ideal way of going about it but as a workaround, I was able to get the inviters_id using a signal after a user signs up. I hope this helps.

...
from invitations.models import Invitation
...

@receiver(user_signed_up)
def teacher_confirmed_(request, user, **kwargs):

       # If invited by school get invite object and assign those teachers to those schools

        invite = Invitation.objects.filter(email=user.email).first()

        if invite:

            # get inviter_id
            schoolAdminID = invite.inviter_id

            # get school user who sent invitation with inviter schooladmin_id
            parentSchoolObj = School.objects.filter(primary_contact_id=schoolAdminID).first()

            # create teacher profile and assign to school
            profile = TeacherProfile.objects.create(
                user=user,
                school_name=parentSchoolObj,
            )

            return profile
ashersuman commented 4 years ago

Thanks a lot!! Signals worked for me.

scambrayj commented 3 years ago

I'll post this here for anyone needing this, but was able to get the key via the below using the invite_accepted receiver.

Note: the receiver currently has an error that is not merged yet and related to #116

the fix for this is listed in the #116 comments to modify installed files for Invitations --> views.py line 181, add request=request to arguments. That will get the signal to fire and the below should work in getting the key.

@receiver(invite_accepted)
def invite_accepted(sender, request, email, **kwargs):
    # extract the key from the request address
    # example request - <WSGIRequest: GET '/invitations/accept-invite/ezgu4srazp9vmrgipgnbwwtd04ifsdivbqba9i2ooe9vi15wss8gcs9sa3vvydwh'>
    request = str(request)
    list1 = request.split('/')
    value = list1[3]
    list2 = value.split("'")
    key = list2[0]
    # use the key and the email to lookup the exact invite and get team assignment from custom model or other fields...
    invite = MemberInvitation.objects.get(key=key, email=email)
    print(invite.team)
valberg commented 2 years ago

These are great ideas to have as small cookbook examples in the docs (https://github.com/jazzband/django-invitations/issues/176)