def invite(site_id): user = users.get_current_user() site = Site.get_admin_or_404(site_id) form = InviteForm(request.form) if form.validate(): invite_hash = sha1() invite_hash.update(str(site.key())) invite_hash.update(os.urandom(8)) invite_hash.update(form.email.data) invite_hash = b32encode(invite_hash.digest()).lower() invite = Invitation(hash=invite_hash, email=form.email.data, site=site, admin=form.admin.data, inviter=user) invite.put() mail.send_mail( sender=settings.invite_sender_email, to=invite.email, subject='%s invited you to join them on %s' % (user.email(), settings.appname), body=render_template('invite_email.txt', invite=invite), html=render_template('invite_email.html', invite=invite), ) if request.is_xhr: return 'OK' flash('Invitation Sent!', 'success') else: errors = sum(form.errors.values(), []) if request.is_xhr: return ', '.join(errors) for error in errors: flash(error, 'error') flash(form.email.data, 'error') return redirect(url_for('edit_siteusers', site_id=site_id))
def add_friend(request,username): user = User.objects.get(username=username) invitation = Invitation(name = user.username, email= user.email, code= User.objects.make_random_password(20),sender=request.user) invitation.save() invitation.send() connection = Connections.objects.filter(connector_id= user.id) variables = RequestContext(request, {'username':username, 'connections': connection, 'users': User.objects.all()}) return render_to_response('user_connections.html',variables)
def add_group_user(request, groupName=None): if request.method == 'POST': users = request.POST.getlist('user') groupName = request.POST['groupName'] sgroup = get_object_or_404(SocialGroup, name=groupName) if request.user != sgroup.creator: msg = 'You dont have the permission' else: msg = "" ul = User.objects.filter(username__in=users) for u in ul: #to make sure user isnot already in and not already sent if not Membership.objects.filter( sgroup=sgroup, member=u ).count( ): # = Invitation.objects.get(sgroup, u).response='accept' if not Invitation.objects.filter( socialgroup=sgroup, invite=u, response='').count(): invite = Invitation(invite=u, socialgroup=sgroup) invite.save() msg = msg + 'Invitation sent to users' else: msg = msg + '%s already in the group or is invited <br />' % u else: sgroup = get_object_or_404(SocialGroup, name=groupName) invitations = Invitation.objects.filter(socialgroup=sgroup).exclude( response="reject") invitedList = [] for invitation in invitations: invitedList.append(invitation.invite) users = User.objects.exclude(id=request.user.id).exclude( id__in=sgroup.members.all()).exclude(username__in=invitedList) # to exclude members already in or already invited return render(request, 'add_group_user.html', { 'groupName': groupName, 'users': users }) return render(request, 'add_group_user.html', { 'groupName': groupName, 'msg': msg })
def invite(request, code): profile = check_profile(code) if not profile: messages.error( request, 'Your personal URL is incorrect, please reset your account or provide proper url' ) return redirect('index') from forms import InvitationForm if request.method == 'POST': form = InvitationForm(request.POST) if form.is_valid(): emails = form.cleaned_data['emails'] emails = emails.split(',') for e in emails: e = e.strip() i = Invitation(email=e, key=generate_key()) i.save() u, created = User.objects.get_or_create(username=e[:30], email=e) notification.send([u], 'invite', { 'key': i.key, 'profile': profile }) if len(emails): messages.info(request, '%d invitation(s) has been sent ' % len(emails)) return redirect('homepage', code=code) else: form = InvitationForm(initial={'key': code}) return render_to_response('researchers/invite.html', { 'profile': profile, 'form': form, 'key': code }, context_instance=RequestContext(request))
def invite(request, next=None): """Handles invite form submission""" invite_form = InviteForm(instance=Invitation(user=request.user), data=request.POST) if invite_form.is_valid(): invites = invite_form.save() emails = ", ".join([invite.email for invite in invites]) messages.success(request, 'Invitation sent to %s' % emails) else: messages.error( request, 'Form values where invalid, please try fill out the form again.') if request.is_ajax() and request.method == 'POST': message_html = loader.render_to_string('_messages.html', {}, RequestContext(request)) return HttpResponse(message_html) next = request.POST.get("next", next) if next: return redirect(next) return redirect("index")
def invite(request): emails = request.POST.getlist('emailList[]', '') custom_message = request.POST.get('custom_message', '') if emails: user = User.objects.get(username=request.user.username) user_account = Account.objects.get(user=user) did_not_invite = [] # TODO: Move this to string templates if custom_message: email_body = "{host_name} has invited you to be part of CiviWiki Beta with the following message: {custom_message}".format( host_name=user_account.full_name, custom_message=custom_message ) else: email_body = "{host_name} has invited you to be part of CiviWiki Beta. Follow the link below to get registered.".format( host_name=user_account.full_name ) email_messages = [] valid_emails = [] for email in emails: if Invitation.objects.filter(invitee_email=email).exists(): did_not_invite.append(email) else: valid_emails.append(email) verification_hash = uuid.uuid4().hex[:31] data = { 'host_user': user, 'invitee_email': email, 'verification_code': verification_hash, } domain = get_current_site(request).domain base_url = "http://{domain}/beta_register/{email}/{token}" url_with_code = base_url.format( domain=domain, email=email, token=verification_hash ) email_context = { 'title' : "You're Invited to Join CiviWiki Beta", 'greeting' : "You're Invited to Join CiviWiki Beta", 'body' : email_body, 'link': url_with_code, 'recipient': [email] } email_messages.append(email_context) new_invitation = Invitation(**data) # new_invitation.host_user = user # new_invitation.invitee_email = email # new_invitation.verification_code = "" new_invitation.save() if email_messages: email_subject = "Invitation to CiviWiki" send_mass_email.delay( subject=email_subject, contexts=email_messages ) if len(did_not_invite) == len(emails): response_data = { "message": "Invitations exist for submitted email(s). No new invitations sent", "error":"INVALID_EMAIL_DATA" } return JsonResponse(response_data, status=400) invitations = Invitation.objects.filter_by_host(host_user=user).order_by("-date_created").all() invitees = [invitation.summarize() for invitation in invitations] response_data = { 'did_not_invite': did_not_invite, 'invitees': invitees } return JsonResponse(response_data) else: # Return an 'invalid login' error message. response = { "message": 'Invalid Email Data', "error":"INVALID_EMAIL_DATA" } return JsonResponse(response, status=400)
def submitcircle(request): new_member = get_current_member(request) new_circle = get_current_circle(request) count = 2 posted_members = {} custom_errors = "" while count <= settings.CIRCLE_MAX_SIZE: member_contact = {} current_name = request.POST.get("name_" + str(count), None) current_contact = request.POST.get("contact_" + str(count), None) if (current_name): if (current_contact): # Check to see if current contact info is valid phone or email if is_phone(current_contact): member_contact["contact_type"] = "phone" if is_email(current_contact): member_contact["contact_type"] = "email" if not is_phone(current_contact) and not is_email(current_contact): # Bad data error custom_errors += "<li>contact_" + str(count) + " must be either a valid phone number OR email</li>" member_contact["contact_info"] = current_contact posted_members[current_name] = member_contact else: # Missing contact data error custom_errors += "<li>name_" + str(count) + " is present but contact_" + str(count) + " is missing</li>" else: if len(posted_members) < (settings.CIRCLE_MIN_SIZE - 1): # Missing name data error custom_errors += "<li>name_" + str(count) + " is missing</li>" count += 1 # Check to see if we have minimum more members added if len(posted_members) < (settings.CIRCLE_MIN_SIZE - 1): custom_errors += "<li>You need at least " + str(settings.CIRCLE_MIN_SIZE) + " members (including yourself) in your circle</li>" if custom_errors != "": custom_errors = format_html("<p><ul>{}</ul></p>", mark_safe(custom_errors)) # If there are any errors, kick out and display them context = {"member":new_member, "num_range_str":settings.CONTACT_RANGE_STR, "custom_errors":custom_errors, } return render(request, "circly/network.html", context) for each_member in posted_members.keys(): # Create new members and add to the circle if (posted_members[each_member]["contact_type"] == "email"): next_member = Member(circle=new_circle, circle_owner=False, member_name=each_member, member_email=posted_members[each_member]["contact_info"], member_created_date=timezone.now(), ) elif (posted_members[each_member]["contact_type"] == "phone"): new_phone = phonenumbers.parse(posted_members[each_member]["contact_info"], "US") new_phone = phonenumbers.format_number(new_phone, phonenumbers.PhoneNumberFormat.E164) next_member = Member(circle=new_circle, circle_owner=False, member_name=each_member, member_phone=new_phone, member_created_date=timezone.now(), ) next_member.save() # Create invite code with short link for profile sign up invite_code = hash_code(posted_members[each_member]["contact_info"]) invite_url = "http://www.circly.org/invite/" + invite_code new_short_url = random_bitly(invite_url) invite = Invitation(member=next_member, invite_code=invite_code, invite_short_url=new_short_url, invite_created_date=timezone.now(), invite_send_date=timezone.now()) invite.save() # Create reminders for all new members to join the circle remind = Reminder(member=next_member, reminder_subject=new_circle.circle_owner_name() + " would like you to join their circle of support", reminder_message="Hey " + each_member + ", visit " + new_short_url + " to fill in your profile and join a circle of preventive care.", reminder_created_date=timezone.now(), reminder_send_date=timezone.now(), ) remind.save() if new_member.member_email: owner_hash = hash_code(new_member.member_email) if new_member.member_phone: owner_hash = hash_code(new_member.member_phone) dashboard_url = "http://www.circly.org/dashboard/" + owner_hash new_short_dashboard_url = random_bitly(dashboard_url) new_circle.circle_short_url = new_short_dashboard_url new_circle.save() set_member_and_circle(request, new_circle, new_member) return HttpResponseRedirect(reverse("connect:dashboard", kwargs={"owner_hash":owner_hash}))
def do_start_user(client, message, *args, **kwargs): session = kwargs.get('session') user = session.query(User).filter(User.chat_id == message.chat.id).first() if user: # Utente era già fibberato fibberations = session.query(Invitation).filter(Invitation.signer == user).all() reply = "👋 **Bentornato {}**\n" \ "☣ Hai fibberato **{}** Persone!\n" \ "💰 Possiedi 💸**{} FiberCoin**\n\n" \ "🔗 Fibbera la gente con questo link:\n{}" \ .format(user.name, len(fibberations), user.coins, BASE_LINK.format(user.code)) message.reply(reply) elif len(message.command) > 1: # fibbera un nuovo utente con invito invite_code = try_parse_int(message.command[1], None) if invite_code: inviter = session.query(User).filter(User.code == invite_code).first() if inviter: # Insert a User in the user table name = message.from_user.username if not name: name = "{} {}".format(message.from_user.first_name, message.from_user.last_name) new_user = User( chat_id=message.chat.id, name=name, code=(random.randint(10000,9999999)^message.chat.id), coins=math.floor(FC_PER_FIBBERATION/2) ) session.add(new_user) #session.commit() # Insert fiberation in the fibberations table new_fibberation = Invitation(signer=inviter, signed=new_user) session.add(new_fibberation) #session.commit() inviter.coins += FC_PER_FIBBERATION session.commit() # Prepare reply app.send_message(chat_id=inviter.chat_id, text="Hai ricevuto 💸**{} FiberCoin** per aver fibberato {}\n" "💰 Borsellino: 💸**{} FiberCoin**"\ .format(FC_PER_FIBBERATION, MENTION.format(new_user.name, new_user.chat_id), inviter.coins) ) message.reply("**SEI STATO FIBBERATO!**⬆\n\n" "🔗 Puoi fibberare la gente con questo link:\n" "{}\n\n"\ "Scopri di più utilizzando il comando /about\n"\ .format(BASE_LINK.format(new_user.code))) else: message.reply("FibberCode non valido!") elif not len(session.query(User).all()): # Fibbera un nuovo utente senza invito (SOLO SE NESSUN ALTRO è STATO FIBBERATO) name = message.from_user.username if not name: name = "{} {}".format(message.from_user.first_name, message.from_user.last_name) new_user = User( chat_id=message.chat.id, name=name, code=(random.randint(10000,9999999)^message.chat.id), coins=math.floor(FC_PER_FIBBERATION / 2), privilege=1 ) session.add(new_user) session.commit() reply = "SEI STATO FIBBERATO!\n\n\n Sei il primo fibberatore della catena.\nPuoi fibberare laggente con questo link:\n{}" \ .format(BASE_LINK.format(new_user.code)) message.reply(reply)