Exemplo n.º 1
0
def activate_account(req, **kwargs):
    """
    :kwargs: phase (there are two phases, first generate, second activate), code
            medium ( phone | email)
    """

    if kwargs['phase'] == 'generate':
        try:
            activation = Activations.create(user=req.user, code=fresh_pin(6))
            message = f"""Here is your activation code from @Africaniz {activation.code}. It will expire in 5 minutes."""
            if kwargs['medium'] == 'email':
                if req.user.email_verified:
                    return {'status': False, 'msg': 'Email Verified.'}
                else:
                    template = {
                        'id': 'd-ddcfb895e0904b98bc78828c27d63df2',
                        'data': {
                            'code': activation.code
                        }
                    }
                    send_email(req.user.email, message, template)
            elif kwargs['medium'] == 'phone':
                if not req.user.phone:
                    return {'status': False, 'msg': 'Null Number.'}
                if req.user.phone_verified:
                    return {'status': False, 'msg': 'Phone Verified.'}
                else:
                    pass
        except Exception as e:
            return {'status': False, 'msg': 'Try Again.'}
        return {'status': True, 'msg': 'Pin Sent.'}
    elif kwargs['phase'] == 'activate':
        try:
            activation = Activations.get_or_none(code=kwargs['code'])
            if activation:
                now = datetime.datetime.now()
                tdelta = now - activation.ctime

                if tdelta.seconds >= 5 * 60:
                    return {'status': False, 'msg': 'Pin Expired.'}
                if activation.user == req.user:
                    if kwargs['medium'] == 'email':
                        Users.update(email_verified=True).where(
                            Users.id == req.user.id).execute()
                        msg = 'Email'
                    elif kwargs['medium'] == 'phone':
                        Users.update(phone_verified=True).where(
                            Users.id == req.user.id).execute()
                        msg = 'Phone'
                else:
                    return {'status': False, 'msg': 'Wrong User.'}
            else:
                return {'status': False, 'msg': 'Ungenerated Pin.'}
        except Exception as e:
            return {'status': False, 'msg': "Server Error."}
        return {'status': True, 'msg': f'{msg} Activated.'}
Exemplo n.º 2
0
def resend_email(req, **kwargs):
    """
    :kwargs: email
    """

    try:

        user = Users.get_or_none(email=kwargs["email"])

        if user:
            message = f"""Welcome to <strong> @Africaniz</strong>,.Akwaaba. 
                        Go to <a href='{req.referer}'>Africaniz Login.</a>"""
            send_email(kwargs["email"], message)
            return {"status": True, "msg": 'Email Sent.'}
        else:
            return {"status": False, "msg": "Missing User."}

    except Exception as e:
        return {"status": False, "msg": "Resend Failed."}
Exemplo n.º 3
0
def create_user(req, **kwargs):
    """
    :kwargs: email, phone, level, country, city, code, phone_code, mode
    """
    try:
        info = {
            'personal': {
                'phone': kwargs['phone'],
                'email': kwargs['email'],
                'country': kwargs['country'],
                'code': kwargs['code'],
                'city': kwargs['city']
            }
        }
        if kwargs['mode'] == 'create':

            user = Users.create(email=kwargs['email'],
                                phone=kwargs['phone_code'],
                                level=kwargs['level'],
                                info=info)
            message = f"""Welcome to <strong> @Africaniz</strong>,.Akwaaba. 
                            Go to <a href='https://www.africaniz.com/auth/reset'>Africaniz Reset Password.</a>"""
            template = {'id': 'd-a57c95d98df745008d1caeedcaec18fd', 'data': {}}
            send_email(kwargs["email"], message, template)
        elif kwargs['mode'] == 'edit':
            user = Users.update(
                email=kwargs['email'],
                phone=kwargs['phone_code'],
                level=kwargs['level'],
            ).where(Users.id == kwargs['id']).execute()
            user = Users.get_by_id(kwargs['id'])
            user.info['personal']['city'] = info['personal']['city']
            user.info['personal']['code'] = info['personal']['code']
            user.info['personal']['country'] = info['personal']['country']
            user.info['personal']['email'] = info['personal']['email']
            user.info['personal']['phone'] = info['personal']['phone']
            user.save()
        else:
            return {'status': False, 'msg': 'Command Unknown.'}

    except Exception as e:
        return {'status': False, 'msg': 'Exception Raised.'}
    return {'status': True, 'msg': 'Task Done.'}
Exemplo n.º 4
0
def reset_password(req, **kwargs):
    """
    :kwargs:
    """
    """
    :kwargs: phase (there are two phases, first generate, second reset), code
            medium ( phone | email)
    """

    if kwargs['phase'] == 'generate':
        try:
            activation = Activations.create(code=fresh_pin(6))
            message = f"""Here is your activation code from @Africaniz {activation.code}. It will expire in 5 minutes."""
            template = {
                'id': 'd-ddcfb895e0904b98bc78828c27d63df2',
                'data': {
                    'code': activation.code
                }
            }
            send_email(kwargs['email'], message, template)
        except Exception as e:
            return {'status': False, 'msg': 'Try Again.'}
        return {'status': True, 'msg': 'Pin Sent.'}
    elif kwargs['phase'] == 'reset':
        try:
            activation = Activations.get_or_none(code=kwargs['code'])
            if activation:
                now = datetime.datetime.now()
                tdelta = now - activation.ctime

                if tdelta.seconds >= 5 * 60:
                    return {'status': False, 'msg': 'Pin Expired.'}
                if 'password' in kwargs and kwargs['password'] != "":
                    Users.update(password=bcrypt.hashpw(
                        kwargs["password"].encode(), bcrypt.gensalt())).where(
                            Users.email == kwargs['email']).execute()
                else:
                    return {'status': False, 'msg': 'Missing Password'}
            else:
                return {'status': False, 'msg': 'Ungenerated Pin.'}
        except Exception as e:
            return {'status': False, 'msg': "Server Error."}
        return {'status': True, 'msg': 'Password Reset.'}
Exemplo n.º 5
0
def register(req, **kwargs):
    """
    :kwargs: email, password
    """

    try:

        user = Users.create(
            email=kwargs["email"],
            password=bcrypt.hashpw(kwargs["password"].encode(),
                                   bcrypt.gensalt()),
        )

        message = f"""Welcome to <strong> @Africaniz</strong>,.Akwaaba. 
                            Go to <a href='https://www.africaniz.com/auth/login'>Africaniz Login.</a>"""
        template = {'id': 'd64d339f-5146-48ea-bdae-35244673c420', 'data': {}}
        send_email(kwargs["email"], message, template)

    except Exception as e:
        return {"status": False, "msg": "Email Exists."}

    return {"status": True, "msg": "Registration Successfull."}