def register():

    try:
        firstname = request.json['fname']
        lastname = request.json['lname']
        email = request.json['email']
        regtype = int(request.json['regtype'])
        mobile = request.json['mobile']
        cityid = request.json['city']
        skill = request.json['skill']
        stateid = request.json['state']
        countryid = request.json['country']
        picurl = request.json['picurl']
        gender = request.json['gender']
        locationid = request.json['locationid']

        if locationid != "":
            locationid = int(locationid)

        if picurl == '':
            picurl = "default-image.jpg"
        '''
        if city!="":
            citylist=db.city.find_one({"city":city},{"_id":0,"cityid":1})
            cityid=citylist['cityid']
        else:
            cityid=""

        if state!="":
            statelist=db.state.find_one({"state":state},{"_id":0,"stateid":1})
            stateid=statelist['stateid']
        else:
            stateid=""

        if country!="":
            countrylist=db.country.find_one({"country":country},{"_id":0,"countryid":1})
            countryid=countrylist['countryid']
        else:
            countryid=""
'''
        addressJSON = {
            "address1": request.json['address1'],
            "address2": request.json['address2'],
            "city": cityid,
            "state": stateid,
            "country": countryid,
            "pincode": request.json['pincode']
        }
        salt = CommonUtils.generateRandomName()
        emailhash = CommonUtils.getHashValue()
        token = CommonUtils.generateRandomName()

        if regtype == 0:
            password = request.json['password']
            password = CommonUtils.password_hash(password, salt)
            sociallogin = False
            emailverified = False
            fbid = ""
            fbaccesstoken = ""
            registeredfrom = "jobwork"

        else:
            password = ""
            sociallogin = True
            emailverified = True
            fbid = request.json['fbid']
            fbaccesstoken = request.json['fbaccesstoken']
            if regtype == 1:
                registeredfrom = "facebook"
            elif regtype == 2:
                registeredfrom = "gmail"
        #return jsonify({"ok":1})
        userdata = list(User.find({"email": email}, {"_id": 0}))

        if User.count({"email": email}) == 0:
            userid = CommonUtils.generateRandomNo(User, "userid")
            User.insert({
                "userid": userid,
                "signupJSON": "",
                "email": email,
                "emailverified": emailverified,
                "emailhash": emailhash,
                "mobile": mobile,
                "mobileverified": False,
                "mobileotp": "",
                "isdcode": None,
                "fbid": fbid,
                "fbaccesstoken": fbaccesstoken,
                "password": password,
                "salt": salt,
                "token": token,
                "firstname": firstname,
                "lastname": lastname,
                "isadmin": False,
                "createddatetime": datetime.now(),
                "updateddatetime": datetime.now(),
                "addressJSON": addressJSON,
                "paymentdetailsJSON": [],
                "skillsJSON": skill,
                "languagesJSON": [],
                "educationCSV": [],
                "workplaceCSV": [],
                "certificateJSON": [],
                "proJSON": [],
                "forgotpasswordJSON": [],
                "gender": gender,
                "dob": "",
                "locationid": locationid,
                "aboutme": "",
                "picurl": picurl,
                "reportedJSON": [],
                "notificationJSON": [],
                "socaillogin": sociallogin,
                "facebookpicurl": "",
                "active": True,
                "paypal_id": "",
                "registeredfrom": registeredfrom
            })
            if regtype != 0:
                response = userDataResponse(email)
                # user = list(User.find({"email": email}, {"_id": 0}))
                return jsonify({
                    "status": 200,
                    "response": response,
                    "message": "",
                    "error": False,
                    "registedfrom": registeredfrom
                })
            else:
                emailhash = CommonUtils.getHashValue()
                result = User.update(
                    {
                        "userid": userid,
                        "token": token,
                        "email": request.json['email']
                    }, {"$set": {
                        "emailhash": emailhash
                    }})
                # Send email verification
                reset_password_link = str(URL) + "emailverify/" + str(
                    emailhash)

                subject = "Your jobwork.io Account Email Verification"
                msg = Message(subject,
                              sender=("JobWork", "*****@*****.**"),
                              recipients=[email])
                msg.html = render_template(
                    '/emailTemplates/email_verification_template.html',
                    name=firstname,
                    resetLink=reset_password_link,
                    email=email)
                mail.send(msg)
                EmailTracks.insert({
                    "emailtrackid":
                    CommonUtils.generateRandomNo(EmailTracks, "emailtrackid"),
                    "userid":
                    userid,
                    "email":
                    request.json['email'],
                    "subject":
                    subject,
                    "emailtext":
                    msg.html,
                    "createdatetime":
                    datetime.now(),
                    "updatedatetime":
                    datetime.now()
                })

            return jsonify({
                "status": 200,
                "response": {},
                "message": "verification mail sent",
                "error": True,
                "registedfrom": registeredfrom
            })

        elif userdata[0]['emailverified'] == False:
            emailhash = CommonUtils.getHashValue()
            userid = CommonUtils.generateRandomNo(User, "userid")
            db.user.remove({"email": email})
            User.insert({
                "userid": userid,
                "signupJSON": "",
                "email": email,
                "emailverified": emailverified,
                "emailhash": emailhash,
                "mobile": mobile,
                "mobileverified": False,
                "mobileotp": "",
                "isdcode": None,
                "fbid": fbid,
                "fbaccesstoken": fbaccesstoken,
                "password": password,
                "salt": salt,
                "token": token,
                "firstname": firstname,
                "lastname": lastname,
                "isadmin": False,
                "createddatetime": datetime.now(),
                "updateddatetime": datetime.now(),
                "addressJSON": addressJSON,
                "paymentdetailsJSON": [],
                "skillsJSON": skill,
                "languagesJSON": [],
                "educationCSV": [],
                "workplaceCSV": [],
                "certificateJSON": [],
                "proJSON": [],
                "forgotpasswordJSON": [],
                "gender": gender,
                "dob": "",
                "locationid": locationid,
                "aboutme": "",
                "picurl": picurl,
                "reportedJSON": [],
                "notificationJSON": [],
                "socaillogin": sociallogin,
                "facebookpicurl": "",
                "active": True,
                "paypal_id": "",
                "registeredfrom": registeredfrom
            })
            if regtype != 0:
                response = userDataResponse(email)
                return jsonify({
                    "status": 200,
                    "response": response,
                    "message": "existed now updated",
                    "error": False,
                    "registedfrom": registeredfrom
                })
            else:
                emailhash = CommonUtils.getHashValue()
                result = User.update(
                    {
                        "userid": userid,
                        "token": token,
                        "email": request.json['email']
                    }, {"$set": {
                        "emailhash": emailhash
                    }})
                # Send email verification
                reset_password_link = str(URL) + "emailverify/" + str(
                    emailhash)

                subject = "Your jobwork.io Account Email Verification"
                msg = Message(subject,
                              sender=("JobWork", "*****@*****.**"),
                              recipients=[email])
                msg.html = render_template(
                    '/emailTemplates/email_verification_template.html',
                    name=firstname,
                    resetLink=reset_password_link,
                    email=email)
                mail.send(msg)
                EmailTracks.insert({
                    "emailtrackid":
                    CommonUtils.generateRandomNo(EmailTracks, "emailtrackid"),
                    "userid":
                    userid,
                    "email":
                    request.json['email'],
                    "subject":
                    subject,
                    "emailtext":
                    msg.html,
                    "createdatetime":
                    datetime.now(),
                    "updatedatetime":
                    datetime.now()
                })

            return jsonify({
                "status": 200,
                "response": {},
                "message":
                "mail id exist but not verified yet verification mail sent",
                "error": True,
                "registedfrom": registeredfrom
            })

        elif regtype != 0:
            verify = False

            user = list(User.find({"email": email}, {"_id": 0}))

            if regtype == 1 and user[0]['registeredfrom'] == "facebook":
                verify = True

            elif regtype == 2 and user[0]['registeredfrom'] == "gmail":
                verify = True

            else:
                message = "account already registered from " + user[0][
                    'registeredfrom']
                return jsonify({
                    "status": 200,
                    "messsage": message,
                    "registedfrom": user[0]['registeredfrom'],
                    "response": {},
                    "error": True
                })
                #user = list(User.find({"email": email}, {"_id": 0}))
            if verify == True:

                #print(userdata[0]['cityname'][0])
                response = userDataResponse(email)
                print((response))
                return jsonify({
                    "status": 200,
                    "response": response,
                    "message": "",
                    "error": False,
                    "registedfrom": user[0]['registeredfrom']
                })
        else:
            return jsonify({
                "status": 200,
                "messsage": "email id already used",
                "response": {},
                "error": True,
                "registedfrom": userdata[0]['registeredfrom']
            })
    except Exception as e:
        return json.dumps(e, indent=4, default=json_util.default)
