Exemplo n.º 1
0
def submitname(request):
    first_name = request.POST.get("first_name", None)
    contact_info = request.POST.get("contact_info", None)

    context = {}
    custom_errors = ""

    if (first_name):
        context["first_name"] = first_name

        if (contact_info):
            context["contact_info"] = contact_info

            # Create a circle
            new_circle = Circle(circle_name=first_name + "'s circle",
                                circle_created_date=timezone.now(), )
            new_circle.save()

            # Create our new member
            new_member = Member(circle=new_circle,
                                circle_owner=True,
                                member_name=first_name,
                                member_created_date=timezone.now(), )

            # Check to see if current contact info is valid phone or email
            if is_phone(contact_info):
                new_phone = phonenumbers.parse(contact_info, "US")
                new_phone = phonenumbers.format_number(new_phone, phonenumbers.PhoneNumberFormat.E164)

                new_member.member_phone = new_phone

            if is_email(contact_info):
                new_member.member_email = contact_info

            if not is_phone(contact_info) and not is_email(contact_info):
                # Bad data error
                custom_errors += "<li>contact info must be either a valid phone number OR email</li>"

            new_member.save()

            set_member_and_circle(request, new_circle, new_member)
        else:
            # Missing contact data error
            custom_errors += "<li>name is present but contact info is missing</li>"
    else:
        # Missing name data error
        custom_errors += "<li>"

        if (contact_info):
            context["contact_info"] = contact_info
            custom_errors += "contact info is present but "

        custom_errors += "name is missing</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["custom_errors"] = custom_errors
        context["anchor"] = "signup"
        return render(request, "circly/index.html", context)

    return HttpResponseRedirect(reverse("connect:flow", 
                                        kwargs={}))
Exemplo n.º 2
0
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}))