Add rename profile rpc method (#682)

* Add rpc method to rename profile

* Add web endpoint for rename profile
This commit is contained in:
Jonathan Zernik 2021-01-24 16:53:09 -08:00 committed by GitHub
parent f2a6cd7e66
commit 83c0b295e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 74 additions and 0 deletions

View file

@ -138,3 +138,8 @@ def peer_id(server_stub, admin_stub):
)
peer_id = create_peer_response.peer_id
yield peer_id
@pytest.fixture
def random_name():
yield "random_name_{}".format(uuid.uuid1())

View file

@ -523,6 +523,24 @@ def test_set_profile_sharing(server_stub, admin_stub, contact_profile_id):
assert get_squeak_profile_response.squeak_profile.sharing
def test_rename_profile(server_stub, admin_stub, contact_profile_id, random_name):
# Rename the profile to something new
admin_stub.RenameSqueakProfile(
squeak_admin_pb2.RenameSqueakProfileRequest(
profile_id=contact_profile_id,
profile_name=random_name,
)
)
# Get the squeak profile
get_squeak_profile_response = admin_stub.GetSqueakProfile(
squeak_admin_pb2.GetSqueakProfileRequest(
profile_id=contact_profile_id,
)
)
assert get_squeak_profile_response.squeak_profile.profile_name == random_name
def test_delete_profile(server_stub, admin_stub, contact_profile_id):
# Delete the profile
admin_stub.DeleteSqueakProfile(

View file

@ -104,6 +104,10 @@ service SqueakAdmin {
*/
rpc SetSqueakProfileSharing (SetSqueakProfileSharingRequest) returns (SetSqueakProfileSharingReply) {}
/** sqkadmin: `renamesqueakprofile`
*/
rpc RenameSqueakProfile (RenameSqueakProfileRequest) returns (RenameSqueakProfileReply) {}
/** sqkadmin: `getsqueakprofileprivatekey`
*/
rpc GetSqueakProfilePrivateKey (GetSqueakProfilePrivateKeyRequest) returns (GetSqueakProfilePrivateKeyReply) {}
@ -318,6 +322,17 @@ message SetSqueakProfileSharingRequest {
message SetSqueakProfileSharingReply {
}
message RenameSqueakProfileRequest {
/// The profile id
int32 profile_id = 1;
/// The new profile name
string profile_name = 2;
}
message RenameSqueakProfileReply {
}
message GetSqueakProfilePrivateKeyRequest {
/// The profile id
int32 profile_id = 1;

View file

@ -195,6 +195,18 @@ class SqueakAdminServerHandler(object):
self.squeak_controller.set_squeak_profile_sharing(profile_id, sharing)
return squeak_admin_pb2.SetSqueakProfileSharingReply()
def handle_rename_squeak_profile(self, request):
profile_id = request.profile_id
profile_name = request.profile_name
logger.info(
"Handle rename squeak profile with profile id: {}, new name: {}".format(
profile_id,
profile_name,
)
)
self.squeak_controller.rename_squeak_profile(profile_id, profile_name)
return squeak_admin_pb2.RenameSqueakProfileReply()
def handle_delete_squeak_profile(self, request):
profile_id = request.profile_id
logger.info(

View file

@ -91,6 +91,9 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer):
def SetSqueakProfileSharing(self, request, context):
return self.handler.handle_set_squeak_profile_sharing(request)
def RenameSqueakProfile(self, request, context):
return self.handler.handle_rename_squeak_profile(request)
def DeleteSqueakProfile(self, request, context):
return self.handler.handle_delete_squeak_profile(request)

View file

@ -225,6 +225,14 @@ def create_app(handler, username, password):
handler.handle_set_squeak_profile_sharing,
)
@app.route("/renamesqueakprofile", methods=["POST"])
@login_required
def renamesqueakprofile():
return handle_request(
squeak_admin_pb2.RenameSqueakProfileRequest(),
handler.handle_rename_squeak_profile,
)
@app.route("/getpeers", methods=["POST"])
@login_required
def getpeers():

View file

@ -181,6 +181,9 @@ class SqueakController:
def set_squeak_profile_sharing(self, profile_id: int, sharing: bool):
self.squeak_db.set_profile_sharing(profile_id, sharing)
def rename_squeak_profile(self, profile_id: int, profile_name: str):
self.squeak_db.set_profile_name(profile_id, profile_name)
def delete_squeak_profile(self, profile_id: int):
self.squeak_db.delete_profile(profile_id)

View file

@ -632,6 +632,16 @@ class SqueakDb:
# with self.get_cursor() as curs:
# curs.execute(sql, (sharing, profile_id,))
def set_profile_name(self, profile_id: int, profile_name: str):
""" Set a profile name. """
stmt = (
self.profiles.update()
.where(self.profiles.c.profile_id == profile_id)
.values(profile_name=profile_name)
)
with self.get_connection() as connection:
connection.execute(stmt)
def delete_profile(self, profile_id: int):
""" Delete a profile. """
delete_profile_stmt = self.profiles.delete().where(