def ajax_username_invite(request): invitee_username = get_val(request.POST, 'username', '') band = get_obj_from_request(request.POST, 'band', Band) try: invitee = User.objects.get(username=invitee_username) except User.DoesNotExist: return json_failure(design.that_user_does_not_exist) if band is None: return json_failure(design.bad_band_id) if not band.permission_to_invite(request.user): return json_failure(design.lack_permission_to_invite) # make sure the user isn't already in the band if BandMember.objects.filter(user=invitee, band=band).count() > 0: return json_failure(design.x_already_in_band.format(invitee.username)) # make sure there isn't already an invitation for them if BandInvitation.objects.filter(invitee=invitee, band=band).count() > 0: return json_failure( design.already_invited_x_to_your_band.format(invitee.username)) invite = BandInvitation() invite.inviter = request.user invite.band = band invite.role = BandMember.BAND_MEMBER invite.invitee = invitee invite.save() # send a heads up email send_invitation_email(invite, request.get_host()) return json_success()
def ajax_username_invite(request): invitee_username = get_val(request.POST, 'username', '') band = get_obj_from_request(request.POST, 'band', Band) try: invitee = User.objects.get(username=invitee_username) except User.DoesNotExist: return json_failure(design.that_user_does_not_exist) if band is None: return json_failure(design.bad_band_id) if not band.permission_to_invite(request.user): return json_failure(design.lack_permission_to_invite) # make sure the user isn't already in the band if BandMember.objects.filter(user=invitee, band=band).count() > 0: return json_failure(design.x_already_in_band.format(invitee.username)) # make sure there isn't already an invitation for them if BandInvitation.objects.filter(invitee=invitee, band=band).count() > 0: return json_failure(design.already_invited_x_to_your_band.format(invitee.username)) invite = BandInvitation() invite.inviter = request.user invite.band = band invite.role = BandMember.BAND_MEMBER invite.invitee = invitee invite.save() # send a heads up email send_invitation_email(invite, request.get_host()) return json_success()
def ajax_email_invite(request): "send a band invitation by email." to_email = get_val(request.POST, 'email', '') band = get_obj_from_request(request.POST, 'band', Band) if band is None: return json_failure(design.bad_band_id) if not band.permission_to_invite(request.user): return json_failure(design.lack_permission_to_invite) if to_email == '': return json_failure(design.you_must_supply_an_email_address) if not is_valid_email(to_email): return json_failure(design.invalid_email_address) # if the email is a registered solid composer user, simply translate into direct invitation. try: local_user = User.objects.get(email=to_email) except User.DoesNotExist: local_user = None invite = BandInvitation() invite.inviter = request.user invite.band = band invite.role = BandMember.BAND_MEMBER subject = design.x_is_inviting_you_to_join_y.format(request.user.username, band.title) if local_user is not None: # make sure the user isn't already in the band if BandMember.objects.filter(user=local_user, band=band).count() > 0: return json_failure(design.x_already_in_band.format(local_user.username)) # make sure there isn't already an invitation for them if BandInvitation.objects.filter(invitee=local_user, band=band).count() > 0: return json_failure(design.already_invited_x_to_your_band.format(local_user.username)) invite.invitee = local_user invite.save() # send a heads up email if local_user.get_profile().email_notifications: context = Context({ 'user': request.user, 'band': band, 'invite': invite, 'host': request.get_host(), }) message_txt = get_template('workbench/email/invitation_direct.txt').render(context) message_html = get_template('workbench/email/invitation_direct.html').render(context) send_html_mail(subject, message_txt, message_html, [to_email]) return json_success() else: # create invitation link invite.expire_date = datetime.now() + timedelta(days=30) invite.code = create_hash(32) invite.save() # send the invitation email context = Context({ 'user': request.user, 'band': band, 'invite': invite, 'host': request.get_host(), }) message_txt = get_template('workbench/email/invitation_link.txt').render(context) message_html = get_template('workbench/email/invitation_link.html').render(context) send_html_mail(subject, message_txt, message_html, [to_email]) return json_success()
def ajax_create_invite(request): band = get_obj_from_request(request.POST, 'band', Band) invitee = get_obj_from_request(request.POST, 'invitee', User) if band is None: return json_failure(design.bad_band_id) if not band.openness == Band.FULL_OPEN: try: member = BandMember.objects.get(user=request.user, band=band) except BandMember.DoesNotExist: return json_failure(design.can_only_invite_to_your_own_band) if member.role != BandMember.MANAGER: return json_failure(design.only_managers_can_create_invitations) # make sure user isn't already in band if BandMember.objects.filter(band=band, user=invitee).count() > 0: return json_failure(design.x_already_in_band.format(invitee.username)) invite = BandInvitation() invite.inviter = request.user invite.band = band invite.expire_date = datetime.now() + timedelta(days=30) if invitee == None: invite.code = create_hash(32) invite.save() data = {'url': invite.redeemHyperlink()} return json_success(data) else: # make sure invitation doesn't exist already if BandInvitation.objects.filter(band=band, invitee=invitee).count() > 0: return json_failure(design.already_invited_x_to_your_band.format(invitee.username)) invite.invitee = invitee invite.save() send_invitation_email(invite, request.get_host()) return json_success()
def ajax_email_invite(request): "send a band invitation by email." to_email = get_val(request.POST, 'email', '') band = get_obj_from_request(request.POST, 'band', Band) if band is None: return json_failure(design.bad_band_id) if not band.permission_to_invite(request.user): return json_failure(design.lack_permission_to_invite) if to_email == '': return json_failure(design.you_must_supply_an_email_address) if not is_valid_email(to_email): return json_failure(design.invalid_email_address) # if the email is a registered solid composer user, simply translate into direct invitation. try: local_user = User.objects.get(email=to_email) except User.DoesNotExist: local_user = None invite = BandInvitation() invite.inviter = request.user invite.band = band invite.role = BandMember.BAND_MEMBER subject = design.x_is_inviting_you_to_join_y.format( request.user.username, band.title) if local_user is not None: # make sure the user isn't already in the band if BandMember.objects.filter(user=local_user, band=band).count() > 0: return json_failure( design.x_already_in_band.format(local_user.username)) # make sure there isn't already an invitation for them if BandInvitation.objects.filter(invitee=local_user, band=band).count() > 0: return json_failure( design.already_invited_x_to_your_band.format( local_user.username)) invite.invitee = local_user invite.save() # send a heads up email if local_user.get_profile().email_notifications: context = Context({ 'user': request.user, 'band': band, 'invite': invite, 'host': request.get_host(), }) message_txt = get_template( 'workbench/email/invitation_direct.txt').render(context) message_html = get_template( 'workbench/email/invitation_direct.html').render(context) send_html_mail(subject, message_txt, message_html, [to_email]) return json_success() else: # create invitation link invite.expire_date = datetime.now() + timedelta(days=30) invite.code = create_hash(32) invite.save() # send the invitation email context = Context({ 'user': request.user, 'band': band, 'invite': invite, 'host': request.get_host(), }) message_txt = get_template( 'workbench/email/invitation_link.txt').render(context) message_html = get_template( 'workbench/email/invitation_link.html').render(context) send_html_mail(subject, message_txt, message_html, [to_email]) return json_success()
def ajax_create_invite(request): band = get_obj_from_request(request.POST, 'band', Band) invitee = get_obj_from_request(request.POST, 'invitee', User) if band is None: return json_failure(design.bad_band_id) if not band.openness == Band.FULL_OPEN: try: member = BandMember.objects.get(user=request.user, band=band) except BandMember.DoesNotExist: return json_failure(design.can_only_invite_to_your_own_band) if member.role != BandMember.MANAGER: return json_failure(design.only_managers_can_create_invitations) # make sure user isn't already in band if BandMember.objects.filter(band=band, user=invitee).count() > 0: return json_failure(design.x_already_in_band.format(invitee.username)) invite = BandInvitation() invite.inviter = request.user invite.band = band invite.expire_date = datetime.now() + timedelta(days=30) if invitee == None: invite.code = create_hash(32) invite.save() data = {'url': invite.redeemHyperlink()} return json_success(data) else: # make sure invitation doesn't exist already if BandInvitation.objects.filter(band=band, invitee=invitee).count() > 0: return json_failure( design.already_invited_x_to_your_band.format(invitee.username)) invite.invitee = invitee invite.save() send_invitation_email(invite, request.get_host()) return json_success()