Beispiel #1
0
    def authorize_phone_number(self, phone_number_str):
        try:
            phone_number = PhoneNumber.objects.get(phone_number=phone_number_str)
        except PhoneNumber.DoesNotExist:
            new_user = User.objects.create_user()
            phone_number = self.create(
                phone_number=phone_number_str, user=new_user, date_created=timezone.now(), verified=False
            )
            new_user.pick_anonymous_avatar(phone_number_str)
            new_user.save()

            user_avatar_changed.send(sender=self, user=new_user)

        confirmation_key = generate_key()
        if is_test_number(phone_number.phone_number):
            confirmation_code = "6666"
        else:
            confirmation_code = crypto.get_random_string(4, string.digits)

        PhoneNumberConfirmSMSCode.objects.get_or_create(
            phone_number=phone_number,
            confirmation_key=confirmation_key,
            confirmation_code=confirmation_code,
            date_created=timezone.now(),
        )
        if not settings.DEBUG:
            send_sms(phone_number.phone_number, "Your Glance Verification Code: " + confirmation_code)

        return confirmation_key
Beispiel #2
0
    def invite_phone_number(
        self,
        inviter,
        phone_number_str,
        nickname,
        date_invited,
        sms_message_formatter,
        sms_analytics_event_name,
        sms_analytics_event_properties,
    ):
        """
        phone_number_str: Must be formatted as international E164

        Returns the user
        """
        try:
            phone_number = PhoneNumber.objects.get(phone_number=phone_number_str)

            if phone_number.verified:
                return phone_number.user
        except PhoneNumber.DoesNotExist:
            new_user = User.objects.create_user(nickname=nickname)
            phone_number = PhoneNumber.objects.create(
                phone_number=phone_number_str, user=new_user, date_created=date_invited, verified=False
            )
            new_user.pick_anonymous_avatar(phone_number_str)
            new_user.save()

            user_avatar_changed.send(sender=self, user=new_user)

        link_code_object, created = PhoneNumberLinkCode.objects.get_or_create(
            phone_number=phone_number,
            defaults={
                "invite_code": PhoneNumberLinkCode.generate_invite_code(),
                "inviting_user": inviter,
                "date_created": date_invited,
                "was_visited": False,
            },
        )

        inviter_phone = inviter.get_primary_phone_number()
        if inviter_phone:
            sender_phone = inviter_phone.phone_number
        else:
            sender_phone = None

        message = sms_message_formatter(link_code_object)

        invite_url_prefix = "https://i.useglance.com"
        send_sms(
            phone_number.phone_number,
            message + "\n" + link_code_object.get_invite_page(invite_url_prefix),
            sender_phone,
        )

        track_event(link_code_object.phone_number.user, sms_analytics_event_name, sms_analytics_event_properties)

        return link_code_object.phone_number.user
Beispiel #3
0
    def process_upload_request(self, request, uploaded_chunks):
        """Uploads user avatar to the randomly picked location
        where we host user avatars and returns Response"""

        avatar_image_filename = settings.AVATAR_FILENAME_FORMAT_STRING.format(
            user_id=request.user.id,
            timestamp=timezone.now().strftime("%s")
        )
        avatar_location_format_str = random.choice(settings.AVATAR_BUCKETS)
        storage, bucket_name, filename = avatar_location_format_str.split(":")

        # Write uploaded data to temporary file
        # File will be delete once handler is closed
        temp_file = tempfile.TemporaryFile()
        for chunk in uploaded_chunks:
            temp_file.write(chunk)
        temp_file.seek(0)

        if storage == "s3":
            # Upload to S3
            try:
                conn = S3Connection(settings.AWS_ACCESS_KEY,
                                    settings.AWS_SECRET_ACCESS_KEY)
                bucket = conn.get_bucket(bucket_name)
                key = Key(bucket, avatar_image_filename)
                key.metadata = {'Content-Type': 'image/jpeg'}
                key.set_contents_from_file(temp_file)
                # Otherwise it's not accessible
                key.make_public()
                key.close(fast=True)
                temp_file.close()
            except:
                temp_file.close()
                raise
        else:
            temp_file.close()
            raise ValueError("Failed to upload avatar. "
                             "Unknown storage '{0}'.".format(storage))

        request.user.avatar_file = avatar_location_format_str.format(
            filename=avatar_image_filename)
        request.user.save()

        user_avatar_changed.send(sender=self, user=request.user)

        return Response()