Example #2
0
def get_or_create_user(user_data, user_type, facebook_token):
    email = user_data.get("email", "")
    if email:
        is_user_exists = User.count({"signupJSON.email": email})
        if is_user_exists == 0:
            mobile, mobileotp = "", ""
            # emailhash = send_email(email)
            emailhash = CommonUtils.getHashValue()

            firstname = user_data.get('first_name', "")
            lastname = user_data.get('last_name', "")
            usertype = user_type
            city, location, device_token = "", "", ""
            fullname = user_data.get("name", "")
            password = CommonUtils.generateRandomName()

            if fullname and usertype:

                salt = CommonUtils.generateRandomName()

                # random.randrange(1000, 9999)

                signupJSON = SignupUtils.get_signup_json(email, fbid=user_data.get("id", ""),
                                                         fbaccesstoken=facebook_token)

                addressJSON = SignupUtils.get_user_address_json(city)

                proJSON = SignupUtils.get_pro_json()

                paymentdetailsJSON = SignupUtils.get_payment_detail_json()

                if password:
                    password = CommonUtils.password_hash(password, salt)
                token = request.json.get('token', "")
                userid = request.json.get('userid', "")
                if not userid:
                    userid = CommonUtils.generateRandomNo(User, "userid")
                if not token:
                    token = CommonUtils.generateRandomName()

                User.insert({
                    "userid": userid,
                    "email": email,
                    "mobile": mobile,
                    "mobileotp": str(mobileotp),
                    "emailhash": emailhash,
                    "password": password,
                    "salt": salt,
                    "token": token,
                    "usertype": usertype,
                    "firstname": firstname,
                    "lastname": lastname,
                    "device_token": device_token,
                    "signupJSON": signupJSON,
                    "emailverified": False,
                    "mobileverified": False,
                    "isdcode": None,
                    "fbid": "",
                    "fbaccesstoken": "",
                    "isadmin": False,
                    "createddatetime": datetime.datetime.now(),
                    "updateddatetime": datetime.datetime.now(),
                    "addressJSON": addressJSON,
                    "paymentdetailsJSON": paymentdetailsJSON,
                    "skillsJSON": [],
                    "languagesJSON": [],
                    "educationCSV": [],
                    "workplaceCSV": [],
                    "certificateJSON": [],
                    "proJSON": proJSON,
                    "forgotpasswordJSON": [],
                    "gender": "",
                    "dob": "",
                    "locationid": location,
                    "aboutme": "",
                    "picurl": "user-no-image.jpg",
                    "reportedJSON": [],
                    "notificationJSON": [],
                    "socaillogin": False,
                    "facebookpicurl": "",
                    "active": True,
                    "paypal_id": ""
                })

                session['cookiesUserid'] = int(userid)
                session['userValid'] = str(salt)
                session['cookiesToken'] = str(token)
                session['city'] = str(city)
                session['userloginName'] = str(fullname)
                session['location'] = str(location)
                session.permanent = True

                # Onboard Push notification
                PushNotificationUtils.notify_onboard(userid, fullname, device_token)
                # Onboard Email
                EmailUtils.send_onboard_email(userid, email, emailhash, firstname)

                return {
                    'status': 200,
                    'message': 'Successfull Register',
                    'userid': userid,
                    'token': token,
                    'city': city,
                    'location': location,
                    "firstname": firstname,
                    "lastname": lastname,
                    "picurl": "user-no-image.jpg",
                    "picPath": Constants.PROFIL_PIC_STATIC_PATH
                }
            else:
                return {'status': 400, 'message': 'Data can not be null.'}
        else:
            user = User.find_one({"email": email},
                                 {"_id": 0, "userid": 1, "token": 1, "city": 1, "location": 1, "firstname": 1,
                                  "lastname": 1, "picurl": 1, "picPath": Constants.PROFIL_PIC_STATIC_PATH})
            return {
                'status': 200,
                'message': 'Successfull Register',
            }.update(user)
    else:
        return {'status': 202, 'message': 'Data can not be null.'}
