def create(): try: data = request.form.to_dict() data['password'] = generate_encrypt_password(data.get('password','')) user = User.objects.create(**data) return render.template('admin/user/detail.html', user=user), 201 except NotUniqueError as e: abort(400, "Duplicated, username is existed") # Duplicate except ValidationError as ve: abort(400, "Validation Error")
def authenticate(username, password): try: user = User.objects.get(username=username) encrypt_pwd = generate_encrypt_password(password) if encrypt_pwd == user.password: if system.get_env() != 'testing': session['user'] = user.to_json() return True else: return False except: return False
def update(user_id): try: data = request.form.to_dict() # Hash the password if exists if 'password' in data: data['password'] = generate_encrypt_password(data.get('password', '')) update_map = dict([('set__' + key, value) for key, value in data.items()]) u = User.objects.get(uid=user_id) u.update(**update_map) u.reload() return render.template('admin/user/detail.html', user=u) except DoesNotExist: abort(404) except NotUniqueError: abort(400, "Duplicated, username is existed") except ValidationError as ve: abort(400, "Validation error")
def setUp(self): # Make user for test user1 = UserFactory.create(username="******", password=generate_encrypt_password("testpass1")) user2 = UserFactory.create(username="******", password=generate_encrypt_password("another"))