def logout_view_staff(request): obj = {} obj['status'] = False data_id = get_param(request, 'data_id', None) if request.user.is_anonymous: user = CAUsers.objects.get(id=data_id) user.auth_token = user.id + random_str_generator() user.save() # logout(user) else: print user.first_name user.auth_token = user.id + random_str_generator() user.save() logout(request) obj['result'] = "" obj['status'] = True obj['message'] = "Logout Success" response = HttpResponse(json.dumps(obj), content_type='application/json') return response # <------------------ End ------------------------->
def reset_pass_staff(request): obj = {} obj['status'] = False obj['result'] = {} obj['result']['user'] = {} obj['result']['auth'] = False password = get_param(request, 'pass', None) secret_string = get_param(request, 'sec_string', None) try: user = CAUsers.objects.get(secret_string=secret_string, active=True) user.set_password(password) randstring = user.id + random_str_generator(size=6) randstring2 = user.id + random_str_generator(size=6) user.secret_string = randstring user.auth_token = randstring2 user.save() obj['result']['user']['auth_token'] = randstring2 obj['result']['user']['id'] = user.id obj['result']['user']['first_name'] = user.first_name obj['result']['user']['last_name'] = user.last_name obj['result']['user']['email'] = user.email obj['result']['user']['user_role'] = user.user_role obj['result']['user']['is_staff'] = user.is_staff obj['result']['auth'] = True obj['message'] = "Password Reset Success" except: obj['message'] = "Invalid Request Please Try Resetting the Password Again" obj['status'] = True return HttpResponse(json.dumps(obj), content_type='application/json')
def send_password_reset(request): obj = {} obj['status'] = False email = get_param(request, 'email', None) randstring = "" try: user = CAUsers.objects.get(email=email, active=True) if user: print 1 randstring = user.id + random_str_generator(size=6) # Mailing function to send email to the user message = "Reset Request Success" user.secret_string = randstring user.save() mailing.send_password_reset_email( name=user.first_name, email=user.email, secret_string=randstring.encode('utf-8')) else: message = "User Doesn't exist" except: message = "User Doesn't exist" obj['user'] = None obj['status'] = True obj['message'] = message response = HttpResponse(json.dumps(obj), content_type='application/json') return response
def upload_file(file): s3 = boto3.resource('s3') ts = time.time() created_at = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') final_filename = "img-" + random_str_generator(2) + str(ts).replace( ".", "") + ".jpg" s3.Object(bucket_name, 'old_q_images/' + final_filename).put(Body=open(file, 'rb')) filepath = "https://s3.amazonaws.com/" + bucket_name + "/old_q_images/" + final_filename if os.path.exists(file): os.remove(file) return filepath
def login_view_staff(request): obj = {} obj['status'] = False email = get_param(request, 'email', None) password = get_param(request, 'pass', None) secret_string = get_param(request, 'sec_string', None) auth_token = get_param(request, 'auth_token', None) status = False print auth_token if email: email = email.lower() email = cleanstring(email) obj['result'] = {} obj['result']['user'] = {} # obj['user'] = {} message = "" if auth_token: try: user = CAUsers.objects.get(auth_token=auth_token, active=True) user.backend = 'django.contrib.auth.backends.ModelBackend' message = "User Found" login(request, user) obj['result']['user']['auth_token'] = auth_token obj['result']['user']['id'] = user.id obj['result']['user']['first_name'] = user.first_name obj['result']['user']['last_name'] = user.last_name obj['result']['user']['email'] = user.email obj['result']['user']['user_role'] = user.user_role obj['result']['user']['is_staff'] = user.is_staff obj['result']['auth'] = True message = "Login Success!" status = True print 1 except: obj['result']['auth'] = False message = "Auth Token Expired" obj['result']['user'] = None status = False print 2 else: try: user = CAUsers.objects.get(email=email, active=True) if user: print 3 user.backend = 'django.contrib.auth.backends.ModelBackend' message = "User Found" if user.check_password(password): print 4 login(request, user) new_string = user.id + random_str_generator() user.auth_token = new_string obj['result']['user']['auth_token'] = new_string obj['result']['user']['id'] = user.id obj['result']['user']['first_name'] = user.first_name obj['result']['user']['last_name'] = user.last_name obj['result']['user']['email'] = user.email obj['result']['user']['user_role'] = user.user_role obj['result']['user']['is_staff'] = user.is_staff obj['result']['auth'] = True message = "Login Success!" status = True user.save() elif user.secret_string == secret_string: print 5 login(request, user) new_string = user.id + random_str_generator() user.auth_token = new_string obj['result']['user']['auth_token'] = new_string obj['result']['user']['id'] = user.id obj['result']['user']['first_name'] = user.first_name obj['result']['user']['last_name'] = user.last_name obj['result']['user']['email'] = user.email obj['result']['user']['user_role'] = user.user_role obj['result']['user']['is_staff'] = user.is_staff obj['result']['auth'] = True status = True message = "Login Success!" user.save() else: print 6 message = "Incorrect Password" obj['result']['auth'] = False status = False else: print 7 message = "User Doesn't exist" obj['result']['auth'] = False obj['result']['user'] = None status = False except: print 8 if email: print 9 message = "User Doesn't exist" obj['result']['auth'] = False obj['result']['user'] = None status = False obj['status'] = status obj['message'] = message response = HttpResponse(json.dumps(obj), content_type='application/json') return response
def create_update_user(request): error = False success = False error_message_list = [] output = CAUsers.objects.none() message = "Request Recieved" operation = get_param(request, 'operation', None) fname = get_param(request, 'fname', None) lname = get_param(request, 'lname', None) email = get_param(request, 'email', None) is_staff = get_param(request, 'is_staff', None) user_role = get_param(request, 'user_role', None) data_id = get_param(request, 'data_id', None) # user fields check and correction if fname: fname = cleanstring(fname.lower()) else: error = True error_message_list.append("Missing fname") if lname: lname = cleanstring(lname.lower()) else: error = True error_message_list.append("Missing lname") if operation == "create": if email: email = cleanstring(email.lower()) if validate_email(email): pass else: error = True error_message_list.append("Invalid email") else: error = True error_message_list.append("Missing email") check_user_role = listvar_check(variable_name="user_role", value=user_role, allowedlist=allowed_roles) if not check_user_role['error']: user_role = str(user_role) else: error = True error_message_list.append(check_user_role['errormessage']) check_is_staff = booleanvar_check(variable_name="is_staff", value=is_staff) if not check_is_staff['error']: is_staff = check_is_staff['output'] if is_staff: check_user_role = listvar_check(variable_name="user_role", value=user_role, allowedlist=staff_roles) if not check_user_role['error']: user_role = str(user_role) else: error = True error_message_list.append(check_user_role['errormessage']) else: user_role = "user" else: error = True error_message_list.append(check_is_staff['errormessage']) if operation == "update": if data_id: try: user = CAUsers.objects.get(id=data_id) except: error = True error_message_list = ['invalid data_id'] else: error = True error_message_list = ['missing data_id'] if not error: if operation == "create": users = CAUsers.objects.filter(email=email) if users.count() > 0: message = "User Already Exists!" output = users success = False else: user_new = CAUsers.objects.create(first_name=fname, last_name=lname, email=email, is_staff=is_staff, user_role=user_role) user_new.set_password(defaultpassword) user_new.secret_string = (user_new.id + random_str_generator()) user_new.auth_token = (user_new.id + random_str_generator()) user_new.save() output = [user_new] success = True message = "User Created!" else: user.first_name = fname user.last_name = lname user.is_staff = is_staff if not is_staff: user.user_role = "user" else: if user_role in staff_roles: user.user_role = user_role else: error = True user.user_role = "staff" user.save() output = [user] success = True message = "User Updated!" else: message = "Errors | Refer Error List!" return { 'output': output, 'message': message, 'error': error, 'error_message_list': error_message_list, 'success': success }