def download_package(): """ Tries to retrieve package from git """ data = request.get_json() validate_data(data, {'token', 'package_id'}) package = Package.get_package_by_id(data['package_id']) package.download_package() return response_ok({'data': 'Done'})
def login_action(): """ API login entry point """ data = request.get_json() validate_data(data, {'username', 'password'}) token = login(data['username'], data['password']) data = {"token": token.token, "verified": "true"} return response_ok(data)
def save_package(): """ Saves package (create or update) """ data = request.get_json() validate_data(data, {'token'}) validate_token(data['token']) package = Package.save_or_create(data) return response_ok_obj(package)
def delete_package(): """ Deletes package """ data = request.get_json() validate_data(data, {'token', 'package_id'}) package = Package.get_package_by_id(data['package_id']) package.delete_package() data = {"data": "done"} return response_ok(data)
def update_user(): """ Update user with suplied data, now works only for email, password, name and user type """ data = request.get_json() validate_data(data, {'token', 'user_id'}) user_id = data['user_id'] token = data['token'] validate_token(token) user = User.update_user_by_id_from_array(user_id, data) return response_ok_obj(user)
def delete_user(): """ Deletes user by it's id (only deactivation) """ data = request.get_json() validate_data(data, {'token', 'user_id'}) token = data['token'] user_id = data['user_id'] validate_token(token) user = User.get_user_by_id(user_id) User.deactivate_user_by_id(user.id) data = {"data": "done"} return response_ok(data)
def is_user_authorised(): """ Information about users token, whenever its valid token or not """ data = request.get_json() validate_data(data, {'token'}) token = check_token_exists(data['token']) if token == None\ or (token.created + timedelta(hours=24) < datetime.utcnow() \ and token.update + timedelta(hours=2) < datetime.utcnow())\ or token.user.active == False: raise ApiException(403, "Invalid token.") tokenData = {"verified": "true" if token.user.activated else "false"} return response_ok(tokenData)
def activation_action(): """ Acc activation """ data = request.get_json() validate_data(data, {'token'}) token = check_token_exists(data['token']) if token == None: raise ApiException(403, "Invalid token.") if token.user.activated: raise ApiException(400, "User has been already activated.") #check code somehow token.user.update_activation_by_id(token.user.id, True) data = {"data": "activated"} return response_ok(data)
def registration_action(): """ Registration """ data = request.get_json() validate_data(data, {'username', 'password', 'email'}) if not validate_email(data['email']): raise ApiException(400, "email") if not validate_username(data['username']): raise ApiException(400, "username") if not validate_password(data['password']): raise ApiException(400, "password") if User.check_unique(data['username'], data['email'], True): userEntity = User(data['username'], data['email'], data['password']) userEntity.save_entity() token = authenticate(None, True, userEntity.id) data = {"user_id": userEntity.id, "token": token.token} return response_ok(data)
def test_validate_data(self): """ Test validate_data """ with pytest.raises(ApiException) as err: validate_data({}) assert err.value.message == "data" assert err.value.errorCode == 400 validate_data({"test": "noerror"}) with pytest.raises(ApiException) as err: validate_data({"test": "noerror"}, ["user_id"]) assert err.value.message == "data" assert err.value.errorCode == 400 validate_data({"test": "noerror"}, ["test"])