Beispiel #1
0
    def clean_phone(self):
        dirty_phone = self.cleaned_data["phone"]

        phone_numeric = check_phone_validity(dirty_phone, self.user)

        # Prevent two users using the same phone
        query = Profile.objects.filter(phone=phone_numeric)
        if query and query[0].verified:
            log.warning("%s: tried to enter phone %s which belongs to %s" % (self.user, clean_phone, query[0].user))
            raise forms.ValidationError("Number already registered by %s" % query[0].user)

        profile = self.user.get_profile()

        # Update phone
        if phone_numeric != profile.phone:
            log.info("%s: changed phone %s -> %s" % (self.user, profile.phone, phone_numeric))
            profile.phone = phone_numeric
            profile.verified = False
            profile.save()

        # Start verification process
        error = send_verification(profile.phone, profile.user)
        if error:
            log.warning("%s: Phone verification failed [%s]" % (self.user, error))
            raise forms.ValidationError(error)

        return phone_numeric
Beispiel #2
0
    def clean_code(self):
        code = self.cleaned_data["code"]

        error = check_verification(self.user, code)
        if error:
            log.warning("%s: code verification failed [%s]" % (self.user, error))
            raise forms.ValidationError(error)

        send_message(User.objects.filter(pk=1)[0], "Welcome aboard ! Add some friends and start SMSing !", [self.user])
        return code
Beispiel #3
0
    def clean_recipients(self):
        rcp = self.cleaned_data["recipients"]
        if len(rcp) > 10:
            log.info("%s: tried to send to %d people" % (self.user, len(rcp)))
            raise forms.ValidationError("Cannot send to more then 10 people")

        if len(rcp) + get_sms_last_24h(self.user) > 30:
            log.warning("%s: tried to send to more then 30 people" % self.user)
            raise forms.ValidationError("Cannot send more then 30 SMS a day")

        for i in rcp:
            last = SMSMessage.objects.filter(
                by=self.user, to=i, date__gt=(datetime.datetime.now() - datetime.timedelta(minutes=1))
            )
            if last:
                log.warning("%s: trying to send too fast" % self.user)
                raise forms.ValidationError(
                    "You can send again to %s in %s seconds"
                    % (i, 60 - (datetime.datetime.now() - last[0].date).seconds)
                )

        return rcp
Beispiel #4
0
def check_verification(user, code):
    pv_list = PhoneVerification.objects.filter(user=user)
    if not pv_list:
        log.warning("%s Checking verification when once was not yet sent" % (user))
        return "No verification code was sent yet"

    pv = pv_list[0]

    if pv.phone != user.get_profile().phone:
        log.warning(
            "%s: Entered verification for different phone %s != %s" % (user, pv.phone, user.get_profile().phone)
        )
        return "Verification code for a phone different then what user has"

    if pv.code != code.lower():
        log.warning("%s: Incorrect Verification code" % (user))
        return "Incorrect code"

    pv.delete()

    return ""