Example #1
0
def user_repo_test():

    user_service = UserService()

    LOGGER.debug("=======USER REPO TEST=======")

    username = "******"
    password = "******"
    first_name = "Muhammad"
    last_name = "Aftab"
    email = "*****@*****.**"

    LOGGER.debug("adding a user")
    user, msg = user_service.add_user(username, password, first_name, last_name, email)
    LOGGER.debug(msg)

    if user is not None:
        LOGGER.debug("fetching the newly added user")
        LOGGER.debug(user_repo.find_by_username(user.username))

    LOGGER.debug("modifying user")
    new_user = {"first_name": "Aftab",
                "last_name": "Khan",
                "email": "*****@*****.**",
                "password": "******"}
    updated_user = user_repo.update_user(username, new_user)
    LOGGER.debug("the updated user is")
    LOGGER.debug(updated_user)

    LOGGER.debug("deleting user")
    status = user_repo.delete_user(username)
    LOGGER.debug(status)
Example #2
0
def get_all_users():
    # get all users
    if request.method == 'GET':
        user = UserService.get_all_user()
        return make_response(jsonify(user), 200)
    # Create user
    if request.method == 'POST':
        user_data = request.get_json()
        user_id = UserService.add_user(user_data)
        return make_response(f"{user_id}", 200)
Example #3
0
def fake_other():
    s = UserService()
    s.add_user("admin", "1234", "*****@*****.**", 1)
    s.add_user("editor", "1234", "*****@*****.**", 2)
    try:
        con = mysql_pool.get_connection()
        con.start_transaction()
        cursor = con.cursor()
        cursor.execute("INSERT INTO `t_role` VALUES ('1', '管理员');")
        cursor.execute("INSERT INTO `t_role` VALUES ('2', '新闻编辑');")
        cursor.execute("INSERT INTO `t_type` VALUES ('1', '要闻');")
        cursor.execute("INSERT INTO `t_type` VALUES ('2', '体育');")
        cursor.execute("INSERT INTO `t_type` VALUES ('3', '科技');")
        cursor.execute("INSERT INTO `t_type` VALUES ('4', '娱乐');")
        cursor.execute("INSERT INTO `t_type` VALUES ('5', '历史');")
        con.commit()
    except Exception as e:
        if "con" in dir():
            con.rollback()
        print(e)
    finally:
        if "con" in dir():
            con.close()
Example #4
0
 def user(self, ansible_json):
     us = UserService(ansible_json)
     try:
         if eq(ansible_json['state'], 'absent'):
             result = yield us.del_user()
         elif eq(ansible_json['state'], 'present'):
             result = yield us.add_user()
         elif eq(ansible_json['state'], 'update'):
             result = yield us.update_password()
         else:
             result = self.return_json(2, 'Not Supported')
     except Exception as err:
         self.write(self.return_json(-1, err.args))
     else:
         self.write(result)
Example #5
0
                                username = input('\n\tusername: '******'\n\tpassword: '******'\n\ttype in your password again: ')
                                if password != re_password:
                                    print('password should be same! return to prev step in 3 sec')
                                    time.sleep(3)
                                    continue
                                email = input('\n\temail: ')
                                result = __role_service.search_list()
                                for i in range(len(result)):
                                    role = result[i]
                                    print(Fore.LIGHTBLUE_EX, '\n\t{0}.{1}'.format(i+1, role[1]))
                                print(Style.RESET_ALL)
                                opt=input('\n\twhat is your role: ')
                                role_id = result[int(opt)-1][0]
                                __user_service.add_user(username, password, email, role_id)
                                print('\n\tsuccessfully save return to prev step in 3 sec')
                                time.sleep(3)

                            elif opt =='2':
                                page = 1
                                while True:
                                    os.system('cls')
                                    count_page = __user_service.search_count_page()
                                    result = __user_service.search_user_list(page)
                                    for i in range(len(result)):
                                        news = result[i]
                                        print(Fore.LIGHTBLUE_EX,
                                              '\n\t{0}\t{1}\t{2}'.format(i + 1, news[1], news[2]))
                                    print(Fore.LIGHTBLUE_EX, '\n\t==================')
                                    print(Fore.LIGHTBLUE_EX, '\n\t{0}/{1}'.format(page, count_page))
Example #6
0
def device_repo_test():

    user_service = UserService()

    print "\n\n\n=======DEVICE REPO TEST======="
    print "\ncreating a test user"
    user, msg = user_service.add_user("muhaftab", "1234", "Muhammad", "Aftab", "*****@*****.**")
    print msg

    print "adding a new device for user:  %s" % user.username
    device1 = device_repo.add_device(user.username, "TableLamp")
    print "returned device is ", device1
    device2 = device_repo.add_device(user.username, "Kettle")
    print "returned device is ", device2

    print "\nfetching the new device from db"
    device = device_repo.find_device(user.username, device1.device_id)[0]
    print device

    print "\nfetching the user to see if device is added for user"
    user = user_service.get_user(user.username)
    print [d.serialize() for d in user.devices]

    print "\nadding some consumption data for the device"
    c1 = DeviceConsumption(10.0, 0.12, False, datetime.datetime.now())
    c2 = DeviceConsumption(11.0, 2.12, True, datetime.datetime.now())
    device_repo.add_device_consumption(device, c1)
    device_repo.add_device_consumption(device, c2)

    print "\ntesting if consumption data is added to device"
    print [c.serialize() for c in device.consumption]

    print "\nModifying device"
    new_device = Device("CoffeeMaker")
    updated_device = device_repo.update_device(user.username, device.device_id, new_device)
    print updated_device

    print "\ntesting if consumption data exists for updated device"
    print [c.serialize() for c in updated_device.consumption]

    print "\nadding device model to the device"
    json_params = {"p_peak": 80.8, "p_stable": 50.0, "lambda": 0.31}
    m1 = DeviceModel("ExponentialDecay", json_params)
    device_repo.add_device_model(updated_device, m1)
    print updated_device.serialize()

    print "\nfetching the user again to see if updated device is shown"
    user = user_service.get_user(user.username)
    print user.serialize()

    print "\nfetching list of devices for the user"
    print [device.serialize() for device in user_service.get_devices(user.username)]

    print "\ndeleting device"
    status = device_repo.delete_device(user.username, device.device_id)
    print status

    print "\nfetching the user agian to see if device is indeed deleted"
    user = user_service.get_user(user.username)
    print [d for d in user.devices]

    print "\nfinally deleting user"
    status = user_service.delete_user(user.username)
    print status