Example #3
0
def user_register():
    email = request.json['email']

    if email:
        is_user_exists = User.count({"signupJSON.email": email})
        if is_user_exists == 0:
            mobile, mobileotp = "", ""
            # emailhash = send_email(email)
            emailhash = CommonUtils.getHashValue()

            firstname = request.json['firstname']
            lastname = request.json['lastname']
            usertype = request.json['usertype']
            city = int(request.json['city'])
            location = int(request.json['location'])
            device_token = request.json.get('device_token', "")
            fullname = firstname + " " + lastname
            password = request.json['password'].strip()

            if firstname and lastname and usertype and city:

                salt = CommonUtils.generateRandomName()

                # random.randrange(1000, 9999)

                signupJSON = SignupUtils.get_signup_json(email)

                addressJSON = SignupUtils.get_user_address_json(city)

                proJSON = SignupUtils.get_pro_json()

                paymentdetailsJSON = SignupUtils.get_payment_detail_json()

                if password:
                    password = CommonUtils.password_hash(password, salt)
                token = request.json.get('token', "")
                userid = request.json.get('userid', "")
                if not userid:
                    userid = CommonUtils.generateRandomNo(User, "userid")
                if not token:
                    token = CommonUtils.generateRandomName()

                User.insert({
                    "userid": userid,
                    "email": email,
                    "mobile": mobile,
                    "mobileotp": str(mobileotp),
                    "emailhash": emailhash,
                    "password": password,
                    "salt": salt,
                    "token": token,
                    "usertype": usertype,
                    "firstname": firstname,
                    "lastname": lastname,
                    "device_token": device_token,
                    "signupJSON": signupJSON,
                    "emailverified": False,
                    "mobileverified": False,
                    "isdcode": None,
                    "fbid": "",
                    "fbaccesstoken": "",
                    "isadmin": False,
                    "createddatetime": datetime.datetime.now(),
                    "updateddatetime": datetime.datetime.now(),
                    "addressJSON": addressJSON,
                    "paymentdetailsJSON": paymentdetailsJSON,
                    "skillsJSON": [],
                    "languagesJSON": [],
                    "educationCSV": [],
                    "workplaceCSV": [],
                    "certificateJSON": [],
                    "proJSON": proJSON,
                    "forgotpasswordJSON": [],
                    "gender": "",
                    "dob": "",
                    "locationid": location,
                    "aboutme": "",
                    "picurl": "user-no-image.jpg",
                    "reportedJSON": [],
                    "notificationJSON": [],
                    "socaillogin": False,
                    "facebookpicurl": "",
                    "active": True,
                    "paypal_id": ""
                })

                session['cookiesUserid'] = int(userid)
                session['userValid'] = str(salt)
                session['cookiesToken'] = str(token)
                session['city'] = str(city)
                session['userloginName'] = str(fullname)
                session['location'] = str(location)
                session.permanent = True

                # Onboard Push notification
                PushNotificationUtils.notify_onboard(userid, fullname,
                                                     device_token)
                # Onboard Email
                EmailUtils.send_onboard_email(userid, email, emailhash,
                                              firstname)

                return make_response(
                    jsonify({
                        'status': 200,
                        'message': 'Successfull Register',
                        'userid': userid,
                        'token': token,
                        'city': city,
                        'location': location,
                        "firstname": firstname,
                        "lastname": lastname,
                        "picurl": "user-no-image.jpg",
                        "picPath": picPath
                    }), 200)
            else:
                return make_response(
                    jsonify({
                        'status': 202,
                        'message': 'Data can not be null.'
                    }), 400)
        else:
            return make_response(
                jsonify({
                    'status': 201,
                    'message': 'Email already exits.'
                }), 400)
    else:
        return make_response(
            jsonify({
                'status': 202,
                'message': 'Data can not be null.'
            }), 400)